Skip to content

Commit 859b7f1

Browse files
torvaldsgitster
authored andcommitted
pathspec: don't error out on all-exclusionary pathspec patterns
Instead of erroring out and telling the user that they should add a positive pattern that covers everything else, just _do_ that. For commands where we honor the current cwd by default (ie grep, ls-files etc), we make that default positive pathspec be the current working directory. And for commands that default to the whole project (ie diff, log, etc), the default positive pathspec is the whole project. Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 42ebeb9 commit 859b7f1

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

Documentation/glossary-content.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,9 @@ Glob magic is incompatible with literal magic.
387387
exclude;;
388388
After a path matches any non-exclude pathspec, it will be run
389389
through all exclude pathspec (magic signature: `!` or its
390-
synonym `^`). If it matches, the path is ignored.
390+
synonym `^`). If it matches, the path is ignored. When there
391+
is no non-exclude pathspec, the exclusion is applied to the
392+
result set as if invoked without any pathspec.
391393
--
392394

393395
[[def_parent]]parent::

pathspec.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ void parse_pathspec(struct pathspec *pathspec,
522522
}
523523

524524
pathspec->nr = n;
525-
ALLOC_ARRAY(pathspec->items, n);
525+
ALLOC_ARRAY(pathspec->items, n + 1);
526526
item = pathspec->items;
527527
prefixlen = prefix ? strlen(prefix) : 0;
528528

@@ -546,10 +546,15 @@ void parse_pathspec(struct pathspec *pathspec,
546546
pathspec->magic |= item[i].magic;
547547
}
548548

549-
if (nr_exclude == n)
550-
die(_("There is nothing to exclude from by :(exclude) patterns.\n"
551-
"Perhaps you forgot to add either ':/' or '.' ?"));
552-
549+
/*
550+
* If everything is an exclude pattern, add one positive pattern
551+
* that matches everyting. We allocated an extra one for this.
552+
*/
553+
if (nr_exclude == n) {
554+
int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;
555+
init_pathspec_item(item + n, 0, prefix, plen, "");
556+
pathspec->nr++;
557+
}
553558

554559
if (pathspec->magic & PATHSPEC_MAXDEPTH) {
555560
if (flags & PATHSPEC_KEEP_ORDER)

t/t6132-pathspec-exclude.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ EOF
2525
test_cmp expect actual
2626
'
2727

28-
test_expect_success 'exclude only should error out' '
29-
test_must_fail git log --oneline --format=%s -- ":(exclude)sub"
28+
test_expect_success 'exclude only no longer errors out' '
29+
git log --oneline --format=%s -- . ":(exclude)sub" >expect &&
30+
git log --oneline --format=%s -- ":(exclude)sub" >actual &&
31+
test_cmp expect actual
3032
'
3133

3234
test_expect_success 't_e_i() exclude sub' '

0 commit comments

Comments
 (0)