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.cpp
More file actions
77 lines (58 loc) · 2.24 KB
/
Copy pathtree.cpp
File metadata and controls
77 lines (58 loc) · 2.24 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
#include <cppgit2/repository.hpp>
#include <functional>
namespace cppgit2 {
tree::tree() : c_ptr_(nullptr), owner_(ownership::libgit2) {}
tree::tree(git_tree *c_ptr, ownership owner) : c_ptr_(c_ptr), owner_(owner) {}
tree::~tree() {
if (c_ptr_ && owner_ == ownership::user)
git_tree_free(c_ptr_);
}
tree::entry tree::lookup_entry_by_id(const oid &id) const {
return tree::entry(
const_cast<git_tree_entry *>(git_tree_entry_byid(c_ptr_, id.c_ptr())));
}
tree::entry tree::lookup_entry_by_index(size_t index) const {
return tree::entry(
const_cast<git_tree_entry *>(git_tree_entry_byindex(c_ptr_, index)));
}
tree::entry tree::lookup_entry_by_name(const std::string &filename) const {
return tree::entry(const_cast<git_tree_entry *>(
git_tree_entry_byname(c_ptr_, filename.c_str())));
}
tree::entry tree::lookup_entry_by_path(const std::string &path) const {
tree::entry result(nullptr, ownership::user);
result.owner_ = ownership::user;
if (git_tree_entry_bypath(&result.c_ptr_, c_ptr_, path.c_str()))
throw git_exception();
return result;
}
oid tree::id() const { return oid(git_tree_id(c_ptr_)); }
tree tree::copy() const {
tree result(nullptr, ownership::user);
if (git_tree_dup(&result.c_ptr_, c_ptr_))
throw git_exception();
return result;
}
size_t tree::size() const { return git_tree_entrycount(c_ptr_); }
repository tree::owner() const { return repository(git_tree_owner(c_ptr_)); }
void tree::walk(traversal_mode mode,
std::function<void(const std::string &, const tree::entry &)>
visitor) const {
struct visitor_wrapper {
std::function<void(const std::string &, const tree::entry &)> fn;
};
visitor_wrapper wrapper;
wrapper.fn = visitor;
auto callback_c = [](const char *root, const git_tree_entry *entry,
void *payload) {
auto wrapper = reinterpret_cast<visitor_wrapper *>(payload);
wrapper->fn(root ? std::string(root) : "", tree::entry(entry));
return 0;
};
if (git_tree_walk(c_ptr_, static_cast<git_treewalk_mode>(mode), callback_c,
(void *)(&wrapper)))
throw git_exception();
}
git_tree *tree::c_ptr() { return c_ptr_; }
const git_tree *tree::c_ptr() const { return c_ptr_; }
} // namespace cppgit2