From 85329e31dd4c864a5a200d0a0ded886599adc2c5 Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Fri, 30 Jan 2026 15:26:35 +0100 Subject: [PATCH 1/4] last-modified: rewrite error message when more than one commit given When more than one commit is passed to the git-last-modified(1) command, this error message was printed: error: last-modified can only operate on one tree at a time Calling these a "tree" is technically not correct. git-last-modified(1) expects revisions that peel to a commit. Rephrase the error message to: error: last-modified can only operate on one commit at a time While at it, modify the test to ensure the correct error message is printed. Signed-off-by: Toon Claes Signed-off-by: Junio C Hamano --- builtin/last-modified.c | 2 +- t/t8020-last-modified.sh | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/builtin/last-modified.c b/builtin/last-modified.c index c80f0535f6..1219f6802e 100644 --- a/builtin/last-modified.c +++ b/builtin/last-modified.c @@ -146,7 +146,7 @@ static int populate_paths_from_revs(struct last_modified *lm) continue; if (num_interesting++) - return error(_("last-modified can only operate on one tree at a time")); + return error(_("last-modified can only operate on one commit at a time")); diff_tree_oid(lm->rev.repo->hash_algo->empty_tree, &obj->item->oid, "", &diffopt); diff --git a/t/t8020-last-modified.sh b/t/t8020-last-modified.sh index 50f4312f71..d1aad12319 100755 --- a/t/t8020-last-modified.sh +++ b/t/t8020-last-modified.sh @@ -12,10 +12,6 @@ test_expect_success 'setup' ' test_commit 3 a/b/file ' -test_expect_success 'cannot run last-modified on two trees' ' - test_must_fail git last-modified HEAD HEAD~1 -' - check_last_modified() { local indir= && while test $# != 0 @@ -230,9 +226,14 @@ test_expect_success 'last-modified merge undoes changes' ' EOF ' +test_expect_success 'cannot run last-modified on two commits' ' + test_must_fail git last-modified HEAD HEAD~1 2>err && + test_grep "last-modified can only operate on one commit at a time" err +' + test_expect_success 'last-modified complains about unknown arguments' ' test_must_fail git last-modified --foo 2>err && - grep "unknown last-modified argument: --foo" err + test_grep "unknown last-modified argument: --foo" err ' test_done From e2505ec170bc0861c3b902bc9763416c5f222455 Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Fri, 30 Jan 2026 15:26:36 +0100 Subject: [PATCH 2/4] last-modified: fix memory leak when more than one commit is given When more than one commit is given, the function populate_paths_from_revs() leaks a `struct pathspec`. Plug it. Signed-off-by: Toon Claes Signed-off-by: Junio C Hamano --- builtin/last-modified.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/builtin/last-modified.c b/builtin/last-modified.c index 1219f6802e..31dea975a0 100644 --- a/builtin/last-modified.c +++ b/builtin/last-modified.c @@ -123,7 +123,7 @@ static void add_path_from_diff(struct diff_queue_struct *q, static int populate_paths_from_revs(struct last_modified *lm) { - int num_interesting = 0; + int num_interesting = 0, ret = 0; struct diff_options diffopt; /* @@ -145,16 +145,20 @@ static int populate_paths_from_revs(struct last_modified *lm) if (obj->item->flags & UNINTERESTING) continue; - if (num_interesting++) - return error(_("last-modified can only operate on one commit at a time")); + if (num_interesting++) { + ret = error(_("last-modified can only operate on one commit at a time")); + goto out; + } diff_tree_oid(lm->rev.repo->hash_algo->empty_tree, &obj->item->oid, "", &diffopt); diff_flush(&diffopt); } + +out: clear_pathspec(&diffopt.pathspec); - return 0; + return ret; } static void last_modified_emit(struct last_modified *lm, From b768485c4b7ad0a80d0fd24ec941308b57ccb6ed Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Fri, 30 Jan 2026 15:26:37 +0100 Subject: [PATCH 3/4] last-modified: remove double error message When the user passes two revisions, they get the following output: $ git last-modified HEAD HEAD~ error: last-modified can only operate on one revision at a time error: unable to setup last-modified The error message about "unable to setup" is not very informative, remove it. Signed-off-by: Toon Claes Signed-off-by: Junio C Hamano --- builtin/last-modified.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/last-modified.c b/builtin/last-modified.c index 31dea975a0..e02ec8428b 100644 --- a/builtin/last-modified.c +++ b/builtin/last-modified.c @@ -495,7 +495,7 @@ static int last_modified_init(struct last_modified *lm, struct repository *r, lm->rev.bloom_filter_settings = get_bloom_filter_settings(lm->rev.repo); if (populate_paths_from_revs(lm) < 0) - return error(_("unable to setup last-modified")); + return -1; CALLOC_ARRAY(lm->all_paths, hashmap_get_size(&lm->paths)); lm->all_paths_nr = 0; From 525ef52301be231c73393da5af4a4071f060eb20 Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Fri, 30 Jan 2026 15:26:38 +0100 Subject: [PATCH 4/4] 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=` to git-name-rev(1) to exclude the new annotated tag. Reported-by: Gusted Signed-off-by: Toon Claes Signed-off-by: Junio C Hamano --- builtin/last-modified.c | 5 +++++ t/t8020-last-modified.sh | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/builtin/last-modified.c b/builtin/last-modified.c index e02ec8428b..d0944673f0 100644 --- a/builtin/last-modified.c +++ b/builtin/last-modified.c @@ -150,6 +150,11 @@ static int populate_paths_from_revs(struct last_modified *lm) goto out; } + if (!repo_peel_to_type(lm->rev.repo, obj->path, 0, obj->item, OBJ_COMMIT)) { + ret = error(_("revision argument '%s' is a %s, not a commit-ish"), obj->name, type_name(obj->item->type)); + goto out; + } + diff_tree_oid(lm->rev.repo->hash_algo->empty_tree, &obj->item->oid, "", &diffopt); diff_flush(&diffopt); diff --git a/t/t8020-last-modified.sh b/t/t8020-last-modified.sh index d1aad12319..ec5bdc6aa0 100755 --- a/t/t8020-last-modified.sh +++ b/t/t8020-last-modified.sh @@ -8,6 +8,7 @@ test_expect_success 'setup' ' test_commit 1 file && mkdir a && test_commit 2 a/file && + git tag -mA t2 2 && mkdir a/b && test_commit 3 a/b/file ' @@ -30,7 +31,7 @@ check_last_modified() { cat >expect && git ${indir:+-C "$indir"} last-modified "$@" >tmp.1 && - git name-rev --annotate-stdin --name-only --tags \ + git name-rev --annotate-stdin --name-only --tags --exclude=t2 \ tmp.2 && tr '\t' ' ' actual && test_cmp expect actual @@ -51,6 +52,13 @@ test_expect_success 'last-modified recursive' ' EOF ' +test_expect_success 'last-modified on annotated tag' ' + check_last_modified t2 <<-\EOF + 2 a + 1 file + EOF +' + test_expect_success 'last-modified recursive with show-trees' ' check_last_modified -r -t <<-\EOF 3 a/b @@ -236,4 +244,9 @@ test_expect_success 'last-modified complains about unknown arguments' ' test_grep "unknown last-modified argument: --foo" err ' +test_expect_success 'last-modified expects commit-ish' ' + test_must_fail git last-modified HEAD^{tree} 2>err && + test_grep "revision argument ${SQ}HEAD^{tree}${SQ} is a tree, not a commit-ish" err +' + test_done