[PATCH 4 of 4] ignore: use 'include:' rules instead of custom syntax

Durham Goode durham at fb.com
Mon May 18 19:23:21 CDT 2015


# HG changeset patch
# User Durham Goode <durham at fb.com>
# Date 1431817582 25200
#      Sat May 16 16:06:22 2015 -0700
# Node ID dab388963ec2fbffea8cc7d929ad0a45defeafd7
# Parent  b7160d16c7b38d38236bbfb2ec075909e361f515
ignore: use 'include:' rules instead of custom syntax

Now that the matcher supports 'include:' rules, let's change the dirstate.ignore
creation to just create a matcher with a bunch of includes. This allows us to
completely delete ignore.py.

I moved some of the syntax documentation over to readpatternfile in match.py so
we don't lose it.

diff --git a/mercurial/dirstate.py b/mercurial/dirstate.py
--- a/mercurial/dirstate.py
+++ b/mercurial/dirstate.py
@@ -7,8 +7,9 @@
 
 from node import nullid
 from i18n import _
-import scmutil, util, ignore, osutil, parsers, encoding, pathutil
+import scmutil, util, osutil, parsers, encoding, pathutil
 import os, stat, errno
+import match as matchmod
 
 propertycache = util.propertycache
 filecache = scmutil.filecache
@@ -151,7 +152,12 @@ class dirstate(object):
                 # we need to use os.path.join here rather than self._join
                 # because path is arbitrary and user-specified
                 files.append(os.path.join(self._rootdir, util.expandpath(path)))
-        return ignore.ignore(self._root, files, self._ui.warn)
+
+        if not files:
+            return util.never
+
+        pats = ['include:%s' % f for f in files]
+        return matchmod.match(self._root, '', [], pats, warn=self._ui.warn)
 
     @propertycache
     def _slash(self):
diff --git a/mercurial/ignore.py b/mercurial/ignore.py
deleted file mode 100644
--- a/mercurial/ignore.py
+++ /dev/null
@@ -1,64 +0,0 @@
-# ignore.py - ignored file handling for mercurial
-#
-# Copyright 2007 Matt Mackall <mpm at selenic.com>
-#
-# This software may be used and distributed according to the terms of the
-# GNU General Public License version 2 or any later version.
-
-from i18n import _
-import util, match
-
-def readpats(root, files, warn):
-    '''return a dict mapping ignore-file-name to list-of-patterns'''
-
-    pats = {}
-    for f in files:
-        if f in pats:
-            continue
-        try:
-            pats[f] = match.readpatternfile(f, warn)
-        except IOError, inst:
-            warn(_("skipping unreadable ignore file '%s': %s\n") %
-                 (f, inst.strerror))
-
-    return [(f, pats[f]) for f in files if f in pats]
-
-def ignore(root, files, warn):
-    '''return matcher covering patterns in 'files'.
-
-    the files parsed for patterns include:
-    .hgignore in the repository root
-    any additional files specified in the [ui] section of ~/.hgrc
-
-    trailing white space is dropped.
-    the escape character is backslash.
-    comments start with #.
-    empty lines are skipped.
-
-    lines can be of the following formats:
-
-    syntax: regexp # defaults following lines to non-rooted regexps
-    syntax: glob   # defaults following lines to non-rooted globs
-    re:pattern     # non-rooted regular expression
-    glob:pattern   # non-rooted glob
-    pattern        # pattern of the current default type'''
-
-    pats = readpats(root, files, warn)
-
-    allpats = []
-    for f, patlist in pats:
-        allpats.extend(patlist)
-    if not allpats:
-        return util.never
-
-    try:
-        ignorefunc = match.match(root, '', [], allpats)
-    except util.Abort:
-        # Re-raise an exception where the src is the right file
-        for f, patlist in pats:
-            try:
-                match.match(root, '', [], patlist)
-            except util.Abort, inst:
-                raise util.Abort('%s: %s' % (f, inst[0]))
-
-    return ignorefunc
diff --git a/mercurial/match.py b/mercurial/match.py
--- a/mercurial/match.py
+++ b/mercurial/match.py
@@ -526,7 +526,21 @@ def _anypats(kindpats):
 def readpatternfile(filepath, warn):
     '''parse a pattern file, returning a list of
     patterns. These patterns should be given to compile()
-    to be validated and converted into a match function.'''
+    to be validated and converted into a match function.
+
+    trailing white space is dropped.
+    the escape character is backslash.
+    comments start with #.
+    empty lines are skipped.
+
+    lines can be of the following formats:
+
+    syntax: regexp # defaults following lines to non-rooted regexps
+    syntax: glob   # defaults following lines to non-rooted globs
+    re:pattern     # non-rooted regular expression
+    glob:pattern   # non-rooted glob
+    pattern        # pattern of the current default type'''
+
     syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:',
                 'include': 'include'}
     syntax = 'relre:'
diff --git a/tests/test-hgignore.t b/tests/test-hgignore.t
--- a/tests/test-hgignore.t
+++ b/tests/test-hgignore.t
@@ -189,3 +189,8 @@ Check recursive uses of 'include:'
   $ echo "glob:*ignore" > nestedignore
   $ hg status
   A dir/b.o
+
+  $ echo "include:badignore" >> otherignore
+  $ hg status
+  skipping unreadable pattern file 'badignore': No such file or directory
+  A dir/b.o


More information about the Mercurial-devel mailing list