diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..7af0c07 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# Changelog + +## 0.1.0 - [2020-08-24] + +- first release diff --git a/README.md b/README.md new file mode 100644 index 0000000..380d24a --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# deno_is_git + +[![tag](https://img.shields.io/github/release/justjavac/deno_is_git)](https://github.com/justjavac/deno_is_git/releases) +[![Build Status](https://github.com/justjavac/deno_is_git/workflows/ci/badge.svg?branch=master)](https://github.com/justjavac/deno_is_git/actions) +[![license](https://img.shields.io/github/license/justjavac/deno_is_git)](https://github.com/justjavac/deno_is_git/blob/master/LICENSE) + +Whether the filepath is a **git repository**. + +## Usage + +```ts +import { isGit, isGitSync } from "https://deno.land/x/is_git/mod.ts"; + +await isGit(); // true or false of Deno.cwd() +await isGit('any/git/repo'); // true or false + +isGitSync(); // true or false of Deno.cwd() +isGitSync('any/git/repo'); // true or false +``` + +## Example + +```bash +deno run --allow-read https://deno.land/x/is_git/example.ts +``` + +## License + +[deno_is_git](https://github.com/justjavac/deno_is_git) is released under the MIT License. See the bundled [LICENSE](./LICENSE) file for details. diff --git a/example.ts b/example.ts new file mode 100644 index 0000000..ee31b85 --- /dev/null +++ b/example.ts @@ -0,0 +1,3 @@ +import { isGit } from "./mod.ts"; + +console.log(`current directory is a git repository: ${await isGit()}`); diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..b9fd7da --- /dev/null +++ b/mod.ts @@ -0,0 +1,44 @@ +import { join, isAbsolute } from "https://deno.land/std/path/mod.ts"; +import { exists, existsSync } from "https://deno.land/std/fs/exists.ts"; + +/** + * Return `true` if the file apth is a git repository. + * + * Requires the `--allow-read` flag. + * + * @param filepath default is `Deno.cwd()`, the current working directory. + */ +export function isGitSync(filepath = Deno.cwd()): boolean { + let current = isAbsolute(filepath) ? filepath : join(Deno.cwd(), filepath); + let parent = join(current, ".."); + + for (; parent! !== current; parent = join(current, "..")) { + if (existsSync(join(current, ".git"))) { + return true; + } + current = parent!; + } + + return existsSync(join(current, ".git")); +} + +/** + * Return `true` if the file apth is a git repository synchronously. + * + * Requires the `--allow-read` flag. + * + * @param filepath default is `Deno.cwd()`, the current working directory. + */ +export async function isGit(filepath = Deno.cwd()): Promise { + let current = isAbsolute(filepath) ? filepath : join(Deno.cwd(), filepath); + let parent = join(current, ".."); + + for (; parent! !== current; parent = join(current, "..")) { + if (await exists(join(current, ".git"))) { + return true; + } + current = parent!; + } + + return exists(join(current, ".git")); +}