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 pathworktree.cpp
More file actions
121 lines (104 loc) · 3.12 KB
/
Copy pathworktree.cpp
File metadata and controls
121 lines (104 loc) · 3.12 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
#include <cppgit2/worktree.hpp>
namespace cppgit2 {
worktree::worktree() : c_ptr_(nullptr), owner_(ownership::libgit2) {}
worktree::worktree(git_worktree *c_ptr, ownership owner)
: c_ptr_(c_ptr), owner_(owner) {}
worktree::~worktree() {
if (c_ptr_ && owner_ == ownership::user)
git_worktree_free(c_ptr_);
}
std::pair<bool, std::string> worktree::is_locked() const {
data_buffer result;
auto ret = git_worktree_is_locked(result.c_ptr(), c_ptr_);
if (ret > 0) {
// Locked
if (result.c_ptr()->size) // size > 0 => reason available
return std::pair<bool, std::string>{true, result.to_string()};
else
return std::pair<bool, std::string>{true, ""};
} else if (ret == 0) {
// Not locked
return std::pair<bool, std::string>{false, ""};
} else {
throw git_exception();
}
}
bool worktree::is_prunable(unsigned int version, uint32_t flags) const {
git_worktree_prune_options options;
options.version = version;
options.flags = flags;
auto ret = git_worktree_is_prunable(c_ptr_, &options);
if (ret == 1)
return true;
else if (ret == 0)
return false;
else
throw git_exception();
}
bool worktree::is_prunable() const {
git_worktree_prune_options options;
// Initializes a git_worktree_prune_options with default values. Equivalent to
// creating an instance with GIT_WORKTREE_PRUNE_OPTIONS_INIT.
auto ret = git_worktree_prune_options_init(
&options, GIT_WORKTREE_PRUNE_OPTIONS_VERSION);
if (ret == 0) {
ret = git_worktree_is_prunable(c_ptr_, &options);
if (ret == 1)
return true;
else if (ret == 0)
return false;
else
throw git_exception();
} else
throw git_exception();
}
void worktree::lock(const std::string &reason) {
const char *reason_c = reason.empty() ? nullptr : reason.c_str();
if (git_worktree_lock(c_ptr_, reason_c))
throw git_exception();
}
std::string worktree::name() const {
auto ret = git_worktree_name(c_ptr_);
if (ret)
return std::string(ret);
else
return "";
}
std::string worktree::path() const {
auto ret = git_worktree_path(c_ptr_);
if (ret)
return std::string(ret);
else
return "";
}
void worktree::prune(unsigned int version, uint32_t flags) {
git_worktree_prune_options options;
options.version = version;
options.flags = flags;
if (git_worktree_prune(c_ptr_, &options))
throw git_exception();
}
void worktree::prune() {
git_worktree_prune_options options;
// Initializes a git_worktree_prune_options with default values. Equivalent to
// creating an instance with GIT_WORKTREE_PRUNE_OPTIONS_INIT.
auto ret = git_worktree_prune_options_init(
&options, GIT_WORKTREE_PRUNE_OPTIONS_VERSION);
if (ret == 0) {
if (git_worktree_prune(c_ptr_, &options))
throw git_exception();
} else
throw git_exception();
}
void worktree::unlock() {
if (git_worktree_unlock(c_ptr_))
throw git_exception();
}
bool worktree::is_valid() { return git_worktree_validate(c_ptr_) == 0; }
bool worktree::validate() {
if (!is_valid())
throw git_exception();
return true;
}
const git_worktree *worktree::c_ptr() const { return c_ptr_; }
} // namespace cppgit2