Debian Codegraph @

plugins/vcs/Index.cpp

raw
1 #include "Index.h"
2
3 #include "GitException.h"
4 #include "Tree.h"
5 #include "Repository.h"
6
7 namespace vcs
8 {
9
10 namespace git
11 {
12
13 Index::Index(git_index* index) :
14 _index(index)
15 {}
16
17 Index::~Index()
18 {
19 git_index_free(_index);
20 }
21
22 std::shared_ptr<Tree> Index::writeTree(Repository& repository)
23 {
24 git_oid treeOid;
25 auto error = git_index_write_tree(&treeOid, _index);
26 GitException::ThrowOnError(error);
27
28 git_tree* tree;
29 error = git_tree_lookup(&tree, repository._get(), &treeOid);
30 GitException::ThrowOnError(error);
31
32 return std::make_shared<Tree>(tree);
33 }
34
35 void Index::addAll()
36 {
37 std::string pathSpec("*");
38 char* pathSpecCstr = pathSpec.data();
39
40 const git_strarray pathSpecs = {
41 &pathSpecCstr,
42 1
43 };
44
45 auto error = git_index_add_all(_index, &pathSpecs, GIT_INDEX_ADD_DEFAULT, nullptr, nullptr);
46 GitException::ThrowOnError(error);
47 }
48
49 void Index::updateAll()
50 {
51 std::string pathSpec("*");
52 char* pathSpecCstr = pathSpec.data();
53
54 const git_strarray pathSpecs = {
55 &pathSpecCstr,
56 1
57 };
58
59 auto error = git_index_update_all(_index, &pathSpecs, nullptr, nullptr);
60 GitException::ThrowOnError(error);
61 }
62
63 void Index::write()
64 {
65 auto error = git_index_write(_index);
66 GitException::ThrowOnError(error);
67 }
68
69 void Index::resolveByUsingOurs(const std::string& relativePath)
70 {
71 auto error = git_index_add_bypath(_index, relativePath.c_str());
72 GitException::ThrowOnError(error);
73 }
74
75 bool Index::fileIsConflicted(const std::string& relativePath)
76 {
77 const git_index_entry* ancestor = nullptr;
78 const git_index_entry* ours = nullptr;
79 const git_index_entry* theirs = nullptr;
80
81 auto error = git_index_conflict_get(&ancestor, &ours, &theirs, _index, relativePath.c_str());
82 return error == 0;
83 }
84
85 bool Index::hasConflicts()
86 {
87 return git_index_has_conflicts(_index);
88 }
89
90 }
91
92 }