Skip to content

Commit 3258c66

Browse files
derrickstoleegitster
authored andcommitted
commit-graph: compute generation numbers
While preparing commits to be written into a commit-graph file, compute the generation numbers using a depth-first strategy. The only commits that are walked in this depth-first search are those without a precomputed generation number. Thus, computation time will be relative to the number of new commits to the commit-graph file. If a computed generation number would exceed GENERATION_NUMBER_MAX, then use GENERATION_NUMBER_MAX instead. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 83073cc commit 3258c66

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

commit-graph.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,8 @@ static void write_graph_chunk_data(struct hashfile *f, int hash_len,
439439
else
440440
packedDate[0] = 0;
441441

442+
packedDate[0] |= htonl((*list)->generation << 2);
443+
442444
packedDate[1] = htonl((*list)->date);
443445
hashwrite(f, packedDate, 8);
444446

@@ -571,6 +573,45 @@ static void close_reachable(struct packed_oid_list *oids)
571573
}
572574
}
573575

576+
static void compute_generation_numbers(struct packed_commit_list* commits)
577+
{
578+
int i;
579+
struct commit_list *list = NULL;
580+
581+
for (i = 0; i < commits->nr; i++) {
582+
if (commits->list[i]->generation != GENERATION_NUMBER_INFINITY &&
583+
commits->list[i]->generation != GENERATION_NUMBER_ZERO)
584+
continue;
585+
586+
commit_list_insert(commits->list[i], &list);
587+
while (list) {
588+
struct commit *current = list->item;
589+
struct commit_list *parent;
590+
int all_parents_computed = 1;
591+
uint32_t max_generation = 0;
592+
593+
for (parent = current->parents; parent; parent = parent->next) {
594+
if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
595+
parent->item->generation == GENERATION_NUMBER_ZERO) {
596+
all_parents_computed = 0;
597+
commit_list_insert(parent->item, &list);
598+
break;
599+
} else if (parent->item->generation > max_generation) {
600+
max_generation = parent->item->generation;
601+
}
602+
}
603+
604+
if (all_parents_computed) {
605+
current->generation = max_generation + 1;
606+
pop_commit(&list);
607+
608+
if (current->generation > GENERATION_NUMBER_MAX)
609+
current->generation = GENERATION_NUMBER_MAX;
610+
}
611+
}
612+
}
613+
}
614+
574615
void write_commit_graph(const char *obj_dir,
575616
const char **pack_indexes,
576617
int nr_packs,
@@ -694,6 +735,8 @@ void write_commit_graph(const char *obj_dir,
694735
if (commits.nr >= GRAPH_PARENT_MISSING)
695736
die(_("too many commits to write graph"));
696737

738+
compute_generation_numbers(&commits);
739+
697740
graph_name = get_commit_graph_filename(obj_dir);
698741
fd = hold_lock_file_for_update(&lk, graph_name, 0);
699742

0 commit comments

Comments
 (0)