Skip to content

Commit 75e0b28

Browse files
committed
Wrap KeyError from fetch_refspec into SCMError.
fix: #154
1 parent ae4c29c commit 75e0b28

File tree

2 files changed

+43
-35
lines changed

2 files changed

+43
-35
lines changed

src/scmrepo/git/backend/dulwich/__init__.py

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -646,15 +646,16 @@ def determine_wants(
646646
if remote_refs[lh] not in self.repo.object_store
647647
]
648648

649-
try:
649+
with reraise(
650+
Exception, SCMError(f"'{url}' is not a valid Git remote or URL")
651+
):
650652
_remote, location = get_remote_repo(self.repo, url)
651653
client, path = get_transport_and_path(location, **kwargs)
652-
except Exception as exc:
653-
raise SCMError(
654-
f"'{url}' is not a valid Git remote or URL"
655-
) from exc
656654

657-
try:
655+
with reraise(
656+
(NotGitRepository, KeyError),
657+
SCMError(f"Git failed to fetch ref from '{url}'"),
658+
):
658659
fetch_result = client.fetch(
659660
path,
660661
self.repo,
@@ -663,37 +664,37 @@ def determine_wants(
663664
else None,
664665
determine_wants=determine_wants,
665666
)
666-
except NotGitRepository as exc:
667-
raise SCMError(f"Git failed to fetch ref from '{url}'") from exc
668-
669-
result = {}
670-
671-
for (lh, rh, _) in fetch_refs:
672-
refname = os.fsdecode(rh)
673-
if rh in self.repo.refs:
674-
if self.repo.refs[rh] == fetch_result.refs[lh]:
675-
result[refname] = SyncStatus.UP_TO_DATE
676-
continue
677-
try:
678-
check_diverged(
679-
self.repo, self.repo.refs[rh], fetch_result.refs[lh]
680-
)
681-
except DivergedBranches:
682-
if not force:
683-
overwrite = (
684-
on_diverged(
685-
os.fsdecode(rh),
686-
os.fsdecode(fetch_result.refs[lh]),
687-
)
688-
if on_diverged
689-
else False
667+
668+
result = {}
669+
670+
for (lh, rh, _) in fetch_refs:
671+
refname = os.fsdecode(rh)
672+
if rh in self.repo.refs:
673+
if self.repo.refs[rh] == fetch_result.refs[lh]:
674+
result[refname] = SyncStatus.UP_TO_DATE
675+
continue
676+
try:
677+
check_diverged(
678+
self.repo,
679+
self.repo.refs[rh],
680+
fetch_result.refs[lh],
690681
)
691-
if not overwrite:
692-
result[refname] = SyncStatus.DIVERGED
693-
continue
682+
except DivergedBranches:
683+
if not force:
684+
overwrite = (
685+
on_diverged(
686+
os.fsdecode(rh),
687+
os.fsdecode(fetch_result.refs[lh]),
688+
)
689+
if on_diverged
690+
else False
691+
)
692+
if not overwrite:
693+
result[refname] = SyncStatus.DIVERGED
694+
continue
694695

695-
self.repo.refs[rh] = fetch_result.refs[lh]
696-
result[refname] = SyncStatus.SUCCESS
696+
self.repo.refs[rh] = fetch_result.refs[lh]
697+
result[refname] = SyncStatus.SUCCESS
697698
return result
698699

699700
def _stash_iter(self, ref: str):

tests/test_git.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import pytest
66
from asyncssh import SFTPClient
77
from asyncssh.connection import SSHClientConnection
8+
from dulwich.client import LocalGitClient
89
from git import Repo as GitPythonRepo
10+
from pytest_mock import MockerFixture
911
from pytest_test_utils import TempDirFactory, TmpDir
1012
from pytest_test_utils.matchers import Matcher
1113

@@ -325,6 +327,7 @@ def test_fetch_refspecs(
325327
git: Git,
326328
remote_git_dir: TmpDir,
327329
use_url: bool,
330+
mocker: MockerFixture,
328331
):
329332

330333
from scmrepo.git.backend.dulwich import SyncStatus
@@ -370,6 +373,10 @@ def test_fetch_refspecs(
370373
}
371374
assert baz_rev == scm.get_ref("refs/foo/baz")
372375

376+
with pytest.raises(SCMError):
377+
mocker.patch.object(LocalGitClient, "fetch", side_effect=KeyError)
378+
git.fetch_refspecs(remote, "refs/foo/bar:refs/foo/bar")
379+
373380

374381
@pytest.mark.skip_git_backend("pygit2", "gitpython")
375382
@pytest.mark.parametrize("use_url", [True, False])

0 commit comments

Comments
 (0)