diff --git a/project.py b/project.py index 07685dae..80a6c395 100644 --- a/project.py +++ b/project.py @@ -727,12 +727,34 @@ class Project: return None def IsRebaseInProgress(self): + """Returns true if a rebase or "am" is in progress""" + # "rebase-apply" is used for "git rebase". + # "rebase-merge" is used for "git am". return ( os.path.exists(self.work_git.GetDotgitPath("rebase-apply")) or os.path.exists(self.work_git.GetDotgitPath("rebase-merge")) or os.path.exists(os.path.join(self.worktree, ".dotest")) ) + def IsCherryPickInProgress(self): + """Returns True if a cherry-pick is in progress.""" + return os.path.exists(self.work_git.GetDotgitPath("CHERRY_PICK_HEAD")) + + def _AbortRebase(self): + """Abort ongoing rebase, cherry-pick or patch apply (am). + + If no rebase, cherry-pick or patch apply was in progress, this method + ignores the status and continues. + """ + + def _git(*args): + # Ignore return code, in case there was no rebase in progress. + GitCommand(self, *args, log_as_error=False).Wait() + + _git("cherry-pick", "--abort") + _git("rebase", "--abort") + _git("am", "--abort") + def IsDirty(self, consider_untracked=True): """Is the working directory modified in some way?""" self.work_git.update_index( @@ -1583,7 +1605,15 @@ class Project: if branch is None or syncbuf.detach_head: # Currently on a detached HEAD. The user is assumed to # not have any local modifications worth worrying about. - if self.IsRebaseInProgress(): + rebase_in_progress = ( + self.IsRebaseInProgress() or self.IsCherryPickInProgress() + ) + if rebase_in_progress and force_checkout: + self._AbortRebase() + rebase_in_progress = ( + self.IsRebaseInProgress() or self.IsCherryPickInProgress() + ) + if rebase_in_progress: fail(_PriorSyncFailedError(project=self.name)) return