Skip to content

Commit a2ba900

Browse files
committed
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.
1 parent 3d8c97c commit a2ba900

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/scmrepo/git/backend/pygit2.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -453,16 +453,24 @@ def _merge_remote_branch(
453453
@contextmanager
454454
def get_remote(self, url: str) -> Generator["Remote", None, None]:
455455
try:
456-
yield self.repo.remotes[url]
456+
remote = self.repo.remotes[url]
457+
url = remote.url
457458
except ValueError:
458-
try:
459-
remote_name = uuid()
460-
yield self.repo.remotes.create(remote_name, url)
461-
finally:
462-
self.repo.remotes.delete(remote_name)
459+
pass
463460
except KeyError:
464461
raise SCMError(f"'{url}' is not a valid Git remote or URL")
465462

463+
if os.name == "nt" and url.startswith("ssh://"):
464+
raise NotImplementedError
465+
if os.name == "nt" and url.startswith("file://"):
466+
url = url[len("file://") :]
467+
468+
try:
469+
remote_name = uuid()
470+
yield self.repo.remotes.create(remote_name, url)
471+
finally:
472+
self.repo.remotes.delete(remote_name)
473+
466474
def fetch_refspecs(
467475
self,
468476
url: str,
@@ -478,14 +486,6 @@ def fetch_refspecs(
478486
refspecs = [refspecs]
479487

480488
with self.get_remote(url) as remote:
481-
if os.name == "nt" and remote.url.startswith("ssh://"):
482-
raise NotImplementedError
483-
484-
if os.name == "nt" and remote.url.startswith("file://"):
485-
url = remote.url[len("file://") :]
486-
self.repo.remotes.set_url(remote.name, url)
487-
remote = self.repo.remotes[remote.name]
488-
489489
fetch_refspecs: List[str] = []
490490
for refspec in refspecs:
491491
if ":" in refspec:

0 commit comments

Comments
 (0)