git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Single brackets matching in .gitignore rules

From
Andrey Loskutov <loskutov@gmx.de>
Date
Sep 26, 2015, 22:01 UTC
Message-ID
<2606743.RipZrg6Xoz@pinguin>
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
Next: Andreas Schwab
Message 1 of 7 in “Single brackets matching in .gitignore rules”
  1. Andrey LoskutovSep 26, 2015
  2. Andreas SchwabSep 27, 2015
  3. Andrey LoskutovSep 27, 2015
  4. Duy NguyenSep 28, 2015
  5. Junio C HamanoSep 28, 2015
  6. Andrey LoskutovSep 28, 2015
  7. Junio C HamanoSep 28, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.