Find .git directory with support of submodules, --separate-git-dir and linked worktrees.
const gitdir = require('find-gitdir')Let's say we have a git repository at /example and that this is our working directory. Its gitdir will commonly be at /example/.git.
await gitdir() // /example/.gitLet's also say we have a git submodule at ./beep. Then find-gitdir will find a symbolic link at ./beep/.git pointing to the gitdir ../.git/modules/beep:
await gitdir('./beep') // /example/.git/modules/beepBy default find-gitdir does not look in parent directories. It can be enabled:
await gitdir('./node_modules/find-gitdir') // null
await gitdir('./node_modules/find-gitdir', { roam: true }) // /example/.gitYields an absolute path to a .git directory or null if not found, given working directory cwd which defaults to process.cwd(). If no callback if provided, a promise is returned. Options:
roam(boolean, defaultfalse): enable looking in parent directoriescommon(boolean, defaultfalse): ifcwdis a linked worktree then return the gitdir of the main worktree (a.k.a.$GIT_COMMON_DIR) instead of the gitdir of the linked worktree. This can be useful if you need non-worktree files likeconfig.
If options is a boolean, it is interpreted as options.roam for backwards compatibility.
Synchronous variant. Returns an absolute path or null.
With npm do:
npm install find-gitdir
MIT © Vincent Weevers