Patchwork using .gitignore file should use glob as default syntax

login
register
mail settings
Submitter Ben Kehoe
Date Feb. 5, 2013, 8:55 p.m.
Message ID <0b5ff522049db893f4d1.1360097740@zoloft>
Download mbox | patch
Permalink /patch/811/
State Rejected
Headers show

Comments

Ben Kehoe - Feb. 5, 2013, 8:55 p.m.
# HG changeset patch
# User Ben Kehoe <benk@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.
Matt Mackall - Feb. 5, 2013, 9:36 p.m.
On Tue, 2013-02-05 at 12:55 -0800, Ben Kehoe wrote:
> # HG changeset patch
> # User Ben Kehoe <benk@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.

Sorry, no. If you want such a feature, it has to be done entirely in
hg-git.

Patch

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]