FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
git2cpp/src/wrapper/repository_wrapper.cpp at main · QuantStack/git2cpp · GitHub
QuantStack
git2cpp
Repository navigation
Code
Issues
6
(6)
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
git2cpp
/
src
/
wrapper
/
repository_wrapper.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
664 lines (571 loc) · 18.2 KB
Breadcrumbs
git2cpp
/
src
/
wrapper
/
repository_wrapper.cpp
Copy path
File metadata and controls
664 lines (571 loc) · 18.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
../wrapper/repository_wrapper.hpp
"
#
include
<
algorithm
>
#
include
<
fstream
>
#
include
<
iostream
>
#
include
"
../utils/config_overrides.hpp
"
#
include
"
../utils/git_exception.hpp
"
#
include
"
../wrapper/commit_wrapper.hpp
"
#
include
"
../wrapper/config_wrapper.hpp
"
#
include
"
../wrapper/diff_wrapper.hpp
"
#
include
"
../wrapper/index_wrapper.hpp
"
#
include
"
../wrapper/object_wrapper.hpp
"
#
include
"
../wrapper/remote_wrapper.hpp
"
repository_wrapper::~repository_wrapper
()
{
git_repository_free
(p_resource);
p_resource =
nullptr
;
}
repository_wrapper
repository_wrapper::open
(std::string_view directory)
{
repository_wrapper rw;
throw_if_error
(
git_repository_open
(&(rw.
p_resource
), directory.
data
()));
apply_config_overrides
(rw.
p_resource
);
return
rw;
}
repository_wrapper
repository_wrapper::init
(std::string_view directory,
bool
bare)
{
repository_wrapper rw;
throw_if_error
(
git_repository_init
(&(rw.
p_resource
), directory.
data
(), bare));
apply_config_overrides
(rw.
p_resource
);
return
rw;
}
repository_wrapper
repository_wrapper::init_ext
(std::string_view directory, git_repository_init_options* opts)
{
repository_wrapper rw;
throw_if_error
(
git_repository_init_ext
(&(rw.
p_resource
), directory.
data
(), opts));
apply_config_overrides
(rw.
p_resource
);
return
rw;
}
repository_wrapper
repository_wrapper::clone
(std::string_view url, std::string_view path,
const
git_clone_options& opts)
{
repository_wrapper rw;
throw_if_error
(
git_clone
(&(rw.
p_resource
), url.
data
(), path.
data
(), &opts));
apply_config_overrides
(rw.
p_resource
);
return
rw;
}
std::string
repository_wrapper::path
()
const
{
return
git_repository_path
(*
this
);
}
git_repository_state_t
repository_wrapper::state
()
const
{
return
git_repository_state_t
(
git_repository_state
(*
this
));
}
void
repository_wrapper::state_cleanup
()
{
throw_if_error
(
git_repository_state_cleanup
(*
this
));
}
bool
repository_wrapper::is_bare
()
const
{
return
git_repository_is_bare
(*
this
);
}
bool
repository_wrapper::is_shallow
()
const
{
return
git_repository_is_shallow
(*
this
);
}
revwalk_wrapper
repository_wrapper::new_walker
()
{
git_revwalk* walker;
throw_if_error
(
git_revwalk_new
(&walker, *
this
));
return
revwalk_wrapper
(walker);
}
bool
repository_wrapper::does_track
(std::string_view path)
const
{
unsigned
int
flags;
throw_if_error
(
git_status_file
(&flags, *
this
, path.
data
()));
return
!(flags &
GIT_STATUS_WT_NEW
) && !(flags &
GIT_STATUS_IGNORED
);
}
//
Head
bool
repository_wrapper::is_head_unborn
()
const
{
return
git_repository_head_unborn
(*
this
) ==
1
;
}
reference_wrapper
repository_wrapper::head
()
const
{
git_reference* ref;
throw_if_error
(
git_repository_head
(&ref, *
this
));
return
reference_wrapper
(ref);
}
std::string
repository_wrapper::head_short_name
()
const
{
git_reference* ref;
std::string name;
throw_if_error
(
git_reference_lookup
(&ref, *
this
,
"
HEAD
"
));
if
(
git_reference_type
(ref) ==
GIT_REFERENCE_DIRECT
)
{
name =
git_reference_shorthand
(ref);
}
else
{
name =
git_reference_symbolic_target
(ref);
name = name.
substr
(name.
find_last_of
(
'
/
'
) +
1
);
}
git_reference_free
(ref);
return
name;
}
//
References
reference_wrapper
repository_wrapper::find_reference
(std::string_view ref_name)
const
{
git_reference* ref;
throw_if_error
(
git_reference_lookup
(&ref, *
this
, ref_name.
data
()));
return
reference_wrapper
(ref);
}
std::optional<reference_wrapper>
repository_wrapper::find_reference_dwim
(std::string_view ref_name)
const
{
git_reference* ref;
int
rc =
git_reference_dwim
(&ref, *
this
, ref_name.
data
());
return
rc ==
0
?
std::make_optional
(
reference_wrapper
(ref)) : std::
nullopt
;
}
std::vector<std::string>
repository_wrapper::refs_list
()
const
{
git_strarray refs = {
0
};
throw_if_error
(
git_reference_list
(&refs, *
this
));
std::vector<std::string> result;
for
(
size_t
i =
0
; i < refs.
count
; ++i)
{
result.
push_back
(refs.
strings
[i]);
}
git_strarray_free
(&refs);
return
result;
}
const
git_oid
repository_wrapper::ref_name_to_id
(std::string ref_name)
const
{
git_oid ref_id;
throw_if_error
(
git_reference_name_to_id
(&ref_id, *
this
, ref_name.
c_str
()));
return
ref_id;
}
//
Index
index_wrapper
repository_wrapper::make_index
()
{
index_wrapper index =
index_wrapper::init
(*
this
);
return
index;
}
//
Branches
branch_wrapper
repository_wrapper::create_branch
(std::string_view name,
bool
force)
{
return
create_branch
(name,
find_commit
(), force);
}
branch_wrapper
repository_wrapper::create_branch
(std::string_view name,
const
commit_wrapper& commit,
bool
force)
{
git_reference* branch =
nullptr
;
throw_if_error
(
git_branch_create
(&branch, *
this
, name.
data
(), commit, force));
return
branch_wrapper
(branch);
}
branch_wrapper
repository_wrapper::create_branch
(std::string_view name,
const
annotated_commit_wrapper& commit,
bool
force)
{
git_reference* branch =
nullptr
;
throw_if_error
(
git_branch_create_from_annotated
(&branch, *
this
, name.
data
(), commit, force));
return
branch_wrapper
(branch);
}
branch_wrapper
repository_wrapper::find_branch
(std::string_view name)
const
{
git_reference* branch =
nullptr
;
throw_if_error
(
git_branch_lookup
(&branch, *
this
, name.
data
(),
GIT_BRANCH_LOCAL
));
return
branch_wrapper
(branch);
}
branch_iterator
repository_wrapper::iterate_branches
(
git_branch_t
type)
const
{
git_branch_iterator* iter =
nullptr
;
throw_if_error
(
git_branch_iterator_new
(&iter, *
this
, type));
return
branch_iterator
(iter);
}
std::optional<reference_wrapper>
repository_wrapper::upstream
()
const
{
git_reference* ref;
reference_wrapper head =
this
->
head
();
int
error =
git_branch_upstream
(&ref, head);
if
(error ==
0
)
{
return
reference_wrapper
(ref);
}
else
{
return
std::
nullopt
;
}
}
std::optional<std::string>
repository_wrapper::branch_upstream_name
(std::string local_branch)
const
{
git_buf buf =
GIT_BUF_INIT
;
int
error =
git_branch_upstream_name
(&buf, *
this
, local_branch.
c_str
());
if
(error !=
0
)
{
git_buf_dispose
(&buf);
return
std::
nullopt
;
}
std::string
result
(buf.
ptr
? buf.
ptr
:
"
"
);
git_buf_dispose
(&buf);
return
result;
}
branch_tracking_info
repository_wrapper::get_tracking_info
()
const
{
branch_tracking_info info;
info.
has_upstream
=
false
;
info.
ahead
=
0
;
info.
behind
=
0
;
info.
upstream_name
=
"
"
;
if
(
this
->
is_head_unborn
())
{
return
info;
}
reference_wrapper head =
this
->
head
();
std::optional<reference_wrapper> upstream =
this
->
upstream
();
if
(upstream)
{
info.
has_upstream
=
true
;
info.
upstream_name
= upstream.
value
().
short_name
();
auto
local_oid = head.
target
();
auto
upstream_oid = upstream.
value
().
target
();
if
(local_oid && upstream_oid)
{
git_graph_ahead_behind
(&info.
ahead
, &info.
behind
, *
this
, local_oid, upstream_oid);
}
}
return
info;
}
//
Commits
commit_wrapper
repository_wrapper::find_commit
(std::string_view ref_name)
const
{
git_oid oid_parent_commit;
throw_if_error
(
git_reference_name_to_id
(&oid_parent_commit, *
this
, ref_name.
data
()));
return
find_commit
(oid_parent_commit);
}
commit_wrapper
repository_wrapper::find_commit
(
const
git_oid& id)
const
{
git_commit* commit;
throw_if_error
(
git_commit_lookup
(&commit, *
this
, &id));
return
commit_wrapper
(commit);
}
void
repository_wrapper::create_commit
(
const
signature_wrapper::author_committer_signatures& author_committer_signatures,
const
std::string_view message,
const
std::optional<commit_list_wrapper>& parents_list
)
{
const
char
* message_encoding =
"
UTF-8
"
;
git_oid commit_id;
std::string update_ref =
"
HEAD
"
;
const
git_commit* placeholder[
1
] = {
nullptr
};
auto
[parents, parents_count] = [&]() -> std::pair<
const
git_commit**,
size_t
>
{
if
(parents_list)
{
auto
pl_size = parents_list.
value
().
size
();
git_commit** pl_value = parents_list.
value
();
auto
pl_value_const =
const_cast
<
const
git_commit**>(pl_value);
return
{pl_value_const, pl_size};
}
else
{
auto
parent =
revparse_single
(update_ref);
size_t
parents_count =
0
;
if
(parent)
{
parents_count =
1
;
placeholder[
0
] = *parent;
}
return
{placeholder, parents_count};
}
}();
index_wrapper index =
this
->
make_index
();
//
Check: initial commit (no parents) with nothing staged
if
(parents_count ==
0
&& index.
entry_count
() ==
0
)
{
throw
std::runtime_error
(
"
On branch main
\n\n
Initial commit
\n\n
nothing to commit
"
"
(create/copy files and use
\"
git add
\"
to track)
"
);
}
git_oid tree_id = index.
write_tree
();
index.
write
();
//
Check: tree is identical to parent tree (nothing changed since last commit)
if
(parents_count >
0
&& placeholder[
0
] !=
nullptr
)
{
const
git_oid* parent_tree_id =
git_commit_tree_id
(placeholder[
0
]);
if
(
git_oid_equal
(&tree_id, parent_tree_id))
{
throw
std::runtime_error
(
"
nothing to commit, working tree clean
"
);
}
}
auto
tree =
this
->
tree_lookup
(&tree_id);
throw_if_error
(
git_commit_create
(
&commit_id,
*
this
,
update_ref.
c_str
(),
author_committer_signatures.
first
,
author_committer_signatures.
second
,
message_encoding,
message.
data
(),
tree,
parents_count,
parents
));
}
std::optional<annotated_commit_wrapper>
repository_wrapper::resolve_local_ref
(
const
std::string_view target_name)
const
{
if
(
auto
ref =
this
->
find_reference_dwim
(target_name))
{
return
this
->
find_annotated_commit
(*ref);
}
else
if
(
auto
obj =
this
->
revparse_single
(target_name))
{
return
this
->
find_annotated_commit
(obj->
oid
());
}
else
{
return
std::
nullopt
;
}
}
//
Annotated commits
annotated_commit_wrapper
repository_wrapper::find_annotated_commit
(
const
git_oid& id)
const
{
git_annotated_commit* commit;
throw_if_error
(
git_annotated_commit_lookup
(&commit, *
this
, &id));
return
annotated_commit_wrapper
(commit);
}
//
Objects
std::optional<object_wrapper>
repository_wrapper::revparse_single
(std::string_view spec)
const
{
git_object* obj;
int
rc =
git_revparse_single
(&obj, *
this
, spec.
data
());
return
rc ==
0
?
std::make_optional
(
object_wrapper
(obj)) : std::
nullopt
;
}
object_wrapper
repository_wrapper::find_object
(
const
git_oid id,
git_object_t
type)
{
git_object* object;
git_object_lookup
(&object, *
this
, &id, type);
return
object_wrapper
(object);
}
//
Head manipulations
void
repository_wrapper::set_head
(std::string_view ref_name)
{
throw_if_error
(
git_repository_set_head
(*
this
, ref_name.
data
()));
}
void
repository_wrapper::set_head_detached
(
const
annotated_commit_wrapper& commit)
{
throw_if_error
(
git_repository_set_head_detached_from_annotated
(*
this
, commit));
}
void
repository_wrapper::reset
(
const
object_wrapper& target,
git_reset_t
reset_type,
const
git_checkout_options& checkout_options
)
{
//
TODO: gerer l'index
throw_if_error
(
git_reset
(*
this
, target, reset_type, &checkout_options));
}
size_t
repository_wrapper::shallow_depth_from_head
()
const
{
if
(!
this
->
is_shallow
())
{
return
0u
;
}
std::string path =
this
->
path
();
std::string shallow_path = path +
"
shallow
"
;
std::vector<git_oid> boundaries_list;
std::ifstream
f
(shallow_path);
std::string line;
while
(
std::getline
(f, line))
{
if
(!line.
empty
())
{
git_oid commit_oid;
git_oid_fromstrp
(&commit_oid, line.
c_str
());
boundaries_list.
push_back
(commit_oid);
}
}
if
(boundaries_list.
size
() ==
0u
)
{
return
0u
;
}
commit_wrapper head_commit =
this
->
find_commit
(
"
HEAD
"
);
commit_list_wrapper commits_list = head_commit.
get_parents_list
();
std::vector<
size_t
>
depth_list
(commits_list.
size
(),
1u
);
std::vector<
size_t
>
final_depths
(boundaries_list.
size
(),
1u
);
bool
has_parent = commits_list.
size
() >
0u
;
while
(has_parent)
{
has_parent =
false
;
std::vector<commit_wrapper> temp_commits_list;
std::vector<
size_t
> temp_depth_list;
commit_list_wrapper
parent_list
({});
for
(
size_t
i =
0u
; i < commits_list.
size
(); i++)
{
const
commit_wrapper& commit = commits_list[i];
size_t
depth = depth_list[i];
const
git_oid& oid = commit.
oid
();
bool
is_boundary =
std::find_if
(
boundaries_list.
cbegin
(),
boundaries_list.
cend
(),
[oid](
const
git_oid& val)
{
return
git_oid_equal
(&oid, &val);
}
)
!= boundaries_list.
cend
();
if
(is_boundary)
{
final_depths.
push_back
(depth +
1u
);
}
else
{
parent_list = commit.
get_parents_list
();
if
(parent_list.
size
() >
0u
)
{
has_parent =
true
;
for
(
size_t
j =
0u
; j < parent_list.
size
(); ++j)
{
const
commit_wrapper& c = parent_list[j];
temp_commits_list.
push_back
(
std::move
(
const_cast
<commit_wrapper&>(c)));
temp_depth_list.
push_back
(depth +
1u
);
}
}
}
}
depth_list = temp_depth_list;
commits_list =
commit_list_wrapper
(
std::move
(temp_commits_list));
}
std::
size_t
depth = *
std::max_element
(final_depths.
begin
(), final_depths.
end
());
return
depth;
}
//
Trees
void
repository_wrapper::checkout_tree
(
const
object_wrapper& target,
const
git_checkout_options opts)
{
throw_if_error
(
git_checkout_tree
(*
this
, target, &opts));
}
tree_wrapper
repository_wrapper::tree_lookup
(
const
git_oid* tree_id)
{
git_tree* tree;
throw_if_error
(
git_tree_lookup
(&tree, *
this
, tree_id));
return
tree_wrapper
(tree);
}
tree_wrapper
repository_wrapper::treeish_to_tree
(
const
std::string& treeish)
{
auto
obj =
this
->
revparse_single
(treeish.
c_str
());
git_tree* tree =
nullptr
;
throw_if_error
(
git_object_peel
(
reinterpret_cast
<git_object**>(&tree), obj.
value
(),
GIT_OBJECT_TREE
));
return
tree_wrapper
(tree);
}
//
Remotes
remote_wrapper
repository_wrapper::find_remote
(std::string_view name)
const
{
git_remote* remote =
nullptr
;
throw_if_error
(
git_remote_lookup
(&remote, *
this
, name.
data
()));
return
remote_wrapper
(remote);
}
remote_wrapper
repository_wrapper::create_remote
(std::string_view name, std::string_view url)
{
git_remote* remote =
nullptr
;
throw_if_error
(
git_remote_create
(&remote, *
this
, name.
data
(), url.
data
()));
return
remote_wrapper
(remote);
}
void
repository_wrapper::delete_remote
(std::string_view name)
{
throw_if_error
(
git_remote_delete
(*
this
, name.
data
()));
}
void
repository_wrapper::rename_remote
(std::string_view old_name, std::string_view new_name)
{
git_strarray problems = {
0
};
int
error =
git_remote_rename
(&problems, *
this
, old_name.
data
(), new_name.
data
());
if
(error !=
0
)
{
for
(
size_t
i =
0
; i < problems.
count
; ++i)
{
std::cerr << problems.
strings
[i] << std::endl;
}
git_strarray_dispose
(&problems);
throw_if_error
(error);
}
git_strarray_dispose
(&problems);
}
void
repository_wrapper::set_remote_url
(std::string_view name, std::string_view url,
bool
push)
{
if
(push)
{
throw_if_error
(
git_remote_set_pushurl
(*
this
, name.
data
(), url.
data
()));
}
else
{
throw_if_error
(
git_remote_set_url
(*
this
, name.
data
(), url.
data
()));
}
}
std::vector<std::string>
repository_wrapper::list_remotes
()
const
{
git_strarray remotes = {
0
};
throw_if_error
(
git_remote_list
(&remotes, *
this
));
std::vector<std::string> result;
for
(
size_t
i =
0
; i < remotes.
count
; ++i)
{
result.
emplace_back
(remotes.
strings
[i]);
}
git_strarray_dispose
(&remotes);
return
result;
}
//
Config
config_wrapper
repository_wrapper::get_config
()
{
git_config* cfg;
throw_if_error
(
git_repository_config
(&cfg, *
this
));
return
config_wrapper
(cfg);
}
//
Diff
diff_wrapper
repository_wrapper::diff_tree_to_index
(
const
tree_wrapper& old_tree,
std::optional<index_wrapper> index,
git_diff_options* diffopts
)
{
git_diff* diff;
git_index* idx =
nullptr
;
if
(index)
{
idx = *index;
}
throw_if_error
(
git_diff_tree_to_index
(&diff, *
this
, old_tree, idx, diffopts));
return
diff_wrapper
(diff);
}
diff_wrapper
repository_wrapper::diff_tree_to_tree
(
const
tree_wrapper& old_tree,
const
tree_wrapper& new_tree,
git_diff_options* diffopts
)
{
git_diff* diff;
throw_if_error
(
git_diff_tree_to_tree
(&diff, *
this
, old_tree, new_tree, diffopts));
return
diff_wrapper
(diff);
}
diff_wrapper
repository_wrapper::diff_tree_to_workdir
(
const
tree_wrapper& old_tree, git_diff_options* diffopts)
{
git_diff* diff;
throw_if_error
(
git_diff_tree_to_workdir
(&diff, *
this
, old_tree, diffopts));
return
diff_wrapper
(diff);
}
diff_wrapper
repository_wrapper::diff_tree_to_workdir_with_index
(
const
tree_wrapper& old_tree, git_diff_options* diffopts)
{
git_diff* diff;
throw_if_error
(
git_diff_tree_to_workdir_with_index
(&diff, *
this
, old_tree, diffopts));
return
diff_wrapper
(diff);
}
diff_wrapper
repository_wrapper::diff_index_to_workdir
(std::optional<index_wrapper> index, git_diff_options* diffopts)
{
git_diff* diff;
git_index* idx =
nullptr
;
if
(index)
{
idx = *index;
}
throw_if_error
(
git_diff_index_to_workdir
(&diff, *
this
, idx, diffopts));
return
diff_wrapper
(diff);
}
//
Tags
strarray_owned_wrapper
repository_wrapper::tag_list_match
(std::string pattern)
{
strarray_owned_wrapper tag_names;
throw_if_error
(
git_tag_list_match
(tag_names, pattern.
c_str
(), *
this
));
return
tag_names;
}
Back
|
FazBrowse Home
|
New Git URL