This repository was archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtree_builder.cpp
More file actions
73 lines (57 loc) · 2.09 KB
/
Copy pathtree_builder.cpp
File metadata and controls
73 lines (57 loc) · 2.09 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
#include <cppgit2/tree_builder.hpp>
#include <functional>
namespace cppgit2 {
tree_builder::tree_builder(repository &repo, tree source) {
if (git_treebuilder_new(&c_ptr_, repo.c_ptr_, source.c_ptr()))
throw git_exception();
}
tree_builder::~tree_builder() {
if (c_ptr_)
git_treebuilder_free(c_ptr_);
}
void tree_builder::clear() { git_treebuilder_clear(c_ptr_); }
void tree_builder::filter(std::function<int(const tree::entry &)> visitor) {
// Wrap user-provided visitor funciton in a struct
struct visitor_wrapper {
std::function<int(const tree::entry &)> fn;
};
visitor_wrapper wrapper;
wrapper.fn = visitor;
auto visitor_c = [](const git_tree_entry *entry, void *payload) {
auto wrapped = reinterpret_cast<visitor_wrapper *>(payload);
const tree::entry entry_arg = tree::entry(entry);
return wrapped->fn(entry_arg);
};
git_treebuilder_filter(c_ptr_, visitor_c, (void *)(&wrapper));
}
size_t tree_builder::size() const { return git_treebuilder_entrycount(c_ptr_); }
tree::entry tree_builder::operator[](const std::string &filename) const {
return tree::entry(const_cast<git_tree_entry *>(
git_treebuilder_get(c_ptr_, filename.c_str())),
ownership::libgit2);
}
void tree_builder::insert(const std::string &filename, const oid &id,
file_mode mode) {
const git_tree_entry *result = nullptr;
if (git_treebuilder_insert(&result, c_ptr_, filename.c_str(), id.c_ptr(),
static_cast<git_filemode_t>(mode)))
throw git_exception();
}
void tree_builder::remove(const std::string &filename) {
if (git_treebuilder_remove(c_ptr_, filename.c_str()))
throw git_exception();
}
oid tree_builder::write() {
git_oid id;
if (git_treebuilder_write(&id, c_ptr_))
throw git_exception();
return oid(&id);
}
oid tree_builder::write(data_buffer &tree) {
git_oid id;
if (git_treebuilder_write_with_buffer(&id, c_ptr_, tree.c_ptr()))
throw git_exception();
return oid(&id);
}
const git_treebuilder *tree_builder::c_ptr() const { return c_ptr_; }
} // namespace cppgit2