-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Changelog | ||
|
||
## 0.1.0 - [2020-08-24] | ||
|
||
- first release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} |