Function: magit-list-worktrees
magit-list-worktrees is a byte-compiled function defined in
magit-git.el.
Signature
(magit-list-worktrees)
Documentation
Return list of the worktrees of this repository.
The returned list has the form (PATH COMMIT BRANCH BARE DETACHED LOCKED PRUNABLE). The last four elements are booleans, with the exception of LOCKED and PRUNABLE, which may also be strings. See git-worktree(1) manpage for the meaning of the various parts.
This function corrects a situation where "git worktree list"
would claim a worktree is bare, even though the working tree is
specified using core.worktree.
Source Code
;; Defined in ~/.emacs.d/elpa/magit-20260411.1452/magit-git.el
(defun magit-list-worktrees ()
"Return list of the worktrees of this repository.
The returned list has the form (PATH COMMIT BRANCH BARE DETACHED
LOCKED PRUNABLE). The last four elements are booleans, with the
exception of LOCKED and PRUNABLE, which may also be strings.
See git-worktree(1) manpage for the meaning of the various parts.
This function corrects a situation where \"git worktree list\"
would claim a worktree is bare, even though the working tree is
specified using `core.worktree'."
(let ((remote (file-remote-p default-directory))
worktrees worktree)
(dolist (line (if (magit-git-version>= "2.36")
(magit-git-items "worktree" "list" "--porcelain" "-z")
(magit-git-lines "worktree" "list" "--porcelain")))
(cond ((string-prefix-p "worktree" line)
(let ((path (substring line 9)))
(when remote
(setq path (concat remote path)))
;; If the git directory is separate from the main
;; worktree, then "git worktree" returns the git
;; directory instead of the worktree, which isn't
;; what it is supposed to do and not what we want.
;; However, if the worktree has been removed, then
;; we want to return it anyway; instead of nil.
(setq path (or (magit-toplevel path) path))
(setq worktree (list path nil nil nil nil nil nil))
(push worktree worktrees)))
((string-prefix-p "HEAD" line)
(setf (nth 1 worktree) (substring line 5)))
((string-prefix-p "branch" line)
(setf (nth 2 worktree) (substring line 18)))
((string-equal line "bare")
(let* ((default-directory (car worktree))
(wt (and (not (magit-get-boolean "core.bare"))
(magit-get "core.worktree"))))
(cond ((and wt (file-exists-p (expand-file-name wt)))
(setf (nth 0 worktree) (expand-file-name wt))
(setf (nth 2 worktree) (magit-rev-parse "HEAD"))
(setf (nth 3 worktree) (magit-get-current-branch)))
((setf (nth 3 worktree) t)))))
((string-equal line "detached")
(setf (nth 4 worktree) t))
((string-prefix-p line "locked")
(setf (nth 5 worktree)
(if (> (length line) 6) (substring line 7) t)))
((string-prefix-p line "prunable")
(setf (nth 6 worktree)
(if (> (length line) 8) (substring line 9) t)))))
(nreverse worktrees)))