diff --git a/pygit2/repository.py b/pygit2/repository.py index 4513d09..0cb57e3 100644 --- a/pygit2/repository.py +++ b/pygit2/repository.py @@ -25,8 +25,12 @@ # the Free Software Foundation, 51 Franklin Street, Fifth Floor, # Boston, MA 02110-1301, USA. +# Import from the Standard Library +from string import hexdigits + # Import from pygit2 from _pygit2 import Repository as _Repository +from _pygit2 import Oid, GIT_OID_HEXSZ, GIT_OID_MINPREFIXLEN class Repository(_Repository): @@ -53,30 +57,34 @@ class Repository(_Repository): # # References # - def create_reference(self, name, target, force=False, symbolic=False): + def create_reference(self, name, target, force=False): """ - Create a new reference "name" which points to a object or another + Create a new reference "name" which points to an object or to another reference. + Based on the type and value of the target parameter, this method tries + to guess whether it is a direct or a symbolic reference. + Keyword arguments: force If True references will be overridden, otherwise (the default) an exception is raised. - symbolic - If True a symbolic reference will be created, then source has to - be a valid existing reference name; if False (the default) a - normal reference will be created, then source must has to be a - valid SHA hash. - Examples:: repo.create_reference('refs/heads/foo', repo.head.hex) - repo.create_reference('refs/tags/foo', 'refs/heads/master', - symbolic=True) + repo.create_reference('refs/tags/foo', 'refs/heads/master') + repo.create_reference('refs/tags/foo', 'bbb78a9cec580') """ - if symbolic: - return self.git_reference_symbolic_create(name, target, force) + direct = ( + type(target) is Oid + or ( + all(c in hexdigits for c in target) + and GIT_OID_MINPREFIXLEN <= len(target) <= GIT_OID_HEXSZ) + ) - return self.git_reference_create(name, target, force) + if direct: + return self.git_reference_create(name, target, force) + + return self.git_reference_symbolic_create(name, target, force) diff --git a/test/test_refs.py b/test/test_refs.py index 823c1ba..3c2af0c 100644 --- a/test/test_refs.py +++ b/test/test_refs.py @@ -49,8 +49,7 @@ class ReferencesTest(utils.RepoTestCase): ['refs/heads/i18n', 'refs/heads/master']) # We add a symbolic reference - repo.create_reference('refs/tags/version1', 'refs/heads/master', - symbolic=True) + repo.create_reference('refs/tags/version1', 'refs/heads/master') self.assertEqual(sorted(repo.listall_references()), ['refs/heads/i18n', 'refs/heads/master', 'refs/tags/version1']) @@ -189,19 +188,18 @@ class ReferencesTest(utils.RepoTestCase): # We add a tag as a new symbolic reference that always points to # "refs/heads/master" reference = self.repo.create_reference('refs/tags/beta', - 'refs/heads/master', symbolic=True) + 'refs/heads/master') self.assertEqual(reference.type, GIT_REF_SYMBOLIC) self.assertEqual(reference.target, 'refs/heads/master') # try to create existing symbolic reference self.assertRaises(ValueError, self.repo.create_reference, - 'refs/tags/beta', 'refs/heads/master', - symbolic=True) + 'refs/tags/beta', 'refs/heads/master') # try to create existing symbolic reference with force reference = self.repo.create_reference('refs/tags/beta', - 'refs/heads/master', force=True, symbolic=True) + 'refs/heads/master', force=True) self.assertEqual(reference.type, GIT_REF_SYMBOLIC) self.assertEqual(reference.target, 'refs/heads/master')