From 1af26da10d420d2bf5366161d48f83662c7bd784 Mon Sep 17 00:00:00 2001 From: FUJIWARA Katsunori Date: Sat, 5 Aug 2017 02:13:11 +0900 Subject: [PATCH] gitdirstate: show pattern error in hgignore file as expected Before this revision, invalid pattern in hgignore file causes unintentional failure for UnboundLocalError of ignorefunc, if hggit is used with Mercurial 3.5 or later. In such case: - checking source of invalid pattern at failure uses "pats" list for hgignore files, but - "pats" list is empty, if ignoremod is None (= Mercurial 3.5 or later) - therefore, checking with matchmod.match() overlooks invalid pattern Then, "return ignorefunc" is executed without assignment to ignorefunc, and causes UnboundLocalError. To show pattern error in hgignore file as expected even with Mercurial 3.5 or later, this revision puts '(FILE, ["include: FILE"])' tuples into "pats" (to avoid code duplication, putting into allpats is shared, too). This makes checking source of invalid pattern at failure work as expected for hgignore files. Fixes #197 --- hggit/gitdirstate.py | 10 +++++++--- tests/test-gitignore.t | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/hggit/gitdirstate.py b/hggit/gitdirstate.py index b0b6ccea12..560e0692b6 100644 --- a/hggit/gitdirstate.py +++ b/hggit/gitdirstate.py @@ -75,10 +75,10 @@ def gignore(root, files, warn, extrapatterns=None): pats = [] if ignoremod: pats = ignore.readpats(root, files, warn) - for f, patlist in pats: - allpats.extend(patlist) else: - allpats.extend(['include:%s' % f for f in files]) + pats = [(f, ['include:%s' % f]) for f in files] + for f, patlist in pats: + allpats.extend(patlist) if extrapatterns: allpats.extend(extrapatterns) @@ -91,6 +91,10 @@ def gignore(root, files, warn, extrapatterns=None): try: matchmod.match(root, '', [], patlist) except util.Abort, inst: + if not ignoremod: + # in this case, patlist is ['include: FILE'], and + # inst[0] should already include FILE + raise raise util.Abort('%s: %s' % (f, inst[0])) if extrapatterns: try: diff --git a/tests/test-gitignore.t b/tests/test-gitignore.t index 235605e88b..7325d229de 100644 --- a/tests/test-gitignore.t +++ b/tests/test-gitignore.t @@ -142,3 +142,22 @@ directory) otherwise, a rogue .gitignore could slow down a hg-only repo ? dir/bar ? foo ? foobar + +show pattern error in hgignore file as expected (issue197) +---------------------------------------------------------- + + $ cat > $TESTTMP/invalidhgignore < # invalid syntax in regexp + > foo( + > EOF + $ hg status --config ui.ignore=$TESTTMP/invalidhgignore + abort: $TESTTMP/invalidhgignore: invalid pattern (relre): foo( + [255] + + $ cat > .hgignore < # invalid syntax in regexp + > foo( + > EOF + $ hg status + abort: $TESTTMP/.hgignore: invalid pattern (relre): foo( + [255]