Skip to content

Commit 7be9e41

Browse files
pks-tgitster
authored andcommitted
commit-graph: stop passing in redundant repository
Many of the commit-graph related functions take in both a repository and the object database source (directly or via `struct commit_graph`) for which we are supposed to load such a commit-graph. In the best case this information is simply redundant as the source already contains a reference to its owning object database, which in turn has a reference to its repository. In the worst case this information could even mismatch when passing in a source that doesn't belong to the same repository. Refactor the code so that we only pass in the object database source in those cases. There is one exception though, namely `load_commit_graph_chain_fd_st()`, which is responsible for loading a commit-graph chain. It is expected that parts of the commit-graph chain aren't located in the same object source as the chain file itself, but in a different one. Consequently, this function doesn't work on the source level but on the database level instead. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ddacfc7 commit 7be9e41

File tree

4 files changed

+59
-81
lines changed

4 files changed

+59
-81
lines changed

builtin/commit-graph.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ static int graph_verify(int argc, const char **argv, const char *prefix,
122122
if (opened == OPENED_NONE)
123123
return 0;
124124
else if (opened == OPENED_GRAPH)
125-
graph = load_commit_graph_one_fd_st(the_repository, fd, &st, source);
125+
graph = load_commit_graph_one_fd_st(source, fd, &st);
126126
else
127-
graph = load_commit_graph_chain_fd_st(the_repository, fd, &st,
127+
graph = load_commit_graph_chain_fd_st(the_repository->objects, fd, &st,
128128
&incomplete_chain);
129129

130130
if (!graph)
131131
return 1;
132132

133-
ret = verify_commit_graph(the_repository, graph, flags);
133+
ret = verify_commit_graph(graph, flags);
134134
free_commit_graph(graph);
135135

136136
if (incomplete_chain) {

commit-graph.c

Lines changed: 50 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -253,25 +253,24 @@ int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
253253
return 1;
254254
}
255255

256-
struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
257-
int fd, struct stat *st,
258-
struct odb_source *source)
256+
struct commit_graph *load_commit_graph_one_fd_st(struct odb_source *source,
257+
int fd, struct stat *st)
259258
{
260259
void *graph_map;
261260
size_t graph_size;
262261
struct commit_graph *ret;
263262

264263
graph_size = xsize_t(st->st_size);
265264

266-
if (graph_size < graph_min_size(r->hash_algo)) {
265+
if (graph_size < graph_min_size(source->odb->repo->hash_algo)) {
267266
close(fd);
268267
error(_("commit-graph file is too small"));
269268
return NULL;
270269
}
271270
graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
272271
close(fd);
273272

274-
ret = parse_commit_graph(r, graph_map, graph_size);
273+
ret = parse_commit_graph(source->odb->repo, graph_map, graph_size);
275274
if (ret)
276275
ret->odb_source = source;
277276
else
@@ -491,11 +490,9 @@ struct commit_graph *parse_commit_graph(struct repository *r,
491490
return NULL;
492491
}
493492

494-
static struct commit_graph *load_commit_graph_one(struct repository *r,
495-
const char *graph_file,
496-
struct odb_source *source)
493+
static struct commit_graph *load_commit_graph_one(struct odb_source *source,
494+
const char *graph_file)
497495
{
498-
499496
struct stat st;
500497
int fd;
501498
struct commit_graph *g;
@@ -504,19 +501,17 @@ static struct commit_graph *load_commit_graph_one(struct repository *r,
504501
if (!open_ok)
505502
return NULL;
506503

507-
g = load_commit_graph_one_fd_st(r, fd, &st, source);
508-
504+
g = load_commit_graph_one_fd_st(source, fd, &st);
509505
if (g)
510506
g->filename = xstrdup(graph_file);
511507

512508
return g;
513509
}
514510

515-
static struct commit_graph *load_commit_graph_v1(struct repository *r,
516-
struct odb_source *source)
511+
static struct commit_graph *load_commit_graph_v1(struct odb_source *source)
517512
{
518513
char *graph_name = get_commit_graph_filename(source);
519-
struct commit_graph *g = load_commit_graph_one(r, graph_name, source);
514+
struct commit_graph *g = load_commit_graph_one(source, graph_name);
520515
free(graph_name);
521516

522517
return g;
@@ -643,7 +638,7 @@ int open_commit_graph_chain(const char *chain_file,
643638
return 1;
644639
}
645640

646-
struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
641+
struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb,
647642
int fd, struct stat *st,
648643
int *incomplete_chain)
649644
{
@@ -653,28 +648,28 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
653648
int i = 0, valid = 1, count;
654649
FILE *fp = xfdopen(fd, "r");
655650

656-
count = st->st_size / (r->hash_algo->hexsz + 1);
651+
count = st->st_size / (odb->repo->hash_algo->hexsz + 1);
657652
CALLOC_ARRAY(oids, count);
658653

659-
odb_prepare_alternates(r->objects);
654+
odb_prepare_alternates(odb);
660655

661656
for (i = 0; i < count; i++) {
662657
struct odb_source *source;
663658

664659
if (strbuf_getline_lf(&line, fp) == EOF)
665660
break;
666661

667-
if (get_oid_hex_algop(line.buf, &oids[i], r->hash_algo)) {
662+
if (get_oid_hex_algop(line.buf, &oids[i], odb->repo->hash_algo)) {
668663
warning(_("invalid commit-graph chain: line '%s' not a hash"),
669664
line.buf);
670665
valid = 0;
671666
break;
672667
}
673668

674669
valid = 0;
675-
for (source = r->objects->sources; source; source = source->next) {
670+
for (source = odb->sources; source; source = source->next) {
676671
char *graph_name = get_split_graph_filename(source, line.buf);
677-
struct commit_graph *g = load_commit_graph_one(r, graph_name, source);
672+
struct commit_graph *g = load_commit_graph_one(source, graph_name);
678673

679674
free(graph_name);
680675

@@ -707,45 +702,33 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
707702
return graph_chain;
708703
}
709704

710-
static struct commit_graph *load_commit_graph_chain(struct repository *r,
711-
struct odb_source *source)
705+
static struct commit_graph *load_commit_graph_chain(struct odb_source *source)
712706
{
713707
char *chain_file = get_commit_graph_chain_filename(source);
714708
struct stat st;
715709
int fd;
716710
struct commit_graph *g = NULL;
717711

718-
if (open_commit_graph_chain(chain_file, &fd, &st, r->hash_algo)) {
712+
if (open_commit_graph_chain(chain_file, &fd, &st, source->odb->repo->hash_algo)) {
719713
int incomplete;
720714
/* ownership of fd is taken over by load function */
721-
g = load_commit_graph_chain_fd_st(r, fd, &st, &incomplete);
715+
g = load_commit_graph_chain_fd_st(source->odb, fd, &st, &incomplete);
722716
}
723717

724718
free(chain_file);
725719
return g;
726720
}
727721

728-
struct commit_graph *read_commit_graph_one(struct repository *r,
729-
struct odb_source *source)
722+
struct commit_graph *read_commit_graph_one(struct odb_source *source)
730723
{
731-
struct commit_graph *g = load_commit_graph_v1(r, source);
724+
struct commit_graph *g = load_commit_graph_v1(source);
732725

733726
if (!g)
734-
g = load_commit_graph_chain(r, source);
727+
g = load_commit_graph_chain(source);
735728

736729
return g;
737730
}
738731

739-
static void prepare_commit_graph_one(struct repository *r,
740-
struct odb_source *source)
741-
{
742-
743-
if (r->objects->commit_graph)
744-
return;
745-
746-
r->objects->commit_graph = read_commit_graph_one(r, source);
747-
}
748-
749732
/*
750733
* Return 1 if commit_graph is non-NULL, and 0 otherwise.
751734
*
@@ -786,10 +769,12 @@ static int prepare_commit_graph(struct repository *r)
786769
return 0;
787770

788771
odb_prepare_alternates(r->objects);
789-
for (source = r->objects->sources;
790-
!r->objects->commit_graph && source;
791-
source = source->next)
792-
prepare_commit_graph_one(r, source);
772+
for (source = r->objects->sources; source; source = source->next) {
773+
r->objects->commit_graph = read_commit_graph_one(source);
774+
if (r->objects->commit_graph)
775+
break;
776+
}
777+
793778
return !!r->objects->commit_graph;
794779
}
795780

@@ -874,8 +859,7 @@ static void load_oid_from_graph(struct commit_graph *g,
874859
g->hash_algo);
875860
}
876861

877-
static struct commit_list **insert_parent_or_die(struct repository *r,
878-
struct commit_graph *g,
862+
static struct commit_list **insert_parent_or_die(struct commit_graph *g,
879863
uint32_t pos,
880864
struct commit_list **pptr)
881865
{
@@ -886,7 +870,7 @@ static struct commit_list **insert_parent_or_die(struct repository *r,
886870
die("invalid parent position %"PRIu32, pos);
887871

888872
load_oid_from_graph(g, pos, &oid);
889-
c = lookup_commit(r, &oid);
873+
c = lookup_commit(g->odb_source->odb->repo, &oid);
890874
if (!c)
891875
die(_("could not find commit %s"), oid_to_hex(&oid));
892876
commit_graph_data_at(c)->graph_pos = pos;
@@ -942,8 +926,7 @@ static inline void set_commit_tree(struct commit *c, struct tree *t)
942926
c->maybe_tree = t;
943927
}
944928

945-
static int fill_commit_in_graph(struct repository *r,
946-
struct commit *item,
929+
static int fill_commit_in_graph(struct commit *item,
947930
struct commit_graph *g, uint32_t pos)
948931
{
949932
uint32_t edge_value;
@@ -969,13 +952,13 @@ static int fill_commit_in_graph(struct repository *r,
969952
edge_value = get_be32(commit_data + g->hash_algo->rawsz);
970953
if (edge_value == GRAPH_PARENT_NONE)
971954
return 1;
972-
pptr = insert_parent_or_die(r, g, edge_value, pptr);
955+
pptr = insert_parent_or_die(g, edge_value, pptr);
973956

974957
edge_value = get_be32(commit_data + g->hash_algo->rawsz + 4);
975958
if (edge_value == GRAPH_PARENT_NONE)
976959
return 1;
977960
if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
978-
pptr = insert_parent_or_die(r, g, edge_value, pptr);
961+
pptr = insert_parent_or_die(g, edge_value, pptr);
979962
return 1;
980963
}
981964

@@ -990,7 +973,7 @@ static int fill_commit_in_graph(struct repository *r,
990973
}
991974
edge_value = get_be32(g->chunk_extra_edges +
992975
sizeof(uint32_t) * parent_data_pos);
993-
pptr = insert_parent_or_die(r, g,
976+
pptr = insert_parent_or_die(g,
994977
edge_value & GRAPH_EDGE_LAST_MASK,
995978
pptr);
996979
parent_data_pos++;
@@ -1056,14 +1039,13 @@ struct commit *lookup_commit_in_graph(struct repository *repo, const struct obje
10561039
if (commit->object.parsed)
10571040
return commit;
10581041

1059-
if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
1042+
if (!fill_commit_in_graph(commit, repo->objects->commit_graph, pos))
10601043
return NULL;
10611044

10621045
return commit;
10631046
}
10641047

1065-
static int parse_commit_in_graph_one(struct repository *r,
1066-
struct commit_graph *g,
1048+
static int parse_commit_in_graph_one(struct commit_graph *g,
10671049
struct commit *item)
10681050
{
10691051
uint32_t pos;
@@ -1072,7 +1054,7 @@ static int parse_commit_in_graph_one(struct repository *r,
10721054
return 1;
10731055

10741056
if (find_commit_pos_in_graph(item, g, &pos))
1075-
return fill_commit_in_graph(r, item, g, pos);
1057+
return fill_commit_in_graph(item, g, pos);
10761058

10771059
return 0;
10781060
}
@@ -1089,7 +1071,7 @@ int parse_commit_in_graph(struct repository *r, struct commit *item)
10891071

10901072
if (!prepare_commit_graph(r))
10911073
return 0;
1092-
return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
1074+
return parse_commit_in_graph_one(r->objects->commit_graph, item);
10931075
}
10941076

10951077
void load_commit_graph_info(struct repository *r, struct commit *item)
@@ -1099,8 +1081,7 @@ void load_commit_graph_info(struct repository *r, struct commit *item)
10991081
fill_commit_graph_info(item, r->objects->commit_graph, pos);
11001082
}
11011083

1102-
static struct tree *load_tree_for_commit(struct repository *r,
1103-
struct commit_graph *g,
1084+
static struct tree *load_tree_for_commit(struct commit_graph *g,
11041085
struct commit *c)
11051086
{
11061087
struct object_id oid;
@@ -1115,26 +1096,25 @@ static struct tree *load_tree_for_commit(struct repository *r,
11151096
graph_pos - g->num_commits_in_base);
11161097

11171098
oidread(&oid, commit_data, g->hash_algo);
1118-
set_commit_tree(c, lookup_tree(r, &oid));
1099+
set_commit_tree(c, lookup_tree(g->odb_source->odb->repo, &oid));
11191100

11201101
return c->maybe_tree;
11211102
}
11221103

1123-
static struct tree *get_commit_tree_in_graph_one(struct repository *r,
1124-
struct commit_graph *g,
1104+
static struct tree *get_commit_tree_in_graph_one(struct commit_graph *g,
11251105
const struct commit *c)
11261106
{
11271107
if (c->maybe_tree)
11281108
return c->maybe_tree;
11291109
if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
11301110
BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
11311111

1132-
return load_tree_for_commit(r, g, (struct commit *)c);
1112+
return load_tree_for_commit(g, (struct commit *)c);
11331113
}
11341114

11351115
struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
11361116
{
1137-
return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
1117+
return get_commit_tree_in_graph_one(r->objects->commit_graph, c);
11381118
}
11391119

11401120
struct packed_commit_list {
@@ -2741,11 +2721,11 @@ static int commit_graph_checksum_valid(struct commit_graph *g)
27412721
g->data, g->data_len);
27422722
}
27432723

2744-
static int verify_one_commit_graph(struct repository *r,
2745-
struct commit_graph *g,
2724+
static int verify_one_commit_graph(struct commit_graph *g,
27462725
struct progress *progress,
27472726
uint64_t *seen)
27482727
{
2728+
struct repository *r = g->odb_source->odb->repo;
27492729
uint32_t i, cur_fanout_pos = 0;
27502730
struct object_id prev_oid, cur_oid;
27512731
struct commit *seen_gen_zero = NULL;
@@ -2779,7 +2759,7 @@ static int verify_one_commit_graph(struct repository *r,
27792759
}
27802760

27812761
graph_commit = lookup_commit(r, &cur_oid);
2782-
if (!parse_commit_in_graph_one(r, g, graph_commit))
2762+
if (!parse_commit_in_graph_one(g, graph_commit))
27832763
graph_report(_("failed to parse commit %s from commit-graph"),
27842764
oid_to_hex(&cur_oid));
27852765
}
@@ -2815,7 +2795,7 @@ static int verify_one_commit_graph(struct repository *r,
28152795
continue;
28162796
}
28172797

2818-
if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2798+
if (!oideq(&get_commit_tree_in_graph_one(g, graph_commit)->object.oid,
28192799
get_commit_tree_oid(odb_commit)))
28202800
graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
28212801
oid_to_hex(&cur_oid),
@@ -2833,7 +2813,7 @@ static int verify_one_commit_graph(struct repository *r,
28332813
}
28342814

28352815
/* parse parent in case it is in a base graph */
2836-
parse_commit_in_graph_one(r, g, graph_parents->item);
2816+
parse_commit_in_graph_one(g, graph_parents->item);
28372817

28382818
if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
28392819
graph_report(_("commit-graph parent for %s is %s != %s"),
@@ -2893,7 +2873,7 @@ static int verify_one_commit_graph(struct repository *r,
28932873
return verify_commit_graph_error;
28942874
}
28952875

2896-
int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2876+
int verify_commit_graph(struct commit_graph *g, int flags)
28972877
{
28982878
struct progress *progress = NULL;
28992879
int local_error = 0;
@@ -2909,13 +2889,13 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
29092889
if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW))
29102890
total += g->num_commits_in_base;
29112891

2912-
progress = start_progress(r,
2892+
progress = start_progress(g->odb_source->odb->repo,
29132893
_("Verifying commits in commit graph"),
29142894
total);
29152895
}
29162896

29172897
for (; g; g = g->base_graph) {
2918-
local_error |= verify_one_commit_graph(r, g, progress, &seen);
2898+
local_error |= verify_one_commit_graph(g, progress, &seen);
29192899
if (flags & COMMIT_GRAPH_VERIFY_SHALLOW)
29202900
break;
29212901
}

0 commit comments

Comments
 (0)