Skip to content

Commit beb76ab

Browse files
committed
repo.active_branch now returns a Head object, not a string
1 parent af9e37c commit beb76ab

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

CHANGES

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ objects Package
2929
Repo
3030
----
3131
* Moved blame method from Blob to repo as it appeared to belong there much more.
32+
* active_branch method now returns a Head object instead of a string with the name
33+
of the active branch.
34+
* tree method now requires a Ref instance as input and defaults to the active_branche
35+
instead of master
3236

3337
Diff
3438
----

lib/git/repo.py

+25-13
Original file line numberDiff line numberDiff line change
@@ -277,19 +277,23 @@ def commit_count(self, start='master', path=''):
277277
"""
278278
return Commit.count(self, start, path)
279279

280-
def commit(self, id, path = ''):
280+
def commit(self, id=None, path = ''):
281281
"""
282282
The Commit object for the specified id
283283
284284
``id``
285-
is the SHA1 identifier of the commit
285+
is the SHA1 identifier of the commit or a ref or a ref name
286+
if None, it defaults to the active branch
287+
286288
287289
``path``
288290
is an optional path, if set the returned commit must contain the path.
289291
290292
Returns
291293
``git.Commit``
292294
"""
295+
if id is None:
296+
id = self.active_branch
293297
options = {'max_count': 1}
294298

295299
commits = Commit.find_all(self, id, path, **options)
@@ -311,22 +315,34 @@ def commit_deltas_from(self, other_repo, ref='master', other_ref='master'):
311315
diff_refs = list(set(other_repo_refs) - set(repo_refs))
312316
return map(lambda ref: Commit.find_all(other_repo, ref, max_count=1)[0], diff_refs)
313317

314-
def tree(self, treeish='master'):
318+
def tree(self, treeish=None):
315319
"""
316320
The Tree object for the given treeish reference
317321
318322
``treeish``
319-
is the reference (default 'master')
323+
is a Ref instance defaulting to the active_branch if None.
320324
321325
Examples::
322326
323-
repo.tree('master')
324-
327+
repo.tree(repo.heads[0])
325328
326329
Returns
327330
``git.Tree``
331+
332+
NOTE
333+
A ref is requried here to assure you point to a commit or tag. Otherwise
334+
it is not garantueed that you point to the root-level tree.
335+
336+
If you need a non-root level tree, find it by iterating the root tree.
328337
"""
329-
return Tree(self, id=treeish)
338+
if treeish is None:
339+
treeish = self.active_branch
340+
if not isinstance(treeish, Ref):
341+
raise ValueError( "Treeish reference required, got %r" % treeish )
342+
343+
# we should also check whether the ref has a valid commit ... but lets n
344+
# not be over-critical
345+
return Tree(self, treeish)
330346

331347
def blob(self, id):
332348
"""
@@ -588,13 +604,9 @@ def active_branch(self):
588604
The name of the currently active branch.
589605
590606
Returns
591-
str (the branch name)
607+
Head to the active branch
592608
"""
593-
branch = self.git.symbolic_ref('HEAD').strip()
594-
if branch.startswith('refs/heads/'):
595-
branch = branch[len('refs/heads/'):]
596-
597-
return branch
609+
return Head( self, self.git.symbolic_ref('HEAD').strip() )
598610

599611
def __repr__(self):
600612
return '<git.Repo "%s">' % self.path

test/git/test_repo.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,12 @@ def test_commit(self, git):
9191
def test_tree(self, git):
9292
git.return_value = fixture('ls_tree_a')
9393

94-
tree = self.repo.tree('master')
94+
tree = self.repo.tree(Head(self.repo, 'master'))
9595

9696
assert_equal(4, len([c for c in tree.values() if isinstance(c, Blob)]))
9797
assert_equal(3, len([c for c in tree.values() if isinstance(c, Tree)]))
9898

9999
assert_true(git.called)
100-
assert_equal(git.call_args, (('ls_tree', 'master'), {}))
101100

102101
@patch_object(Git, '_call_process')
103102
def test_blob(self, git):
@@ -255,7 +254,7 @@ def test_is_dirty_with_dirty_working_dir(self, git):
255254
@patch_object(Git, '_call_process')
256255
def test_active_branch(self, git):
257256
git.return_value = 'refs/heads/major-refactoring'
258-
assert_equal(self.repo.active_branch, 'major-refactoring')
257+
assert_equal(self.repo.active_branch.name, 'major-refactoring')
259258
assert_equal(git.call_args, (('symbolic_ref', 'HEAD'), {}))
260259

261260
@patch_object(Git, '_call_process')

0 commit comments

Comments
 (0)