Skip to content

Commit 39664cb

Browse files
committed
log: diagnose -L used with pathspec as an error
The -L option is documented to accept no pathspec, but the command line option parser has allowed the combination without checking so far. Ensure that there is no pathspec when the -L option is in effect to fix this. Incidentally, this change fixes another bug in the command line option parser, which has allowed the -L option used together with the --follow option. Because the latter requires exactly one path given, but the former takes no pathspec, they become mutually incompatible automatically. Because the -L option follows renames on its own, there is no reason to give --follow at the same time. The new tests say they may fail with "-L and --follow being incompatible" instead of "-L and pathspec being incompatible". Currently the expected failure can come only from the latter, but this is to futureproof them, in case we decide to add code to explicititly die on -L and --follow used together. Heled-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 898f807 commit 39664cb

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

builtin/log.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
206206
if (argc > 1)
207207
die(_("unrecognized argument: %s"), argv[1]);
208208

209+
if (rev->line_level_traverse && rev->prune_data.nr)
210+
die(_("-L<range>:<file> cannot be used with pathspec"));
211+
209212
memset(&w, 0, sizeof(w));
210213
userformat_find_requirements(NULL, &w);
211214

t/t4211-line-log.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@ test_expect_success 'setup (import history)' '
88
git reset --hard
99
'
1010

11+
test_expect_success 'basic command line parsing' '
12+
# This may fail due to "no such path a.c in commit", or
13+
# "-L is incompatible with pathspec", depending on the
14+
# order the error is checked. Either is acceptable.
15+
test_must_fail git log -L1,1:a.c -- a.c &&
16+
17+
# -L requires there is no pathspec
18+
test_must_fail git log -L1,1:b.c -- b.c 2>error &&
19+
test_i18ngrep "cannot be used with pathspec" error &&
20+
21+
# This would fail because --follow wants a single path, but
22+
# we may fail due to incompatibility between -L/--follow in
23+
# the future. Either is acceptable.
24+
test_must_fail git log -L1,1:b.c --follow &&
25+
test_must_fail git log --follow -L1,1:b.c &&
26+
27+
# This would fail because -L wants no pathspec, but
28+
# we may fail due to incompatibility between -L/--follow in
29+
# the future. Either is acceptable.
30+
test_must_fail git log --follow -L1,1:b.c -- b.c
31+
'
32+
1133
canned_test_1 () {
1234
test_expect_$1 "$2" "
1335
git log $2 >actual &&

0 commit comments

Comments
 (0)