Function: magit-toplevel
magit-toplevel is a byte-compiled function defined in magit-git.el.
Signature
(magit-toplevel &optional DIRECTORY)
Documentation
Return the absolute path to the toplevel of the current repository.
From within the working tree or control directory of a repository return the absolute path to the toplevel directory of the working tree. As a special case, from within a bare repository return the control directory instead. When called outside a repository, return nil.
When optional DIRECTORY is non-nil, return the toplevel for that
directory, instead of the one for default-directory.
Try to respect the option find-file-visit-truename, i.e., when the
value of that option is nil, avoid needlessly returning the truename.
When a symlink to a sub-directory of the working tree is involved,
or when called from within a sub-directory of the gitdir or from the
toplevel of a gitdir, which itself is not located within the working
tree, then it is not possible to avoid returning the truename.
Source Code
;; Defined in ~/.emacs.d/elpa/magit-20260822.1158/magit-git.el
(defun magit-toplevel (&optional directory)
"Return the absolute path to the toplevel of the current repository.
From within the working tree or control directory of a repository
return the absolute path to the toplevel directory of the working
tree. As a special case, from within a bare repository return the
control directory instead. When called outside a repository, return
nil.
When optional DIRECTORY is non-nil, return the toplevel for that
directory, instead of the one for `default-directory'.
Try to respect the option `find-file-visit-truename', i.e., when the
value of that option is nil, avoid needlessly returning the truename.
When a symlink to a sub-directory of the working tree is involved,
or when called from within a sub-directory of the gitdir or from the
toplevel of a gitdir, which itself is not located within the working
tree, then it is not possible to avoid returning the truename."
(magit--with-refresh-cache
(cons (or directory default-directory) 'magit-toplevel)
(magit--with-safe-default-directory directory
(cond-let*
([topdir (magit-rev-parse-safe "--show-toplevel")]
[topdir (magit-expand-git-file-name topdir)]
(cond-let*
(;; Always honor these settings.
[_(not find-file-visit-truename)]
[_(not (getenv "GIT_WORK_TREE"))]
;; "--show-cdup" is the relative path to the toplevel from
;; (file-truename default-directory). Here we pretend it is
;; relative to `default-directory', and go to that directory.
;; Then we check whether "--show-toplevel" still returns the
;; same value and whether "--show-cdup" now is the empty
;; string. If both is the case, then we are at the toplevel
;; of the same working tree, but also avoided needlessly
;; following any symlinks.
[updir (file-name-as-directory
(magit-rev-parse-safe "--show-cdup"))]
[updir (if (file-name-absolute-p updir)
(concat (file-remote-p default-directory) updir)
(expand-file-name updir))]
[updir->topdir
(let ((default-directory updir))
(and (string-equal (magit-rev-parse-safe "--show-cdup") "")
(magit-rev-parse-safe "--show-toplevel")))]
[_(string-equal (magit-expand-git-file-name updir->topdir) topdir)]
updir)
((concat (file-remote-p default-directory)
(file-name-as-directory topdir)))))
([gitdir (magit-rev-parse-safe "--git-dir")]
[gitdir (file-name-as-directory
(if (file-name-absolute-p gitdir)
;; We might have followed a symlink.
(concat (file-remote-p default-directory)
(magit-expand-git-file-name gitdir))
(expand-file-name gitdir)))]
(cond-let*
((magit-bare-repo-p) gitdir)
;; Return the linked working tree, if any.
([link (expand-file-name "gitdir" gitdir)]
[wtree (and (file-exists-p link)
(magit-file-line link))]
;; Ignore ".git/gitdir" files that result from a Git bug.
;; This has long been fixed, but old repository may still
;; exist that contain such a file. See #2364.
[_(not (equal wtree ".git"))]
(file-name-directory (expand-file-name wtree default-directory)))
;; The working directory may not be the parent
;; directory of .git if it was set up with
;; "git init --separate-git-dir". See #2955.
((car (rassoc gitdir magit--separated-gitdirs)))
;; Step outside the control directory to enter the working tree.
((file-name-directory (directory-file-name gitdir)))))))))