Skip to content

Commit

Permalink
scm: add force parameter to checkout() (iterative#5582)
Browse files Browse the repository at this point in the history
The `GitPythonBackend.checkout()` method already implicitly supported
this by forwarding `**kwargs` to the backend. `pygit2` didn't support
this yet.

This patch fixes this inconsistency.
  • Loading branch information
duijf authored Mar 10, 2021
1 parent 8433b36 commit 9f224ec
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
6 changes: 5 additions & 1 deletion dvc/scm/git/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ def commit(self, msg: str, no_verify: bool = False):

@abstractmethod
def checkout(
self, branch: str, create_new: Optional[bool] = False, **kwargs,
self,
branch: str,
create_new: Optional[bool] = False,
force: bool = False,
**kwargs,
):
pass

Expand Down
6 changes: 5 additions & 1 deletion dvc/scm/git/backend/dulwich.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ def commit(self, msg: str, no_verify: bool = False):
raise SCMError("Git commit failed") from exc

def checkout(
self, branch: str, create_new: Optional[bool] = False, **kwargs,
self,
branch: str,
create_new: Optional[bool] = False,
force: bool = False,
**kwargs,
):
raise NotImplementedError

Expand Down
10 changes: 7 additions & 3 deletions dvc/scm/git/backend/gitpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,16 @@ def commit(self, msg: str, no_verify: bool = False):
raise SCMError("Git pre-commit hook failed") from exc

def checkout(
self, branch: str, create_new: Optional[bool] = False, **kwargs
self,
branch: str,
create_new: Optional[bool] = False,
force: bool = False,
**kwargs,
):
if create_new:
self.repo.git.checkout("HEAD", b=branch, **kwargs)
self.repo.git.checkout("HEAD", b=branch, force=force, **kwargs)
else:
self.repo.git.checkout(branch, **kwargs)
self.repo.git.checkout(branch, force=force, **kwargs)

def pull(self, **kwargs):
infos = self.repo.remote().pull(**kwargs)
Expand Down
14 changes: 10 additions & 4 deletions dvc/scm/git/backend/pygit2.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,28 @@ def commit(self, msg: str, no_verify: bool = False):
raise NotImplementedError

def checkout(
self, branch: str, create_new: Optional[bool] = False, **kwargs,
self,
branch: str,
create_new: Optional[bool] = False,
force: bool = False,
**kwargs,
):
from pygit2 import GitError
from pygit2 import GIT_CHECKOUT_FORCE, GitError

checkout_strategy = GIT_CHECKOUT_FORCE if force else None

if create_new:
commit = self.repo.revparse_single("HEAD")
new_branch = self.repo.branches.local.create(branch, commit)
self.repo.checkout(new_branch)
self.repo.checkout(new_branch, strategy=checkout_strategy)
else:
if branch == "-":
branch = "@{-1}"
try:
commit, ref = self.repo.resolve_refish(branch)
except (KeyError, GitError):
raise RevError(f"unknown Git revision '{branch}'")
self.repo.checkout_tree(commit)
self.repo.checkout_tree(commit, strategy=checkout_strategy)
detach = kwargs.get("detach", False)
if ref and not detach:
self.repo.set_head(ref.name)
Expand Down

0 comments on commit 9f224ec

Please sign in to comment.