diff options
-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() |