Skip to content

Commit

Permalink
Fetch/Push:
Browse files Browse the repository at this point in the history
Add `on_diverged` back
  • Loading branch information
karajan1001 committed Apr 19, 2022
1 parent 6cef60b commit 89dcf4b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
12 changes: 12 additions & 0 deletions scmrepo/git/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ def push_refspecs(
url: str,
refspecs: Union[str, Iterable[str]],
force: bool = False,
on_diverged: Optional[Callable[[str, str], bool]] = None,
progress: Callable[["GitProgressEvent"], None] = None,
**kwargs,
) -> Mapping[str, SyncStatus]:
Expand All @@ -228,6 +229,11 @@ def push_refspecs(
refspecs: Iterable containing refspecs to fetch.
Note that this will not match subkeys.
force: If True, remote refs will be overwritten.
on_diverged: Callback function which will be called if local ref
and remote have diverged and force is False. If the callback
returns True the remote ref will be overwritten.
Callback will be of the form:
on_diverged(local_refname, remote_sha)
"""

@abstractmethod
Expand All @@ -236,6 +242,7 @@ def fetch_refspecs(
url: str,
refspecs: Union[str, Iterable[str]],
force: bool = False,
on_diverged: Optional[Callable[[str, str], bool]] = None,
progress: Callable[["GitProgressEvent"], None] = None,
**kwargs,
) -> Mapping[str, SyncStatus]:
Expand All @@ -247,6 +254,11 @@ def fetch_refspecs(
refspecs: Iterable containing refspecs to fetch.
Note that this will not match subkeys.
force: If True, local refs will be overwritten.
on_diverged: Callback function which will be called if local ref
and remote have diverged and force is False. If the callback
returns True the local ref will be overwritten.
Callback will be of the form:
on_diverged(local_refname, remote_sha)
"""

@abstractmethod
Expand Down
31 changes: 26 additions & 5 deletions scmrepo/git/backend/dulwich/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ def push_refspecs(
url: str,
refspecs: Union[str, Iterable[str]],
force: bool = False,
on_diverged: Optional[Callable[[str, str], bool]] = None,
progress: Callable[["GitProgressEvent"], None] = None,
**kwargs,
) -> Mapping[str, SyncStatus]:
Expand Down Expand Up @@ -533,8 +534,16 @@ def update_refs(refs):
check_diverged(self.repo, refs[rh], self.repo.refs[lh])
except DivergedBranches:
if not force:
change_result[refname] = SyncStatus.DIVERGED
continue
overwrite = (
on_diverged(
os.fsdecode(lh), os.fsdecode(refs[rh])
)
if on_diverged
else False
)
if not overwrite:
change_result[refname] = SyncStatus.DIVERGED
continue

if lh is None:
value = ZERO_SHA
Expand All @@ -556,7 +565,8 @@ def update_refs(refs):
),
)
except (NotGitRepository, SendPackError) as exc:
raise SCMError(f"Git failed to push ref to '{url}'") from exc
src = [lh for (lh, _, _) in selected_refs]
raise SCMError(f"Git failed to push '{src}' to '{url}'") from exc
except HTTPUnauthorized:
raise AuthError(url)
return change_result
Expand All @@ -566,6 +576,7 @@ def fetch_refspecs(
url: str,
refspecs: Union[str, Iterable[str]],
force: Optional[bool] = False,
on_diverged: Optional[Callable[[str, str], bool]] = None,
progress: Callable[["GitProgressEvent"], None] = None,
**kwargs,
) -> Mapping[str, SyncStatus]:
Expand Down Expand Up @@ -629,8 +640,18 @@ def determine_wants(remote_refs):
)
except DivergedBranches:
if not force:
result[refname] = SyncStatus.DIVERGED
continue
overwrite = (
on_diverged(
os.fsdecode(rh),
os.fsdecode(fetch_result.refs[lh]),
)
if on_diverged
else False
)
if not overwrite:
result[refname] = SyncStatus.DIVERGED
continue

self.repo.refs[rh] = fetch_result.refs[lh]
result[refname] = SyncStatus.SUCCESS
return result
Expand Down
2 changes: 2 additions & 0 deletions scmrepo/git/backend/gitpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ def push_refspecs(
url: str,
refspecs: Union[str, Iterable[str]],
force: bool = False,
on_diverged: Optional[Callable[[str, str], bool]] = None,
progress: Callable[["GitProgressEvent"], None] = None,
**kwargs,
) -> Mapping[str, SyncStatus]:
Expand All @@ -489,6 +490,7 @@ def fetch_refspecs(
url: str,
refspecs: Union[str, Iterable[str]],
force: bool = False,
on_diverged: Optional[Callable[[str, str], bool]] = None,
progress: Callable[["GitProgressEvent"], None] = None,
**kwargs,
) -> Mapping[str, SyncStatus]:
Expand Down
2 changes: 2 additions & 0 deletions scmrepo/git/backend/pygit2.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ def push_refspecs(
url: str,
refspecs: Union[str, Iterable[str]],
force: bool = False,
on_diverged: Optional[Callable[[str, str], bool]] = None,
progress: Callable[["GitProgressEvent"], None] = None,
**kwargs,
) -> Mapping[str, SyncStatus]:
Expand All @@ -429,6 +430,7 @@ def fetch_refspecs(
url: str,
refspecs: Union[str, Iterable[str]],
force: bool = False,
on_diverged: Optional[Callable[[str, str], bool]] = None,
progress: Callable[["GitProgressEvent"], None] = None,
**kwargs,
) -> Mapping[str, SyncStatus]:
Expand Down

0 comments on commit 89dcf4b

Please sign in to comment.