From af74966685e1d1f18390a783f6b8d26b3b1c26d1 Mon Sep 17 00:00:00 2001 From: Piotr Pietraszkiewicz Date: Wed, 13 Apr 2016 17:00:34 +0200 Subject: [PATCH 1/2] fix(index): avoid recursing endlessly in add() Issue #407 --- git/index/base.py | 14 +++++++++++--- git/test/test_index.py | 11 +++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/git/index/base.py b/git/index/base.py index f80eb290d..3e68f843c 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -380,9 +380,17 @@ def raise_exc(e): # resolve globs if possible if '?' in path or '*' in path or '[' in path: - for f in self._iter_expand_paths(glob.glob(abs_path)): - yield f.replace(rs, '') - continue + resolved_paths = glob.glob(abs_path) + # not abs_path in resolved_paths: + # a glob() resolving to the same path we are feeding it with + # is a glob() that failed to resolve. If we continued calling + # ourselves we'd endlessly recurse. If the condition below + # evaluates to true then we are likely dealing with a file + # whose name contains wildcard characters. + if abs_path not in resolved_paths: + for f in self._iter_expand_paths(glob.glob(abs_path)): + yield f.replace(rs, '') + continue # END glob handling try: for root, dirs, files in os.walk(abs_path, onerror=raise_exc): diff --git a/git/test/test_index.py b/git/test/test_index.py index a928fe5e7..7546f6be8 100644 --- a/git/test/test_index.py +++ b/git/test/test_index.py @@ -796,3 +796,14 @@ def test_add_utf8P_path(self, rw_dir): r = Repo.init(rw_dir) r.index.add([fp]) r.index.commit('Added orig and prestable') + + @with_rw_directory + def test_add_a_file_with_wildcard_chars(self, rw_dir): + # see issue #407 + fp = os.path.join(rw_dir, '[.exe') + with open(fp, "w") as f: + f.write(b'something') + + r = Repo.init(rw_dir) + r.index.add([fp]) + r.index.commit('Added [.exe') From 6f6713669a8a32af90a73d03a7fa24e6154327f2 Mon Sep 17 00:00:00 2001 From: Piotr Pietraszkiewicz Date: Wed, 13 Apr 2016 17:12:51 +0200 Subject: [PATCH 2/2] fixed unittest of issue #407 for Python3 --- git/test/test_index.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git/test/test_index.py b/git/test/test_index.py index 7546f6be8..f5f9d7073 100644 --- a/git/test/test_index.py +++ b/git/test/test_index.py @@ -801,7 +801,7 @@ def test_add_utf8P_path(self, rw_dir): def test_add_a_file_with_wildcard_chars(self, rw_dir): # see issue #407 fp = os.path.join(rw_dir, '[.exe') - with open(fp, "w") as f: + with open(fp, "wb") as f: f.write(b'something') r = Repo.init(rw_dir)