antongolub/git-up

View on GitHub
README.md

Summary

Maintainability
Test Coverage
# @antongolub/git-root
Find the closest .git containing path — the git root.

[![CI](https://github.com/antongolub/git-root/workflows/CI/badge.svg)](https://github.com/antongolub/git-root/actions)
[![Maintainability](https://api.codeclimate.com/v1/badges/77d281e69a451680e1d1/maintainability)](https://codeclimate.com/github/antongolub/git-root/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/77d281e69a451680e1d1/test_coverage)](https://codeclimate.com/github/antongolub/git-root/test_coverage)
[![npm (scoped)](https://img.shields.io/npm/v/@antongolub/git-root)](https://www.npmjs.com/package/@antongolub/git-root)

## Motivation
It's known for certain the best way to find git root:
```shell
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](https://github.com/sindresorhus/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
```shell
yarn add @antongolub/git-root
```

## Usage
```ts
import { gitRoot, gitRootSync } from '@antongolub/git-root'

// async
const gitRoot1 = await gitRoot('/optional/cwd/path/')

// sync
const gitRoot2 = gitRoot('/defaults/to/process/cwd/', true)

// sync too
const gitRoot3 = gitRoot.sync()

// `gitRootSync` is an alias for `gitRoot.sync`
const gitRoot4 = gitRootSync()
```

## Alternatives

* [find-git-root](https://github.com/banyudu/find-git-root)
* [git-root](https://github.com/JPeer264/node-git-root)
* [git-toplevel](https://github.com/royriojas/git-toplevel)
* [git-root-path](https://github.com/VishnuTSuresh/git-root-path)

## License
[MIT](./LICENSE)