Skip to content

Commit a0b1cb6

Browse files
committed
Merge branch 'cb/maint-ls-files-error-report' into maint
* cb/maint-ls-files-error-report: t3005: do not assume a particular order of stdout and stderr of git-ls-files ls-files: fix pathspec display on error
2 parents 85b3c75 + 385ceec commit a0b1cb6

File tree

6 files changed

+90
-10
lines changed

6 files changed

+90
-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 */
@@ -1063,7 +1063,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
10631063
if (1 < !!opts.writeout_stage + !!opts.force + !!opts.merge)
10641064
die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\nchecking out of the index."));
10651065

1066-
return checkout_paths(source_tree, pathspec, &opts);
1066+
return checkout_paths(source_tree, pathspec, prefix, &opts);
10671067
}
10681068

10691069
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
@@ -1189,7 +1189,7 @@ extern int ws_blank_line(const char *line, int len, unsigned ws_rule);
11891189
#define ws_tab_width(rule) ((rule) & WS_TAB_WIDTH_MASK)
11901190

11911191
/* ls-files */
1192-
int report_path_error(const char *ps_matched, const char **pathspec, int prefix_offset);
1192+
int report_path_error(const char *ps_matched, const char **pathspec, const char *prefix);
11931193
void overlay_tree_on_cache(const char *tree_name, const char *prefix);
11941194

11951195
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: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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.err &&
49+
echo "Did you forget to ${sq}git add${sq}?" >>expect.err &&
50+
ls ../x* >expect.out &&
51+
test_must_fail git ls-files -c --error-unmatch ../[xy]* >actual.out 2>actual.err &&
52+
test_cmp expect.out actual.out &&
53+
test_cmp expect.err actual.err
54+
)
55+
'
56+
57+
test_expect_success 'ls-files -o' '
58+
(
59+
cd top/sub &&
60+
for f in ../x*
61+
do
62+
echo "error: pathspec $sq$f$sq did not match any file(s) known to git."
63+
done >expect.err &&
64+
echo "Did you forget to ${sq}git add${sq}?" >>expect.err &&
65+
ls ../y* >expect.out &&
66+
test_must_fail git ls-files -o --error-unmatch ../[xy]* >actual.out 2>actual.err &&
67+
test_cmp expect.out actual.out &&
68+
test_cmp expect.err actual.err
69+
)
70+
'
71+
72+
test_done

0 commit comments

Comments
 (0)