commit-reach(repo_in_merge_bases_many): optionally expect missing commits

Currently this function treats unrelated commit histories the same way as commit histories with missing commit objects. Typically, missing commit objects constitute a corrupt repository, though, and should be reported as such. The next commits will make it so, but there is one exception: In `git fetch --update-shallow` we _expect_ commit objects to be missing, and we do want to treat the now-incomplete commit histories as unrelated. To allow for that, let's introduce an additional parameter that is passed to `repo_in_merge_bases_many()` to trigger this behavior, and use it in the two callers in `shallow.c`. This commit changes behavior slightly: unless called from the `shallow.c` functions that set the `ignore_missing_commits` bit, any non-existing tip commit that is passed to `repo_in_merge_bases_many()` will now result in an error. Note: When encountering missing commits while traversing the commit history in search for merge bases, with this commit there won't be a change in behavior just yet, their children will still be interpreted as root commits. This bug will get fixed by follow-up commits. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Feb 28, 2024 at 09:44 UTC 207c40e1e43c992a8b268a3395ca104566612c6e
5 files changed +12 -9
commit-reach.c
+5 -4
@@ -466,7 +466,7 @@ int repo_is_descendant_of(struct repository *r,
466
467 other = with_commit->item;
468 with_commit = with_commit->next;
469 - if (repo_in_merge_bases_many(r, other, 1, &commit))
469 + if (repo_in_merge_bases_many(r, other, 1, &commit, 0))
470 return 1;
471 }
472 return 0;
@@ -477,17 +477,18 @@ int repo_is_descendant_of(struct repository *r,
477 * Is "commit" an ancestor of one of the "references"?
478 */
479 int repo_in_merge_bases_many(struct repository *r, struct commit *commit,
480 - int nr_reference, struct commit **reference)
480 + int nr_reference, struct commit **reference,
481 + int ignore_missing_commits)
482 {
483 struct commit_list *bases;
484 int ret = 0, i;
485 timestamp_t generation, max_generation = GENERATION_NUMBER_ZERO;
486
487 if (repo_parse_commit(r, commit))
487 - return ret;
488 + return ignore_missing_commits ? 0 : -1;
489 for (i = 0; i < nr_reference; i++) {
490 if (repo_parse_commit(r, reference[i]))
490 - return ret;
491 + return ignore_missing_commits ? 0 : -1;
492
493 generation = commit_graph_generation(reference[i]);
494 if (generation > max_generation)
commit-reach.h
+2 -1
@@ -30,7 +30,8 @@ int repo_in_merge_bases(struct repository *r,
30 struct commit *reference);
31 int repo_in_merge_bases_many(struct repository *r,
32 struct commit *commit,
33 - int nr_reference, struct commit **reference);
33 + int nr_reference, struct commit **reference,
34 + int ignore_missing_commits);
35
36 /*
37 * Takes a list of commits and returns a new list where those
remote.c
+1 -1
@@ -2680,7 +2680,7 @@ static int is_reachable_in_reflog(const char *local, const struct ref *remote)
2680 if (MERGE_BASES_BATCH_SIZE < size)
2681 size = MERGE_BASES_BATCH_SIZE;
2682
2683 - if ((ret = repo_in_merge_bases_many(the_repository, commit, size, chunk)))
2683 + if ((ret = repo_in_merge_bases_many(the_repository, commit, size, chunk, 0)))
2684 break;
2685 }
2686
shallow.c
+3 -2
@@ -796,7 +796,7 @@ static void post_assign_shallow(struct shallow_info *info,
796 for (j = 0; j < bitmap_nr; j++)
797 if (bitmap[0][j] &&
798 /* Step 7, reachability test at commit level */
799 - !repo_in_merge_bases_many(the_repository, c, ca.nr, ca.commits)) {
799 + !repo_in_merge_bases_many(the_repository, c, ca.nr, ca.commits, 1)) {
800 update_refstatus(ref_status, info->ref->nr, *bitmap);
801 dst++;
802 break;
@@ -827,7 +827,8 @@ int delayed_reachability_test(struct shallow_info *si, int c)
827 si->reachable[c] = repo_in_merge_bases_many(the_repository,
828 commit,
829 si->nr_commits,
830 - si->commits);
830 + si->commits,
831 + 1);
832 si->need_reachability_test[c] = 0;
833 }
834 return si->reachable[c];
t/helper/test-reach.c
+1 -1
@@ -111,7 +111,7 @@ int cmd__reach(int ac, const char **av)
111 repo_in_merge_bases(the_repository, A, B));
112 else if (!strcmp(av[1], "in_merge_bases_many"))
113 printf("%s(A,X):%d\n", av[1],
114 - repo_in_merge_bases_many(the_repository, A, X_nr, X_array));
114 + repo_in_merge_bases_many(the_repository, A, X_nr, X_array, 0));
115 else if (!strcmp(av[1], "is_descendant_of"))
116 printf("%s(A,X):%d\n", av[1], repo_is_descendant_of(r, A, X));
117 else if (!strcmp(av[1], "get_merge_bases_many")) {