Re: [PATCH] fix git add :!x exiting with error when x is in .gitignore
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Feb 4, 2026, 18:47 UTC
- Message-ID
- <xmqq5x8cpcrd.fsf@gitster.g>
- In-Reply-To
- <9c5be231-f340-4a97-850e-d43c78b2c889@gmail.com>
Tian Yuchen <a3205153416@gmail.com> writes:
Show 18 quoted lines
> As you suggested, The loop needs to verify that the path matches at
> least one positive item AND matches none of the negative items. A
> possible way to acheive it is:
> (Notice that we no longer return 1 in the half way)
>
> >bool matched_positive = false;
> >
> >for (item in pathspec) {
> > if (item matches patch) {
> > if (item is exclude) {
> > return 0;
> > } else {
> > matched_positive = true;
> > }
> > }
> >}
> >
> >return matched_positive ? 1 : 0;One caveat. The case without any positive pathspec entries needs special consideration. I suspect, but can be totally wrong as I didn't think things through thoroughly, that
git add "!$y"
would want to behave as if an implicit "everything matches" was given, i.e.,
git add "!$y" .
while a pathspec with one or more positive entries would not need and want such an implicit "everything" treatment.
> By the way, I think extreme cases like 'git add x :!x' should be added > into the test scripts.
True.