changeset 27595:9e2d01707e71

match: add option to return line and lineno from readpattern This will be used to display the line and linenumber of ignorefile that matched an ignored file (issue4856).
author Laurent Charignon <lcharignon@fb.com>
date Sat, 26 Dec 2015 19:40:38 -0800
parents 0921caca7703
children c881367688fe
files mercurial/match.py
diffstat 1 files changed, 11 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/match.py	Wed Dec 23 11:52:54 2015 -0800
+++ b/mercurial/match.py	Sat Dec 26 19:40:38 2015 -0800
@@ -632,7 +632,7 @@
 
 _commentre = None
 
-def readpatternfile(filepath, warn):
+def readpatternfile(filepath, warn, sourceinfo=False):
     '''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.
@@ -648,7 +648,11 @@
     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'''
+    pattern        # pattern of the current default type
+
+    if sourceinfo is set, returns a list of tuples:
+    (pattern, lineno, originalline). This is useful to debug ignore patterns.
+    '''
 
     syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:',
                 'include': 'include', 'subinclude': 'subinclude'}
@@ -656,7 +660,7 @@
     patterns = []
 
     fp = open(filepath)
-    for line in fp:
+    for lineno, line in enumerate(fp, start=1):
         if "#" in line:
             global _commentre
             if not _commentre:
@@ -691,6 +695,9 @@
                 linesyntax = rels
                 line = line[len(s) + 1:]
                 break
-        patterns.append(linesyntax + line)
+        if sourceinfo:
+            patterns.append((linesyntax + line, lineno, line))
+        else:
+            patterns.append(linesyntax + line)
     fp.close()
     return patterns