@antongolub/git-root
Find the closest .git containing path — the git root.
Motivation
It's known for certain the best way to find git root:
git rev-parse --show-toplevel
However, if git
executable or child_process.exec
are not available for some (security?) reasons, it's advisable to use tools like this one.
Inspired by pkg-dir.
Features
- Searches for
.git
up the dir tree - Handles
gitdir: </some/path.git>
redirects - TS and Flow typings out of box
- Sync/async methods
Install
yarn add @antongolub/git-root
Usage
import { gitRoot, gitRootSync } from '@antongolub/git-root'
const gitRoot1 = await gitRoot('/optional/cwd/path/')
const gitRoot2 = gitRoot('/defaults/to/process/cwd/', true)
const gitRoot3 = gitRoot.sync()
const gitRoot4 = gitRootSync()
Alternatives