Debian Codegraph @

plugins/vcs/Tree.h

raw
1 #pragma once
2
3 #include <git2.h>
4 #include "GitArchiveTextFile.h"
5 #include "GitException.h"
6 #include "Repository.h"
7
8 namespace vcs
9 {
10
11 namespace git
12 {
13
14 class Tree final
15 {
16 private:
17 git_tree* _tree;
18
19 public:
20 Tree(git_tree* tree) :
21 _tree(tree)
22 {}
23
24 ~Tree()
25 {
26 git_tree_free(_tree);
27 }
28
29 GitArchiveTextFile::Ptr openTextFile(const std::string& filePath, Repository& repository)
30 {
31 // Tree entry is owned by the tree, it's not allowed to free it
32 git_tree_entry* entry = nullptr;
33 auto error = git_tree_entry_bypath(&entry, _tree, filePath.c_str());
34 GitException::ThrowOnError(error);
35
36 git_blob* blob;
37 error = git_blob_lookup(&blob, repository._get(), git_tree_entry_id(entry));
38
39 // Free the entry before checking for errors
40 git_tree_entry_free(entry);
41
42 if (error < 0)
43 {
44 GitException::ThrowOnError(error);
45 }
46
47 // GitArchiveTextFile assumes ownership of the blob
48 return std::make_shared<GitArchiveTextFile>(blob, filePath);
49 }
50
51 git_tree* _get() const
52 {
53 return _tree;
54 }
55 };
56
57 }
58
59 }