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 <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed May 1, 2018 at 12:47 UTC 3258c66332abaf6e3e8fd81cab07ae804760cd08
1 file changed +43
commit-graph.c
+43
@@ -439,6 +439,8 @@ static void write_graph_chunk_data(struct hashfile *f, int hash_len,
439 else
440 packedDate[0] = 0;
441
442 + packedDate[0] |= htonl((*list)->generation << 2);
443 +
444 packedDate[1] = htonl((*list)->date);
445 hashwrite(f, packedDate, 8);
446
@@ -571,6 +573,45 @@ static void close_reachable(struct packed_oid_list *oids)
573 }
574 }
575
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 +
615 void write_commit_graph(const char *obj_dir,
616 const char **pack_indexes,
617 int nr_packs,
@@ -694,6 +735,8 @@ void write_commit_graph(const char *obj_dir,
735 if (commits.nr >= GRAPH_PARENT_MISSING)
736 die(_("too many commits to write graph"));
737
738 + compute_generation_numbers(&commits);
739 +
740 graph_name = get_commit_graph_filename(obj_dir);
741 fd = hold_lock_file_for_update(&lk, graph_name, 0);
742