Skip to content

Commit 3b1e135

Browse files
committed
Merge branch 'ex/deprecate-empty-pathspec-as-match-all'
An empty string used as a pathspec element has always meant 'everything matches', but it is too easy to write a script that finds a path to remove in $path and run 'git rm "$paht"', which ends up removing everything. Start warning about this use of an empty string used for 'everything matches' and ask users to use a more explicit '.' for that instead. The hope is that existing users will not mind this change, and eventually the warning can be turned into a hard error, upgrading the deprecation into removal of this (mis)feature. * ex/deprecate-empty-pathspec-as-match-all: pathspec: warn on empty strings as pathspec
2 parents 4abeeb6 + d426430 commit 3b1e135

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

pathspec.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ void parse_pathspec(struct pathspec *pathspec,
364364
{
365365
struct pathspec_item *item;
366366
const char *entry = argv ? *argv : NULL;
367-
int i, n, prefixlen, nr_exclude = 0;
367+
int i, n, prefixlen, warn_empty_string, nr_exclude = 0;
368368

369369
memset(pathspec, 0, sizeof(*pathspec));
370370

@@ -402,8 +402,15 @@ void parse_pathspec(struct pathspec *pathspec,
402402
}
403403

404404
n = 0;
405-
while (argv[n])
405+
warn_empty_string = 1;
406+
while (argv[n]) {
407+
if (*argv[n] == '\0' && warn_empty_string) {
408+
warning(_("empty strings as pathspecs will be made invalid in upcoming releases. "
409+
"please use . instead if you meant to match all paths"));
410+
warn_empty_string = 0;
411+
}
406412
n++;
413+
}
407414

408415
pathspec->nr = n;
409416
ALLOC_ARRAY(pathspec->items, n);

t/t3600-rm.sh

+5
Original file line numberDiff line numberDiff line change
@@ -881,4 +881,9 @@ test_expect_success 'rm files with two different errors' '
881881
test_i18ncmp expect actual
882882
'
883883

884+
test_expect_success 'rm empty string should invoke warning' '
885+
git rm -rf "" 2>output &&
886+
test_i18ngrep "warning: empty strings" output
887+
'
888+
884889
test_done

t/t3700-add.sh

+5
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,11 @@ test_expect_success 'git add --dry-run --ignore-missing of non-existing file out
331331
test_i18ncmp expect.err actual.err
332332
'
333333

334+
test_expect_success 'git add empty string should invoke warning' '
335+
git add "" 2>output &&
336+
test_i18ngrep "warning: empty strings" output
337+
'
338+
334339
test_expect_success 'git add --chmod=[+-]x stages correctly' '
335340
rm -f foo1 &&
336341
echo foo >foo1 &&

0 commit comments

Comments
 (0)