-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit2pp.h
555 lines (448 loc) · 17 KB
/
git2pp.h
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//
// libgit2pp
//
// https://github.com/marcelocantos/libgit2pp
//
// Distributed under the Apache License, Version 2.0. (See accompanying
// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0)
//
#ifndef GIT2PP_H
#define GIT2PP_H
#include <git2.h>
#if !(LIBGIT2_VER_MAJOR == 0 && LIBGIT2_VER_MINOR < 28)
# define LIBGIT2PP_HAVE_INDEX_ITERATOR 1
#else
# define LIBGIT2PP_HAVE_INDEX_ITERATOR 0
#endif
#if !(LIBGIT2_VER_MAJOR == 0 && LIBGIT2_VER_MINOR < 25)
# define LIBGIT2PP_HAVE_REFERENCE_DUP 1
#else
# define LIBGIT2PP_HAVE_REFERENCE_DUP 0
#endif
#include <functional>
#include <iostream>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>
#include <tuple>
#include <type_traits>
inline
std::ostream & operator<<(std::ostream & os, git_oid const * oid) {
return os << git_oid_tostr_s(oid);
}
namespace git2pp {
class Error : public std::runtime_error {
public:
using runtime_error::runtime_error;
};
inline
void check(int rc) {
if (rc < 0) {
std::ostringstream oss;
oss << "git2 error " << rc;
if (auto err = giterr_last()) {
oss << "/" << err->klass << ": " << err->message;
}
throw Error{oss.str()};
}
}
namespace detail {
template <typename T> struct obj_free {};
template <typename T> struct obj_no_free {
void operator()(T * t) const { }
};
}
template <typename T, typename Free = detail::obj_free<T>>
class UniquePtr;
namespace detail {
#define GIT2PP_OBJ_FREE_(type) \
template <> struct obj_free<::git_##type> { \
void operator()(::git_##type * t) const { ::git_##type##_free(t); } \
};
GIT2PP_OBJ_FREE_(annotated_commit);
GIT2PP_OBJ_FREE_(blame);
GIT2PP_OBJ_FREE_(blob);
GIT2PP_OBJ_FREE_(branch_iterator);
GIT2PP_OBJ_FREE_(buf);
GIT2PP_OBJ_FREE_(commit);
GIT2PP_OBJ_FREE_(config);
GIT2PP_OBJ_FREE_(config_entry);
GIT2PP_OBJ_FREE_(config_iterator);
//GIT2PP_OBJ_FREE_(cred);
GIT2PP_OBJ_FREE_(describe_result);
GIT2PP_OBJ_FREE_(diff);
GIT2PP_OBJ_FREE_(diff_stats);
GIT2PP_OBJ_FREE_(filter_list);
//GIT2PP_OBJ_FREE_(hashsig);
GIT2PP_OBJ_FREE_(index);
#if LIBGIT2PP_HAVE_INDEX_ITERATOR
GIT2PP_OBJ_FREE_(index_iterator);
#endif
GIT2PP_OBJ_FREE_(index_conflict_iterator);
GIT2PP_OBJ_FREE_(indexer);
GIT2PP_OBJ_FREE_(merge_file_result);
GIT2PP_OBJ_FREE_(note);
GIT2PP_OBJ_FREE_(note_iterator);
GIT2PP_OBJ_FREE_(object);
GIT2PP_OBJ_FREE_(odb);
GIT2PP_OBJ_FREE_(odb_object);
GIT2PP_OBJ_FREE_(odb_stream);
GIT2PP_OBJ_FREE_(oid_shorten);
GIT2PP_OBJ_FREE_(oidarray);
GIT2PP_OBJ_FREE_(packbuilder);
GIT2PP_OBJ_FREE_(patch);
GIT2PP_OBJ_FREE_(pathspec);
GIT2PP_OBJ_FREE_(pathspec_match_list);
GIT2PP_OBJ_FREE_(rebase);
GIT2PP_OBJ_FREE_(refdb);
GIT2PP_OBJ_FREE_(reference);
GIT2PP_OBJ_FREE_(reference_iterator);
GIT2PP_OBJ_FREE_(reflog);
GIT2PP_OBJ_FREE_(remote);
GIT2PP_OBJ_FREE_(repository);
GIT2PP_OBJ_FREE_(revwalk);
GIT2PP_OBJ_FREE_(signature);
GIT2PP_OBJ_FREE_(status_list);
GIT2PP_OBJ_FREE_(strarray);
GIT2PP_OBJ_FREE_(submodule);
GIT2PP_OBJ_FREE_(tag);
GIT2PP_OBJ_FREE_(tree);
GIT2PP_OBJ_FREE_(tree_entry);
GIT2PP_OBJ_FREE_(treebuilder);
#define GIT2PP_OBJ_NO_FREE_(type) \
template <> struct obj_free<type> { \
void operator()(type * t) const { } \
};
GIT2PP_OBJ_NO_FREE_(const git_index_entry);
template <typename T> struct obj_dup {};
#define GIT2PP_OBJ_DUP_(type) \
template <> struct obj_dup<git_##type> { \
static int dup(git_##type * * out, git_##type const * t) { \
return git_##type##_dup(out, (git_##type *)t); \
} \
};
GIT2PP_OBJ_DUP_(object);
GIT2PP_OBJ_DUP_(odb_object);
#if LIBGIT2PP_HAVE_REFERENCE_DUP
GIT2PP_OBJ_DUP_(reference);
#endif
GIT2PP_OBJ_DUP_(remote);
GIT2PP_OBJ_DUP_(signature);
GIT2PP_OBJ_DUP_(tree_entry);
#define GIT2PP_OBJ_OBJECT_DUP_(type) \
template <> struct obj_dup<git_##type> { \
static int dup(git_##type * * out, git_##type const * t) { \
return git_object_dup((git_object * *)out, (git_object *)t); \
} \
};
GIT2PP_OBJ_OBJECT_DUP_(blob);
GIT2PP_OBJ_OBJECT_DUP_(commit);
GIT2PP_OBJ_OBJECT_DUP_(tag);
GIT2PP_OBJ_OBJECT_DUP_(tree);
}
namespace detail {
template <typename T> class MaybeIterable {
using iterable = std::nullptr_t;
};
template <typename T, typename... Params, typename... Args>
UniquePtr<T> wrap(int (*f)(T * * t, Params... params), Args &&... args);
template <typename... Params, typename... Args>
git_oid wrapOid(int (*f)(git_oid * oid, Params... params), Args &&... args);
}
template <typename T, typename Free>
class UniquePtr : public detail::MaybeIterable<UniquePtr<T, Free>> {
public:
using value_type = T;
UniquePtr(T * t = nullptr) : t_{t} { }
template <typename U = T>
UniquePtr(UniquePtr const & t, std::enable_if_t<LIBGIT2PP_HAVE_REFERENCE_DUP || !std::is_same_v<U, git_reference>>* = 0)
: UniquePtr{t ? t[&detail::obj_dup<T>::dup]() : nullptr}
{ }
UniquePtr(UniquePtr &&) = default;
UniquePtr & operator=(UniquePtr const & t) {
if (this != &t) {
t_ = std::move(t[&detail::obj_dup<T>::dup]().t_);
}
return *this;
}
UniquePtr & operator=(UniquePtr &&) = default;
void reset(T * t = nullptr) {
t_.reset(t);
}
explicit operator bool() const { return bool(t_); }
T & operator*() const { return *t_; }
T * operator->() const { return t_.get(); }
bool operator==(UniquePtr const & that) const { return t_ == that.t_; }
bool operator!=(UniquePtr const & that) const { return t_ != that.t_; }
template <typename U, typename... Params, typename = std::enable_if<!std::is_const_v<T>>>
auto operator[](int (* method)(U * *, T *, Params...)) const {
return [this, method](auto &&... args) {
return detail::wrap(method, &*t_, std::forward<decltype(args)>(args)...);
};
}
template <typename U, typename... Params>
auto operator[](int (* method)(U * *, T const *, Params...)) const {
return [this, method](auto &&... args) {
return detail::wrap(method, &*t_, std::forward<decltype(args)>(args)...);
};
}
template <typename R, typename... Params>
auto operator[](R (* method)(git_oid *, Params...)) const {
return [this, method](auto &&... args) {
return detail::wrapOid(method, &*t_, std::forward<decltype(args)>(args)...);
};
}
template <typename R, typename... Params, typename = std::enable_if<!std::is_const_v<T>>>
auto operator[](R (* method)(T *, Params...)) const {
return [this, method](auto &&... args) {
return method(&*t_, std::forward<decltype(args)>(args)...);
};
}
template <typename R, typename... Params>
auto operator[](R (* method)(T const *, Params...)) const {
return [this, method](auto &&... args) {
return method(&*t_, std::forward<decltype(args)>(args)...);
};
}
template <typename U>
UniquePtr<U> as() && {
return (U *)t_.release();
}
template <typename U>
U * as() const {
return (U *)t_.get();
}
private:
std::unique_ptr<T, Free> t_;
};
namespace detail {
template <typename T, typename... Params, typename... Args>
UniquePtr<T> wrap(int (*f)(T * * t, Params... params), Args &&... args) {
T * t;
check(f(&t, std::forward<Args>(args)...));
return t;
}
template <typename... Params, typename... Args>
git_oid wrapOid(int (*f)(git_oid * oid, Params... params), Args &&... args) {
git_oid oid;
check(f(&oid, std::forward<Args>(args)...));
return oid;
}
}
class Session {
public:
Session() {
git_libgit2_init();
}
~Session() {
git_libgit2_shutdown();
}
template <typename T, typename... Params>
auto operator[](int (* method)(T * *, Params...)) {
return [this, method](auto &&... args) {
return detail::wrap(method, std::forward<decltype(args)>(args)...);
};
}
};
template <typename I, typename NextF, typename Derived>
class IteratorBase {
public:
using git_iterator = I;
using next_f = NextF;
IteratorBase(UniquePtr<I> * i, NextF next) : i_{i}, next_{std::move(next)} { }
IteratorBase() : i_{nullptr} {}
IteratorBase(IteratorBase const &) = delete;
IteratorBase(IteratorBase &&) = delete;
bool operator==(Derived const & that) { return done() && that.done(); }
bool operator!=(Derived const & that) { return !(*this == that); }
Derived & operator++() {
auto self = static_cast<Derived *>(this);
int rc;
self->increment(rc);
if (rc != 0) {
if (rc != GIT_ITEROVER) {
check(rc);
}
this->i_ = nullptr;
}
return *self;
}
protected:
NextF next_;
UniquePtr<I> * i_;
private:
bool done() const { return !i_; }
};
template <typename I, typename NextF, typename T, typename Free = detail::obj_free<T>>
class Iterator : public IteratorBase<I, NextF, Iterator<I, NextF, T, Free>> {
private:
using base = IteratorBase<I, NextF, Iterator>;
public:
Iterator(UniquePtr<I> * i, NextF next) : base{i, std::move(next)} { ++*this; }
Iterator() = default;
void increment(int & rc) {
T * t;
if ((rc = base::next_(&t, &**base::i_)) == 0) {
t_.reset(t);
}
}
UniquePtr<T, Free> operator*() const { return std::move(t_); }
UniquePtr<T, Free> & operator->() const { return t_; }
private:
mutable UniquePtr<T, Free> t_;
friend base;
};
template <typename I, typename NextF, typename T>
class StructIterator : public IteratorBase<I, NextF, StructIterator<I, NextF, T>> {
private:
using base = IteratorBase<I, NextF, StructIterator>;
public:
StructIterator(UniquePtr<I> * i, NextF next) : base{i, std::move(next)} { ++*this; }
StructIterator() = default;
void increment(int & rc) {
T t;
if ((rc = base::next_(&t, &**base::i_)) == 0) {
t_ = t;
}
}
T operator*() const { return t_; }
T & operator->() const { return t_; }
private:
mutable T t_;
friend base;
};
template <typename I, typename NextF>
class BranchIterator : public IteratorBase<I, NextF, BranchIterator<I, NextF>> {
private:
using base = IteratorBase<I, NextF, BranchIterator>;
public:
struct Entry {
Entry() = default;
Entry(Entry const &) = delete;
Entry& operator=(Entry const &) = delete;
Entry(Entry &&) = default;
Entry& operator=(Entry &&) = default;
UniquePtr<git_reference> ref;
git_branch_t type;
};
BranchIterator(UniquePtr<I> * i, NextF next) : base{i, std::move(next)} { ++*this; }
BranchIterator() = default;
void increment(int & rc) {
git_reference * ref;
git_branch_t type;
if ((rc = base::next_(&ref, &type, &**base::i_)) == 0) {
e_ = {std::move(ref), type};
}
}
Entry const & operator*() const { return e_; }
Entry const * operator->() const { return &e_; }
private:
Entry e_;
friend base;
};
template <typename I, typename NextF>
class IndexConflictIterator : public IteratorBase<I, NextF, IndexConflictIterator<I, NextF>> {
private:
using base = IteratorBase<I, NextF, IndexConflictIterator>;
public:
struct Entry {
git_index_entry const * ancestor;
git_index_entry const * our;
git_index_entry const * their;
};
IndexConflictIterator(UniquePtr<I> * i, NextF next) : base{i, std::move(next)} { ++*this; }
IndexConflictIterator() = default;
void increment(int & rc) {
Entry e;
if ((rc = base::next_(&e.ancestor, &e.our, &e.their, &**base::i_)) == 0) {
e_ = e;
}
}
Entry const & operator*() const { return e_; }
Entry const * operator->() const { return &e_; }
private:
Entry e_;
friend base;
};
template <typename I, typename NextF>
class NoteIterator : public IteratorBase<I, NextF, NoteIterator<I, NextF>> {
private:
using base = IteratorBase<I, NextF, NoteIterator>;
public:
struct Entry {
git_oid note_id;
git_oid annotated_id;
};
NoteIterator(UniquePtr<I> * i, NextF next) : base{i, std::move(next)} { ++*this; }
NoteIterator() = default;
void increment(int & rc) {
Entry e;
if ((rc = base::next_(&e.note_id, &e.annotated_id, &**base::i_)) == 0) {
e_ = e;
}
}
Entry const & operator*() const { return e_; }
Entry const * operator->() const { return &e_; }
private:
Entry e_;
friend base;
};
template <typename Iterator>
class Iterable {
public:
using iterator = Iterator;
using git_iterator = typename iterator::git_iterator;
using next_f = typename iterator::next_f;
Iterable(next_f next) : next_(std::move(next)) { }
Iterator begin() { return {static_cast<UniquePtr<git_iterator> *>(this), next_}; }
Iterator end() { return {}; }
private:
next_f next_;
};
namespace detail {
using ConfigIterable = Iterable<Iterator<git_config_iterator, decltype(&git_config_next), git_config_entry, obj_no_free<git_config_entry>>>;
template <> class MaybeIterable<UniquePtr<git_config_iterator>> : public ConfigIterable {
public:
MaybeIterable() : ConfigIterable(git_config_next) {}
};
#if LIBGIT2PP_HAVE_INDEX_ITERATOR
using IndexIteratorIterable = Iterable<Iterator<git_index_iterator, decltype(&git_index_iterator_next), const git_index_entry>>;
template <> class MaybeIterable<UniquePtr<git_index_iterator>> : public IndexIteratorIterable {
public:
MaybeIterable() : IndexIteratorIterable(git_index_iterator_next) {}
};
#endif
using ReferenceIterable = Iterable<Iterator<git_reference_iterator, decltype(&git_reference_next), git_reference>>;
template <> class MaybeIterable<UniquePtr<git_reference_iterator>> : public ReferenceIterable {
public:
MaybeIterable() : ReferenceIterable(git_reference_next) {}
};
using RebaseIterable = Iterable<StructIterator<git_rebase, decltype(&git_rebase_next), git_rebase_operation *>>;
template <> class MaybeIterable<UniquePtr<git_rebase>> : public RebaseIterable {
public:
MaybeIterable() : RebaseIterable(git_rebase_next) {}
};
using RevwalkIterable = Iterable<StructIterator<git_revwalk, decltype(&git_revwalk_next), git_oid>>;
template <> class MaybeIterable<UniquePtr<git_revwalk>> : public RevwalkIterable {
public:
MaybeIterable() : RevwalkIterable(git_revwalk_next) {}
};
using BranchIterable = Iterable<BranchIterator<git_branch_iterator, decltype(&git_branch_next)>>;
template <> class MaybeIterable<UniquePtr<git_branch_iterator>> : public BranchIterable {
public:
MaybeIterable() : BranchIterable(git_branch_next) {}
};
using IndexConflictIterable = Iterable<IndexConflictIterator<git_index_conflict_iterator, decltype(&git_index_conflict_next)>>;
template <> class MaybeIterable<UniquePtr<git_index_conflict_iterator>> : public IndexConflictIterable {
public:
MaybeIterable() : IndexConflictIterable(git_index_conflict_next) {}
};
using NoteIterable = Iterable<NoteIterator<git_note_iterator, decltype(&git_note_next)>>;
template <> class MaybeIterable<UniquePtr<git_note_iterator>> : public NoteIterable {
public:
MaybeIterable() : NoteIterable(git_note_next) {}
};
}
}
#endif // GIT2PP_H