Skip to content

Commit 0bb2fc8

Browse files
committed
Commit.create: now handles empty repositories correctly
1 parent 15ee0ac commit 0bb2fc8

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

lib/git/objects/commit.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,13 @@ def create_from_tree(cls, repo, tree, message, parent_commits=None, head=False):
313313
"""
314314
parents = parent_commits
315315
if parent_commits is None:
316-
parent_commits = [ repo.head.commit ]
316+
try:
317+
parent_commits = [ repo.head.commit ]
318+
except ValueError:
319+
# empty repositories have no head commit
320+
parent_commits = list()
321+
# END handle parent commits
322+
# END if parent commits are unset
317323

318324
parent_args = [ ("-p", str(commit)) for commit in parent_commits ]
319325

@@ -331,7 +337,14 @@ def create_from_tree(cls, repo, tree, message, parent_commits=None, head=False):
331337
new_commit = cls(repo, commit_sha)
332338

333339
if head:
334-
repo.head.commit = new_commit
340+
try:
341+
repo.head.commit = new_commit
342+
except ValueError:
343+
# head is not yet set to master - create it and set it
344+
import git.refs
345+
master = git.refs.Head.create(repo, 'master', commit=new_commit)
346+
repo.head.reference = master
347+
# END handle empty repositories
335348
# END advance head handling
336349

337350
return new_commit

0 commit comments

Comments
 (0)