From 70df084ed37addfc67047283690483fe5e87a7b3 Mon Sep 17 00:00:00 2001 From: karajan1001 Date: Mon, 6 Feb 2023 16:16:42 +0800 Subject: [PATCH] Solve the url change problem in `pygit2` fetch_refspec Because for now `pygit2` fetch didn't support `file://` format on Windows, we removed this prefix. But didn't add it back after the fetch operation finished, and it makes the following `dulwich` backend goes wrong. So here in the PR, we always create a new remote and delete it after the operation to make the remote config unchanged after the operation. --- src/scmrepo/git/backend/pygit2.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) 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: