Function: magit-gitdir

magit-gitdir is a byte-compiled function defined in magit-git.el.

Signature

(magit-gitdir &optional DIRECTORY COMMON)

Documentation

Return the absolute and resolved path of the .git directory.

If the GIT_DIR environment variable is defined, return that. Otherwise return the .git directory for DIRECTORY, or if that is nil, then for default-directory instead. If the directory is not located inside a Git repository, then return nil.

For a linked worktree, the worktree-specific .git directory is returned by default. If COMMON is non-nil, return the GIT_COMMON_DIR environment variable, if defined. Otherwise return the common .git directory, falling back to the main .git directory when DIRECTORY does not belong to a linked worktree.

Source Code

;; Defined in ~/.emacs.d/elpa/magit-20260822.1158/magit-git.el
(defun magit-gitdir (&optional directory common)
  "Return the absolute and resolved path of the .git directory.

If the `GIT_DIR' environment variable is defined, return that.
Otherwise return the .git directory for DIRECTORY, or if that is
nil, then for `default-directory' instead.  If the directory is
not located inside a Git repository, then return nil.

For a linked worktree, the worktree-specific .git directory is returned
by default.  If COMMON is non-nil, return the `GIT_COMMON_DIR'
environment variable, if defined.  Otherwise return the common .git
directory, falling back to the main .git directory when DIRECTORY does
not belong to a linked worktree."
  (let ((default-directory (or directory default-directory)))
    (magit--with-refresh-cache (list default-directory 'magit-gitdir)
      (magit--with-safe-default-directory nil
        (and-let*
            ((dir (magit-rev-parse-safe (if common "--git-common-dir" "--git-dir")))
             (dir (file-name-as-directory (magit-expand-git-file-name dir))))
          (if (file-remote-p dir)
              dir
            (concat (file-remote-p default-directory) dir)))))))