diff --git a/src/scmrepo/git/backend/pygit2.py b/src/scmrepo/git/backend/pygit2.py index b26da06a..b6b0978c 100644 --- a/src/scmrepo/git/backend/pygit2.py +++ b/src/scmrepo/git/backend/pygit2.py @@ -453,16 +453,24 @@ def _merge_remote_branch( @contextmanager def get_remote(self, url: str) -> Generator["Remote", None, None]: try: - yield self.repo.remotes[url] + remote = self.repo.remotes[url] + url = remote.url except ValueError: - try: - remote_name = uuid() - yield self.repo.remotes.create(remote_name, url) - finally: - self.repo.remotes.delete(remote_name) + pass except KeyError: raise SCMError(f"'{url}' is not a valid Git remote or URL") + if os.name == "nt" and url.startswith("ssh://"): + raise NotImplementedError + if os.name == "nt" and url.startswith("file://"): + url = url[len("file://") :] + + try: + remote_name = uuid() + yield self.repo.remotes.create(remote_name, url) + finally: + self.repo.remotes.delete(remote_name) + def fetch_refspecs( self, url: str, @@ -478,14 +486,6 @@ def fetch_refspecs( refspecs = [refspecs] with self.get_remote(url) as remote: - if os.name == "nt" and remote.url.startswith("ssh://"): - raise NotImplementedError - - if os.name == "nt" and remote.url.startswith("file://"): - url = remote.url[len("file://") :] - self.repo.remotes.set_url(remote.name, url) - remote = self.repo.remotes[remote.name] - fetch_refspecs: List[str] = [] for refspec in refspecs: if ":" in refspec: