last-modified: verify revision argument is a commit-ish

Passing a non-committish revision to git-last-modified(1) triggers the following BUG: git last-modified HEAD^{tree} BUG: builtin/last-modified.c:456: paths remaining beyond boundary in last-modified Fix this error by ensuring that the given revision peels to a commit. This change also adds a test to verify git-last-modified(1) can operate on an annotated tag. For this an annotated tag is added that points to the second commit. But this causes ambiguous results when calling git-name-rev(1) with `--tags`, because now two tags point to the same commit. To remove this ambiguity, pass `--exclude=<tag>` to git-name-rev(1) to exclude the new annotated tag. Reported-by: Gusted <gusted@codeberg.org> Signed-off-by: Toon Claes <toon@iotcl.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Toon Claes committed Jan 30, 2026 at 15:26 UTC 525ef52301be231c73393da5af4a4071f060eb20
2 files changed +19 -1
builtin/last-modified.c
+5
@@ -150,6 +150,11 @@ static int populate_paths_from_revs(struct last_modified *lm)
150 goto out;
151 }
152
153 + if (!repo_peel_to_type(lm->rev.repo, obj->path, 0, obj->item, OBJ_COMMIT)) {
154 + ret = error(_("revision argument '%s' is a %s, not a commit-ish"), obj->name, type_name(obj->item->type));
155 + goto out;
156 + }
157 +
158 diff_tree_oid(lm->rev.repo->hash_algo->empty_tree,
159 &obj->item->oid, "", &diffopt);
160 diff_flush(&diffopt);
t/t8020-last-modified.sh
+14 -1
@@ -8,6 +8,7 @@ test_expect_success 'setup' '
8 test_commit 1 file &&
9 mkdir a &&
10 test_commit 2 a/file &&
11 + git tag -mA t2 2 &&
12 mkdir a/b &&
13 test_commit 3 a/b/file
14 '
@@ -30,7 +31,7 @@ check_last_modified() {
31
32 cat >expect &&
33 git ${indir:+-C "$indir"} last-modified "$@" >tmp.1 &&
33 - git name-rev --annotate-stdin --name-only --tags \
34 + git name-rev --annotate-stdin --name-only --tags --exclude=t2 \
35 <tmp.1 >tmp.2 &&
36 tr '\t' ' ' <tmp.2 >actual &&
37 test_cmp expect actual
@@ -51,6 +52,13 @@ test_expect_success 'last-modified recursive' '
52 EOF
53 '
54
55 +test_expect_success 'last-modified on annotated tag' '
56 + check_last_modified t2 <<-\EOF
57 + 2 a
58 + 1 file
59 + EOF
60 +'
61 +
62 test_expect_success 'last-modified recursive with show-trees' '
63 check_last_modified -r -t <<-\EOF
64 3 a/b
@@ -236,4 +244,9 @@ test_expect_success 'last-modified complains about unknown arguments' '
244 test_grep "unknown last-modified argument: --foo" err
245 '
246
247 +test_expect_success 'last-modified expects commit-ish' '
248 + test_must_fail git last-modified HEAD^{tree} 2>err &&
249 + test_grep "revision argument ${SQ}HEAD^{tree}${SQ} is a tree, not a commit-ish" err
250 +'
251 +
252 test_done