Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 125b487

Browse files
committed
Added a read-only property Repo.is_dirty which reflects the status of the
working directory. A working directory is dirty if it has any uncommitted changes (in the working directory or in the index). Bare repositories are by nature always clean.
1 parent 3131d1a commit 125b487

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/git/repo.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,5 +441,21 @@ def _set_alternates(self, alts):
441441

442442
alternates = property(_get_alternates, _set_alternates)
443443

444+
@property
445+
def is_dirty(self):
446+
"""Returns the status of the working directory.
447+
448+
Returns
449+
``True``, if the working directory has any uncommitted changes,
450+
otherwise ``False``
451+
452+
"""
453+
if self.bare:
454+
# Bare repositories with no associated working directory are
455+
# always consired to be clean.
456+
return False
457+
458+
return len(self.git.diff('HEAD').strip()) > 0
459+
444460
def __repr__(self):
445461
return '<GitPython.Repo "%s">' % self.path

test/git/test_repo.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,21 @@ def test_log_with_path_and_options(self, git):
296296
# # Commit.expects(:find_all).with(other_repo, ref, :max_count => 1).returns([stub()])
297297
# delta_commits = self.repo.commit_deltas_from(other_repo)
298298
# assert_equal(3, len(delta_commits))
299+
300+
def test_is_dirty_with_bare_repository(self):
301+
self.repo.bare = True
302+
assert_false(self.repo.is_dirty)
303+
304+
@patch(Git, '_call_process')
305+
def test_is_dirty_with_clean_working_dir(self, git):
306+
self.repo.bare = False
307+
git.return_value = ''
308+
assert_false(self.repo.is_dirty)
309+
assert_equal(git.call_args, (('diff', 'HEAD'), {}))
310+
311+
@patch(Git, '_call_process')
312+
def test_is_dirty_with_dirty_working_dir(self, git):
313+
self.repo.bare = False
314+
git.return_value = '''-aaa\n+bbb'''
315+
assert_true(self.repo.is_dirty)
316+
assert_equal(git.call_args, (('diff', 'HEAD'), {}))

0 commit comments

Comments
 (0)