Skip to content

Commit 015fba3

Browse files
committed
Merge branch 'lt/pathspec-negative'
The "negative" pathspec feature was somewhat more cumbersome to use than necessary in that its short-hand used "!" which needed to be escaped from shells, and it required "exclude from what?" specified. * lt/pathspec-negative: pathspec: don't error out on all-exclusionary pathspec patterns pathspec magic: add '^' as alias for '!'
2 parents fb75e31 + 859b7f1 commit 015fba3

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

Documentation/glossary-content.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,10 @@ Glob magic is incompatible with literal magic.
386386

387387
exclude;;
388388
After a path matches any non-exclude pathspec, it will be run
389-
through all exclude pathspec (magic signature: `!`). If it
390-
matches, the path is ignored.
389+
through all exclude pathspec (magic signature: `!` or its
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: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)
224224
char ch = *pos;
225225
int i;
226226

227+
/* Special case alias for '!' */
228+
if (ch == '^') {
229+
*magic |= PATHSPEC_EXCLUDE;
230+
continue;
231+
}
232+
227233
if (!is_pathspec_magic(ch))
228234
break;
229235

@@ -516,7 +522,7 @@ void parse_pathspec(struct pathspec *pathspec,
516522
}
517523

518524
pathspec->nr = n;
519-
ALLOC_ARRAY(pathspec->items, n);
525+
ALLOC_ARRAY(pathspec->items, n + 1);
520526
item = pathspec->items;
521527
prefixlen = prefix ? strlen(prefix) : 0;
522528

@@ -540,10 +546,15 @@ void parse_pathspec(struct pathspec *pathspec,
540546
pathspec->magic |= item[i].magic;
541547
}
542548

543-
if (nr_exclude == n)
544-
die(_("There is nothing to exclude from by :(exclude) patterns.\n"
545-
"Perhaps you forgot to add either ':/' or '.' ?"));
546-
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+
}
547558

548559
if (pathspec->magic & PATHSPEC_MAXDEPTH) {
549560
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)