Skip to content

Commit f3f1b3b

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 238b369 commit f3f1b3b

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
@@ -251,25 +251,24 @@ int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
251251
return 1;
252252
}
253253

254-
struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
255-
int fd, struct stat *st,
256-
struct odb_source *source)
254+
struct commit_graph *load_commit_graph_one_fd_st(struct odb_source *source,
255+
int fd, struct stat *st)
257256
{
258257
void *graph_map;
259258
size_t graph_size;
260259
struct commit_graph *ret;
261260

262261
graph_size = xsize_t(st->st_size);
263262

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

272-
ret = parse_commit_graph(r, graph_map, graph_size);
271+
ret = parse_commit_graph(source->odb->repo, graph_map, graph_size);
273272
if (ret)
274273
ret->odb_source = source;
275274
else
@@ -489,11 +488,9 @@ struct commit_graph *parse_commit_graph(struct repository *r,
489488
return NULL;
490489
}
491490

492-
static struct commit_graph *load_commit_graph_one(struct repository *r,
493-
const char *graph_file,
494-
struct odb_source *source)
491+
static struct commit_graph *load_commit_graph_one(struct odb_source *source,
492+
const char *graph_file)
495493
{
496-
497494
struct stat st;
498495
int fd;
499496
struct commit_graph *g;
@@ -502,19 +499,17 @@ static struct commit_graph *load_commit_graph_one(struct repository *r,
502499
if (!open_ok)
503500
return NULL;
504501

505-
g = load_commit_graph_one_fd_st(r, fd, &st, source);
506-
502+
g = load_commit_graph_one_fd_st(source, fd, &st);
507503
if (g)
508504
g->filename = xstrdup(graph_file);
509505

510506
return g;
511507
}
512508

513-
static struct commit_graph *load_commit_graph_v1(struct repository *r,
514-
struct odb_source *source)
509+
static struct commit_graph *load_commit_graph_v1(struct odb_source *source)
515510
{
516511
char *graph_name = get_commit_graph_filename(source);
517-
struct commit_graph *g = load_commit_graph_one(r, graph_name, source);
512+
struct commit_graph *g = load_commit_graph_one(source, graph_name);
518513
free(graph_name);
519514

520515
return g;
@@ -641,7 +636,7 @@ int open_commit_graph_chain(const char *chain_file,
641636
return 1;
642637
}
643638

644-
struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
639+
struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb,
645640
int fd, struct stat *st,
646641
int *incomplete_chain)
647642
{
@@ -652,28 +647,28 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
652647
FILE *fp = xfdopen(fd, "r");
653648
size_t count;
654649

655-
count = st->st_size / (r->hash_algo->hexsz + 1);
650+
count = st->st_size / (odb->repo->hash_algo->hexsz + 1);
656651
CALLOC_ARRAY(oids, count);
657652

658-
odb_prepare_alternates(r->objects);
653+
odb_prepare_alternates(odb);
659654

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

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

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

673668
valid = 0;
674-
for (source = r->objects->sources; source; source = source->next) {
669+
for (source = odb->sources; source; source = source->next) {
675670
char *graph_name = get_split_graph_filename(source, line.buf);
676-
struct commit_graph *g = load_commit_graph_one(r, graph_name, source);
671+
struct commit_graph *g = load_commit_graph_one(source, graph_name);
677672

678673
free(graph_name);
679674

@@ -706,45 +701,33 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
706701
return graph_chain;
707702
}
708703

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

717-
if (open_commit_graph_chain(chain_file, &fd, &st, r->hash_algo)) {
711+
if (open_commit_graph_chain(chain_file, &fd, &st, source->odb->repo->hash_algo)) {
718712
int incomplete;
719713
/* ownership of fd is taken over by load function */
720-
g = load_commit_graph_chain_fd_st(r, fd, &st, &incomplete);
714+
g = load_commit_graph_chain_fd_st(source->odb, fd, &st, &incomplete);
721715
}
722716

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

727-
struct commit_graph *read_commit_graph_one(struct repository *r,
728-
struct odb_source *source)
721+
struct commit_graph *read_commit_graph_one(struct odb_source *source)
729722
{
730-
struct commit_graph *g = load_commit_graph_v1(r, source);
723+
struct commit_graph *g = load_commit_graph_v1(source);
731724

732725
if (!g)
733-
g = load_commit_graph_chain(r, source);
726+
g = load_commit_graph_chain(source);
734727

735728
return g;
736729
}
737730

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

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

@@ -873,8 +858,7 @@ static void load_oid_from_graph(struct commit_graph *g,
873858
g->hash_algo);
874859
}
875860

876-
static struct commit_list **insert_parent_or_die(struct repository *r,
877-
struct commit_graph *g,
861+
static struct commit_list **insert_parent_or_die(struct commit_graph *g,
878862
uint32_t pos,
879863
struct commit_list **pptr)
880864
{
@@ -885,7 +869,7 @@ static struct commit_list **insert_parent_or_die(struct repository *r,
885869
die("invalid parent position %"PRIu32, pos);
886870

887871
load_oid_from_graph(g, pos, &oid);
888-
c = lookup_commit(r, &oid);
872+
c = lookup_commit(g->odb_source->odb->repo, &oid);
889873
if (!c)
890874
die(_("could not find commit %s"), oid_to_hex(&oid));
891875
commit_graph_data_at(c)->graph_pos = pos;
@@ -941,8 +925,7 @@ static inline void set_commit_tree(struct commit *c, struct tree *t)
941925
c->maybe_tree = t;
942926
}
943927

944-
static int fill_commit_in_graph(struct repository *r,
945-
struct commit *item,
928+
static int fill_commit_in_graph(struct commit *item,
946929
struct commit_graph *g, uint32_t pos)
947930
{
948931
uint32_t edge_value;
@@ -968,13 +951,13 @@ static int fill_commit_in_graph(struct repository *r,
968951
edge_value = get_be32(commit_data + g->hash_algo->rawsz);
969952
if (edge_value == GRAPH_PARENT_NONE)
970953
return 1;
971-
pptr = insert_parent_or_die(r, g, edge_value, pptr);
954+
pptr = insert_parent_or_die(g, edge_value, pptr);
972955

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

@@ -989,7 +972,7 @@ static int fill_commit_in_graph(struct repository *r,
989972
}
990973
edge_value = get_be32(g->chunk_extra_edges +
991974
sizeof(uint32_t) * parent_data_pos);
992-
pptr = insert_parent_or_die(r, g,
975+
pptr = insert_parent_or_die(g,
993976
edge_value & GRAPH_EDGE_LAST_MASK,
994977
pptr);
995978
parent_data_pos++;
@@ -1055,14 +1038,13 @@ struct commit *lookup_commit_in_graph(struct repository *repo, const struct obje
10551038
if (commit->object.parsed)
10561039
return commit;
10571040

1058-
if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
1041+
if (!fill_commit_in_graph(commit, repo->objects->commit_graph, pos))
10591042
return NULL;
10601043

10611044
return commit;
10621045
}
10631046

1064-
static int parse_commit_in_graph_one(struct repository *r,
1065-
struct commit_graph *g,
1047+
static int parse_commit_in_graph_one(struct commit_graph *g,
10661048
struct commit *item)
10671049
{
10681050
uint32_t pos;
@@ -1071,7 +1053,7 @@ static int parse_commit_in_graph_one(struct repository *r,
10711053
return 1;
10721054

10731055
if (find_commit_pos_in_graph(item, g, &pos))
1074-
return fill_commit_in_graph(r, item, g, pos);
1056+
return fill_commit_in_graph(item, g, pos);
10751057

10761058
return 0;
10771059
}
@@ -1088,7 +1070,7 @@ int parse_commit_in_graph(struct repository *r, struct commit *item)
10881070

10891071
if (!prepare_commit_graph(r))
10901072
return 0;
1091-
return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
1073+
return parse_commit_in_graph_one(r->objects->commit_graph, item);
10921074
}
10931075

10941076
void load_commit_graph_info(struct repository *r, struct commit *item)
@@ -1098,8 +1080,7 @@ void load_commit_graph_info(struct repository *r, struct commit *item)
10981080
fill_commit_graph_info(item, r->objects->commit_graph, pos);
10991081
}
11001082

1101-
static struct tree *load_tree_for_commit(struct repository *r,
1102-
struct commit_graph *g,
1083+
static struct tree *load_tree_for_commit(struct commit_graph *g,
11031084
struct commit *c)
11041085
{
11051086
struct object_id oid;
@@ -1114,26 +1095,25 @@ static struct tree *load_tree_for_commit(struct repository *r,
11141095
graph_pos - g->num_commits_in_base);
11151096

11161097
oidread(&oid, commit_data, g->hash_algo);
1117-
set_commit_tree(c, lookup_tree(r, &oid));
1098+
set_commit_tree(c, lookup_tree(g->odb_source->odb->repo, &oid));
11181099

11191100
return c->maybe_tree;
11201101
}
11211102

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

1131-
return load_tree_for_commit(r, g, (struct commit *)c);
1111+
return load_tree_for_commit(g, (struct commit *)c);
11321112
}
11331113

11341114
struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
11351115
{
1136-
return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
1116+
return get_commit_tree_in_graph_one(r->objects->commit_graph, c);
11371117
}
11381118

11391119
struct packed_commit_list {
@@ -2739,11 +2719,11 @@ static int commit_graph_checksum_valid(struct commit_graph *g)
27392719
g->data, g->data_len);
27402720
}
27412721

2742-
static int verify_one_commit_graph(struct repository *r,
2743-
struct commit_graph *g,
2722+
static int verify_one_commit_graph(struct commit_graph *g,
27442723
struct progress *progress,
27452724
uint64_t *seen)
27462725
{
2726+
struct repository *r = g->odb_source->odb->repo;
27472727
uint32_t i, cur_fanout_pos = 0;
27482728
struct object_id prev_oid, cur_oid;
27492729
struct commit *seen_gen_zero = NULL;
@@ -2777,7 +2757,7 @@ static int verify_one_commit_graph(struct repository *r,
27772757
}
27782758

27792759
graph_commit = lookup_commit(r, &cur_oid);
2780-
if (!parse_commit_in_graph_one(r, g, graph_commit))
2760+
if (!parse_commit_in_graph_one(g, graph_commit))
27812761
graph_report(_("failed to parse commit %s from commit-graph"),
27822762
oid_to_hex(&cur_oid));
27832763
}
@@ -2813,7 +2793,7 @@ static int verify_one_commit_graph(struct repository *r,
28132793
continue;
28142794
}
28152795

2816-
if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2796+
if (!oideq(&get_commit_tree_in_graph_one(g, graph_commit)->object.oid,
28172797
get_commit_tree_oid(odb_commit)))
28182798
graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
28192799
oid_to_hex(&cur_oid),
@@ -2831,7 +2811,7 @@ static int verify_one_commit_graph(struct repository *r,
28312811
}
28322812

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

28362816
if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
28372817
graph_report(_("commit-graph parent for %s is %s != %s"),
@@ -2891,7 +2871,7 @@ static int verify_one_commit_graph(struct repository *r,
28912871
return verify_commit_graph_error;
28922872
}
28932873

2894-
int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2874+
int verify_commit_graph(struct commit_graph *g, int flags)
28952875
{
28962876
struct progress *progress = NULL;
28972877
int local_error = 0;
@@ -2907,13 +2887,13 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
29072887
if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW))
29082888
total += g->num_commits_in_base;
29092889

2910-
progress = start_progress(r,
2890+
progress = start_progress(g->odb_source->odb->repo,
29112891
_("Verifying commits in commit graph"),
29122892
total);
29132893
}
29142894

29152895
for (; g; g = g->base_graph) {
2916-
local_error |= verify_one_commit_graph(r, g, progress, &seen);
2896+
local_error |= verify_one_commit_graph(g, progress, &seen);
29172897
if (flags & COMMIT_GRAPH_VERIFY_SHALLOW)
29182898
break;
29192899
}

0 commit comments

Comments
 (0)