Skip to content

Commit 9dcc09b

Browse files
To1negitster
authored andcommitted
last-modified: change default max-depth to 0
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 9bfaf78 commit 9dcc09b

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

Documentation/git-last-modified.adoc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ OPTIONS
2525

2626
`-r`::
2727
`--recursive`::
28-
Instead of showing tree entries, step into subtrees and show all entries
29-
inside them recursively.
28+
Recursively traverse into all subtrees. By default, the command only
29+
shows tree entries matching the `<pathspec>`. With this option, it
30+
descends into subtrees and displays all entries within them.
31+
Equivalent to `--max-depth=-1`.
3032

3133
`-t`::
3234
`--show-trees`::
33-
Show tree entries even when recursing into them. It has no effect
34-
without `--recursive`.
35+
Show tree entries even when recursing into them.
3536

3637
`--max-depth=<depth>`::
3738
For each pathspec given on the command line, traverse at most `<depth>`

builtin/last-modified.c

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ define_commit_slab(active_paths_for_commit, struct bitmap *);
5353
struct last_modified {
5454
struct hashmap paths;
5555
struct rev_info rev;
56-
bool recursive;
5756
bool show_trees;
5857
bool nul_termination;
5958
int max_depth;
@@ -481,14 +480,10 @@ static int last_modified_init(struct last_modified *lm, struct repository *r,
481480
lm->rev.no_commit_id = 1;
482481
lm->rev.diff = 1;
483482
lm->rev.diffopt.flags.no_recursive_diff_tree_combined = 1;
484-
lm->rev.diffopt.flags.recursive = lm->recursive;
483+
lm->rev.diffopt.flags.recursive = 1;
485484
lm->rev.diffopt.flags.tree_in_recursive = lm->show_trees;
486-
487-
if (lm->max_depth >= 0) {
488-
lm->rev.diffopt.flags.recursive = 1;
489-
lm->rev.diffopt.max_depth = lm->max_depth;
490-
lm->rev.diffopt.max_depth_valid = 1;
491-
}
485+
lm->rev.diffopt.max_depth = lm->max_depth;
486+
lm->rev.diffopt.max_depth_valid = lm->max_depth >= 0;
492487

493488
argc = setup_revisions(argc, argv, &lm->rev, NULL);
494489
if (argc > 1) {
@@ -524,8 +519,8 @@ int cmd_last_modified(int argc, const char **argv, const char *prefix,
524519
};
525520

526521
struct option last_modified_options[] = {
527-
OPT_BOOL('r', "recursive", &lm.recursive,
528-
N_("recurse into subtrees")),
522+
OPT_SET_INT('r', "recursive", &lm.max_depth,
523+
N_("recurse into subtrees"), -1),
529524
OPT_BOOL('t', "show-trees", &lm.show_trees,
530525
N_("show tree entries when recursing into subtrees")),
531526
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,
535530
OPT_END()
536531
};
537532

538-
/*
539-
* Set the default of a max-depth to "unset". This will change in a
540-
* subsequent commit.
541-
*/
542-
lm.max_depth = -1;
543-
544533
argc = parse_options(argc, argv, prefix, last_modified_options,
545534
last_modified_usage,
546535
PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT);

t/t8020-last-modified.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,41 @@ test_expect_success 'last-modified subdir recursive' '
8585
EOF
8686
'
8787

88+
test_expect_success 'last-modified subdir non-recursive' '
89+
check_last_modified a <<-\EOF
90+
3 a
91+
EOF
92+
'
93+
94+
test_expect_success 'last-modified path in subdir non-recursive' '
95+
check_last_modified a/file <<-\EOF
96+
2 a/file
97+
EOF
98+
'
99+
100+
test_expect_success 'last-modified subdir with wildcard non-recursive' '
101+
check_last_modified a/* <<-\EOF
102+
3 a/b
103+
2 a/file
104+
EOF
105+
'
106+
107+
test_expect_success 'last-modified with negative max-depth' '
108+
check_last_modified --max-depth=-1 <<-\EOF
109+
3 a/b/file
110+
2 a/file
111+
1 file
112+
EOF
113+
'
114+
115+
test_expect_success 'last-modified with max-depth of 1' '
116+
check_last_modified --max-depth=1 <<-\EOF
117+
3 a/b
118+
2 a/file
119+
1 file
120+
EOF
121+
'
122+
88123
test_expect_success 'last-modified from non-HEAD commit' '
89124
check_last_modified HEAD^ <<-\EOF
90125
2 a

0 commit comments

Comments
 (0)