forked from QuantStack/git2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex_wrapper.cpp
More file actions
133 lines (110 loc) · 3.3 KB
/
index_wrapper.cpp
File metadata and controls
133 lines (110 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "index_wrapper.hpp"
#include <algorithm>
#include <iostream>
#include <vector>
#include <git2/index.h>
#include "../utils/common.hpp"
#include "../utils/git_exception.hpp"
#include "../wrapper/repository_wrapper.hpp"
index_wrapper::~index_wrapper()
{
git_index_free(p_resource);
p_resource = nullptr;
}
index_wrapper index_wrapper::init(repository_wrapper& rw)
{
index_wrapper index;
throw_if_error(git_repository_index(&(index.p_resource), rw));
return index;
}
void index_wrapper::add_entry(const std::string& path)
{
throw_if_error(git_index_add_bypath(*this, path.c_str()));
}
void index_wrapper::add_entries(std::vector<std::string> patterns)
{
add_impl(std::move(patterns));
}
void index_wrapper::add_all()
{
add_impl({{"."}});
}
void index_wrapper::add_impl(std::vector<std::string> patterns)
{
git_strarray_wrapper array{patterns};
throw_if_error(git_index_add_all(*this, array, 0, NULL, NULL));
}
void index_wrapper::remove_entry(const std::string& path)
{
throw_if_error(git_index_remove_bypath(*this, path.c_str()));
}
void index_wrapper::remove_entries(std::vector<std::string> paths)
{
git_strarray_wrapper array{paths};
throw_if_error(git_index_remove_all(*this, array, NULL, NULL));
}
void index_wrapper::remove_directories(std::vector<std::string> entries)
{
std::for_each(
entries.cbegin(),
entries.cend(),
[this](const std::string& path)
{
throw_if_error(git_index_remove_directory(*this, path.c_str(), 0));
}
);
}
void index_wrapper::write()
{
throw_if_error(git_index_write(*this));
}
git_oid index_wrapper::write_tree()
{
git_oid tree_id;
throw_if_error(git_index_write_tree(&tree_id, *this));
return tree_id;
}
size_t index_wrapper::entry_count() const
{
return git_index_entrycount(*this);
}
bool index_wrapper::has_conflict() const
{
return git_index_has_conflicts(*this);
}
git_index_conflict_iterator* index_wrapper::create_conflict_iterator()
{
git_index_conflict_iterator* conflict_iterator;
throw_if_error(git_index_conflict_iterator_new(&conflict_iterator, *this));
return conflict_iterator;
}
void index_wrapper::output_conflicts()
{
git_index_conflict_iterator* conflicts = create_conflict_iterator();
const git_index_entry* ancestor;
const git_index_entry* our;
const git_index_entry* their;
int err = 0;
std::string msg_conflict;
while ((err = git_index_conflict_next(&ancestor, &our, &their, conflicts)) == 0)
{
std::string ancestor_path = ancestor ? ancestor->path : "";
std::string our_path = our->path ? our->path : "NULL";
std::string their_path = their->path ? their->path : "NULL";
msg_conflict = "conflict: " + ancestor_path + " " + our_path + " " + their_path;
std::cout << msg_conflict << std::endl;
// Message with git is a bit different:
// Auto-merging mook_file.txt
// CONFLICT (add/add): Merge conflict in mook_file.txt
// Automatic merge failed; fix conflicts and then commit the result.
}
if (err != GIT_ITEROVER)
{
std::cout << "error iterating conflicts" << std::endl;
}
git_index_conflict_iterator_free(conflicts);
}
void index_wrapper::conflict_cleanup()
{
throw_if_error(git_index_conflict_cleanup(*this));
}