|
3 | 3 | import logging |
4 | 4 | import os |
5 | 5 | import re |
| 6 | +import typing |
6 | 7 | from collections import OrderedDict |
7 | 8 | from collections.abc import Mapping |
8 | 9 | from contextlib import contextmanager |
9 | 10 | from functools import partialmethod |
10 | | -from typing import Dict, Iterable, Optional, Tuple, Type, Union |
| 11 | +from typing import TYPE_CHECKING, Callable, Dict, Iterable, Optional, Tuple, Type, Union |
11 | 12 |
|
12 | 13 | from funcy import cached_property, first |
13 | 14 | from pathspec.patterns import GitWildMatchPattern |
|
16 | 17 | from scmrepo.exceptions import FileNotInRepoError, GitHookAlreadyExists, RevError |
17 | 18 | from scmrepo.utils import relpath |
18 | 19 |
|
19 | | -from .backend.base import BaseGitBackend, NoGitBackendError |
| 20 | +from .backend.base import BaseGitBackend, NoGitBackendError, SyncStatus |
20 | 21 | from .backend.dulwich import DulwichBackend |
21 | 22 | from .backend.gitpython import GitPythonBackend |
22 | 23 | from .backend.pygit2 import Pygit2Backend |
23 | 24 | from .stash import Stash |
24 | 25 |
|
| 26 | +if TYPE_CHECKING: |
| 27 | + from scmrepo.progress import GitProgressEvent |
| 28 | + |
25 | 29 | logger = logging.getLogger(__name__) |
26 | 30 |
|
27 | 31 | BackendCls = Type[BaseGitBackend] |
@@ -310,6 +314,35 @@ def add_commit( |
310 | 314 | self.add(paths) |
311 | 315 | self.commit(msg=message) |
312 | 316 |
|
| 317 | + _fetch_refspecs = partialmethod( |
| 318 | + _backend_func, "fetch_refspecs", backends=["pygit2", "dulwich"] |
| 319 | + ) |
| 320 | + |
| 321 | + def fetch_refspecs( |
| 322 | + self, |
| 323 | + url: str, |
| 324 | + refspecs: Union[str, Iterable[str]], |
| 325 | + force: bool = False, |
| 326 | + on_diverged: Optional[Callable[[str, str], bool]] = None, |
| 327 | + progress: Optional[Callable[["GitProgressEvent"], None]] = None, |
| 328 | + **kwargs, |
| 329 | + ) -> typing.Mapping[str, SyncStatus]: |
| 330 | + from .credentials import get_matching_helper_commands |
| 331 | + |
| 332 | + if "dulwich" in kwargs.get("backends", self.backends.backends) and any( |
| 333 | + get_matching_helper_commands(url, self.dulwich.repo.get_config_stack()) |
| 334 | + ): |
| 335 | + kwargs["backends"] = ["dulwich"] |
| 336 | + |
| 337 | + return self._fetch_refspecs( |
| 338 | + url, |
| 339 | + refspecs, |
| 340 | + force=force, |
| 341 | + on_diverged=on_diverged, |
| 342 | + progress=progress, |
| 343 | + **kwargs, |
| 344 | + ) |
| 345 | + |
313 | 346 | is_ignored = partialmethod(_backend_func, "is_ignored") |
314 | 347 | add = partialmethod(_backend_func, "add") |
315 | 348 | commit = partialmethod(_backend_func, "commit") |
@@ -337,9 +370,6 @@ def add_commit( |
337 | 370 | iter_remote_refs = partialmethod(_backend_func, "iter_remote_refs") |
338 | 371 | get_refs_containing = partialmethod(_backend_func, "get_refs_containing") |
339 | 372 | push_refspecs = partialmethod(_backend_func, "push_refspecs") |
340 | | - fetch_refspecs = partialmethod( |
341 | | - _backend_func, "fetch_refspecs", backends=["pygit2", "dulwich"] |
342 | | - ) |
343 | 373 | _stash_iter = partialmethod(_backend_func, "_stash_iter") |
344 | 374 | _stash_push = partialmethod(_backend_func, "_stash_push") |
345 | 375 | _stash_apply = partialmethod(_backend_func, "_stash_apply") |
|
0 commit comments