Skip to content

Commit 0f64bfa

Browse files
Clemens Buchachergitster
authored andcommitted
ls-files: fix pathspec display on error
The following sequence of commands reveals an issue with error reporting of relative paths: $ mkdir sub $ cd sub $ git ls-files --error-unmatch ../bbbbb error: pathspec 'b' did not match any file(s) known to git. $ git commit --error-unmatch ../bbbbb error: pathspec 'b' did not match any file(s) known to git. This bug is visible only if the normalized path (i.e., the relative path from the repository root) is longer than the prefix. Otherwise, the code skips over the normalized path and reads from an unused memory location which still contains a leftover of the original command line argument. So instead, use the existing facilities to deal with relative paths correctly. Also fix inconsistency between "checkout" and "commit", e.g. $ cd Documentation $ git checkout nosuch.txt error: pathspec 'Documentation/nosuch.txt' did not match... $ git commit nosuch.txt error: pathspec 'nosuch.txt' did not match... by propagating the prefix down the codepath that reports the error. Signed-off-by: Clemens Buchacher <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d5b6629 commit 0f64bfa

File tree

6 files changed

+88
-10
lines changed

6 files changed

+88
-10
lines changed

builtin/checkout.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ static int checkout_merged(int pos, struct checkout *state)
201201
}
202202

203203
static int checkout_paths(struct tree *source_tree, const char **pathspec,
204-
struct checkout_opts *opts)
204+
const char *prefix, struct checkout_opts *opts)
205205
{
206206
int pos;
207207
struct checkout state;
@@ -231,7 +231,7 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,
231231
match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, ps_matched);
232232
}
233233

234-
if (report_path_error(ps_matched, pathspec, 0))
234+
if (report_path_error(ps_matched, pathspec, prefix))
235235
return 1;
236236

237237
/* "checkout -m path" to recreate conflicted state */
@@ -1060,7 +1060,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
10601060
if (1 < !!opts.writeout_stage + !!opts.force + !!opts.merge)
10611061
die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\nchecking out of the index."));
10621062

1063-
return checkout_paths(source_tree, pathspec, &opts);
1063+
return checkout_paths(source_tree, pathspec, prefix, &opts);
10641064
}
10651065

10661066
if (patch_mode)

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ static int list_paths(struct string_list *list, const char *with_tree,
272272
item->util = item; /* better a valid pointer than a fake one */
273273
}
274274

275-
return report_path_error(m, pattern, prefix ? strlen(prefix) : 0);
275+
return report_path_error(m, pattern, prefix);
276276
}
277277

278278
static void add_remove_files(struct string_list *list)

builtin/ls-files.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,13 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)
388388
}
389389
}
390390

391-
int report_path_error(const char *ps_matched, const char **pathspec, int prefix_len)
391+
int report_path_error(const char *ps_matched, const char **pathspec, const char *prefix)
392392
{
393393
/*
394394
* Make sure all pathspec matched; otherwise it is an error.
395395
*/
396+
struct strbuf sb = STRBUF_INIT;
397+
const char *name;
396398
int num, errors = 0;
397399
for (num = 0; pathspec[num]; num++) {
398400
int other, found_dup;
@@ -417,10 +419,12 @@ int report_path_error(const char *ps_matched, const char **pathspec, int prefix_
417419
if (found_dup)
418420
continue;
419421

422+
name = quote_path_relative(pathspec[num], -1, &sb, prefix);
420423
error("pathspec '%s' did not match any file(s) known to git.",
421-
pathspec[num] + prefix_len);
424+
name);
422425
errors++;
423426
}
427+
strbuf_release(&sb);
424428
return errors;
425429
}
426430

@@ -611,7 +615,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
611615

612616
if (ps_matched) {
613617
int bad;
614-
bad = report_path_error(ps_matched, pathspec, prefix_len);
618+
bad = report_path_error(ps_matched, pathspec, prefix);
615619
if (bad)
616620
fprintf(stderr, "Did you forget to 'git add'?\n");
617621

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,7 @@ extern int ws_blank_line(const char *line, int len, unsigned ws_rule);
11751175
#define ws_tab_width(rule) ((rule) & WS_TAB_WIDTH_MASK)
11761176

11771177
/* ls-files */
1178-
int report_path_error(const char *ps_matched, const char **pathspec, int prefix_offset);
1178+
int report_path_error(const char *ps_matched, const char **pathspec, const char *prefix);
11791179
void overlay_tree_on_cache(const char *tree_name, const char *prefix);
11801180

11811181
char *alias_lookup(const char *alias);

quote.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,12 @@ static const char *path_relative(const char *in, int len,
325325

326326
if (len < 0)
327327
len = strlen(in);
328-
if (prefix && prefix_len < 0)
329-
prefix_len = strlen(prefix);
328+
if (prefix_len < 0) {
329+
if (prefix)
330+
prefix_len = strlen(prefix);
331+
else
332+
prefix_len = 0;
333+
}
330334

331335
off = 0;
332336
i = 0;

t/t3005-ls-files-relative.sh

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/sh
2+
3+
test_description='ls-files tests with relative paths
4+
5+
This test runs git ls-files with various relative path arguments.
6+
'
7+
8+
. ./test-lib.sh
9+
10+
new_line='
11+
'
12+
sq=\'
13+
14+
test_expect_success 'prepare' '
15+
: >never-mind-me &&
16+
git add never-mind-me &&
17+
mkdir top &&
18+
(
19+
cd top &&
20+
mkdir sub &&
21+
x="x xa xbc xdef xghij xklmno" &&
22+
y=$(echo "$x" | tr x y) &&
23+
touch $x &&
24+
touch $y &&
25+
cd sub &&
26+
git add ../x*
27+
)
28+
'
29+
30+
test_expect_success 'ls-files with mixed levels' '
31+
(
32+
cd top/sub &&
33+
cat >expect <<-EOF &&
34+
../../never-mind-me
35+
../x
36+
EOF
37+
git ls-files $(cat expect) >actual &&
38+
test_cmp expect actual
39+
)
40+
'
41+
42+
test_expect_success 'ls-files -c' '
43+
(
44+
cd top/sub &&
45+
for f in ../y*
46+
do
47+
echo "error: pathspec $sq$f$sq did not match any file(s) known to git."
48+
done >expect &&
49+
echo "Did you forget to ${sq}git add${sq}?" >>expect &&
50+
ls ../x* >>expect &&
51+
test_must_fail git ls-files -c --error-unmatch ../[xy]* >actual 2>&1 &&
52+
test_cmp expect actual
53+
)
54+
'
55+
56+
test_expect_success 'ls-files -o' '
57+
(
58+
cd top/sub &&
59+
for f in ../x*
60+
do
61+
echo "error: pathspec $sq$f$sq did not match any file(s) known to git."
62+
done >expect &&
63+
echo "Did you forget to ${sq}git add${sq}?" >>expect &&
64+
ls ../y* >>expect &&
65+
test_must_fail git ls-files -o --error-unmatch ../[xy]* >actual 2>&1 &&
66+
test_cmp expect actual
67+
)
68+
'
69+
70+
test_done

0 commit comments

Comments
 (0)