[PATCH] using .gitignore file should use glob as default syntax

Ben Kehoe benk at berkeley.edu
Tue Feb 5 14:55:40 CST 2013


# HG changeset patch
# User Ben Kehoe <benk at berkeley.edu>
# Date 1359580170 28800
# Node ID 0b5ff522049db893f4d1afb2030cd721f8e17761
# Parent  1516d5624a2911fcb90ee051c6dc0679b49aef55
using .gitignore file should use glob as default syntax

.gitignore files use glob syntax instead of regexp.
When a repo is configured to use a custom ignore file named .gitignore, it
should be parsed using relglob syntax. This is implemented by adding a
default syntax parameter to ignorepats(), which when passed as None keeps
the current behavior. In readpats(), files named exactly '.gitignore' pass
'relglob' to ingorepats() as the default syntax.
Tests are added to verify the behavior.
This patch will work in concert with a patch to hg-git to automatically use
the .gitignore file if it exists.

diff -r 1516d5624a29 -r 0b5ff522049d mercurial/ignore.py
--- a/mercurial/ignore.py	Mon Feb 04 16:39:02 2013 -0600
+++ b/mercurial/ignore.py	Wed Jan 30 13:09:30 2013 -0800
@@ -11,12 +11,16 @@
 
 _commentre = None
 
-def ignorepats(lines):
+def ignorepats(lines, defaultsyntax = None):
     '''parse lines (iterable) of .hgignore text, returning a tuple of
     (patterns, parse errors). These patterns should be given to compile()
     to be validated and converted into a match function.'''
     syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:'}
-    syntax = 'relre:'
+    if defaultsyntax:
+        syntax = defaultsyntax
+    else:
+        syntax = 'relre:'
+
     patterns = []
     warnings = []
 
@@ -62,7 +66,10 @@
         try:
             pats[f] = []
             fp = open(f)
-            pats[f], warnings = ignorepats(fp)
+            defaultsyntax = None
+            if re.search(r'(/|^)\.gitignore$', fp.name):
+                defaultsyntax = 'relglob:'
+            pats[f], warnings = ignorepats(fp, defaultsyntax)
             fp.close()
             for warning in warnings:
                 warn("%s: %s\n" % (f, warning))
diff -r 1516d5624a29 -r 0b5ff522049d tests/test-hgignore.t
--- a/tests/test-hgignore.t	Mon Feb 04 16:39:02 2013 -0600
+++ b/tests/test-hgignore.t	Wed Jan 30 13:09:30 2013 -0800
@@ -124,3 +124,40 @@
   (?:(?:|.*/)[^/]*(?:/|$))
 
   $ cd ..
+
+Check ignoring using .gitignore defaults to glob syntax
+
+  $ rm .hgignore
+  $ echo "*.o" > .gitignore
+  $ hg st
+  A dir/b.o
+  ? .gitignore
+  ? a.c
+  ? a.o
+  ? dir/c.o
+  ? syntax
+
+  $ echo "[ui]" > .hg/hgrc
+  $ echo "ignore=.gitignore" >> .hg/hgrc
+  $ hg st
+  A dir/b.o
+  ? .gitignore
+  ? a.c
+  ? syntax
+
+  $ echo ".*\.o" > .gitignore
+  $ hg st
+  A dir/b.o
+  ? .gitignore
+  ? a.c
+  ? a.o
+  ? dir/c.o
+  ? syntax
+
+  $ rm .gitignore
+  $ echo "*.o" > foo.gitignore
+  $ echo "[ui]" > .hg/hgrc
+  $ echo "ignore=foo.gitignore" >> .hg/hgrc
+  $ hg st
+  abort: foo.gitignore: invalid pattern (relre): *.o
+  [255]


More information about the Mercurial-devel mailing list