Skip to content

Commit fa8fe4c

Browse files
committed
Added a read-only Repo.active_branch property which returns the name of the currently active branch.
1 parent d7781e1 commit fa8fe4c

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

‎lib/git/repo.py

+14
Original file line numberDiff line numberDiff line change
@@ -458,5 +458,19 @@ def is_dirty(self):
458458

459459
return len(self.git.diff('HEAD').strip()) > 0
460460

461+
@property
462+
def active_branch(self):
463+
"""
464+
The name of the currently active branch.
465+
466+
Returns
467+
str (the branch name)
468+
"""
469+
branch = self.git.symbolic_ref('HEAD').strip()
470+
if branch.startswith('refs/heads/'):
471+
branch = branch[len('refs/heads/'):]
472+
473+
return branch
474+
461475
def __repr__(self):
462476
return '<GitPython.Repo "%s">' % self.path

‎test/git/test_repo.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,16 @@ def test_is_dirty_with_clean_working_dir(self, git):
307307
git.return_value = ''
308308
assert_false(self.repo.is_dirty)
309309
assert_equal(git.call_args, (('diff', 'HEAD'), {}))
310-
310+
311311
@patch(Git, '_call_process')
312312
def test_is_dirty_with_dirty_working_dir(self, git):
313313
self.repo.bare = False
314314
git.return_value = '''-aaa\n+bbb'''
315315
assert_true(self.repo.is_dirty)
316316
assert_equal(git.call_args, (('diff', 'HEAD'), {}))
317+
318+
@patch(Git, '_call_process')
319+
def test_active_branch(self, git):
320+
git.return_value = 'refs/heads/major-refactoring'
321+
assert_equal(self.repo.active_branch, 'major-refactoring')
322+
assert_equal(git.call_args, (('symbolic_ref', 'HEAD'), {}))

0 commit comments

Comments
 (0)