     (and (straight-vc-git--ensure-remotes recipe)
          (or (and (straight-vc-git--ensure-nothing-in-progress local-repo)
                   (straight-vc-git--ensure-worktree local-repo)
-                  (straight-vc-git--ensure-head
+                  (straight-vc-git--ensure-head-at-branch
                    local-repo
                    (or branch (straight-vc-git--default-remote-branch
                                remote local-repo))))

                              remote local-repo))))
      (while t
        (and (straight-vc-git--ensure-local recipe)
             (or (straight-vc-git--ensure-head
                  local-repo remote-branch
                  (format "%s/%s" remote remote-branch))
                 (straight-register-repo-modification local-repo))

              (straight-vc-git--encode-url
               fork-repo fork-host fork-protocol))))))

(defun straight-vc-git--rebase-in-progress-p ()
  "Return t if rebase is in progress.
Delegate to magit's version of this function if available; it may
handle cases we don't try to here. For example, magit's version does
something more complicated to locate the git directory from Emacs
running under cygwin."
  (let ((default-directory straight--default-directory))
    (if (fboundp 'magit-rebase-in-progress-p)
        (magit-rebase-in-progress-p)
      (let ((git-dir (file-name-as-directory
                      (straight--process-output
                       "git" "rev-parse" "--git-dir"))))
        (or (file-exists-p (expand-file-name "rebase-merge" git-dir))
            (file-exists-p (expand-file-name
                            "rebase-apply/onto" git-dir)))))))

(defun straight-vc-git--ensure-nothing-in-progress (local-repo)
  "Ensure that no merge or rebase is unfinished in LOCAL-REPO.
LOCAL-REPO is a string."
  (cl-flet ((short-status () (straight--split-and-trim
                              (straight--process-output
                               "git" "status" "--short")
                              2)))
    (cond
     ((straight-vc-git--rebase-in-progress-p)
      (ignore
       (straight-vc-git--popup
         (format "Repository %S has an unfinished rebase:\n\n%s"
                 local-repo (short-status))
         ("a" "Abort rebase"
          (straight--process-output "git" "rebase" "--abort")))))
     ((not (string-empty-p
            (straight--process-output "git" "ls-files" "--unmerged")))
      (ignore
       (straight-vc-git--popup
         (format "Repository %S has a merge conflict:\n\n%s"
                 local-repo (short-status))
         ("a" "Abort merge"
          (straight--process-output "git" "merge" "--abort")))))
     (t t))))

(cl-defun straight-vc-git--ensure-worktree (local-repo)
  "Ensure that LOCAL-REPO has a clean worktree.
