forked from AndreyG/libgit2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepo.cpp
More file actions
386 lines (337 loc) · 10.6 KB
/
Copy pathrepo.cpp
File metadata and controls
386 lines (337 loc) · 10.6 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include <git2/commit.h>
#include <git2/branch.h>
#include <git2/types.h>
#include <git2/merge.h>
#include <git2/submodule.h>
#include <git2/errors.h>
#include <git2/tag.h>
#include <git2/revwalk.h>
#include <git2/blob.h>
#include <cassert>
#include "git2cpp/repo.h"
#include "git2cpp/error.h"
#include "git2cpp/internal/optional.h"
namespace git
{
namespace
{
int write_branch_name(const char * name, git_branch_t, void * payload)
{
std::vector<std::string> * out = reinterpret_cast<std::vector<std::string> *>(payload);
out->emplace_back(name);
return 0;
}
}
Repository::Repository(std::string const & dir)
{
if (git_repository_open_ext(&repo_, dir.c_str(), 0, NULL))
throw repository_open_error();
}
Repository::Repository(std::string const & dir, init_tag)
{
if (git_repository_init(&repo_, dir.c_str(), 0) < 0)
throw repository_init_error(dir);
}
Repository::Repository(std::string const & dir, init_tag, git_repository_init_options opts)
{
if (git_repository_init_ext(&repo_, dir.c_str(), &opts) < 0)
throw repository_init_error(dir);
}
Repository::~Repository()
{
git_repository_free(repo_);
}
bool Repository::is_bare() const
{
return git_repository_is_bare(repo_);
}
Signature Repository::signature() const
{
return Signature(repo_);
}
Commit Repository::commit_lookup(git_oid const & oid) const
{
git_commit * commit;
if (git_commit_lookup(&commit, repo_, &oid))
throw commit_lookup_error(oid);
else
return { commit, *this };
}
Tree Repository::tree_lookup(git_oid const & oid) const
{
git_tree * tree;
if (git_tree_lookup(&tree, repo_, &oid))
throw tree_lookup_error(oid);
else
return { tree, *this };
}
Tag Repository::tag_lookup(git_oid const & oid) const
{
git_tag * tag;
if (git_tag_lookup(&tag, repo_, &oid))
throw tag_lookup_error(oid);
else
return { tag, *this };
}
Blob Repository::blob_lookup(git_oid const & oid) const
{
git_blob * blob;
if (git_blob_lookup(&blob, repo_, &oid))
throw blob_lookup_error(oid);
else
return Blob(blob);
}
Revspec Repository::revparse(const char * spec) const
{
git_revspec revspec;
if (git_revparse(&revspec, repo_, spec))
throw revparse_error(spec);
else
return { revspec, *this };
}
Revspec Repository::revparse_single(const char * spec) const
{
git_object * obj;
if (git_revparse_single(&obj, repo_, spec) < 0)
throw revparse_error(spec);
return Revspec(obj, *this);
}
Index Repository::index() const
{
return Index(repo_);
}
Odb Repository::odb() const
{
return Odb(repo_);
}
git_status_t Repository::file_status(const char * filepath) const
{
git_status_t res;
switch (git_status_file(reinterpret_cast<unsigned int *>(&res), repo_, filepath))
{
case GIT_ENOTFOUND:
throw file_not_found_error(filepath);
case GIT_EAMBIGUOUS:
throw ambiguous_path_error(filepath);
case -1:
throw unknown_file_status_error(filepath);
}
return res;
}
struct branch_iterator
{
branch_iterator(git_repository * repo, branch_type type)
: type_(convert(type))
{
git_branch_iterator_new(&base_, repo, type_);
++(*this);
}
~branch_iterator()
{
git_branch_iterator_free(base_);
}
explicit operator bool () const { return ref_.is_initialized(); }
void operator ++ ()
{
git_branch_t type;
git_reference * ref;
switch (git_branch_next(&ref, &type, base_))
{
case GIT_OK:
assert(type == type_);
internal::emplace(ref_, ref);
break;
case GIT_ITEROVER:
ref_ = internal::none;
break;
default:
ref_ = internal::none;
throw std::logic_error("unknown git_branch_next error");
}
}
Reference const * operator -> () const
{
return ref_.get_ptr();
}
private:
git_branch_t convert(branch_type t) const
{
switch (t)
{
case branch_type::LOCAL : return GIT_BRANCH_LOCAL;
case branch_type::REMOTE: return GIT_BRANCH_REMOTE;
case branch_type::ALL: return GIT_BRANCH_REMOTE;
default:
throw std::logic_error("invalid branch type");
}
}
private:
git_branch_t type_;
git_branch_iterator * base_;
internal::optional<Reference> ref_;
};
std::vector<std::string> Repository::branches(branch_type type) const
{
std::vector<std::string> res;
for (branch_iterator it(repo_, type); it; ++it)
res.push_back(it->name());
return res;
}
RevWalker Repository::rev_walker() const
{
git_revwalk * walker;
if (git_revwalk_new(&walker, repo_))
throw revwalk_new_error();
else
return { walker, *this };
}
git_oid Repository::merge_base(git_oid const & a, git_oid const & b) const
{
git_oid res;
if (git_merge_base(&res, repo_, &a, &b))
throw merge_base_error(a, b);
return res;
}
git_oid Repository::merge_base(Revspec::Range const & range) const
{
return merge_base(range.from.id(), range.to.id());
}
Reference Repository::head() const
{
git_reference * head;
switch (git_repository_head(&head, repo_))
{
case GIT_OK:
return Reference(head);
case GIT_EUNBORNBRANCH:
throw non_existing_branch_error();
case GIT_ENOTFOUND:
throw missing_head_error();
default:
throw unknown_get_current_branch_error();
}
}
Reference Repository::ref(const char * name) const
{
git_reference * ref;
git_reference_lookup(&ref, repo_, name);
return Reference(ref);
}
Status Repository::status(git_status_options const & opts) const
{
return Status(repo_, opts);
}
StrArray Repository::reference_list() const
{
git_strarray str_array;
git_reference_list(&str_array, repo_);
return StrArray(str_array);
}
Submodule Repository::submodule_lookup(const char * name) const
{
git_submodule * sm;
if (git_submodule_lookup(&sm, repo_, name))
throw submodule_lookup_error(name);
else
return { sm, repo_ };
}
const char * Repository::path() const
{
return git_repository_path(repo_);
}
const char * Repository::workdir() const
{
return git_repository_workdir(repo_);
}
git_oid Repository::create_commit(const char * update_ref,
Signature const & author,
Signature const & commiter,
const char * message_encoding,
const char * message,
Tree const & tree)
{
git_oid res;
if (git_commit_create_v(&res, repo_, update_ref,
author.ptr(), commiter.ptr(),
message_encoding, message,
tree.ptr(), 0))
{
throw commit_create_error();
}
return res;
}
git_oid Repository::create_commit(const char * update_ref,
Signature const & author,
Signature const & commiter,
const char * message_encoding,
const char * message,
Tree const & tree,
Commit const & parent)
{
git_oid res;
if (git_commit_create_v(&res, repo_, update_ref,
author.ptr(), commiter.ptr(),
message_encoding, message,
tree.ptr(), 1, parent.ptr()))
{
throw commit_create_error();
}
return res;
}
namespace
{
Object tree_entry_to_object(git_tree_entry const * entry, Repository const & repo, git_repository * repo_ptr)
{
git_object * obj;
git_tree_entry_to_object(&obj, repo_ptr, entry);
return Object(obj, repo);
}
}
Object Repository::entry_to_object(Tree::BorrowedEntry entry) const
{
return tree_entry_to_object(entry.ptr(), *this, repo_);
}
Object Repository::entry_to_object(Tree::OwnedEntry entry) const
{
return tree_entry_to_object(entry.ptr(), *this, repo_);
}
Object revparse_single(Repository const & repo, const char * spec)
{
return std::move(*repo.revparse_single(spec).single());
}
Diff Repository::diff(Tree & a, Tree & b, git_diff_options const & opts) const
{
git_diff * diff;
auto op_res = git_diff_tree_to_tree(&diff, repo_, a.ptr(), b.ptr(), &opts);
assert(op_res == 0);
return Diff(diff);
}
Diff Repository::diff_to_index(Tree & t, git_diff_options const & opts) const
{
git_diff * diff;
auto op_res = git_diff_tree_to_index(&diff, repo_, t.ptr(), nullptr, &opts);
assert(op_res == 0);
return Diff(diff);
}
Diff Repository::diff_to_workdir(Tree & t, git_diff_options const & opts) const
{
git_diff * diff;
auto op_res = git_diff_tree_to_workdir(&diff, repo_, t.ptr(), &opts);
assert(op_res == 0);
return Diff(diff);
}
Diff Repository::diff_to_workdir_with_index(Tree & t, git_diff_options const & opts) const
{
git_diff * diff;
auto op_res = git_diff_tree_to_workdir_with_index(&diff, repo_, t.ptr(), &opts);
assert(op_res == 0);
return Diff(diff);
}
Diff Repository::diff_index_to_workdir(git_diff_options const & opts) const
{
git_diff * diff;
auto op_res = git_diff_index_to_workdir(&diff, repo_, nullptr, &opts);
assert(op_res == 0);
return Diff(diff);
}
}