Skip to content

Commit f67cec2

Browse files
committed
release v0.1.0
1 parent e4022fd commit f67cec2

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
## 0.1.0 - [2020-08-24]
4+
5+
- first release

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# deno_is_git
2+
3+
[![tag](https://img.shields.io/github/release/justjavac/deno_is_git)](https://github.com/justjavac/deno_is_git/releases)
4+
[![Build Status](https://github.com/justjavac/deno_is_git/workflows/ci/badge.svg?branch=master)](https://github.com/justjavac/deno_is_git/actions)
5+
[![license](https://img.shields.io/github/license/justjavac/deno_is_git)](https://github.com/justjavac/deno_is_git/blob/master/LICENSE)
6+
7+
Whether the filepath is a **git repository**.
8+
9+
## Usage
10+
11+
```ts
12+
import { isGit, isGitSync } from "https://deno.land/x/is_git/mod.ts";
13+
14+
await isGit(); // true or false of Deno.cwd()
15+
await isGit('any/git/repo'); // true or false
16+
17+
isGitSync(); // true or false of Deno.cwd()
18+
isGitSync('any/git/repo'); // true or false
19+
```
20+
21+
## Example
22+
23+
```bash
24+
deno run --allow-read https://deno.land/x/is_git/example.ts
25+
```
26+
27+
## License
28+
29+
[deno_is_git](https://github.com/justjavac/deno_is_git) is released under the MIT License. See the bundled [LICENSE](./LICENSE) file for details.

example.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { isGit } from "./mod.ts";
2+
3+
console.log(`current directory is a git repository: ${await isGit()}`);

mod.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { join, isAbsolute } from "https://deno.land/std/path/mod.ts";
2+
import { exists, existsSync } from "https://deno.land/std/fs/exists.ts";
3+
4+
/**
5+
* Return `true` if the file apth is a git repository.
6+
*
7+
* Requires the `--allow-read` flag.
8+
*
9+
* @param filepath default is `Deno.cwd()`, the current working directory.
10+
*/
11+
export function isGitSync(filepath = Deno.cwd()): boolean {
12+
let current = isAbsolute(filepath) ? filepath : join(Deno.cwd(), filepath);
13+
let parent = join(current, "..");
14+
15+
for (; parent! !== current; parent = join(current, "..")) {
16+
if (existsSync(join(current, ".git"))) {
17+
return true;
18+
}
19+
current = parent!;
20+
}
21+
22+
return existsSync(join(current, ".git"));
23+
}
24+
25+
/**
26+
* Return `true` if the file apth is a git repository synchronously.
27+
*
28+
* Requires the `--allow-read` flag.
29+
*
30+
* @param filepath default is `Deno.cwd()`, the current working directory.
31+
*/
32+
export async function isGit(filepath = Deno.cwd()): Promise<boolean> {
33+
let current = isAbsolute(filepath) ? filepath : join(Deno.cwd(), filepath);
34+
let parent = join(current, "..");
35+
36+
for (; parent! !== current; parent = join(current, "..")) {
37+
if (await exists(join(current, ".git"))) {
38+
return true;
39+
}
40+
current = parent!;
41+
}
42+
43+
return exists(join(current, ".git"));
44+
}

0 commit comments

Comments
 (0)