[PATCH] add debugignore which yields the combined ignore patten of the .hgignore files

Jason Harris jason.f.harris at gmail.com
Sat Jan 15 09:39:53 CST 2011


# HG changeset patch
# User jfh <jason at jasonfharris.com>
# Date 1295103723 -3600
# Node ID a4f7a503527c84d73985b2eb2d1e45f5404a8da5
# Parent  129f5c8fcf76598918c68f39b4156b10d84fa08e
add debugignore which yields the combined ignore patten of the .hgignore files

For GUI clients its sometimes important to know which files will be ignored and
which files will be important. This allows the GUI client to skipping redoing a
'hg status' when the files are ignored but have changed. (For instance, a
typical case is that the "build" directory inside some project is ignored but
files in it frequently change.)

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1261,6 +1261,10 @@
         m = util.matchdate(range)
         ui.write("match: %s\n" % m(d[0]))
 
+def debugignore(ui, repo, *values, **opts):
+    """display the combined ignore pattern"""
+    ui.write("%s\n" % repo.debugignorepat())
+
 def debugindex(ui, repo, file_, **opts):
     """dump the contents of an index file"""
     r = None
@@ -4172,6 +4176,7 @@
          _('[-e] DATE [RANGE]')),
     "debugdata": (debugdata, [], _('FILE REV')),
     "debugfsinfo": (debugfsinfo, [], _('[PATH]')),
+    "debugignore": (debugignore, [], ''),
     "debugindex": (debugindex,
                    [('f', 'format', 0, _('revlog format'), _('FORMAT'))],
                    _('FILE')),
diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -1259,6 +1259,10 @@
                 n = p[0]
         return b
 
+    def debugignorepat(self):
+        ignore = self.dirstate._ignore
+        return ignore.includepat
+
     def between(self, pairs):
         r = []
 
diff --git a/mercurial/match.py b/mercurial/match.py
--- a/mercurial/match.py
+++ b/mercurial/match.py
@@ -39,11 +39,11 @@
         self._anypats = bool(include or exclude)
 
         if include:
-            im = _buildmatch(_normalize(include, 'glob', root, cwd, auditor),
-                             '(?:/|$)')
+            pats = _normalize(include, 'glob', root, cwd, auditor)
+            self.includepat, im = _buildmatch(pats, '(?:/|$)')
         if exclude:
-            em = _buildmatch(_normalize(exclude, 'glob', root, cwd, auditor),
-                             '(?:/|$)')
+            pats = _normalize(exclude, 'glob', root, cwd, auditor)
+            self.excludepat, em = _buildmatch(pats, '(?:/|$)')
         if exact:
             self._files = patterns
             pm = self.exact
@@ -51,7 +51,7 @@
             pats = _normalize(patterns, default, root, cwd, auditor)
             self._files = _roots(pats)
             self._anypats = self._anypats or _anypats(pats)
-            pm = _buildmatch(pats, '$')
+            self.patternspat, pm = _buildmatch(pats, '$')
 
         if patterns or exact:
             if include:
@@ -246,7 +246,7 @@
         pat = '(?:%s)' % '|'.join([_regex(k, p, tail) for (k, p) in pats])
         if len(pat) > 20000:
             raise OverflowError()
-        return re.compile(pat).match
+        return pat, re.compile(pat).match
     except OverflowError:
         # We're using a Python with a tiny regex engine and we
         # made it explode, so we'll divide the pattern list in two
@@ -254,8 +254,9 @@
         l = len(pats)
         if l < 2:
             raise
-        a, b = _buildmatch(pats[:l//2], tail), _buildmatch(pats[l//2:], tail)
-        return lambda s: a(s) or b(s)
+        pata, a = _buildmatch(pats[:l//2], tail),
+        patb, b = _buildmatch(pats[l//2:], tail)
+        return pat, lambda s: a(s) or b(s)
     except re.error:
         for k, p in pats:
             try:
diff --git a/tests/test-debugignore.t b/tests/test-debugignore.t
new file mode 100644
--- /dev/null
+++ b/tests/test-debugignore.t
@@ -0,0 +1,11 @@
+Just exercize debugignore
+Create a short .hgignore file
+  $ hg init t
+  $ cd t
+  $ echo bob > .hgignore
+  $ echo .DS_Store >> .hgignore
+  $ echo '^\.hg' >> .hgignore
+
+We should now see the contents of the .hgignore file reflected in the pattern
+  $ hg debugignore
+  (?:.*bob|.*.DS_Store|^\.hg)


More information about the Mercurial-devel mailing list