Skip to content

Commit 59207ea

Browse files
authored
Merge pull request #422 from projectsyn/fix/gitrepo-push-empty-remote
Fix `GitRepo.push()` when pushing to empty remote repository
2 parents 4ecdf77 + 9bbc165 commit 59207ea

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

commodore/gitrepo.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,14 @@ def commit(self, commit_message: str):
374374

375375
self._repo.index.commit(commit_message, author=author, committer=author)
376376

377-
def push(self, remote: Optional[str] = None) -> Iterable[PushInfo]:
377+
def push(
378+
self, remote: Optional[str] = None, version: Optional[str] = None
379+
) -> Iterable[PushInfo]:
378380
if not remote:
379381
remote = "origin"
380-
return self._repo.remote(remote).push()
382+
if not version:
383+
version = self._default_version()
384+
return self._repo.remote(remote).push(version)
381385

382386
def reset(self, working_tree: bool = False):
383387
self._repo.head.reset(working_tree=working_tree)

tests/test_gitrepo.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,17 @@ def test_gitrepo_reset(tmp_path: Path):
166166
r.reset(working_tree=True)
167167

168168
assert not r.repo.is_dirty()
169+
170+
171+
def test_gitrepo_push_empty_remote(tmp_path: Path):
172+
git.Repo.init(tmp_path / "remote.git", mkdir=True, bare=True)
173+
r = gitrepo.GitRepo(
174+
f"file:///{tmp_path}/remote.git", tmp_path / "local", force_init=True
175+
)
176+
177+
testf = r.working_tree_dir / "test.txt"
178+
with open(testf, "w") as f:
179+
f.write("Hello, world!\n")
180+
r.stage_all()
181+
r.commit("Initial commit")
182+
r.push()

0 commit comments

Comments
 (0)