Skip to content

Commit

Permalink
Merge pull request #512 from ACCESS-NRI/405-missing-origin-repository
Browse files Browse the repository at this point in the history
 Fix missing origin repository bug in payu checkout
  • Loading branch information
jo-basevi committed Sep 18, 2024
2 parents e59d400 + d7f9e50 commit 68d8482
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
14 changes: 13 additions & 1 deletion payu/git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,19 @@ def remote_branches_dict(self) -> Dict[str, git.Head]:
objects"""
branch_names_dict = {}
for remote in self.repo.remotes:
remote.fetch()
try:
remote.fetch()
except git.exc.GitCommandError:
warnings.warn(
f"Failed to fetch from remote repository: {remote.name} " +
f"(url: {remote.url}). Payu is not able to determine " +
"remote branch names, which are used in payu checkout " +
"to check if a branch already exists, or when creating " +
"a new branch from a remote branch.",
PayuGitWarning
)
continue

for ref in remote.refs:
branch_names_dict[ref.remote_head] = ref
return branch_names_dict
Expand Down
24 changes: 24 additions & 0 deletions test/test_git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,27 @@ def test_git_checkout_existing_branch():
current_hash = current_branch.object.hexsha
assert str(current_branch) == 'branch-1'
assert current_hash == branch_1_hash


def test_git_checkout_missing_origin_repo():
"""In Issue #405, there was a bug where origin remote path was deleted,
the payu checkout would raise an error when fetching remote repository"""
# Create remote repository
remote_repo_path = tmpdir / 'remote_repo'
remote_repo = create_new_repo(remote_repo_path)

# Clone repository
cloned_repo_path = tmpdir / 'cloned_repo'
remote_repo.clone(cloned_repo_path)

# Remove remote directory - this will raise errors when remote is fetched
shutil.rmtree(remote_repo_path)

repo = GitRepository(cloned_repo_path)

# Check remote branches runs with a warning
with pytest.warns(PayuGitWarning):
repo.checkout_branch(branch_name="branch-1", new_branch=True)

# No remote branches found
assert repo.remote_branches_dict() == {}

0 comments on commit 68d8482

Please sign in to comment.