Skip to content

Commit

Permalink
magit-insert-remote-branches: Skip HEAD if not symref
Browse files Browse the repository at this point in the history
magit-insert-remote-branches strips the remote name from the short
refname (e.g., "origin/foo") when magit-refs-show-remote-prefix is
nil.  That can trigger an error if 1) the remote is mis-configured in
a way where for-each-ref reports an empty symref field for the HEAD
ref and 2) the Git version is recent enough to shorten the HEAD ref to
"$remote" instead of "$remote/HEAD".

Note that mis-configuring a remote in such a way isn't as easy as just
checking out a detached HEAD in the remote repo (git-clone won't
create the HEAD ref in that case) or creating an ambiguous
refs/heads/HEAD branch in the remote repo (git-clone will still use
the regular HEAD symref in that case).  The key pieces of the
problematic remotes reported in gh-5092 are 1) an ambiguous
refs/heads/HEAD and 2) the regular HEAD symref pointing to a ref that
does not exist.
  • Loading branch information
kyleam committed Feb 26, 2024
1 parent 039a180 commit 45168b3
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions lisp/magit-refs.el
Original file line number Diff line number Diff line change
Expand Up @@ -578,14 +578,18 @@ line is inserted at all."
(cl-substitute nil ""
(split-string line "\0")
:test #'equal)))
(if head-branch
;; Note: Use `ref' instead of `branch' for the check
;; below because 'refname:short' shortens the remote
;; HEAD to '<remote>' instead of '<remote>/HEAD' as of
;; Git v2.40.0.
(progn (cl-assert
(equal ref (concat "refs/remotes/" remote "/HEAD")))
(setq head head-branch))
(cond
(head-branch
;; Note: Use `ref' instead of `branch' for the check
;; below because 'refname:short' shortens the remote
;; HEAD to '<remote>' instead of '<remote>/HEAD' as of
;; Git v2.40.0.
(cl-assert
(equal ref (concat "refs/remotes/" remote "/HEAD")))
(setq head head-branch))
((not (equal ref (concat "refs/remotes/" remote "/HEAD")))
;; ^ Skip mis-configured remotes where HEAD is not a
;; symref. See #5092.
(when (magit-refs--insert-refname-p branch)
(magit-insert-section (branch branch t)
(let ((headp (equal branch head))
Expand All @@ -605,7 +609,7 @@ line is inserted at all."
(and msg (magit-log-propertize-keywords nil msg))))
(when (magit-buffer-margin-p)
(magit-refs--format-margin branch))
(magit-refs--insert-cherry-commits branch)))))))
(magit-refs--insert-cherry-commits branch))))))))
(insert ?\n)
(magit-make-margin-overlay nil t))))

Expand Down

0 comments on commit 45168b3

Please sign in to comment.