debugignore: find out why a file is being ignored (issue4856)

This patch adds a capability to hg debugignore: to explain why a given file is
being ignores by mercurial. We display the filename, line and linenumber of the
rule that lead us to ignore the file.
This commit is contained in:
Laurent Charignon 2016-01-05 07:47:08 -08:00
parent f5214fc7b3
commit 95ad56f2f0
2 changed files with 11 additions and 1 deletions

View File

@ -2441,7 +2441,8 @@ def debugignore(ui, repo, *files, **opts):
With no argument display the combined ignore pattern.
Given space separated file names, shows if the given file is ignored.
Given space separated file names, shows if the given file is ignored and
if so, show the ignore rule (file and line number) that matched it.
"""
ignore = repo.dirstate._ignore
if not files:
@ -2454,13 +2455,16 @@ def debugignore(ui, repo, *files, **opts):
else:
for f in files:
ignored = None
ignoredata = None
if f != '.':
if ignore(f):
ignored = f
ignoredata = repo.dirstate._ignorefileandline(f)
else:
for p in util.finddirs(f):
if ignore(p):
ignored = p
ignoredata = repo.dirstate._ignorefileandline(p)
break
if ignored:
if ignored == f:
@ -2468,6 +2472,9 @@ def debugignore(ui, repo, *files, **opts):
else:
ui.write("%s is ignored because of containing folder %s\n"
% (f, ignored))
ignorefile, lineno, line = ignoredata
ui.write("(ignore rule in %s, line %d: '%s')\n"
% (ignorefile, lineno, line))
else:
ui.write("%s is not ignored\n" % f)

View File

@ -168,6 +168,7 @@ Test relative ignore path (issue4473):
$ hg debugignore b.o
b.o is ignored
(ignore rule in $TESTTMP/ignorerepo/.hgignore, line 1: '*')
$ cd ..
@ -198,6 +199,7 @@ Check recursive glob pattern matches no directories (dir/**/c.o matches dir/c.o)
a.c is not ignored
$ hg debugignore dir/c.o
dir/c.o is ignored
(ignore rule in $TESTTMP/ignorerepo/.hgignore, line 2: 'dir/**/c.o')
Check using 'include:' in ignore file
@ -283,3 +285,4 @@ Check include subignore at the same level
[1]
$ hg debugignore dir1/file2
dir1/file2 is ignored
(ignore rule in dir2/.hgignore, line 1: 'file*2')