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 pathrepository.cpp
More file actions
1981 lines (1704 loc) · 67.1 KB
/
Copy pathrepository.cpp
File metadata and controls
1981 lines (1704 loc) · 67.1 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
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cppgit2/repository.hpp>
#include <functional>
namespace cppgit2 {
repository::repository(git_repository *c_ptr) : c_ptr_(c_ptr) {}
repository::~repository() {
if (c_ptr_)
git_repository_free(c_ptr_);
}
repository repository::init(const std::string &path, bool is_bare) {
repository result(nullptr);
if (git_repository_init(&result.c_ptr_, path.c_str(), is_bare))
throw git_exception();
return result;
}
repository repository::init_ext(const std::string &repo_path,
const init_options &options) {
repository result(nullptr);
if (git_repository_init_ext(&result.c_ptr_, repo_path.c_str(),
options.c_ptr_))
throw git_exception();
return result;
}
repository repository::open(const std::string &path) {
repository result(nullptr);
if (git_repository_open(&result.c_ptr_, path.c_str()))
throw git_exception();
return result;
}
repository repository::open_bare(const std::string &path) {
repository result(nullptr);
if (git_repository_open_bare(&result.c_ptr_, path.c_str()))
throw git_exception();
return result;
}
repository repository::open_ext(const std::string &path, open_flag flags,
const std::string &ceiling_dirs) {
repository result(nullptr);
if (git_repository_open_ext(&result.c_ptr_, path.c_str(),
static_cast<unsigned int>(flags),
ceiling_dirs.c_str()))
throw git_exception();
return result;
}
repository repository::open_from_worktree(const worktree &wt) {
repository result(nullptr);
if (git_repository_open_from_worktree(&result.c_ptr_, wt.c_ptr_))
throw git_exception();
return result;
}
repository repository::clone(const std::string &url,
const std::string &local_path,
const clone::options &options) {
repository result;
if (git_clone(&result.c_ptr_, url.c_str(), local_path.c_str(),
options.c_ptr()))
throw git_exception();
return result;
}
std::string repository::path() const {
return std::string(git_repository_path(c_ptr_));
}
bool repository::is_bare() const { return git_repository_is_bare(c_ptr_); }
bool repository::is_empty() const {
auto ret = git_repository_is_empty(c_ptr_);
if (ret == 1)
return true;
else if (ret == 0)
return false;
else
throw git_exception();
}
bool repository::is_shallow() const {
return git_repository_is_shallow(c_ptr_);
}
bool repository::is_worktree() const {
return git_repository_is_worktree(c_ptr_);
}
std::string repository::commondir() const {
auto ret = git_repository_commondir(c_ptr_);
if (ret)
return std::string(ret);
else
throw git_exception();
}
cppgit2::config repository::config() const {
cppgit2::config result(nullptr, ownership::user);
if (git_repository_config(&result.c_ptr_, c_ptr_))
throw git_exception();
return result;
}
cppgit2::config repository::config_snapshot() const {
cppgit2::config result(nullptr, ownership::user);
if (git_repository_config_snapshot(&result.c_ptr_, c_ptr_))
throw git_exception();
return result;
}
void repository::detach_head() const {
if (git_repository_detach_head(c_ptr_))
throw git_exception();
}
std::string repository::discover_path(const std::string &start_path,
bool across_fs,
const std::string &ceiling_dirs) {
// TODO: Update this hardcoded size
data_buffer buffer;
if (git_repository_discover(buffer.c_ptr(), start_path.c_str(), across_fs,
ceiling_dirs.c_str()))
throw git_exception();
return buffer.to_string();
}
std::string repository::discover_path(const std::string &start_path) {
return discover_path(start_path, false, "");
}
void repository::for_each_fetch_head(
std::function<void(const std::string &, const std::string &, const oid &,
bool)>
visitor) const {
struct visitor_wrapper {
std::function<void(const std::string &, const std::string &, const oid &,
bool)>
fn;
};
visitor_wrapper wrapper;
wrapper.fn = visitor;
auto callback_c = [](const char *ref_name, const char *remote_url,
const git_oid *oid_c, unsigned int is_merge,
void *payload) {
auto wrapper = reinterpret_cast<visitor_wrapper *>(payload);
wrapper->fn(ref_name, remote_url, oid(oid_c), is_merge);
return 0;
};
if (git_repository_fetchhead_foreach(c_ptr_, callback_c, (void *)(&wrapper)))
throw git_exception();
}
void repository::for_each_merge_head(std::function<void(const oid &)> visitor) const {
struct visitor_wrapper {
std::function<void(const oid &)> fn;
};
visitor_wrapper wrapper;
wrapper.fn = visitor;
auto callback_c = [](const git_oid *oid_c, void *payload) {
auto wrapper = reinterpret_cast<visitor_wrapper *>(payload);
wrapper->fn(oid(oid_c));
return 0;
};
if (git_repository_mergehead_foreach(c_ptr_, callback_c, (void *)(&wrapper)))
throw git_exception();
}
std::string repository::namespace_() const {
auto ret = git_repository_get_namespace(c_ptr_);
if (ret)
return std::string(ret);
else
throw git_exception("namespace directory does not exist");
}
oid repository::hashfile(const std::string &path, object::object_type type,
const std::string &as_path) const {
oid result;
if (git_repository_hashfile(result.c_ptr(), c_ptr_, path.c_str(),
static_cast<git_object_t>(type), as_path.c_str()))
throw git_exception();
return result;
}
oid repository::hashfile(const std::string &path, object::object_type type) const {
return hashfile(path, type, path);
}
reference repository::head() const {
reference result(nullptr, ownership::user);
if (git_repository_head(&result.c_ptr_, c_ptr_))
throw git_exception();
return result;
}
reference repository::head_for_worktree(const std::string &name) const {
reference result(nullptr, ownership::user);
if (git_repository_head_for_worktree(&result.c_ptr_, c_ptr_, name.c_str()))
throw git_exception();
return result;
}
bool repository::is_head_detached() const {
auto ret = git_repository_head_detached(c_ptr_);
if (ret == 0)
return false;
else if (ret == 1)
return true;
else
throw git_exception();
}
bool repository::is_head_detached_for_worktree(const std::string &path) {
auto ret = git_repository_head_detached_for_worktree(c_ptr_, path.c_str());
if (ret == 0)
return false;
else if (ret == 1)
return true;
else
throw git_exception();
}
bool repository::is_head_unborn() const {
auto ret = git_repository_head_unborn(c_ptr_);
if (ret == 0)
return false;
else if (ret == 1)
return true;
else
throw git_exception();
}
std::pair<std::string, std::string> repository::identity() const {
const char *name_c, *email_c;
if (git_repository_ident(&name_c, &email_c, c_ptr_))
throw git_exception();
return std::pair<std::string, std::string>{name_c ? name_c : "",
email_c ? email_c : ""};
}
cppgit2::index repository::index() const {
cppgit2::index result(nullptr, ownership::user);
if (git_repository_index(&result.c_ptr_, c_ptr_))
throw git_exception();
return result;
}
std::string repository::path(repository::item item) const {
data_buffer buffer;
if (git_repository_item_path(buffer.c_ptr(), c_ptr_,
static_cast<git_repository_item_t>(item)))
throw git_exception();
return buffer.to_string();
}
std::string repository::message() const {
data_buffer buffer;
if (git_repository_message(buffer.c_ptr(), c_ptr_))
throw git_exception();
return buffer.to_string();
}
void repository::remove_message() const { git_repository_message_remove(c_ptr_); }
cppgit2::odb repository::odb() const {
cppgit2::odb result(nullptr, ownership::user);
if (git_repository_odb(&result.c_ptr_, c_ptr_))
throw git_exception();
return result;
}
cppgit2::refdb repository::refdb() const {
cppgit2::refdb result(nullptr, ownership::user);
if (git_repository_refdb(&result.c_ptr_, c_ptr_))
throw git_exception();
return result;
}
void repository::set_head(const std::string &refname) const {
if (git_repository_set_head(c_ptr_, refname.c_str()))
throw git_exception();
}
void repository::set_head_detached(const oid &commitish) const {
if (git_repository_set_head_detached(c_ptr_, commitish.c_ptr()))
throw git_exception();
}
void repository::set_head_detached(const annotated_commit &commitish) const {
if (git_repository_set_head_detached_from_annotated(c_ptr_, commitish.c_ptr_))
throw git_exception();
}
void repository::set_identity(const std::string &name,
const std::string &email) const {
if (git_repository_set_ident(c_ptr_, name.c_str(), email.c_str()))
throw git_exception();
}
void repository::unset_identity() const {
if (git_repository_set_ident(c_ptr_, nullptr, nullptr))
throw git_exception();
}
void repository::set_namespace(const std::string &namespace_) const {
if (git_repository_set_namespace(c_ptr_, namespace_.c_str()))
throw git_exception();
}
void repository::set_workdir(const std::string &workdir, bool update_gitlink) const {
if (git_repository_set_workdir(c_ptr_, workdir.c_str(), update_gitlink))
throw git_exception();
}
void repository::cleanup_state() const {
if (git_repository_state_cleanup(c_ptr_))
throw git_exception();
}
repository::repository_state repository::state() const {
switch (git_repository_state(c_ptr_)) {
case GIT_REPOSITORY_STATE_NONE:
return repository_state::none;
case GIT_REPOSITORY_STATE_REBASE_INTERACTIVE:
return repository_state::rebase_interactive;
case GIT_REPOSITORY_STATE_REBASE_MERGE:
return repository_state::rebase_merge;
case GIT_REPOSITORY_STATE_REBASE:
return repository_state::rebase;
case GIT_REPOSITORY_STATE_APPLY_MAILBOX:
return repository_state::apply_mailbox;
case GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE:
return repository_state::apply_mailbox_or_rebase;
case GIT_REPOSITORY_STATE_MERGE:
return repository_state::merge;
case GIT_REPOSITORY_STATE_REVERT:
return repository_state::revert;
case GIT_REPOSITORY_STATE_REVERT_SEQUENCE:
return repository_state::revert_sequence;
case GIT_REPOSITORY_STATE_CHERRYPICK:
return repository_state::cherrypick;
case GIT_REPOSITORY_STATE_CHERRYPICK_SEQUENCE:
return repository_state::cherrypick_sequence;
case GIT_REPOSITORY_STATE_BISECT:
return repository_state::bisect;
case -1:
default:
return repository_state::unknown;
}
}
std::string repository::workdir() const {
auto ret = git_repository_workdir(c_ptr_);
if (ret) {
return std::string(ret);
} else {
throw git_exception("working directory does not exist");
}
}
repository repository::wrap_odb(const cppgit2::odb &odb) {
repository result(nullptr);
if (git_repository_wrap_odb(&result.c_ptr_, odb.c_ptr_))
throw git_exception();
return result;
}
const git_repository *repository::c_ptr() const { return c_ptr_; }
annotated_commit
repository::create_annotated_commit(const std::string &branch_name,
const std::string &remote_url,
const oid &id) const {
annotated_commit result(nullptr, ownership::user);
if (git_annotated_commit_from_fetchhead(&result.c_ptr_, c_ptr_,
branch_name.c_str(),
remote_url.c_str(), id.c_ptr()))
throw git_exception();
return result;
}
annotated_commit
repository::create_annotated_commit(const std::string &revspec) const {
annotated_commit result(nullptr, ownership::user);
if (git_annotated_commit_from_revspec(&result.c_ptr_, c_ptr_,
revspec.c_str()))
throw git_exception();
return result;
}
annotated_commit repository::create_annotated_commit(const reference &ref) const {
annotated_commit result(nullptr, ownership::user);
if (git_annotated_commit_from_ref(&result.c_ptr_, c_ptr_, ref.c_ptr()))
throw git_exception();
return result;
}
annotated_commit repository::lookup_annotated_commit(const oid &id) const {
annotated_commit result(nullptr, ownership::user);
if (git_annotated_commit_lookup(&result.c_ptr_, c_ptr_, id.c_ptr()))
throw git_exception();
return result;
}
void repository::apply_diff(const diff &diff, apply::location location,
const apply::options &options) const {
if (git_apply(c_ptr_, const_cast<git_diff *>(diff.c_ptr()),
static_cast<git_apply_location_t>(location), options.c_ptr()))
throw git_exception();
}
cppgit2::index repository::apply_diff(const tree &preimage, const diff &diff,
const apply::options &options) const {
git_index *result; // the postimage of the application
if (git_apply_to_tree(&result, c_ptr_,
const_cast<git_tree *>(preimage.c_ptr()),
const_cast<git_diff *>(diff.c_ptr()), options.c_ptr()))
throw git_exception();
return cppgit2::index(result, ownership::user);
}
void repository::add_attributes_macro(const std::string &name,
const std::string &values) const {
if (git_attr_add_macro(c_ptr_, name.c_str(), values.c_str()))
throw git_exception();
}
void repository::flush_attributes_cache() const {
if (git_attr_cache_flush(c_ptr_))
throw git_exception();
}
void repository::for_each_attribute(
attribute::flag flags, const std::string &path,
std::function<void(const std::string &, const std::string &)> visitor) const {
struct visitor_wrapper {
std::function<void(const std::string &, const std::string &)> fn;
};
visitor_wrapper wrapper;
wrapper.fn = visitor;
auto callback_c = [](const char *name, const char *value, void *payload) {
auto wrapper = reinterpret_cast<visitor_wrapper *>(payload);
wrapper->fn(name, value);
return 0;
};
if (git_attr_foreach(c_ptr_, static_cast<uint32_t>(flags), path.c_str(),
callback_c, (void *)(&wrapper)))
throw git_exception();
}
std::string repository::lookup_attribute(attribute::flag flags,
const std::string &path,
const std::string &name) const {
const char *result;
if (git_attr_get(&result, c_ptr_, static_cast<uint32_t>(flags), path.c_str(),
name.c_str()))
throw git_exception();
if (result)
return std::string(result);
else
return "";
}
std::vector<std::string>
repository::lookup_multiple_attributes(attribute::flag flags,
const std::string &path,
const std::vector<std::string> &names) const {
const char **values = (const char **)malloc(names.size());
// const char *values[names.size()]; // TODO: Fix this
std::vector<const char *> names_c;
for (auto &name : names)
names_c.push_back(name.c_str());
if (git_attr_get_many(values, c_ptr_, static_cast<uint32_t>(flags),
path.c_str(), names.size(), names_c.data()))
throw git_exception();
std::vector<std::string> result;
for (size_t i = 0; i < names.size(); ++i) {
if (values[i]) {
result.push_back(values[i]);
} else {
result.push_back("");
}
}
free(values);
return result;
}
blame repository::blame_file(const std::string &path,
blame::options options) const {
blame result(nullptr, ownership::user);
if (git_blame_file(&result.c_ptr_, c_ptr_, path.c_str(), options.c_ptr_))
throw git_exception();
return result;
}
oid repository::create_blob_from_buffer(const std::string &buffer) const {
oid result;
if (git_blob_create_frombuffer(result.c_ptr(), c_ptr_, buffer.c_str(),
buffer.size()))
throw git_exception();
return result;
}
oid repository::create_blob_from_disk(const std::string &path) const {
oid result;
if (git_blob_create_fromdisk(result.c_ptr(), c_ptr_, path.c_str()))
throw git_exception();
return result;
}
oid repository::create_blob_from_workdir(const std::string &relative_path) const {
oid result;
if (git_blob_create_fromworkdir(result.c_ptr(), c_ptr_,
relative_path.c_str()))
throw git_exception();
return result;
}
blob repository::lookup_blob(const oid &id) const {
blob result;
if (git_blob_lookup(&result.c_ptr_, c_ptr_, id.c_ptr()))
throw git_exception();
return result;
}
blob repository::lookup_blob(const oid &id, size_t len) const {
blob result;
if (git_blob_lookup_prefix(&result.c_ptr_, c_ptr_, id.c_ptr(), len))
throw git_exception();
return result;
}
reference repository::create_branch(const std::string &branch_name,
const commit &target, bool force) const {
reference result(nullptr, ownership::user);
if (git_branch_create(&result.c_ptr_, c_ptr_, branch_name.c_str(),
target.c_ptr(), force))
throw git_exception();
return result;
}
reference repository::create_branch(const std::string &branch_name,
const annotated_commit &commit,
bool force) const {
reference result(nullptr, ownership::user);
if (git_branch_create_from_annotated(
&result.c_ptr_, c_ptr_, branch_name.c_str(), commit.c_ptr(), force))
throw git_exception();
return result;
}
void repository::delete_branch(const reference &ref) const {
if (git_branch_delete(ref.c_ptr_))
throw git_exception();
}
void repository::delete_branch(const std::string &branch_name,
branch::branch_type branch_type) const {
delete_branch(lookup_branch(branch_name, branch_type));
}
bool repository::is_branch_checked_out(const reference &ref) const {
auto ret = git_branch_is_checked_out(ref.c_ptr());
if (ret == 1)
return true;
else if (ret == 0)
return false;
else
throw git_exception();
}
bool repository::is_branch_checked_out(const std::string &branch_name,
branch::branch_type branch_type) const {
return is_branch_checked_out(lookup_branch(branch_name, branch_type));
}
bool repository::is_head_pointing_to_branch(const reference &ref) const {
// 1 if HEAD points at the branch, 0 if it isn't, or a negative value as an
// error code.
auto ret = git_branch_is_head(ref.c_ptr());
if (ret == 1)
return true;
else if (ret == 0)
return false;
else
throw git_exception();
}
bool repository::is_head_pointing_to_branch(const std::string &branch_name,
branch::branch_type branch_type) const {
return is_head_pointing_to_branch(lookup_branch(branch_name, branch_type));
}
reference repository::rename_branch(const reference &ref,
const std::string &new_branch_name,
bool force) const {
reference result(nullptr, ownership::user);
if (git_branch_move(&result.c_ptr_, ref.c_ptr_, new_branch_name.c_str(),
force))
throw git_exception();
return result;
}
reference repository::rename_branch(const std::string &branch_name,
const std::string &new_branch_name,
bool force,
branch::branch_type branch_type) const {
auto ref = lookup_branch(branch_name, branch_type);
return rename_branch(ref, new_branch_name, force);
}
std::string repository::branch_name(const reference &branch) const {
const char *name;
auto ret = git_branch_name(&name, branch.c_ptr());
if (ret == 0) {
if (name)
return std::string(name);
else
return "";
} else
throw git_exception();
}
std::string repository::branch_remote_name(const std::string &refname) const {
data_buffer result;
if (git_branch_remote_name(result.c_ptr(), c_ptr_, refname.c_str()))
throw git_exception();
return result.to_string();
}
void repository::set_branch_upstream(const reference &ref,
const std::string &upstream_name) const {
if (git_branch_set_upstream(ref.c_ptr_, upstream_name.c_str()))
throw git_exception();
}
void repository::set_branch_upstream(const std::string &branch_name,
const std::string &upstream_name) const {
auto ref = lookup_branch(branch_name, branch::branch_type::local);
return set_branch_upstream(ref, upstream_name);
}
void repository::unset_branch_upstream(const reference &ref) const {
if (git_branch_set_upstream(ref.c_ptr_, nullptr))
throw git_exception();
}
void repository::unset_branch_upstream(const std::string &branch_name) const {
auto ref = lookup_branch(branch_name, branch::branch_type::local);
return unset_branch_upstream(ref);
}
reference repository::branch_upstream(const reference &local_branch) const {
reference result(nullptr, ownership::user);
if (git_branch_upstream(&result.c_ptr_, local_branch.c_ptr()))
throw git_exception();
return result;
}
reference repository::branch_upstream(const std::string &local_branch_name) const {
auto ref = lookup_branch(local_branch_name, branch::branch_type::local);
return branch_upstream(ref);
}
std::string repository::branch_upstream_name(const std::string &refname) const {
data_buffer result;
if (git_branch_upstream_name(result.c_ptr(), c_ptr_, refname.c_str()))
throw git_exception();
return result.to_string();
}
std::string
repository::branch_upstream_remote(const std::string &refname) const {
data_buffer result;
if (git_branch_upstream_remote(result.c_ptr(), c_ptr_, refname.c_str()))
throw git_exception();
return result.to_string();
}
reference repository::lookup_branch(const std::string &branch_name,
branch::branch_type branch_type) const {
reference result(nullptr, ownership::user);
if (git_branch_lookup(&result.c_ptr_, c_ptr_, branch_name.c_str(),
static_cast<git_branch_t>(branch_type)))
throw git_exception();
return result;
}
void repository::for_each_branch(std::function<void(const reference &)> visitor,
branch::branch_type branch_type) const {
git_branch_t branch_type_c = static_cast<git_branch_t>(branch_type);
git_branch_iterator *iter;
git_branch_iterator_new(&iter, c_ptr_, branch_type_c);
git_reference *ref_c;
int ret;
while ((ret = git_branch_next(&ref_c, &branch_type_c, iter)) == 0) {
reference payload(ref_c);
visitor(payload);
}
git_branch_iterator_free(iter);
}
void repository::checkout_head(const checkout::options &options) const {
// Note that this is not the correct mechanism used to switch branches; do not
// change your HEAD and then call this method, that would leave you with
// checkout conflicts since your working directory would then appear to be
// dirty. Instead, checkout the target of the branch and then update HEAD
// using repository.set_head to point to the branch you checked out.
if (git_checkout_head(c_ptr_, options.c_ptr())) // options may be NULL
throw git_exception();
}
void repository::checkout_index(const cppgit2::index &index,
const checkout::options &options) const {
if (git_checkout_index(c_ptr_, const_cast<git_index *>(index.c_ptr()),
options.c_ptr())) // index & options may be NULL
throw git_exception();
}
void repository::checkout_tree(const object &treeish,
const checkout::options &options) const {
if (git_checkout_tree(c_ptr_, treeish.c_ptr(), options.c_ptr()))
throw git_exception();
}
void repository::cherrypick_commit(const commit &commit,
const cherrypick::options &options) const {
if (git_cherrypick(c_ptr_, commit.c_ptr_, options.c_ptr()))
throw git_exception();
}
cppgit2::index
repository::cherrypick_commit(const commit &cherrypick_commit,
const commit &our_commit, unsigned int mainline,
const merge::options &merge_options) const {
cppgit2::index result(nullptr, ownership::user);
if (git_cherrypick_commit(&result.c_ptr_, c_ptr_, cherrypick_commit.c_ptr_,
our_commit.c_ptr_, mainline, merge_options.c_ptr()))
throw git_exception();
return result;
}
oid repository::create_commit(const std::string &update_ref,
const signature &author,
const signature &committer,
const std::string &message_encoding,
const std::string &message, const tree &tree,
const std::vector<commit> &parents) const {
oid result;
const char *update_ref_c = (update_ref == "") ? NULL : update_ref.c_str();
const char *message_encoding_c =
message_encoding == "" ? NULL : message_encoding.c_str();
std::vector<const git_commit *> parents_c;
for (auto &p : parents) {
parents_c.push_back(p.c_ptr());
}
if (git_commit_create(result.c_ptr(), c_ptr_, update_ref_c, author.c_ptr(),
committer.c_ptr(), message_encoding_c, message.c_str(),
tree.c_ptr(), parents.size(), parents_c.data()))
throw git_exception();
return result;
}
data_buffer repository::create_commit(const signature &author,
const signature &committer,
const std::string &message_encoding,
const std::string &message,
const tree &tree,
const std::vector<commit> &parents) const {
data_buffer result;
const char *message_encoding_c =
message_encoding == "" ? NULL : message_encoding.c_str();
std::vector<const git_commit *> parents_c;
for (auto &p : parents) {
parents_c.push_back(p.c_ptr());
}
if (git_commit_create_buffer(result.c_ptr(), c_ptr_, author.c_ptr(),
committer.c_ptr(), message_encoding_c,
message.c_str(), tree.c_ptr(), parents.size(),
parents_c.data()))
throw git_exception();
return result;
}
oid repository::create_commit(const std::string &commit_content,
const std::string &signature,
const std::string &signature_field) const {
oid result;
const char *signature_c = signature == "" ? NULL : signature.c_str();
const char *signature_field_c =
signature_field == "" ? "" : signature_field.c_str();
if (git_commit_create_with_signature(result.c_ptr(), c_ptr_,
commit_content.c_str(), signature_c,
signature_field_c))
throw git_exception();
return result;
}
std::pair<data_buffer, data_buffer>
repository::extract_signature_from_commit(oid id,
const std::string &signature_field) const {
data_buffer sig, signed_data;
if (git_commit_extract_signature(sig.c_ptr(), signed_data.c_ptr(), c_ptr_,
id.c_ptr(), signature_field.c_str()))
throw git_exception();
return std::pair<data_buffer, data_buffer>{sig, signed_data};
}
commit repository::lookup_commit(const oid &id) const {
commit result(nullptr, ownership::user);
if (git_commit_lookup(&result.c_ptr_, c_ptr_, id.c_ptr()))
throw git_exception();
return result;
}
commit repository::lookup_commit(const oid &id, size_t length) const {
commit result(nullptr, ownership::user);
if (git_commit_lookup_prefix(&result.c_ptr_, c_ptr_, id.c_ptr(), length))
throw git_exception();
return result;
}
void repository::for_each_commit(std::function<void(const commit &id)> visitor,
revision::sort sort_ordering) const {
git_revwalk *iter;
auto ret = git_revwalk_new(&iter, c_ptr_);
git_revwalk_push_head(iter);
if (ret != 0) {
git_revwalk_free(iter);
throw git_exception();
}
git_revwalk_sorting(iter, static_cast<unsigned int>(sort_ordering));
git_oid id_c;
while ((ret = git_revwalk_next(&id_c, iter)) == 0) {
oid id(&id_c);
visitor(lookup_commit(id));
}
git_revwalk_free(iter);
}
void repository::for_each_commit(std::function<void(const commit &id)> visitor,
const commit &start_from,
revision::sort sort_ordering) const {
git_revwalk *iter;
auto ret = git_revwalk_new(&iter, c_ptr_);
git_revwalk_push(iter, start_from.id().c_ptr());
if (ret != 0) {
git_revwalk_free(iter);
throw git_exception();
}
git_revwalk_sorting(iter, static_cast<unsigned int>(sort_ordering));
git_oid id_c;
while ((ret = git_revwalk_next(&id_c, iter)) == 0) {
oid payload(&id_c);
visitor(lookup_commit(payload));
}
git_revwalk_free(iter);
}
void repository::add_ondisk_config_file(const cppgit2::config &cfg,
const std::string &path,
config::priority_level level,
bool force) const {
if (git_config_add_file_ondisk(cfg.c_ptr_, path.c_str(),
static_cast<git_config_level_t>(level), c_ptr_,
force))
throw git_exception();
}
data_buffer repository::create_diff_commit_as_email(
const commit &commit, size_t patch_no, size_t total_patches,
diff::format_email_flag flags, const diff::options &options) const {
data_buffer result;
if (git_diff_commit_as_email(result.c_ptr(), c_ptr_, commit.c_ptr_, patch_no,
total_patches, static_cast<uint32_t>(flags),
options.c_ptr()))
throw git_exception();
return result;
}
diff repository::create_diff_index_to_index(const cppgit2::index &old_index,
const cppgit2::index &new_index,
const diff::options &options) const {
diff result(nullptr, ownership::user);
if (git_diff_index_to_index(&result.c_ptr_, c_ptr_, old_index.c_ptr_,
new_index.c_ptr_, options.c_ptr()))
throw git_exception();
return result;
}
diff repository::create_diff_index_to_workdir(const cppgit2::index &index,
const diff::options &options) const {
diff result(nullptr, ownership::user);
if (git_diff_index_to_workdir(&result.c_ptr_, c_ptr_, index.c_ptr_,
options.c_ptr()))
throw git_exception();
return result;
}
diff repository::create_diff_tree_to_index(const tree &old_tree,
const cppgit2::index &index,
const diff::options &options) const {
diff result(nullptr, ownership::user);
if (git_diff_tree_to_index(&result.c_ptr_, c_ptr_, old_tree.c_ptr_,
index.c_ptr_, options.c_ptr()))
throw git_exception();
return result;
}
diff repository::create_diff_tree_to_tree(const tree &old_tree,
const tree &new_tree,
const diff::options &options) const {
diff result(nullptr, ownership::user);
if (git_diff_tree_to_tree(&result.c_ptr_, c_ptr_, old_tree.c_ptr_,
new_tree.c_ptr_, options.c_ptr()))
throw git_exception();
return result;
}
diff repository::create_diff_tree_to_workdir(const tree &old_tree,
const diff::options &options) const {
diff result(nullptr, ownership::user);
if (git_diff_tree_to_workdir(&result.c_ptr_, c_ptr_, old_tree.c_ptr_,
options.c_ptr()))
throw git_exception();
return result;
}
diff repository::create_diff_tree_to_workdir_with_index(
const tree &old_tree, const diff::options &options) const {
diff result(nullptr, ownership::user);
if (git_diff_tree_to_workdir_with_index(&result.c_ptr_, c_ptr_,
old_tree.c_ptr_, options.c_ptr()))
throw git_exception();
return result;
}
std::pair<size_t, size_t>
repository::unique_commits_ahead_behind(const oid &local,
const oid &upstream) const {
size_t ahead, behind;
if (git_graph_ahead_behind(&ahead, &behind, c_ptr_, local.c_ptr(),
upstream.c_ptr()))
throw git_exception();
return std::pair<size_t, size_t>{ahead, behind};
}
bool repository::is_descendant_of(const oid &commit,
const oid &ancestor) const {
return git_graph_descendant_of(c_ptr_, commit.c_ptr(), ancestor.c_ptr());
}
void repository::add_ignore_rules(const std::string &rules) const {
if (git_ignore_add_rule(c_ptr_, rules.c_str()))
throw git_exception();
}
void repository::clear_ignore_rules() const {
if (git_ignore_clear_internal_rules(c_ptr_))
throw git_exception();
}
bool repository::is_path_ignored(const std::string &path) const {
int result;
if (git_ignore_path_is_ignored(&result, c_ptr_, path.c_str()))
throw git_exception();
return result;
}
std::pair<merge::analysis_result, merge::preference>
repository::analyze_merge(const std::vector<annotated_commit> &their_heads) const {
git_merge_analysis_t analysis_result;
git_merge_preference_t preference;
std::vector<git_annotated_commit *const *> their_heads_c;
size_t num_commits = their_heads.size();
for (size_t i = 0; i < num_commits; ++i) {
their_heads_c.push_back(&their_heads[i].c_ptr_);