diff: fix --merge-base with annotated tags

Checking early for OBJ_COMMIT excludes other objects that can be resolved to commits, like annotated tags. If we remove it, annotated tags will be resolved and handled just fine by lookup_commit_reference(), and if we are given something that can't be resolved to a commit, we'll still get a useful error message, e.g.: > error: object 21ab162211ac3ef13c37603ca88b27e9c7e0d40b is a tree, not a commit > fatal: no merge base found Signed-off-by: Alyssa Ross <hi@alyssa.is> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Alyssa Ross committed Oct 1, 2023 at 15:18 UTC 4adceb5a299164aad1ef9794cdcbdb232ab10299
2 files changed +10 -5
diff-lib.c
-2
@@ -572,8 +572,6 @@ void diff_get_merge_base(const struct rev_info *revs, struct object_id *mb)
572 struct object *obj = revs->pending.objects[i].item;
573 if (obj->flags)
574 die(_("--merge-base does not work with ranges"));
575 - if (obj->type != OBJ_COMMIT)
576 - die(_("--merge-base only works with commits"));
575 }
576
577 /*
t/t4068-diff-symmetric-merge-base.sh
+10 -3
@@ -34,7 +34,7 @@ test_expect_success setup '
34 echo c >c &&
35 git add c &&
36 git commit -m C &&
37 - git tag commit-C &&
37 + git tag -m commit-C commit-C &&
38 git merge -m D main &&
39 git tag commit-D &&
40 git checkout main &&
@@ -109,6 +109,13 @@ do
109 test_cmp expect actual
110 '
111
112 + test_expect_success "$cmd --merge-base with annotated tag" '
113 + git checkout main &&
114 + git $cmd commit-C >expect &&
115 + git $cmd --merge-base commit-C >actual &&
116 + test_cmp expect actual
117 + '
118 +
119 test_expect_success "$cmd --merge-base with one commit and unstaged changes" '
120 git checkout main &&
121 test_when_finished git reset --hard &&
@@ -143,7 +150,7 @@ do
150 test_expect_success "$cmd --merge-base with non-commit" '
151 git checkout main &&
152 test_must_fail git $cmd --merge-base main^{tree} 2>err &&
146 - test_i18ngrep "fatal: --merge-base only works with commits" err
153 + test_i18ngrep "is a tree, not a commit" err
154 '
155
156 test_expect_success "$cmd --merge-base with no merge bases and one commit" '
@@ -169,7 +176,7 @@ do
176
177 test_expect_success "$cmd --merge-base commit and non-commit" '
178 test_must_fail git $cmd --merge-base br2 main^{tree} 2>err &&
172 - test_i18ngrep "fatal: --merge-base only works with commits" err
179 + test_i18ngrep "is a tree, not a commit" err
180 '
181
182 test_expect_success "$cmd --merge-base with no merge bases and two commits" '