Skip to content

Commit 7758f84

Browse files
committed
Merge branch 'tc/last-modified-options-cleanup'
The "-z" and "--max-depth" documentation (and implementation of "-z") in the "git last-modified" command have been updated. * tc/last-modified-options-cleanup: last-modified: change default max-depth to 0 last-modified: document option '--max-depth' last-modified: document option '-z' last-modified: clarify in the docs the command takes a pathspec
2 parents 1f17604 + 9dcc09b commit 7758f84

3 files changed

Lines changed: 87 additions & 18 deletions

File tree

Documentation/git-last-modified.adoc

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ git-last-modified - EXPERIMENTAL: Show when files were last modified
99
SYNOPSIS
1010
--------
1111
[synopsis]
12-
git last-modified [--recursive] [--show-trees] [<revision-range>] [[--] <path>...]
12+
git last-modified [--recursive] [--show-trees] [--max-depth=<depth>] [-z]
13+
[<revision-range>] [[--] <pathspec>...]
1314

1415
DESCRIPTION
1516
-----------
@@ -24,13 +25,23 @@ OPTIONS
2425

2526
`-r`::
2627
`--recursive`::
27-
Instead of showing tree entries, step into subtrees and show all entries
28-
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`.
2932

3033
`-t`::
3134
`--show-trees`::
32-
Show tree entries even when recursing into them. It has no effect
33-
without `--recursive`.
35+
Show tree entries even when recursing into them.
36+
37+
`--max-depth=<depth>`::
38+
For each pathspec given on the command line, traverse at most `<depth>`
39+
levels into subtrees. A negative value means no limit.
40+
The default is 0, which shows all paths matching the pathspec
41+
without descending into subtrees.
42+
43+
`-z`::
44+
Terminate each line with a _NUL_ character rather than a newline.
3445

3546
`<revision-range>`::
3647
Only traverse commits in the specified revision range. When no
@@ -39,10 +50,26 @@ OPTIONS
3950
spell `<revision-range>`, see the 'Specifying Ranges' section of
4051
linkgit:gitrevisions[7].
4152

42-
`[--] <path>...`::
43-
For each _<path>_ given, the commit which last modified it is returned.
44-
Without an optional path parameter, all files and subdirectories
45-
in path traversal the are included in the output.
53+
`[--] <pathspec>...`::
54+
Show the commit that last modified each path matching _<pathspec>_.
55+
If no _<pathspec>_ is given, all files and subdirectories are included.
56+
See linkgit:gitglossary[7] for details on pathspec syntax.
57+
58+
OUTPUT
59+
------
60+
61+
The output is in the format:
62+
63+
------------
64+
<oid> TAB <path> LF
65+
------------
66+
67+
If a path contains any special characters, the path is C-style quoted. To
68+
avoid quoting, pass option `-z` to terminate each line with a NUL.
69+
70+
------------
71+
<oid> TAB <path> NUL
72+
------------
4673

4774
SEE ALSO
4875
--------

builtin/last-modified.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ 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;
57+
bool nul_termination;
58+
int max_depth;
5859

5960
const char **all_paths;
6061
size_t all_paths_nr;
@@ -165,10 +166,10 @@ static void last_modified_emit(struct last_modified *lm,
165166
putchar('^');
166167
printf("%s\t", oid_to_hex(&commit->object.oid));
167168

168-
if (lm->rev.diffopt.line_termination)
169-
write_name_quoted(path, stdout, '\n');
170-
else
169+
if (lm->nul_termination)
171170
printf("%s%c", path, '\0');
171+
else
172+
write_name_quoted(path, stdout, '\n');
172173
}
173174

174175
static void mark_path(const char *path, const struct object_id *oid,
@@ -479,8 +480,10 @@ static int last_modified_init(struct last_modified *lm, struct repository *r,
479480
lm->rev.no_commit_id = 1;
480481
lm->rev.diff = 1;
481482
lm->rev.diffopt.flags.no_recursive_diff_tree_combined = 1;
482-
lm->rev.diffopt.flags.recursive = lm->recursive;
483+
lm->rev.diffopt.flags.recursive = 1;
483484
lm->rev.diffopt.flags.tree_in_recursive = lm->show_trees;
485+
lm->rev.diffopt.max_depth = lm->max_depth;
486+
lm->rev.diffopt.max_depth_valid = lm->max_depth >= 0;
484487

485488
argc = setup_revisions(argc, argv, &lm->rev, NULL);
486489
if (argc > 1) {
@@ -510,16 +513,20 @@ int cmd_last_modified(int argc, const char **argv, const char *prefix,
510513
struct last_modified lm = { 0 };
511514

512515
const char * const last_modified_usage[] = {
513-
N_("git last-modified [--recursive] [--show-trees] "
514-
"[<revision-range>] [[--] <path>...]"),
516+
N_("git last-modified [--recursive] [--show-trees] [--max-depth=<depth>] [-z]\n"
517+
" [<revision-range>] [[--] <pathspec>...]"),
515518
NULL
516519
};
517520

518521
struct option last_modified_options[] = {
519-
OPT_BOOL('r', "recursive", &lm.recursive,
520-
N_("recurse into subtrees")),
522+
OPT_SET_INT('r', "recursive", &lm.max_depth,
523+
N_("recurse into subtrees"), -1),
521524
OPT_BOOL('t', "show-trees", &lm.show_trees,
522525
N_("show tree entries when recursing into subtrees")),
526+
OPT_INTEGER_F(0, "max-depth", &lm.max_depth,
527+
N_("maximum tree depth to recurse"), PARSE_OPT_NONEG),
528+
OPT_BOOL('z', NULL, &lm.nul_termination,
529+
N_("lines are separated with NUL character")),
523530
OPT_END()
524531
};
525532

t/t8020-last-modified.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,41 @@ test_expect_success 'last-modified subdir recursive' '
9393
EOF
9494
'
9595

96+
test_expect_success 'last-modified subdir non-recursive' '
97+
check_last_modified a <<-\EOF
98+
3 a
99+
EOF
100+
'
101+
102+
test_expect_success 'last-modified path in subdir non-recursive' '
103+
check_last_modified a/file <<-\EOF
104+
2 a/file
105+
EOF
106+
'
107+
108+
test_expect_success 'last-modified subdir with wildcard non-recursive' '
109+
check_last_modified a/* <<-\EOF
110+
3 a/b
111+
2 a/file
112+
EOF
113+
'
114+
115+
test_expect_success 'last-modified with negative max-depth' '
116+
check_last_modified --max-depth=-1 <<-\EOF
117+
3 a/b/file
118+
2 a/file
119+
1 file
120+
EOF
121+
'
122+
123+
test_expect_success 'last-modified with max-depth of 1' '
124+
check_last_modified --max-depth=1 <<-\EOF
125+
3 a/b
126+
2 a/file
127+
1 file
128+
EOF
129+
'
130+
96131
test_expect_success 'last-modified from non-HEAD commit' '
97132
check_last_modified HEAD^ <<-\EOF
98133
2 a

0 commit comments

Comments
 (0)