Skip to content

Commit 6133e4d

Browse files
committed
Merge branch 'cb/maint-ls-files-error-report'
* cb/maint-ls-files-error-report: ls-files: fix pathspec display on error
2 parents d0b4650 + 0f64bfa commit 6133e4d

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

1067-
return checkout_paths(source_tree, pathspec, &opts);
1067+
return checkout_paths(source_tree, pathspec, prefix, &opts);
10681068
}
10691069

10701070
if (patch_mode)

builtin/commit.c

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

277-
return report_path_error(m, pattern, prefix ? strlen(prefix) : 0);
277+
return report_path_error(m, pattern, prefix);
278278
}
279279

280280
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
@@ -353,11 +353,13 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)
353353
}
354354
}
355355

356-
int report_path_error(const char *ps_matched, const char **pathspec, int prefix_len)
356+
int report_path_error(const char *ps_matched, const char **pathspec, const char *prefix)
357357
{
358358
/*
359359
* Make sure all pathspec matched; otherwise it is an error.
360360
*/
361+
struct strbuf sb = STRBUF_INIT;
362+
const char *name;
361363
int num, errors = 0;
362364
for (num = 0; pathspec[num]; num++) {
363365
int other, found_dup;
@@ -382,10 +384,12 @@ int report_path_error(const char *ps_matched, const char **pathspec, int prefix_
382384
if (found_dup)
383385
continue;
384386

387+
name = quote_path_relative(pathspec[num], -1, &sb, prefix);
385388
error("pathspec '%s' did not match any file(s) known to git.",
386-
pathspec[num] + prefix_len);
389+
name);
387390
errors++;
388391
}
392+
strbuf_release(&sb);
389393
return errors;
390394
}
391395

@@ -577,7 +581,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
577581

578582
if (ps_matched) {
579583
int bad;
580-
bad = report_path_error(ps_matched, pathspec, prefix_len);
584+
bad = report_path_error(ps_matched, pathspec, prefix);
581585
if (bad)
582586
fprintf(stderr, "Did you forget to 'git add'?\n");
583587

cache.h

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

11971197
/* ls-files */
1198-
int report_path_error(const char *ps_matched, const char **pathspec, int prefix_offset);
1198+
int report_path_error(const char *ps_matched, const char **pathspec, const char *prefix);
11991199
void overlay_tree_on_cache(const char *tree_name, const char *prefix);
12001200

12011201
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)