From bdc0ddf8ad4ee8be4b908a3a55070958bdf512a0 Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Fri, 16 Jan 2026 14:08:37 +0100 Subject: [PATCH 1/4] last-modified: rewrite error message when more than one revision given When more than one revision 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 revision at a time Signed-off-by: Toon Claes Signed-off-by: Junio C Hamano --- builtin/last-modified.c | 2 +- t/t8020-last-modified.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/builtin/last-modified.c b/builtin/last-modified.c index c80f0535f6..7d95244e3f 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 revision 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..1183ae667b 100755 --- a/t/t8020-last-modified.sh +++ b/t/t8020-last-modified.sh @@ -12,7 +12,7 @@ test_expect_success 'setup' ' test_commit 3 a/b/file ' -test_expect_success 'cannot run last-modified on two trees' ' +test_expect_success 'cannot run last-modified on two revision' ' test_must_fail git last-modified HEAD HEAD~1 ' From c13a915532566f7d3ea3dbc7f4ab626c05d4b139 Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Fri, 16 Jan 2026 14:08:38 +0100 Subject: [PATCH 2/4] last-modified: fix memory leak when more than one revision is given When more than one revision 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 7d95244e3f..06e3f79aec 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 revision at a time")); + if (num_interesting++) { + ret = error(_("last-modified can only operate on one revision 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 be38275cd8f6ade73c92c52452b78770d5f54f6f Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Fri, 16 Jan 2026 14:08:39 +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 06e3f79aec..0df85be318 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 06643fb8a5e2833823e879c0a6bf3bd54951d43a Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Fri, 16 Jan 2026 14:08:40 +0100 Subject: [PATCH 4/4] last-modified: verify revision argument is a commit-ish Passing a tree OID to git-last-modified(1) would trigger BUG behavior. git last-modified HEAD^{tree} BUG: builtin/last-modified.c:456: paths remaining beyond boundary in last-modified Fix this error by verifying the parsed revision is a commit-ish. Reported-by: Gusted Signed-off-by: Toon Claes Signed-off-by: Junio C Hamano --- builtin/last-modified.c | 5 +++++ t/t8020-last-modified.sh | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/builtin/last-modified.c b/builtin/last-modified.c index 0df85be318..5366cedd0f 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 is not a commit-ish")); + 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 1183ae667b..22635de447 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 ' @@ -55,6 +56,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 @@ -235,4 +243,9 @@ test_expect_success 'last-modified complains about unknown arguments' ' 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 && + grep "revision argument is not a commit-ish" err +' + test_done