Skip to content

is_dirty supports path. Fixes #482. #496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions git/repo/base.py
Original file line number Diff line number Diff line change
@@ -585,7 +585,7 @@ def _set_alternates(self, alts):
doc="Retrieve a list of alternates paths or set a list paths to be used as alternates")

def is_dirty(self, index=True, working_tree=True, untracked_files=False,
submodules=True):
submodules=True, path=None):
"""
:return:
``True``, the repository is considered dirty. By default it will react
@@ -600,6 +600,8 @@ def is_dirty(self, index=True, working_tree=True, untracked_files=False,
default_args = ['--abbrev=40', '--full-index', '--raw']
if not submodules:
default_args.append('--ignore-submodules')
if path:
default_args.append(path)
if index:
# diff index against HEAD
if isfile(self.index.path) and \
@@ -612,7 +614,7 @@ def is_dirty(self, index=True, working_tree=True, untracked_files=False,
return True
# END working tree handling
if untracked_files:
if len(self._get_untracked_files(ignore_submodules=not submodules)):
if len(self._get_untracked_files(path, ignore_submodules=not submodules)):
return True
# END untracked files
return False
@@ -633,9 +635,10 @@ def untracked_files(self):
consider caching it yourself."""
return self._get_untracked_files()

def _get_untracked_files(self, **kwargs):
def _get_untracked_files(self, *args, **kwargs):
# make sure we get all files, no only untracked directores
proc = self.git.status(porcelain=True,
proc = self.git.status(*args,
porcelain=True,
untracked_files=True,
as_process=True,
**kwargs)
18 changes: 18 additions & 0 deletions git/test/test_repo.py
Original file line number Diff line number Diff line change
@@ -271,6 +271,24 @@ def test_is_dirty(self):
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

with open(os.path.join(rwrepo.working_dir, "git", "util.py"), "at") as f:
f.write("junk")
assert rwrepo.is_dirty(path="git") is True
assert rwrepo.is_dirty(path="doc") is False

rwrepo.git.add(os.path.join("git", "util.py"))
assert rwrepo.is_dirty(index=False, path="git") is False
assert rwrepo.is_dirty(path="git") is True

with open(os.path.join(rwrepo.working_dir, "doc", "no-such-file.txt"), "wt") as f:
f.write("junk")
assert rwrepo.is_dirty(path="doc") is False
assert rwrepo.is_dirty(untracked_files=True, path="doc") is True

def test_head(self):
assert self.rorepo.head.reference.object == self.rorepo.active_branch.object