Skip to content

Commit

Permalink
release v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
justjavac committed Aug 24, 2020
1 parent e4022fd commit f67cec2
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## 0.1.0 - [2020-08-24]

- first release
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { isGit } from "./mod.ts";

console.log(`current directory is a git repository: ${await isGit()}`);
44 changes: 44 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
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"));
}

0 comments on commit f67cec2

Please sign in to comment.