# Single brackets matching in .gitignore rules

7 messages from 2015-09-26 to 2015-09-28. Participants: Andrey Loskutov, Andreas Schwab, Duy Nguyen, Junio C Hamano.
Thread: https://gitlist.dev/t/40435

## Andrey Loskutov, 2015-09-26 22:01

Subject: Single brackets matching in .gitignore rules
Message-ID: <2606743.RipZrg6Xoz@pinguin>
URL: https://gitlist.dev/e/2606743.RipZrg6Xoz%40pinguin

```
Hi,

I'm trying to make JGit .gitignore parser compatible to Git behavior and need help to understand some corner cases.

Git seem to fail to match file names if the ignore rule contain a single bracket '[' and seem to have inconsistent behavior for a single ']'.
For all experiments below I was using git 2.1.0 from Fedora 21 repositories.

Example table for '[':
----------------------
rule  | file | match?
----------------------
[           [          false
[*         [          false
*[         [          false
*[         a[        false
----------------------

I would expect that in all cases above Git ignore rules must match, since there is no valid character group defined and so the pattern should be interpreted literally.
However, it looks like Git simply gives up on parsing the rule, probably because it contains unmatched '[' character starting a (broken) character group.

Next, the surprising table for ']':
----------------------
rule  | file | match?
----------------------
]           ]          true
]*         ]          true
*]         ]          true
*]         a]        true
----------------------

Here Git does not give up on parsing, treats unmatched ']' character literally, and doesn't dislike that it is an "unmatched" end of a broken character group.
Why?

If Git interprets [ character only as part of the glob rules (character groups), it should interpret unmatched ] character equally.
Also if Git interprets unmatched  ] character literally, why not same behavior for the [ character?
IMHO the first table for single '[' character is just a bug in Git, and it should be consistent to the ']' table.

Anyway, it would be nice to hear what should be the "right" way to interpret the tables above.

BTW the only official documentation I found about ignore rules:

https://www.kernel.org/pub/software/scm/git/docs/gitignore.html
http://man7.org/linux/man-pages/man3/fnmatch.3.html
http://man7.org/linux/man-pages/man7/glob.7.html

-- 
Kind regards,
google.com/+AndreyLoskutov

```

## Andreas Schwab, 2015-09-27 08:58

Subject: Re: Single brackets matching in .gitignore rules
Message-ID: <m2oagotf1d.fsf@linux-m68k.org>
URL: https://gitlist.dev/e/m2oagotf1d.fsf%40linux-m68k.org
In-Reply-To: <2606743.RipZrg6Xoz@pinguin>

```
Andrey Loskutov <loskutov@gmx.de> writes:

> Next, the surprising table for ']':
> ----------------------
> rule  | file | match?
> ----------------------
> ]           ]          true
> ]*         ]          true
> *]         ]          true
> *]         a]        true
> ----------------------
>
> Here Git does not give up on parsing, treats unmatched ']' character literally, and doesn't dislike that it is an "unmatched" end of a broken character group.
> Why?

] by itself is not special in a glob.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

```

## Andrey Loskutov, 2015-09-27 16:35

Subject: Re: Single brackets matching in .gitignore rules
Message-ID: <2578959.p0AHc2Ulk9@pinguin>
URL: https://gitlist.dev/e/2578959.p0AHc2Ulk9%40pinguin
In-Reply-To: <m2oagotf1d.fsf@linux-m68k.org>

```
On Sunday 27 September 2015 10:58 Andreas Schwab wrote:
> 
> ] by itself is not special in a glob.
> 
> Andreas.
> 

OK, this would explain the current state: once Git sees opened bracket, it always tries to parse the glob pattern and if this fails (if the bracket is not closed), the ignore rule does not match anything. This does not happen for closing bracket and therefore the difference in behavior.

Thanks!

P.S.
In case anyone is interested, here is the path for JGit: https://git.eclipse.org/r/#/c/56773/4

-- 
Kind regards,
google.com/+AndreyLoskutov

```

## Duy Nguyen, 2015-09-28 13:15

Subject: Re: Single brackets matching in .gitignore rules
Message-ID: <CACsJy8DiTevhSnkkab-kfvpibitoUMHdjC2UdGT244JwLRxqWQ@mail.gmail.com>
URL: https://gitlist.dev/e/CACsJy8DiTevhSnkkab-kfvpibitoUMHdjC2UdGT244JwLRxqWQ%40mail.gmail.com
In-Reply-To: <2606743.RipZrg6Xoz@pinguin>

```
On Sun, Sep 27, 2015 at 5:01 AM, Andrey Loskutov <loskutov@gmx.de> wrote:
> ...
> Anyway, it would be nice to hear what should be the "right" way to interpret the tables above.
>
> BTW the only official documentation I found about ignore rules:
>
> https://www.kernel.org/pub/software/scm/git/docs/gitignore.html
> http://man7.org/linux/man-pages/man3/fnmatch.3.html
> http://man7.org/linux/man-pages/man7/glob.7.html

This is already answered. I just want to add that C Git has stopped
using system fnmatch for some time (part of the reason is system
fnmatch behaves differently in corner cases). If you don't mind C,
have a look at dowild() in wildmatch.c, or t/t3070-wildmatch.sh for
some corner cases (but your cases aren't there, may be worth adding
too)
--
Duy

```

## Junio C Hamano, 2015-09-28 17:40

Subject: Re: Single brackets matching in .gitignore rules
Message-ID: <xmqq7fnae92a.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqq7fnae92a.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <CACsJy8DiTevhSnkkab-kfvpibitoUMHdjC2UdGT244JwLRxqWQ@mail.gmail.com>

```
Duy Nguyen <pclouds@gmail.com> writes:

> This is already answered. I just want to add that C Git has stopped
> using system fnmatch for some time (part of the reason is system
> fnmatch behaves differently in corner cases). If you don't mind C,
> have a look at dowild() in wildmatch.c, or t/t3070-wildmatch.sh for
> some corner cases (but your cases aren't there, may be worth adding
> too)

What exactly are you suggesting to "fix", though?  Barf when an
unmatched ] appears as a part of the pattern, instead of taking it
literally?

```

## Andrey Loskutov, 2015-09-28 17:46

Subject: Re: Single brackets matching in .gitignore rules
Message-ID: <4259524.1vtm9ffbuH@pinguin>
URL: https://gitlist.dev/e/4259524.1vtm9ffbuH%40pinguin
In-Reply-To: <xmqq7fnae92a.fsf@gitster.mtv.corp.google.com>

```
On Monday 28 September 2015 10:40 Junio C Hamano wrote:
> Duy Nguyen <pclouds@gmail.com> writes:
> 
> > This is already answered. I just want to add that C Git has stopped
> > using system fnmatch for some time (part of the reason is system
> > fnmatch behaves differently in corner cases). If you don't mind C,
> > have a look at dowild() in wildmatch.c, or t/t3070-wildmatch.sh for
> > some corner cases (but your cases aren't there, may be worth adding
> > too)
> 
> What exactly are you suggesting to "fix", though?  Barf when an
> unmatched ] appears as a part of the pattern, instead of taking it
> literally?

I would rather treat the opened but unmatched [ bracket literally, so that it works similar like ]. 
Originally I've implemented this "symmetrical" behavior in JGit without to know that unmatched [ and ] have different behavior in C Git.

-- 
Kind regards,
google.com/+AndreyLoskutov

```

## Junio C Hamano, 2015-09-28 18:04

Subject: Re: Single brackets matching in .gitignore rules
Message-ID: <xmqqy4fqctdt.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqy4fqctdt.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <4259524.1vtm9ffbuH@pinguin>

```
Andrey Loskutov <loskutov@gmx.de> writes:

> On Monday 28 September 2015 10:40 Junio C Hamano wrote:
>> Duy Nguyen <pclouds@gmail.com> writes:
>> 
>> > This is already answered. I just want to add that C Git has stopped
>> > using system fnmatch for some time (part of the reason is system
>> > fnmatch behaves differently in corner cases). If you don't mind C,
>> > have a look at dowild() in wildmatch.c, or t/t3070-wildmatch.sh for
>> > some corner cases (but your cases aren't there, may be worth adding
>> > too)
>> 
>> What exactly are you suggesting to "fix", though?  Barf when an
>> unmatched ] appears as a part of the pattern, instead of taking it
>> literally?
>
> I would rather treat the opened but unmatched [ bracket literally, so
> that it works similar like ].

Ah, OK.  As long as the (broken) pattern '[' has never been
accepted, it is safe to assume that no working .gitignore file
people have should contain such a string and relying that it
does not match anything, I guess.

```
