summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKai Lautaportti <kai.lautaportti@hexagonit.fi>2008-09-12 22:03:56 +0300
committerKai Lautaportti <kai.lautaportti@hexagonit.fi>2008-09-12 22:03:56 +0300
commit125b4875b5035dc4f6bad4351651a4236b82baeb (patch)
tree602068aa79e32eb575b6e65a2f66ba970fa46f52
parent3131d1a5295508f583ae22788a1065144bec3cee (diff)
downloadgitpython-125b4875b5035dc4f6bad4351651a4236b82baeb.tar.gz
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.
-rw-r--r--lib/git/repo.py16
-rw-r--r--test/git/test_repo.py18
2 files changed, 34 insertions, 0 deletions
diff --git a/lib/git/repo.py b/lib/git/repo.py
index 0e52fab7..ef440cd1 100644
--- a/lib/git/repo.py
+++ b/lib/git/repo.py
@@ -441,5 +441,21 @@ class Repo(object):
alternates = property(_get_alternates, _set_alternates)
+ @property
+ def is_dirty(self):
+ """Returns the status of the working directory.
+
+ Returns
+ ``True``, if the working directory has any uncommitted changes,
+ otherwise ``False``
+
+ """
+ if self.bare:
+ # Bare repositories with no associated working directory are
+ # always consired to be clean.
+ return False
+
+ return len(self.git.diff('HEAD').strip()) > 0
+
def __repr__(self):
return '<GitPython.Repo "%s">' % self.path
diff --git a/test/git/test_repo.py b/test/git/test_repo.py
index 7550e1d6..4a5ee8ac 100644
--- a/test/git/test_repo.py
+++ b/test/git/test_repo.py
@@ -296,3 +296,21 @@ class TestRepo(object):
# # Commit.expects(:find_all).with(other_repo, ref, :max_count => 1).returns([stub()])
# delta_commits = self.repo.commit_deltas_from(other_repo)
# assert_equal(3, len(delta_commits))
+
+ def test_is_dirty_with_bare_repository(self):
+ self.repo.bare = True
+ assert_false(self.repo.is_dirty)
+
+ @patch(Git, '_call_process')
+ def test_is_dirty_with_clean_working_dir(self, git):
+ self.repo.bare = False
+ git.return_value = ''
+ assert_false(self.repo.is_dirty)
+ assert_equal(git.call_args, (('diff', 'HEAD'), {}))
+
+ @patch(Git, '_call_process')
+ def test_is_dirty_with_dirty_working_dir(self, git):
+ self.repo.bare = False
+ git.return_value = '''-aaa\n+bbb'''
+ assert_true(self.repo.is_dirty)
+ assert_equal(git.call_args, (('diff', 'HEAD'), {}))