Skip to content

Commit e866c4a

Browse files
authored
Merge pull request #496 from haizaar/master
is_dirty supports path. Fixes #482.
2 parents 83ebc65 + c3c70da commit e866c4a

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

git/repo/base.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ def _set_alternates(self, alts):
585585
doc="Retrieve a list of alternates paths or set a list paths to be used as alternates")
586586

587587
def is_dirty(self, index=True, working_tree=True, untracked_files=False,
588-
submodules=True):
588+
submodules=True, path=None):
589589
"""
590590
:return:
591591
``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,
600600
default_args = ['--abbrev=40', '--full-index', '--raw']
601601
if not submodules:
602602
default_args.append('--ignore-submodules')
603+
if path:
604+
default_args.append(path)
603605
if index:
604606
# diff index against HEAD
605607
if isfile(self.index.path) and \
@@ -612,7 +614,7 @@ def is_dirty(self, index=True, working_tree=True, untracked_files=False,
612614
return True
613615
# END working tree handling
614616
if untracked_files:
615-
if len(self._get_untracked_files(ignore_submodules=not submodules)):
617+
if len(self._get_untracked_files(path, ignore_submodules=not submodules)):
616618
return True
617619
# END untracked files
618620
return False
@@ -633,9 +635,10 @@ def untracked_files(self):
633635
consider caching it yourself."""
634636
return self._get_untracked_files()
635637

636-
def _get_untracked_files(self, **kwargs):
638+
def _get_untracked_files(self, *args, **kwargs):
637639
# make sure we get all files, no only untracked directores
638-
proc = self.git.status(porcelain=True,
640+
proc = self.git.status(*args,
641+
porcelain=True,
639642
untracked_files=True,
640643
as_process=True,
641644
**kwargs)

git/test/test_repo.py

+18
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,24 @@ def test_is_dirty(self):
271271
assert self.rorepo.is_dirty() is False
272272
self.rorepo._bare = orig_val
273273

274+
@with_rw_repo('HEAD')
275+
def test_is_dirty_with_path(self, rwrepo):
276+
assert rwrepo.is_dirty(path="git") is False
277+
278+
with open(os.path.join(rwrepo.working_dir, "git", "util.py"), "at") as f:
279+
f.write("junk")
280+
assert rwrepo.is_dirty(path="git") is True
281+
assert rwrepo.is_dirty(path="doc") is False
282+
283+
rwrepo.git.add(os.path.join("git", "util.py"))
284+
assert rwrepo.is_dirty(index=False, path="git") is False
285+
assert rwrepo.is_dirty(path="git") is True
286+
287+
with open(os.path.join(rwrepo.working_dir, "doc", "no-such-file.txt"), "wt") as f:
288+
f.write("junk")
289+
assert rwrepo.is_dirty(path="doc") is False
290+
assert rwrepo.is_dirty(untracked_files=True, path="doc") is True
291+
274292
def test_head(self):
275293
assert self.rorepo.head.reference.object == self.rorepo.active_branch.object
276294

0 commit comments

Comments
 (0)