git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 4/4] last-modified: change default max-depth to 0

From
Toon Claes <toon@iotcl.com>
Date
Jan 20, 2026, 21:47 UTC
Message-ID
<20260120-toon-last-modified-zzzz-v3-4-9bffd4968b0e@iotcl.com>
In-Reply-To
<20260120-toon-last-modified-zzzz-v3-0-9bffd4968b0e@iotcl.com>

By default git-last-modified(1) doesn't recurse into subtrees. So when the pathspec contained a path in a subtree, the command would only print the commit information about the parent tree of the path, like:

    $ git last-modified -- path/file
    aaa0aab1bbb2bcc3ccc4ddd5dde6eee7eff8fff9	path

Change the default behavior to give commit information about the exact path instead:

    $ git last-modified -- path/file
    aaa0aab1bbb2bcc3ccc4ddd5dde6eee7eff8fff9	path/file

To achieve this, the default max-depth is changed to 0 and recursive is always enabled.

The handling of option '-r' is modified to disable a max-depth, resulting in the behavior of this option to remain unchanged.

No existing tests were modified, because there didn't exist any tests covering the example above. But more tests are added to cover this now.

Signed-off-by: Toon Claes <toon@iotcl.com>
---
 Documentation/git-last-modified.adoc |  9 +++++----
 builtin/last-modified.c              | 21 +++++----------------
 t/t8020-last-modified.sh             | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 45 insertions(+), 20 deletions(-)
diff --git a/Documentation/git-last-modified.adoc b/Documentation/git-last-modified.adoc
index 6f9b119bb6..d7d16fc4f7 100644
--- a/Documentation/git-last-modified.adoc
+++ b/Documentation/git-last-modified.adoc
@@ -25,13 +25,14 @@ OPTIONS
 
 `-r`::
 `--recursive`::
-	Instead of showing tree entries, step into subtrees and show all entries
-	inside them recursively.
+	Recursively traverse into all subtrees. By default, the command only
+	shows tree entries matching the `<pathspec>`. With this option, it
+	descends into subtrees and displays all entries within them.
+	Equivalent to `--max-depth=-1`.
 
 `-t`::
 `--show-trees`::
-	Show tree entries even when recursing into them. It has no effect
-	without `--recursive`.
+	Show tree entries even when recursing into them.
 
 `--max-depth=<depth>`::
 	For each pathspec given on the command line, traverse at most `<depth>`
diff --git a/builtin/last-modified.c b/builtin/last-modified.c
index 28fc77a0f8..f7f4c5109c 100644
--- a/builtin/last-modified.c
+++ b/builtin/last-modified.c
@@ -53,7 +53,6 @@ define_commit_slab(active_paths_for_commit, struct bitmap *);
 struct last_modified {
 	struct hashmap paths;
 	struct rev_info rev;
-	bool recursive;
 	bool show_trees;
 	bool nul_termination;
 	int max_depth;
@@ -481,14 +480,10 @@ static int last_modified_init(struct last_modified *lm, struct repository *r,
 	lm->rev.no_commit_id = 1;
 	lm->rev.diff = 1;
 	lm->rev.diffopt.flags.no_recursive_diff_tree_combined = 1;
-	lm->rev.diffopt.flags.recursive = lm->recursive;
+	lm->rev.diffopt.flags.recursive = 1;
 	lm->rev.diffopt.flags.tree_in_recursive = lm->show_trees;
-
-	if (lm->max_depth >= 0) {
-		lm->rev.diffopt.flags.recursive = 1;
-		lm->rev.diffopt.max_depth = lm->max_depth;
-		lm->rev.diffopt.max_depth_valid = 1;
-	}
+	lm->rev.diffopt.max_depth = lm->max_depth;
+	lm->rev.diffopt.max_depth_valid = lm->max_depth >= 0;
 
 	argc = setup_revisions(argc, argv, &lm->rev, NULL);
 	if (argc > 1) {
@@ -524,8 +519,8 @@ int cmd_last_modified(int argc, const char **argv, const char *prefix,
 	};
 
 	struct option last_modified_options[] = {
-		OPT_BOOL('r', "recursive", &lm.recursive,
-			 N_("recurse into subtrees")),
+		OPT_SET_INT('r', "recursive", &lm.max_depth,
+			    N_("recurse into subtrees"), -1),
 		OPT_BOOL('t', "show-trees", &lm.show_trees,
 			 N_("show tree entries when recursing into subtrees")),
 		OPT_INTEGER_F(0, "max-depth", &lm.max_depth,
@@ -535,12 +530,6 @@ int cmd_last_modified(int argc, const char **argv, const char *prefix,
 		OPT_END()
 	};
 
-	/*
-	 * Set the default of a max-depth to "unset". This will change in a
-	 * subsequent commit.
-	 */
-	lm.max_depth = -1;
-
 	argc = parse_options(argc, argv, prefix, last_modified_options,
 			     last_modified_usage,
 			     PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT |
diff --git a/t/t8020-last-modified.sh b/t/t8020-last-modified.sh
index 50f4312f71..3944d2e153 100755
--- a/t/t8020-last-modified.sh
+++ b/t/t8020-last-modified.sh
@@ -93,6 +93,41 @@ test_expect_success 'last-modified subdir recursive' '
 	EOF
 '
 
+test_expect_success 'last-modified subdir non-recursive' '
+	check_last_modified a <<-\EOF
+	3 a
+	EOF
+'
+
+test_expect_success 'last-modified path in subdir non-recursive' '
+	check_last_modified a/file <<-\EOF
+	2 a/file
+	EOF
+'
+
+test_expect_success 'last-modified subdir with wildcard non-recursive' '
+	check_last_modified a/* <<-\EOF
+	3 a/b
+	2 a/file
+	EOF
+'
+
+test_expect_success 'last-modified with negative max-depth' '
+	check_last_modified --max-depth=-1 <<-\EOF
+	3 a/b/file
+	2 a/file
+	1 file
+	EOF
+'
+
+test_expect_success 'last-modified with max-depth of 1' '
+	check_last_modified --max-depth=1 <<-\EOF
+	3 a/b
+	2 a/file
+	1 file
+	EOF
+'
+
 test_expect_success 'last-modified from non-HEAD commit' '
 	check_last_modified HEAD^ <<-\EOF
 	2 a
-- 
2.52.0
Previous: Toon ClaesNext: Kristoffer Haugsbakk
Message 32 of 36 in “Expand and enhance git-last-modified(1) documentation”
  1. 0/3 Expand and enhance git-last-modified(1) documentationToon Claes, Nov 26, 2025
  2. 1/3 last-modified: handle and document NUL terminationToon Claes, Nov 26, 2025
  3. Karthik NayakNov 26, 2025
  4. Junio C HamanoNov 26, 2025
  5. Toon ClaesNov 28, 2025
  6. Patrick SteinhardtDec 1, 2025
  7. 2/3 last-modified: document option --max-depthToon Claes, Nov 26, 2025
  8. Karthik NayakNov 26, 2025
  9. Toon ClaesJan 16, 2026
  10. Junio C HamanoNov 26, 2025
  11. Toon ClaesNov 28, 2025
  12. 3/3 last-modified: better document how depth in handledToon Claes, Nov 26, 2025
  13. Eric SunshineNov 26, 2025
  14. Patrick SteinhardtDec 1, 2025
  15. Toon ClaesDec 2, 2025
  16. Patrick SteinhardtDec 2, 2025
  17. 0/5 Change git-last-modified(1) default behavior and add documentationToon Claes, Jan 16, 2026
  18. 1/5 last-modified: document NUL terminationToon Claes, Jan 16, 2026
  19. 2/5 last-modified: add option '-z' to help outputToon Claes, Jan 16, 2026
  20. Junio C HamanoJan 16, 2026
  21. 3/5 last-modified: document option --max-depthToon Claes, Jan 16, 2026
  22. 4/5 last-modified: add option '--max-depth' to help outputToon Claes, Jan 16, 2026
  23. Junio C HamanoJan 16, 2026
  24. 5/5 last-modified: change default max-depth to 0Toon Claes, Jan 16, 2026
  25. Junio C HamanoJan 16, 2026
  26. Kristoffer HaugsbakkJan 16, 2026
  27. Toon ClaesJan 20, 2026
  28. 0/4 Change git-last-modified(1) default behavior and add documentationToon Claes, Jan 20, 2026
  29. 1/4 last-modified: clarify in the docs the command takes a pathspecToon Claes, Jan 20, 2026
  30. 2/4 last-modified: document option '-z'Toon Claes, Jan 20, 2026
  31. 3/4 last-modified: document option '--max-depth'Toon Claes, Jan 20, 2026
  32. 4/4 last-modified: change default max-depth to 0Toon Claes, Jan 20, 2026
  33. Kristoffer HaugsbakkJan 25, 2026
  34. Junio C HamanoJan 21, 2026
  35. Karthik NayakFeb 3, 2026
  36. Junio C HamanoFeb 3, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.