diff options
-rw-r--r-- | lib/git/repo.py | 14 | ||||
-rw-r--r-- | test/git/test_repo.py | 8 |
2 files changed, 21 insertions, 1 deletions
diff --git a/lib/git/repo.py b/lib/git/repo.py index aaa7cecc..55f73f66 100644 --- a/lib/git/repo.py +++ b/lib/git/repo.py @@ -458,5 +458,19 @@ class Repo(object): return len(self.git.diff('HEAD').strip()) > 0 + @property + def active_branch(self): + """ + The name of the currently active branch. + + Returns + str (the branch name) + """ + branch = self.git.symbolic_ref('HEAD').strip() + if branch.startswith('refs/heads/'): + branch = branch[len('refs/heads/'):] + + return branch + def __repr__(self): return '<GitPython.Repo "%s">' % self.path diff --git a/test/git/test_repo.py b/test/git/test_repo.py index 4a5ee8ac..13846555 100644 --- a/test/git/test_repo.py +++ b/test/git/test_repo.py @@ -307,10 +307,16 @@ class TestRepo(object): 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'), {})) + + @patch(Git, '_call_process') + def test_active_branch(self, git): + git.return_value = 'refs/heads/major-refactoring' + assert_equal(self.repo.active_branch, 'major-refactoring') + assert_equal(git.call_args, (('symbolic_ref', 'HEAD'), {})) |