From 6e7628265e5e38521e0dc403ec9fff48995d369b Mon Sep 17 00:00:00 2001 From: Arnaud Patard Date: Mon, 28 Sep 2020 12:24:33 +0000 Subject: [PATCH] git/repo/base.py: is_dirty(): Fix pathspec handling It's possible to specify a pathspec (eg :!foo) to git diff/status/... but it currently fails with: git.exc.GitCommandError: Cmd('/usr/bin/git') failed due to: exit code(128) cmdline: /usr/bin/git diff --abbrev=40 --full-index --raw :!foo stderr: 'fatal: ambiguous argument ':!foo': unknown revision or path not in the working tree. Add missing '--' to the arguments to fix this ambiguity Signed-off-by: Arnaud Patard --- git/repo/base.py | 2 +- test/test_repo.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/git/repo/base.py b/git/repo/base.py index 2579a7d7a..69e3e3136 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -639,7 +639,7 @@ def is_dirty(self, index=True, working_tree=True, untracked_files=False, if not submodules: default_args.append('--ignore-submodules') if path: - default_args.append(path) + default_args.extend(["--", path]) if index: # diff index against HEAD if osp.isfile(self.index.path) and \ diff --git a/test/test_repo.py b/test/test_repo.py index 0809175f4..d5ea8664a 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -357,6 +357,20 @@ def test_is_dirty(self): assert self.rorepo.is_dirty() is False self.rorepo._bare = orig_val + def test_is_dirty_pathspec(self): + self.rorepo._bare = False + for index in (0, 1): + for working_tree in (0, 1): + for untracked_files in (0, 1): + assert self.rorepo.is_dirty(index, working_tree, untracked_files, path=':!foo') in (True, False) + # END untracked files + # END working tree + # END index + orig_val = self.rorepo._bare + self.rorepo._bare = True + assert self.rorepo.is_dirty() is False + self.rorepo._bare = orig_val + @with_rw_repo('HEAD') def test_is_dirty_with_path(self, rwrepo): assert rwrepo.is_dirty(path="git") is False