Skip to content

Commit 240b728

Browse files
author
Aaron O'Mullan
committed
Add Gittle.dwim_reference
Allows to resolve shorthand references, for example : “master” => “refs/heads/master” “0.0.1” => “refs/tags/0.0.1”
1 parent 21c49c6 commit 240b728

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

gittle/gittle.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,9 +768,34 @@ def _commit_sha(self, commit_obj):
768768
return commit_obj
769769
elif isinstance(commit_obj, basestring):
770770
# Can't use self[commit_obj] to avoid infinite recursion
771-
commit_obj = self.repo[commit_obj]
771+
commit_obj = self.repo[self.dwim_reference(commit_obj)]
772772
return commit_obj.id
773773

774+
def dwim_reference(self, ref):
775+
"""Dwim resolves a short reference to a full reference
776+
"""
777+
778+
# Formats of refs we want to try in order
779+
formats = [
780+
"%s",
781+
"refs/%s",
782+
"refs/tags/%s",
783+
"refs/heads/%s",
784+
"refs/remotes/%s",
785+
"refs/remotes/%s/HEAD",
786+
]
787+
788+
for f in formats:
789+
try:
790+
fullref = f % ref
791+
if not fullref in self.repo:
792+
continue
793+
return fullref
794+
except:
795+
continue
796+
797+
raise Exception("Could not resolve ref")
798+
774799
def blob_data(self, sha):
775800
"""Return a blobs content for a given SHA
776801
"""

0 commit comments

Comments
 (0)