diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2015-01-08 17:07:01 +0100 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2015-01-08 17:08:02 +0100 |
commit | b08651e5ae2bffef5e4fb44fbdd7d467715e3b73 (patch) | |
tree | 2f507920b8c63a6b0074eb2938fb3b033467b612 | |
parent | fc94b89dabd9df49631cbf6b18800325f3521864 (diff) | |
download | gitpython-b08651e5ae2bffef5e4fb44fbdd7d467715e3b73.tar.gz |
Improved handling of name-resolution, which will not mangle names anymore.
Previously, an unresolvable ref name like HEAD would end up as HEX and
was presented as BadObject error, even though that exception is for
invalid shas only.
Now BadName is thrown, which converts into a more useful error message.
Improves #105
-rw-r--r-- | git/repo/fun.py | 9 | ||||
-rw-r--r-- | git/test/test_repo.py | 27 |
2 files changed, 32 insertions, 4 deletions
diff --git a/git/repo/fun.py b/git/repo/fun.py index 233666c9..f8342260 100644 --- a/git/repo/fun.py +++ b/git/repo/fun.py @@ -2,7 +2,10 @@ import os from string import digits -from gitdb.exc import BadObject +from gitdb.exc import ( + BadObject, + BadName +) from git.refs import SymbolicReference from git.objects import Object from gitdb.util import ( @@ -83,7 +86,7 @@ def name_to_object(repo, name, return_ref=False): :return: object specified by the given name, hexshas ( short and long ) as well as references are supported :param return_ref: if name specifies a reference, we will return the reference - instead of the object. Otherwise it will raise BadObject + instead of the object. Otherwise it will raise BadObject or BadName """ hexsha = None @@ -119,7 +122,7 @@ def name_to_object(repo, name, return_ref=False): # tried everything ? fail if hexsha is None: - raise BadObject(name) + raise BadName(name) # END assert hexsha was found return Object.new_from_sha(repo, hex_to_bin(hexsha)) diff --git a/git/test/test_repo.py b/git/test/test_repo.py index 9d9f727f..1b3f3ed4 100644 --- a/git/test/test_repo.py +++ b/git/test/test_repo.py @@ -26,8 +26,10 @@ from git import ( GitDB, Submodule, GitCmdObjectDB, - Remote + Remote, + BadName ) +from git.repo.fun import touch from git.util import join_path_native from git.exc import BadObject from gitdb.util import bin_to_hex @@ -35,6 +37,7 @@ from git.compat import ( string_types, defenc ) +from gitdb.test.lib import with_rw_directory import os import sys @@ -682,3 +685,25 @@ class TestRepo(TestBase): def test_remote_method(self): self.failUnlessRaises(ValueError, self.rorepo.remote, 'foo-blue') assert isinstance(self.rorepo.remote(name='origin'), Remote) + + @with_rw_directory + def test_empty_repo(self, rw_dir): + """Assure we can handle empty repositories""" + r = Repo.init(rw_dir, mkdir=False) + # It's ok not to be able to iterate a commit, as there is none + self.failUnlessRaises(ValueError, r.iter_commits) + assert r.active_branch.name == 'master' + assert not r.active_branch.is_valid(), "Branch is yet to be born" + + # actually, when trying to create a new branch without a commit, git itself fails + # We should, however, not fail ungracefully + self.failUnlessRaises(BadName, r.create_head, 'foo') + + new_file_path = os.path.join(rw_dir, "new_file.ext") + touch(new_file_path) + r.index.add([new_file_path]) + r.index.commit("initial commit") + + # Now a branch should be creatable + nb = r.create_head('foo') + assert nb.is_valid() |