When .git Is a Symlink: Fixing Commitlint's Git Directory Detection

In a project where I keep the real .git directory outside the working tree, the project root has a symlink named .git. I wrote a commit message, ran commitlint -e, and got EISDIR: illegal operation on a directory, read. The same command worked in a normal clone. That pointed at the symlink, but the error came from a file read, so I traced how commitlint resolves the edit path.

getEditFilePath(top, edit) checks <top>/.git with fs.lstat(dotgitPath). If it is a directory, it returns .git/COMMIT_EDITMSG; otherwise it treats .git as a worktree pointer file containing gitdir: .... lstat() does not follow symlinks, so a symlink to the real Git directory reported isDirectory() === false. The code took the pointer-file branch and called fs.readFile() on the symlink, which resolved to a directory and threw EISDIR.

const dotgitPath = path.join(top, ".git");

// Before: describes the symlink, not the directory it points to.
const before = await fs.lstat(dotgitPath);

// After: follows the symlink.
const dotgitStats: Stats = await fs.stat(dotgitPath);

if (dotgitStats.isDirectory()) {
  return path.join(top, ".git/COMMIT_EDITMSG");
}

Changing the check to fs.stat(dotgitPath) follows the link, so isDirectory() is true and the direct edit path is returned. Real .git pointer files still read as files, so the worktree branch is unaffected. I considered resolving the path with git rev-parse --git-path COMMIT_EDITMSG, which Git already supports for worktrees and symlinks, but spawning a Git process every time commitlint resolves the edit path felt heavier than the filesystem fix.

The test uses gitBootstrap, renames .git to actual-git-dir, creates a symlink (junction on Windows), writes a valid COMMIT_EDITMSG, and runs commitlint -e. It expects the [type-enum] error and ExitCode.CommitlintErrorDefault. I verified it reproduced EISDIR with the old lstat implementation, which matters because a one-line fix can otherwise pass for the wrong reason. View the pull request on GitHub →

               . .
    . .       . . .
 . . . .       . .     .
. . . .       . . .   . . .               ,
 . . . .     . . .     . .       _       d#D
. . . . .   . . . . ._. . .    _(%)_      `
   .     . . . . . _(#)_ .    (%%O%%)     "Bzzzz"
        . . . . _ (##O##) .   _ (%)   _
       . . .  _(#)_ (#)  . _ (%)_:  _(%)_
      . . . .(##O##)._\  _(#)%O%%):(%%O%%)
         . .   (#)  _  \(##O##))  |,'(%)
              `. ._(#)_ \,(#)  `. ;  _
                 (##O##) `.      Y _(#)_
                  .(#)\ . .).   / (##O##)
                 . . . `-.(. . ;  ,'(#)
                  . . . . .\. .| (
                 . . . d888888888888b
                  . . ,88888888888888b
                   . .d888888888888888
                    . d888888888888888
                     .`88888888888888P
                       Y8888888888888
                        Y8888888888P
_________________________`Y8888888P____________