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 <drizzd@aon.at> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Clemens Buchacher committed Aug 1, 2011 at 23:19 UTC 0f64bfa9567f697d6152ef9ea937d4b573029a08
6 files changed +88 -10
builtin/checkout.c
+3 -3
@@ -201,7 +201,7 @@ static int checkout_merged(int pos, struct checkout *state)
201 }
202
203 static int checkout_paths(struct tree *source_tree, const char **pathspec,
204 - struct checkout_opts *opts)
204 + const char *prefix, struct checkout_opts *opts)
205 {
206 int pos;
207 struct checkout state;
@@ -231,7 +231,7 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,
231 match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, ps_matched);
232 }
233
234 - if (report_path_error(ps_matched, pathspec, 0))
234 + if (report_path_error(ps_matched, pathspec, prefix))
235 return 1;
236
237 /* "checkout -m path" to recreate conflicted state */
@@ -1060,7 +1060,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1060 if (1 < !!opts.writeout_stage + !!opts.force + !!opts.merge)
1061 die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\nchecking out of the index."));
1062
1063 - return checkout_paths(source_tree, pathspec, &opts);
1063 + return checkout_paths(source_tree, pathspec, prefix, &opts);
1064 }
1065
1066 if (patch_mode)
builtin/commit.c
+1 -1
@@ -272,7 +272,7 @@ static int list_paths(struct string_list *list, const char *with_tree,
272 item->util = item; /* better a valid pointer than a fake one */
273 }
274
275 - return report_path_error(m, pattern, prefix ? strlen(prefix) : 0);
275 + return report_path_error(m, pattern, prefix);
276 }
277
278 static void add_remove_files(struct string_list *list)
builtin/ls-files.c
+7 -3
@@ -388,11 +388,13 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)
388 }
389 }
390
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)
392 {
393 /*
394 * Make sure all pathspec matched; otherwise it is an error.
395 */
396 + struct strbuf sb = STRBUF_INIT;
397 + const char *name;
398 int num, errors = 0;
399 for (num = 0; pathspec[num]; num++) {
400 int other, found_dup;
@@ -417,10 +419,12 @@ int report_path_error(const char *ps_matched, const char **pathspec, int prefix_
419 if (found_dup)
420 continue;
421
422 + name = quote_path_relative(pathspec[num], -1, &sb, prefix);
423 error("pathspec '%s' did not match any file(s) known to git.",
421 - pathspec[num] + prefix_len);
424 + name);
425 errors++;
426 }
427 + strbuf_release(&sb);
428 return errors;
429 }
430
@@ -611,7 +615,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
615
616 if (ps_matched) {
617 int bad;
614 - bad = report_path_error(ps_matched, pathspec, prefix_len);
618 + bad = report_path_error(ps_matched, pathspec, prefix);
619 if (bad)
620 fprintf(stderr, "Did you forget to 'git add'?\n");
621
cache.h
+1 -1
@@ -1175,7 +1175,7 @@ extern int ws_blank_line(const char *line, int len, unsigned ws_rule);
1175 #define ws_tab_width(rule) ((rule) & WS_TAB_WIDTH_MASK)
1176
1177 /* 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);
1179 void overlay_tree_on_cache(const char *tree_name, const char *prefix);
1180
1181 char *alias_lookup(const char *alias);
quote.c
+6 -2
@@ -325,8 +325,12 @@ static const char *path_relative(const char *in, int len,
325
326 if (len < 0)
327 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 + }
334
335 off = 0;
336 i = 0;
t/t3005-ls-files-relative.sh new
+70
@@ -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