@simbo/git-repository 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/LICENSE.md +20 -0
- package/README.md +138 -2
- package/dist/find-git-repository-root.d.ts +13 -0
- package/dist/find-git-repository-root.js +29 -0
- package/dist/find-git-repository-root.js.map +1 -0
- package/dist/get-remote-url.d.ts +14 -0
- package/dist/get-remote-url.js +33 -0
- package/dist/get-remote-url.js.map +1 -0
- package/dist/git-failure.d.ts +33 -0
- package/dist/git-failure.js +53 -0
- package/dist/git-failure.js.map +1 -0
- package/dist/git-repository-error.d.ts +22 -0
- package/dist/git-repository-error.js +25 -0
- package/dist/git-repository-error.js.map +1 -0
- package/dist/git-repository.types.d.ts +31 -0
- package/dist/git-repository.types.js +2 -0
- package/dist/git-repository.types.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/is-git-repository-root.d.ts +13 -0
- package/dist/is-git-repository-root.js +24 -0
- package/dist/is-git-repository-root.js.map +1 -0
- package/dist/is-shallow-repository.d.ts +13 -0
- package/dist/is-shallow-repository.js +24 -0
- package/dist/is-shallow-repository.js.map +1 -0
- package/dist/resolve-commit.d.ts +16 -0
- package/dist/resolve-commit.js +38 -0
- package/dist/resolve-commit.js.map +1 -0
- package/package.json +73 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# @simbo/git-repository
|
|
2
|
+
|
|
3
|
+
## 1.0.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- ef863df: Publish from CI with npm trusted publishing.
|
|
8
|
+
|
|
9
|
+
- This release is the first one published by the release workflow, with
|
|
10
|
+
provenance, instead of by hand; the package itself is unchanged.
|
|
11
|
+
|
|
12
|
+
## 1.0.0
|
|
13
|
+
|
|
14
|
+
### Major Changes
|
|
15
|
+
|
|
16
|
+
- e79c771: Add package `@simbo/git-repository` to query a Git repository with
|
|
17
|
+
Git itself, including linked worktrees and submodules. It takes over
|
|
18
|
+
`@simbo/find-git-repository-root` and `@simbo/is-git-repository-root` with the
|
|
19
|
+
same behavior.
|
|
20
|
+
|
|
21
|
+
- `findGitRepositoryRoot()` finds the root of the working tree that contains a
|
|
22
|
+
directory, and `isGitRepositoryRoot()` checks whether a directory is such a
|
|
23
|
+
root.
|
|
24
|
+
- `resolveCommit()` resolves a ref to the full SHA of its commit, peels
|
|
25
|
+
annotated tags, and never reads a ref that starts with a dash as an option.
|
|
26
|
+
- `isShallowRepository()` checks whether a clone has an incomplete history.
|
|
27
|
+
- `getRemoteUrl()` reads the URL of a remote, by default `origin`.
|
|
28
|
+
- Throws a `GitRepositoryError` with a `reason` (`no-repository`,
|
|
29
|
+
`unknown-ref`, `missing-remote`) where the caller can act on the error, and
|
|
30
|
+
the `GitError` of `@simbo/git-exec` otherwise.
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright © 2026 Simon Lepel <simbo@simbo.de> (https://simbo.de)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,139 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Git Repository
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
📦
|
|
4
|
+
[**`@simbo/git-repository`**](https://npmjs.com/package/@simbo/git-repository)
|
|
5
|
+
|
|
6
|
+
Query a Git repository: find and check the **root of a working tree**, resolve
|
|
7
|
+
refs to commits, detect shallow clones and read remote URLs. Each query runs
|
|
8
|
+
Git, so the results match what Git itself sees, including linked worktrees and
|
|
9
|
+
submodules.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- `findGitRepositoryRoot()` finds the root of the working tree that contains a
|
|
14
|
+
directory, with symbolic links resolved
|
|
15
|
+
|
|
16
|
+
- `isGitRepositoryRoot()` checks whether a directory is the root of a working
|
|
17
|
+
tree
|
|
18
|
+
|
|
19
|
+
- `resolveCommit()` resolves a ref, such as a branch, a tag or `HEAD~1`, to the
|
|
20
|
+
full SHA of its commit
|
|
21
|
+
|
|
22
|
+
- `isShallowRepository()` checks whether a clone has an incomplete history
|
|
23
|
+
|
|
24
|
+
- `getRemoteUrl()` reads the URL of a remote, by default `origin`
|
|
25
|
+
|
|
26
|
+
- Errors the caller can act on are a `GitRepositoryError` with a `reason`
|
|
27
|
+
|
|
28
|
+
- Async and Promise-based API, fully typed with TypeScript
|
|
29
|
+
|
|
30
|
+
## Requirements
|
|
31
|
+
|
|
32
|
+
Requires Node.js `^24.15.0 || >=26.0.0` and Git on the `PATH`.
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
Install `@simbo/git-repository` from the npm registry:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npm i [-D] @simbo/git-repository
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
For a complete API reference, see the
|
|
45
|
+
[documentation](https://simbo.de/packages/modules/_simbo_git-repository/).
|
|
46
|
+
|
|
47
|
+
### Repository Root
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import {
|
|
51
|
+
findGitRepositoryRoot,
|
|
52
|
+
isGitRepositoryRoot,
|
|
53
|
+
} from '@simbo/git-repository';
|
|
54
|
+
|
|
55
|
+
// Default: start from the current working directory
|
|
56
|
+
const root = await findGitRepositoryRoot();
|
|
57
|
+
|
|
58
|
+
// Or specify a starting directory
|
|
59
|
+
const rootFromPath = await findGitRepositoryRoot('/path/to/subdir');
|
|
60
|
+
|
|
61
|
+
if (await isGitRepositoryRoot('/path/to/dir')) {
|
|
62
|
+
console.log('That directory is a Git repository root.');
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Both functions take a directory and default to `process.cwd()`. They never
|
|
67
|
+
reject for a directory that is not inside a working tree:
|
|
68
|
+
|
|
69
|
+
| Directory | `findGitRepositoryRoot()` | `isGitRepositoryRoot()` |
|
|
70
|
+
| ----------------------------------------------- | ------------------------- | ----------------------- |
|
|
71
|
+
| Root of a repository, worktree or submodule | the root | `true` |
|
|
72
|
+
| Symbolic link to such a root | the canonical root | `true` |
|
|
73
|
+
| Subdirectory of a working tree | the root | `false` |
|
|
74
|
+
| Bare repository or `.git` directory | `undefined` | `false` |
|
|
75
|
+
| Outside a repository, even with a `.git` folder | `undefined` | `false` |
|
|
76
|
+
| Missing path or file | `undefined` | `false` |
|
|
77
|
+
| Git cannot be started | rejects (`GitError`) | rejects (`GitError`) |
|
|
78
|
+
|
|
79
|
+
### Commits, Shallow Clones and Remotes
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import {
|
|
83
|
+
getRemoteUrl,
|
|
84
|
+
isShallowRepository,
|
|
85
|
+
resolveCommit,
|
|
86
|
+
} from '@simbo/git-repository';
|
|
87
|
+
|
|
88
|
+
const sha = await resolveCommit('v1.0.0', { cwd: '/path/to/repo' });
|
|
89
|
+
|
|
90
|
+
if (await isShallowRepository()) {
|
|
91
|
+
console.log('Fetch the full history first.');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const url = await getRemoteUrl({ remote: 'upstream' });
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
The options take `cwd`, a directory inside the repository, which defaults to
|
|
98
|
+
`process.cwd()`. `getRemoteUrl()` also takes `remote`, which defaults to
|
|
99
|
+
`origin`.
|
|
100
|
+
|
|
101
|
+
`resolveCommit()` resolves annotated tags to their commit and never reads a ref
|
|
102
|
+
that starts with a dash as an option, so the SHA is safe to pass on to other Git
|
|
103
|
+
commands. `getRemoteUrl()` never reads a remote name that starts with a dash as
|
|
104
|
+
an option, and applies `url.<base>.insteadOf` rewrites from the Git
|
|
105
|
+
configuration.
|
|
106
|
+
|
|
107
|
+
### Errors
|
|
108
|
+
|
|
109
|
+
`resolveCommit()`, `isShallowRepository()` and `getRemoteUrl()` reject with a
|
|
110
|
+
`GitRepositoryError` whose `reason` tells what went wrong. The failed Git
|
|
111
|
+
command is its `cause`.
|
|
112
|
+
|
|
113
|
+
| `reason` | Meaning |
|
|
114
|
+
| ---------------- | ----------------------------------------------------------------- |
|
|
115
|
+
| `no-repository` | The directory is missing or Git cannot open a repository from it. |
|
|
116
|
+
| `unknown-ref` | The ref does not exist or does not point to a commit. |
|
|
117
|
+
| `missing-remote` | The remote does not exist. |
|
|
118
|
+
|
|
119
|
+
Every function rejects with the `GitError` of
|
|
120
|
+
[`@simbo/git-exec`](https://npmjs.com/package/@simbo/git-exec) if Git cannot be
|
|
121
|
+
started, is terminated, or fails for another reason.
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
import { GitRepositoryError, resolveCommit } from '@simbo/git-repository';
|
|
125
|
+
|
|
126
|
+
try {
|
|
127
|
+
await resolveCommit('main');
|
|
128
|
+
} catch (error) {
|
|
129
|
+
if (error instanceof GitRepositoryError && error.reason === 'unknown-ref') {
|
|
130
|
+
console.error(error.message);
|
|
131
|
+
} else {
|
|
132
|
+
throw error;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## License
|
|
138
|
+
|
|
139
|
+
[MIT © Simon Lepel](https://simbo.mit-license.org/2026/)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finds the root directory of the Git working tree that contains the given directory.
|
|
3
|
+
*
|
|
4
|
+
* Asks Git for the top-level directory with `git rev-parse --show-toplevel`. Inside a linked worktree or a submodule,
|
|
5
|
+
* this is the root of the worktree or submodule. Git resolves symbolic links, so the path is canonical.
|
|
6
|
+
*
|
|
7
|
+
* @param workingDir - The directory from which to find the root. Defaults to the current working directory.
|
|
8
|
+
* @returns A promise that resolves to the absolute path of the root, or `undefined` if the directory is not inside a
|
|
9
|
+
* working tree, for example outside a repository, in a bare repository or in the `.git` directory, or if it does not
|
|
10
|
+
* exist or is not a directory.
|
|
11
|
+
* @throws {GitError} if Git cannot be started or is terminated.
|
|
12
|
+
*/
|
|
13
|
+
export declare function findGitRepositoryRoot(workingDir?: string): Promise<string | undefined>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { cwd } from 'node:process';
|
|
2
|
+
import { isDirectory } from '@simbo/accessible';
|
|
3
|
+
import { git } from '@simbo/git-exec';
|
|
4
|
+
import { assertGitExited } from './git-failure.js';
|
|
5
|
+
/**
|
|
6
|
+
* Finds the root directory of the Git working tree that contains the given directory.
|
|
7
|
+
*
|
|
8
|
+
* Asks Git for the top-level directory with `git rev-parse --show-toplevel`. Inside a linked worktree or a submodule,
|
|
9
|
+
* this is the root of the worktree or submodule. Git resolves symbolic links, so the path is canonical.
|
|
10
|
+
*
|
|
11
|
+
* @param workingDir - The directory from which to find the root. Defaults to the current working directory.
|
|
12
|
+
* @returns A promise that resolves to the absolute path of the root, or `undefined` if the directory is not inside a
|
|
13
|
+
* working tree, for example outside a repository, in a bare repository or in the `.git` directory, or if it does not
|
|
14
|
+
* exist or is not a directory.
|
|
15
|
+
* @throws {GitError} if Git cannot be started or is terminated.
|
|
16
|
+
*/
|
|
17
|
+
export async function findGitRepositoryRoot(workingDir = cwd()) {
|
|
18
|
+
if (!(await isDirectory(workingDir))) {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
return await git(['rev-parse', '--show-toplevel'], { cwd: workingDir });
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
assertGitExited(error);
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=find-git-repository-root.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"find-git-repository-root.js","sourceRoot":"","sources":["../src/find-git-repository-root.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAEnC,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAEtC,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAEnD;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,UAAU,GAAG,GAAG,EAAE;IAC5D,IAAI,CAAC,CAAC,MAAM,WAAW,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC;QACrC,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,IAAI,CAAC;QACH,OAAO,MAAM,GAAG,CAAC,CAAC,WAAW,EAAE,iBAAiB,CAAC,EAAE,EAAE,GAAG,EAAE,UAAU,EAAE,CAAC,CAAC;IAC1E,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,eAAe,CAAC,KAAK,CAAC,CAAC;QACvB,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC","sourcesContent":["import { cwd } from 'node:process';\n\nimport { isDirectory } from '@simbo/accessible';\nimport { git } from '@simbo/git-exec';\n\nimport { assertGitExited } from './git-failure.js';\n\n/**\n * Finds the root directory of the Git working tree that contains the given directory.\n *\n * Asks Git for the top-level directory with `git rev-parse --show-toplevel`. Inside a linked worktree or a submodule,\n * this is the root of the worktree or submodule. Git resolves symbolic links, so the path is canonical.\n *\n * @param workingDir - The directory from which to find the root. Defaults to the current working directory.\n * @returns A promise that resolves to the absolute path of the root, or `undefined` if the directory is not inside a\n * working tree, for example outside a repository, in a bare repository or in the `.git` directory, or if it does not\n * exist or is not a directory.\n * @throws {GitError} if Git cannot be started or is terminated.\n */\nexport async function findGitRepositoryRoot(workingDir = cwd()): Promise<string | undefined> {\n if (!(await isDirectory(workingDir))) {\n return undefined;\n }\n\n try {\n return await git(['rev-parse', '--show-toplevel'], { cwd: workingDir });\n } catch (error) {\n assertGitExited(error);\n return undefined;\n }\n}\n"]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { RemoteUrlOptions } from './git-repository.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Reads the URL of a Git remote.
|
|
4
|
+
*
|
|
5
|
+
* Runs `git remote get-url -- <remote>`, so that a remote name that starts with a dash is never read as an option.
|
|
6
|
+
* Git applies `url.<base>.insteadOf` rewrites from the Git configuration.
|
|
7
|
+
*
|
|
8
|
+
* @param options - The directory of the repository and the name of the remote.
|
|
9
|
+
* @returns A promise that resolves to the URL of the remote.
|
|
10
|
+
* @throws {GitRepositoryError} With the reason `missing-remote` if the remote does not exist, or `no-repository` if
|
|
11
|
+
* the directory is missing or Git cannot open a repository from it.
|
|
12
|
+
* @throws {GitError} if Git fails otherwise, cannot be started or is terminated.
|
|
13
|
+
*/
|
|
14
|
+
export declare function getRemoteUrl(options?: RemoteUrlOptions): Promise<string>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { cwd } from 'node:process';
|
|
2
|
+
import { git } from '@simbo/git-exec';
|
|
3
|
+
import { getExitCode, toRepositoryError } from './git-failure.js';
|
|
4
|
+
import { GitRepositoryError } from './git-repository-error.js';
|
|
5
|
+
/**
|
|
6
|
+
* The exit code of `git remote get-url` for a remote that does not exist.
|
|
7
|
+
*/
|
|
8
|
+
const MISSING_REMOTE_EXIT_CODE = 2;
|
|
9
|
+
/**
|
|
10
|
+
* Reads the URL of a Git remote.
|
|
11
|
+
*
|
|
12
|
+
* Runs `git remote get-url -- <remote>`, so that a remote name that starts with a dash is never read as an option.
|
|
13
|
+
* Git applies `url.<base>.insteadOf` rewrites from the Git configuration.
|
|
14
|
+
*
|
|
15
|
+
* @param options - The directory of the repository and the name of the remote.
|
|
16
|
+
* @returns A promise that resolves to the URL of the remote.
|
|
17
|
+
* @throws {GitRepositoryError} With the reason `missing-remote` if the remote does not exist, or `no-repository` if
|
|
18
|
+
* the directory is missing or Git cannot open a repository from it.
|
|
19
|
+
* @throws {GitError} if Git fails otherwise, cannot be started or is terminated.
|
|
20
|
+
*/
|
|
21
|
+
export async function getRemoteUrl(options = {}) {
|
|
22
|
+
const { cwd: directory = cwd(), remote = 'origin' } = options;
|
|
23
|
+
try {
|
|
24
|
+
return await git(['remote', 'get-url', '--', remote], { cwd: directory });
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
if (getExitCode(error) === MISSING_REMOTE_EXIT_CODE) {
|
|
28
|
+
throw new GitRepositoryError(`The Git remote "${remote}" does not exist.`, 'missing-remote', error);
|
|
29
|
+
}
|
|
30
|
+
throw await toRepositoryError(error, directory);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=get-remote-url.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-remote-url.js","sourceRoot":"","sources":["../src/get-remote-url.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAEnC,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAEtC,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAClE,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAG/D;;GAEG;AACH,MAAM,wBAAwB,GAAG,CAAC,CAAC;AAEnC;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,UAA4B,EAAE;IAC/D,MAAM,EAAE,GAAG,EAAE,SAAS,GAAG,GAAG,EAAE,EAAE,MAAM,GAAG,QAAQ,EAAE,GAAG,OAAO,CAAC;IAC9D,IAAI,CAAC;QACH,OAAO,MAAM,GAAG,CAAC,CAAC,QAAQ,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,CAAC,EAAE,EAAE,GAAG,EAAE,SAAS,EAAE,CAAC,CAAC;IAC5E,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,WAAW,CAAC,KAAK,CAAC,KAAK,wBAAwB,EAAE,CAAC;YACpD,MAAM,IAAI,kBAAkB,CAAC,mBAAmB,MAAM,mBAAmB,EAAE,gBAAgB,EAAE,KAAK,CAAC,CAAC;QACtG,CAAC;QACD,MAAM,MAAM,iBAAiB,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;IAClD,CAAC;AACH,CAAC","sourcesContent":["import { cwd } from 'node:process';\n\nimport { git } from '@simbo/git-exec';\n\nimport { getExitCode, toRepositoryError } from './git-failure.js';\nimport { GitRepositoryError } from './git-repository-error.js';\nimport type { RemoteUrlOptions } from './git-repository.types.js';\n\n/**\n * The exit code of `git remote get-url` for a remote that does not exist.\n */\nconst MISSING_REMOTE_EXIT_CODE = 2;\n\n/**\n * Reads the URL of a Git remote.\n *\n * Runs `git remote get-url -- <remote>`, so that a remote name that starts with a dash is never read as an option.\n * Git applies `url.<base>.insteadOf` rewrites from the Git configuration.\n *\n * @param options - The directory of the repository and the name of the remote.\n * @returns A promise that resolves to the URL of the remote.\n * @throws {GitRepositoryError} With the reason `missing-remote` if the remote does not exist, or `no-repository` if\n * the directory is missing or Git cannot open a repository from it.\n * @throws {GitError} if Git fails otherwise, cannot be started or is terminated.\n */\nexport async function getRemoteUrl(options: RemoteUrlOptions = {}): Promise<string> {\n const { cwd: directory = cwd(), remote = 'origin' } = options;\n try {\n return await git(['remote', 'get-url', '--', remote], { cwd: directory });\n } catch (error) {\n if (getExitCode(error) === MISSING_REMOTE_EXIT_CODE) {\n throw new GitRepositoryError(`The Git remote \"${remote}\" does not exist.`, 'missing-remote', error);\n }\n throw await toRepositoryError(error, directory);\n }\n}\n"]}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { GitError } from '@simbo/git-exec';
|
|
2
|
+
/**
|
|
3
|
+
* Asserts that Git ran and exited with an error code, and rethrows any other
|
|
4
|
+
* error, such as Git that cannot be started or was terminated.
|
|
5
|
+
*
|
|
6
|
+
* @param error - The error of a Git command.
|
|
7
|
+
* @throws {unknown} The error itself if Git did not exit on its own.
|
|
8
|
+
*/
|
|
9
|
+
export declare function assertGitExited(error: unknown): asserts error is GitError & {
|
|
10
|
+
exitCode: number;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Returns the exit code of a failed Git command.
|
|
14
|
+
*
|
|
15
|
+
* @param error - The error of a Git command.
|
|
16
|
+
* @returns The exit code, or `undefined` if the error is no `GitError` or Git did not exit on its own.
|
|
17
|
+
*/
|
|
18
|
+
export declare function getExitCode(error: unknown): number | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Turns the error of a Git command into a `GitRepositoryError` with the reason
|
|
21
|
+
* `no-repository` if the directory is missing or Git cannot open a repository
|
|
22
|
+
* from it, and returns other errors unchanged.
|
|
23
|
+
*
|
|
24
|
+
* A fatal error alone does not tell a missing repository apart from other
|
|
25
|
+
* fatal errors, such as a ref beyond the end of the reflog, so a second Git
|
|
26
|
+
* command checks the repository.
|
|
27
|
+
*
|
|
28
|
+
* @param error - The error of a Git command.
|
|
29
|
+
* @param directory - The directory the command ran in.
|
|
30
|
+
* @returns The error to throw.
|
|
31
|
+
* @throws {GitError} if Git cannot be started for the check.
|
|
32
|
+
*/
|
|
33
|
+
export declare function toRepositoryError(error: unknown, directory: string): Promise<unknown>;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { isDirectory } from '@simbo/accessible';
|
|
2
|
+
import { GitError, gitSucceeds } from '@simbo/git-exec';
|
|
3
|
+
import { GitRepositoryError } from './git-repository-error.js';
|
|
4
|
+
/**
|
|
5
|
+
* The exit code of Git for a fatal error, such as a directory outside a
|
|
6
|
+
* repository.
|
|
7
|
+
*/
|
|
8
|
+
const FATAL_EXIT_CODE = 128;
|
|
9
|
+
/**
|
|
10
|
+
* Asserts that Git ran and exited with an error code, and rethrows any other
|
|
11
|
+
* error, such as Git that cannot be started or was terminated.
|
|
12
|
+
*
|
|
13
|
+
* @param error - The error of a Git command.
|
|
14
|
+
* @throws {unknown} The error itself if Git did not exit on its own.
|
|
15
|
+
*/
|
|
16
|
+
export function assertGitExited(error) {
|
|
17
|
+
if (getExitCode(error) === undefined) {
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Returns the exit code of a failed Git command.
|
|
23
|
+
*
|
|
24
|
+
* @param error - The error of a Git command.
|
|
25
|
+
* @returns The exit code, or `undefined` if the error is no `GitError` or Git did not exit on its own.
|
|
26
|
+
*/
|
|
27
|
+
export function getExitCode(error) {
|
|
28
|
+
return error instanceof GitError ? error.exitCode : undefined;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Turns the error of a Git command into a `GitRepositoryError` with the reason
|
|
32
|
+
* `no-repository` if the directory is missing or Git cannot open a repository
|
|
33
|
+
* from it, and returns other errors unchanged.
|
|
34
|
+
*
|
|
35
|
+
* A fatal error alone does not tell a missing repository apart from other
|
|
36
|
+
* fatal errors, such as a ref beyond the end of the reflog, so a second Git
|
|
37
|
+
* command checks the repository.
|
|
38
|
+
*
|
|
39
|
+
* @param error - The error of a Git command.
|
|
40
|
+
* @param directory - The directory the command ran in.
|
|
41
|
+
* @returns The error to throw.
|
|
42
|
+
* @throws {GitError} if Git cannot be started for the check.
|
|
43
|
+
*/
|
|
44
|
+
export async function toRepositoryError(error, directory) {
|
|
45
|
+
const exitCode = getExitCode(error);
|
|
46
|
+
const noRepository = exitCode === undefined
|
|
47
|
+
? !(await isDirectory(directory))
|
|
48
|
+
: exitCode === FATAL_EXIT_CODE && !(await gitSucceeds(['rev-parse', '--git-dir'], { cwd: directory }));
|
|
49
|
+
return noRepository
|
|
50
|
+
? new GitRepositoryError(`Git cannot open a repository from "${directory}".`, 'no-repository', error)
|
|
51
|
+
: error;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=git-failure.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"git-failure.js","sourceRoot":"","sources":["../src/git-failure.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAExD,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAE/D;;;GAGG;AACH,MAAM,eAAe,GAAG,GAAG,CAAC;AAE5B;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAAC,KAAc;IAC5C,IAAI,WAAW,CAAC,KAAK,CAAC,KAAK,SAAS,EAAE,CAAC;QACrC,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAC,KAAc;IACxC,OAAO,KAAK,YAAY,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC;AAChE,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAC,KAAc,EAAE,SAAiB;IACvE,MAAM,QAAQ,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC;IACpC,MAAM,YAAY,GAChB,QAAQ,KAAK,SAAS;QACpB,CAAC,CAAC,CAAC,CAAC,MAAM,WAAW,CAAC,SAAS,CAAC,CAAC;QACjC,CAAC,CAAC,QAAQ,KAAK,eAAe,IAAI,CAAC,CAAC,MAAM,WAAW,CAAC,CAAC,WAAW,EAAE,WAAW,CAAC,EAAE,EAAE,GAAG,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;IAC3G,OAAO,YAAY;QACjB,CAAC,CAAC,IAAI,kBAAkB,CAAC,sCAAsC,SAAS,IAAI,EAAE,eAAe,EAAE,KAAK,CAAC;QACrG,CAAC,CAAC,KAAK,CAAC;AACZ,CAAC","sourcesContent":["import { isDirectory } from '@simbo/accessible';\nimport { GitError, gitSucceeds } from '@simbo/git-exec';\n\nimport { GitRepositoryError } from './git-repository-error.js';\n\n/**\n * The exit code of Git for a fatal error, such as a directory outside a\n * repository.\n */\nconst FATAL_EXIT_CODE = 128;\n\n/**\n * Asserts that Git ran and exited with an error code, and rethrows any other\n * error, such as Git that cannot be started or was terminated.\n *\n * @param error - The error of a Git command.\n * @throws {unknown} The error itself if Git did not exit on its own.\n */\nexport function assertGitExited(error: unknown): asserts error is GitError & { exitCode: number } {\n if (getExitCode(error) === undefined) {\n throw error;\n }\n}\n\n/**\n * Returns the exit code of a failed Git command.\n *\n * @param error - The error of a Git command.\n * @returns The exit code, or `undefined` if the error is no `GitError` or Git did not exit on its own.\n */\nexport function getExitCode(error: unknown): number | undefined {\n return error instanceof GitError ? error.exitCode : undefined;\n}\n\n/**\n * Turns the error of a Git command into a `GitRepositoryError` with the reason\n * `no-repository` if the directory is missing or Git cannot open a repository\n * from it, and returns other errors unchanged.\n *\n * A fatal error alone does not tell a missing repository apart from other\n * fatal errors, such as a ref beyond the end of the reflog, so a second Git\n * command checks the repository.\n *\n * @param error - The error of a Git command.\n * @param directory - The directory the command ran in.\n * @returns The error to throw.\n * @throws {GitError} if Git cannot be started for the check.\n */\nexport async function toRepositoryError(error: unknown, directory: string): Promise<unknown> {\n const exitCode = getExitCode(error);\n const noRepository =\n exitCode === undefined\n ? !(await isDirectory(directory))\n : exitCode === FATAL_EXIT_CODE && !(await gitSucceeds(['rev-parse', '--git-dir'], { cwd: directory }));\n return noRepository\n ? new GitRepositoryError(`Git cannot open a repository from \"${directory}\".`, 'no-repository', error)\n : error;\n}\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { GitRepositoryErrorReason } from './git-repository.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* An error thrown when a query on a Git repository fails for a reason the
|
|
4
|
+
* caller can act on.
|
|
5
|
+
*
|
|
6
|
+
* The failed Git command is the `cause`, a `GitError` of `@simbo/git-exec`.
|
|
7
|
+
*/
|
|
8
|
+
export declare class GitRepositoryError extends Error {
|
|
9
|
+
readonly name = "GitRepositoryError";
|
|
10
|
+
/**
|
|
11
|
+
* Why the query failed.
|
|
12
|
+
*/
|
|
13
|
+
readonly reason: GitRepositoryErrorReason;
|
|
14
|
+
/**
|
|
15
|
+
* Creates an error for a failed query.
|
|
16
|
+
*
|
|
17
|
+
* @param message - The error message.
|
|
18
|
+
* @param reason - Why the query failed.
|
|
19
|
+
* @param cause - The error of the Git command.
|
|
20
|
+
*/
|
|
21
|
+
constructor(message: string, reason: GitRepositoryErrorReason, cause: unknown);
|
|
22
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An error thrown when a query on a Git repository fails for a reason the
|
|
3
|
+
* caller can act on.
|
|
4
|
+
*
|
|
5
|
+
* The failed Git command is the `cause`, a `GitError` of `@simbo/git-exec`.
|
|
6
|
+
*/
|
|
7
|
+
export class GitRepositoryError extends Error {
|
|
8
|
+
name = 'GitRepositoryError';
|
|
9
|
+
/**
|
|
10
|
+
* Why the query failed.
|
|
11
|
+
*/
|
|
12
|
+
reason;
|
|
13
|
+
/**
|
|
14
|
+
* Creates an error for a failed query.
|
|
15
|
+
*
|
|
16
|
+
* @param message - The error message.
|
|
17
|
+
* @param reason - Why the query failed.
|
|
18
|
+
* @param cause - The error of the Git command.
|
|
19
|
+
*/
|
|
20
|
+
constructor(message, reason, cause) {
|
|
21
|
+
super(message, { cause });
|
|
22
|
+
this.reason = reason;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=git-repository-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"git-repository-error.js","sourceRoot":"","sources":["../src/git-repository-error.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAClB,IAAI,GAAG,oBAAoB,CAAC;IAErD;;OAEG;IACa,MAAM,CAA2B;IAEjD;;;;;;OAMG;IACH,YAAmB,OAAe,EAAE,MAAgC,EAAE,KAAc;QAClF,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QAC1B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF","sourcesContent":["import type { GitRepositoryErrorReason } from './git-repository.types.js';\n\n/**\n * An error thrown when a query on a Git repository fails for a reason the\n * caller can act on.\n *\n * The failed Git command is the `cause`, a `GitError` of `@simbo/git-exec`.\n */\nexport class GitRepositoryError extends Error {\n public override readonly name = 'GitRepositoryError';\n\n /**\n * Why the query failed.\n */\n public readonly reason: GitRepositoryErrorReason;\n\n /**\n * Creates an error for a failed query.\n *\n * @param message - The error message.\n * @param reason - Why the query failed.\n * @param cause - The error of the Git command.\n */\n public constructor(message: string, reason: GitRepositoryErrorReason, cause: unknown) {\n super(message, { cause });\n this.reason = reason;\n }\n}\n"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for a query on a Git repository.
|
|
3
|
+
*/
|
|
4
|
+
export interface GitRepositoryOptions {
|
|
5
|
+
/**
|
|
6
|
+
* A directory inside the Git repository.
|
|
7
|
+
*
|
|
8
|
+
* @default process.cwd()
|
|
9
|
+
*/
|
|
10
|
+
cwd?: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The options of `getRemoteUrl()`.
|
|
14
|
+
*/
|
|
15
|
+
export interface RemoteUrlOptions extends GitRepositoryOptions {
|
|
16
|
+
/**
|
|
17
|
+
* The name of the remote.
|
|
18
|
+
*
|
|
19
|
+
* @default 'origin'
|
|
20
|
+
*/
|
|
21
|
+
remote?: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Why a query on a Git repository failed.
|
|
25
|
+
*
|
|
26
|
+
* - `no-repository`: the directory is missing or Git cannot open a repository
|
|
27
|
+
* from it, for example because the directory is outside a repository
|
|
28
|
+
* - `unknown-ref`: the ref does not exist or does not point to a commit
|
|
29
|
+
* - `missing-remote`: the remote does not exist
|
|
30
|
+
*/
|
|
31
|
+
export type GitRepositoryErrorReason = 'no-repository' | 'unknown-ref' | 'missing-remote';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"git-repository.types.js","sourceRoot":"","sources":["../src/git-repository.types.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Options for a query on a Git repository.\n */\nexport interface GitRepositoryOptions {\n /**\n * A directory inside the Git repository.\n *\n * @default process.cwd()\n */\n cwd?: string;\n}\n\n/**\n * The options of `getRemoteUrl()`.\n */\nexport interface RemoteUrlOptions extends GitRepositoryOptions {\n /**\n * The name of the remote.\n *\n * @default 'origin'\n */\n remote?: string;\n}\n\n/**\n * Why a query on a Git repository failed.\n *\n * - `no-repository`: the directory is missing or Git cannot open a repository\n * from it, for example because the directory is outside a repository\n * - `unknown-ref`: the ref does not exist or does not point to a commit\n * - `missing-remote`: the remote does not exist\n */\nexport type GitRepositoryErrorReason = 'no-repository' | 'unknown-ref' | 'missing-remote';\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { findGitRepositoryRoot } from './find-git-repository-root.js';
|
|
2
|
+
export { getRemoteUrl } from './get-remote-url.js';
|
|
3
|
+
export { GitRepositoryError } from './git-repository-error.js';
|
|
4
|
+
export type * from './git-repository.types.js';
|
|
5
|
+
export { isGitRepositoryRoot } from './is-git-repository-root.js';
|
|
6
|
+
export { isShallowRepository } from './is-shallow-repository.js';
|
|
7
|
+
export { resolveCommit } from './resolve-commit.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { findGitRepositoryRoot } from './find-git-repository-root.js';
|
|
2
|
+
export { getRemoteUrl } from './get-remote-url.js';
|
|
3
|
+
export { GitRepositoryError } from './git-repository-error.js';
|
|
4
|
+
export { isGitRepositoryRoot } from './is-git-repository-root.js';
|
|
5
|
+
export { isShallowRepository } from './is-shallow-repository.js';
|
|
6
|
+
export { resolveCommit } from './resolve-commit.js';
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAE/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC","sourcesContent":["export { findGitRepositoryRoot } from './find-git-repository-root.js';\nexport { getRemoteUrl } from './get-remote-url.js';\nexport { GitRepositoryError } from './git-repository-error.js';\nexport type * from './git-repository.types.js';\nexport { isGitRepositoryRoot } from './is-git-repository-root.js';\nexport { isShallowRepository } from './is-shallow-repository.js';\nexport { resolveCommit } from './resolve-commit.js';\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the given directory is the root of a Git working tree.
|
|
3
|
+
*
|
|
4
|
+
* Finds the root with `findGitRepositoryRoot()` and compares it with the given directory after resolving symbolic
|
|
5
|
+
* links in both paths. The roots of linked worktrees and submodules count as roots; bare repositories, the `.git`
|
|
6
|
+
* directory and directories outside a repository do not.
|
|
7
|
+
*
|
|
8
|
+
* @param workingDir - The directory to check. Defaults to the current working directory.
|
|
9
|
+
* @returns A promise that resolves to `true` if the directory is the root of a Git working tree, `false` otherwise,
|
|
10
|
+
* also if it does not exist or is not a directory.
|
|
11
|
+
* @throws {GitError} if Git cannot be started or is terminated.
|
|
12
|
+
*/
|
|
13
|
+
export declare function isGitRepositoryRoot(workingDir?: string): Promise<boolean>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { realpath } from 'node:fs/promises';
|
|
2
|
+
import { cwd } from 'node:process';
|
|
3
|
+
import { findGitRepositoryRoot } from './find-git-repository-root.js';
|
|
4
|
+
/**
|
|
5
|
+
* Checks if the given directory is the root of a Git working tree.
|
|
6
|
+
*
|
|
7
|
+
* Finds the root with `findGitRepositoryRoot()` and compares it with the given directory after resolving symbolic
|
|
8
|
+
* links in both paths. The roots of linked worktrees and submodules count as roots; bare repositories, the `.git`
|
|
9
|
+
* directory and directories outside a repository do not.
|
|
10
|
+
*
|
|
11
|
+
* @param workingDir - The directory to check. Defaults to the current working directory.
|
|
12
|
+
* @returns A promise that resolves to `true` if the directory is the root of a Git working tree, `false` otherwise,
|
|
13
|
+
* also if it does not exist or is not a directory.
|
|
14
|
+
* @throws {GitError} if Git cannot be started or is terminated.
|
|
15
|
+
*/
|
|
16
|
+
export async function isGitRepositoryRoot(workingDir = cwd()) {
|
|
17
|
+
const root = await findGitRepositoryRoot(workingDir);
|
|
18
|
+
if (root === undefined) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
const [rootPath, directoryPath] = await Promise.all([realpath(root), realpath(workingDir)]);
|
|
22
|
+
return rootPath === directoryPath;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=is-git-repository-root.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-git-repository-root.js","sourceRoot":"","sources":["../src/is-git-repository-root.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAEnC,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AAEtE;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAC,UAAU,GAAG,GAAG,EAAE;IAC1D,MAAM,IAAI,GAAG,MAAM,qBAAqB,CAAC,UAAU,CAAC,CAAC;IACrD,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;QACvB,OAAO,KAAK,CAAC;IACf,CAAC;IACD,MAAM,CAAC,QAAQ,EAAE,aAAa,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;IAC5F,OAAO,QAAQ,KAAK,aAAa,CAAC;AACpC,CAAC","sourcesContent":["import { realpath } from 'node:fs/promises';\nimport { cwd } from 'node:process';\n\nimport { findGitRepositoryRoot } from './find-git-repository-root.js';\n\n/**\n * Checks if the given directory is the root of a Git working tree.\n *\n * Finds the root with `findGitRepositoryRoot()` and compares it with the given directory after resolving symbolic\n * links in both paths. The roots of linked worktrees and submodules count as roots; bare repositories, the `.git`\n * directory and directories outside a repository do not.\n *\n * @param workingDir - The directory to check. Defaults to the current working directory.\n * @returns A promise that resolves to `true` if the directory is the root of a Git working tree, `false` otherwise,\n * also if it does not exist or is not a directory.\n * @throws {GitError} if Git cannot be started or is terminated.\n */\nexport async function isGitRepositoryRoot(workingDir = cwd()): Promise<boolean> {\n const root = await findGitRepositoryRoot(workingDir);\n if (root === undefined) {\n return false;\n }\n const [rootPath, directoryPath] = await Promise.all([realpath(root), realpath(workingDir)]);\n return rootPath === directoryPath;\n}\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { GitRepositoryOptions } from './git-repository.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Checks if a Git repository is a shallow clone, whose history is incomplete.
|
|
4
|
+
*
|
|
5
|
+
* Runs `git rev-parse --is-shallow-repository`.
|
|
6
|
+
*
|
|
7
|
+
* @param options - The directory of the repository.
|
|
8
|
+
* @returns A promise that resolves to `true` if the repository is shallow, `false` otherwise.
|
|
9
|
+
* @throws {GitRepositoryError} With the reason `no-repository` if the directory is missing or Git cannot
|
|
10
|
+
* open a repository from it.
|
|
11
|
+
* @throws {GitError} if Git fails otherwise, cannot be started or is terminated.
|
|
12
|
+
*/
|
|
13
|
+
export declare function isShallowRepository(options?: GitRepositoryOptions): Promise<boolean>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { cwd } from 'node:process';
|
|
2
|
+
import { git } from '@simbo/git-exec';
|
|
3
|
+
import { toRepositoryError } from './git-failure.js';
|
|
4
|
+
/**
|
|
5
|
+
* Checks if a Git repository is a shallow clone, whose history is incomplete.
|
|
6
|
+
*
|
|
7
|
+
* Runs `git rev-parse --is-shallow-repository`.
|
|
8
|
+
*
|
|
9
|
+
* @param options - The directory of the repository.
|
|
10
|
+
* @returns A promise that resolves to `true` if the repository is shallow, `false` otherwise.
|
|
11
|
+
* @throws {GitRepositoryError} With the reason `no-repository` if the directory is missing or Git cannot
|
|
12
|
+
* open a repository from it.
|
|
13
|
+
* @throws {GitError} if Git fails otherwise, cannot be started or is terminated.
|
|
14
|
+
*/
|
|
15
|
+
export async function isShallowRepository(options = {}) {
|
|
16
|
+
const { cwd: directory = cwd() } = options;
|
|
17
|
+
try {
|
|
18
|
+
return (await git(['rev-parse', '--is-shallow-repository'], { cwd: directory })) === 'true';
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
throw await toRepositoryError(error, directory);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=is-shallow-repository.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-shallow-repository.js","sourceRoot":"","sources":["../src/is-shallow-repository.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAEnC,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAEtC,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAGrD;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAC,UAAgC,EAAE;IAC1E,MAAM,EAAE,GAAG,EAAE,SAAS,GAAG,GAAG,EAAE,EAAE,GAAG,OAAO,CAAC;IAC3C,IAAI,CAAC;QACH,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,WAAW,EAAE,yBAAyB,CAAC,EAAE,EAAE,GAAG,EAAE,SAAS,EAAE,CAAC,CAAC,KAAK,MAAM,CAAC;IAC9F,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,MAAM,iBAAiB,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;IAClD,CAAC;AACH,CAAC","sourcesContent":["import { cwd } from 'node:process';\n\nimport { git } from '@simbo/git-exec';\n\nimport { toRepositoryError } from './git-failure.js';\nimport type { GitRepositoryOptions } from './git-repository.types.js';\n\n/**\n * Checks if a Git repository is a shallow clone, whose history is incomplete.\n *\n * Runs `git rev-parse --is-shallow-repository`.\n *\n * @param options - The directory of the repository.\n * @returns A promise that resolves to `true` if the repository is shallow, `false` otherwise.\n * @throws {GitRepositoryError} With the reason `no-repository` if the directory is missing or Git cannot\n * open a repository from it.\n * @throws {GitError} if Git fails otherwise, cannot be started or is terminated.\n */\nexport async function isShallowRepository(options: GitRepositoryOptions = {}): Promise<boolean> {\n const { cwd: directory = cwd() } = options;\n try {\n return (await git(['rev-parse', '--is-shallow-repository'], { cwd: directory })) === 'true';\n } catch (error) {\n throw await toRepositoryError(error, directory);\n }\n}\n"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { GitRepositoryOptions } from './git-repository.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Resolves a Git ref to the commit it points to.
|
|
4
|
+
*
|
|
5
|
+
* Runs `git rev-parse --verify --quiet --end-of-options <ref>^{commit}`, so that an annotated tag resolves to its
|
|
6
|
+
* commit and a ref that starts with a dash is never read as an option. The resolved SHA is safe to pass on to other
|
|
7
|
+
* Git commands.
|
|
8
|
+
*
|
|
9
|
+
* @param ref - The ref, such as a branch, a tag, `HEAD~1` or a SHA.
|
|
10
|
+
* @param options - The directory of the repository.
|
|
11
|
+
* @returns A promise that resolves to the full SHA of the commit.
|
|
12
|
+
* @throws {GitRepositoryError} With the reason `unknown-ref` if the ref does not exist or does not point to a commit,
|
|
13
|
+
* or `no-repository` if the directory is missing or Git cannot open a repository from it.
|
|
14
|
+
* @throws {GitError} if Git cannot be started or is terminated.
|
|
15
|
+
*/
|
|
16
|
+
export declare function resolveCommit(ref: string, options?: GitRepositoryOptions): Promise<string>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { cwd } from 'node:process';
|
|
2
|
+
import { git } from '@simbo/git-exec';
|
|
3
|
+
import { getExitCode, toRepositoryError } from './git-failure.js';
|
|
4
|
+
import { GitRepositoryError } from './git-repository-error.js';
|
|
5
|
+
/**
|
|
6
|
+
* The suffix that resolves a ref to the commit it points to.
|
|
7
|
+
*/
|
|
8
|
+
const PEEL_TO_COMMIT = '^{commit}';
|
|
9
|
+
/**
|
|
10
|
+
* Resolves a Git ref to the commit it points to.
|
|
11
|
+
*
|
|
12
|
+
* Runs `git rev-parse --verify --quiet --end-of-options <ref>^{commit}`, so that an annotated tag resolves to its
|
|
13
|
+
* commit and a ref that starts with a dash is never read as an option. The resolved SHA is safe to pass on to other
|
|
14
|
+
* Git commands.
|
|
15
|
+
*
|
|
16
|
+
* @param ref - The ref, such as a branch, a tag, `HEAD~1` or a SHA.
|
|
17
|
+
* @param options - The directory of the repository.
|
|
18
|
+
* @returns A promise that resolves to the full SHA of the commit.
|
|
19
|
+
* @throws {GitRepositoryError} With the reason `unknown-ref` if the ref does not exist or does not point to a commit,
|
|
20
|
+
* or `no-repository` if the directory is missing or Git cannot open a repository from it.
|
|
21
|
+
* @throws {GitError} if Git cannot be started or is terminated.
|
|
22
|
+
*/
|
|
23
|
+
export async function resolveCommit(ref, options = {}) {
|
|
24
|
+
const { cwd: directory = cwd() } = options;
|
|
25
|
+
try {
|
|
26
|
+
return await git(['rev-parse', '--verify', '--quiet', '--end-of-options', `${ref}${PEEL_TO_COMMIT}`], {
|
|
27
|
+
cwd: directory,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
const repositoryError = await toRepositoryError(error, directory);
|
|
32
|
+
// In a repository, `rev-parse --verify` fails only for the ref, with code 1 or with a fatal error.
|
|
33
|
+
throw repositoryError === error && getExitCode(error) !== undefined
|
|
34
|
+
? new GitRepositoryError(`Cannot resolve the Git ref "${ref}" to a commit.`, 'unknown-ref', error)
|
|
35
|
+
: repositoryError;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=resolve-commit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve-commit.js","sourceRoot":"","sources":["../src/resolve-commit.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAEnC,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAEtC,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAClE,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAG/D;;GAEG;AACH,MAAM,cAAc,GAAG,WAAW,CAAC;AAEnC;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,GAAW,EAAE,UAAgC,EAAE;IACjF,MAAM,EAAE,GAAG,EAAE,SAAS,GAAG,GAAG,EAAE,EAAE,GAAG,OAAO,CAAC;IAC3C,IAAI,CAAC;QACH,OAAO,MAAM,GAAG,CAAC,CAAC,WAAW,EAAE,UAAU,EAAE,SAAS,EAAE,kBAAkB,EAAE,GAAG,GAAG,GAAG,cAAc,EAAE,CAAC,EAAE;YACpG,GAAG,EAAE,SAAS;SACf,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,eAAe,GAAG,MAAM,iBAAiB,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;QAClE,mGAAmG;QACnG,MAAM,eAAe,KAAK,KAAK,IAAI,WAAW,CAAC,KAAK,CAAC,KAAK,SAAS;YACjE,CAAC,CAAC,IAAI,kBAAkB,CAAC,+BAA+B,GAAG,gBAAgB,EAAE,aAAa,EAAE,KAAK,CAAC;YAClG,CAAC,CAAC,eAAe,CAAC;IACtB,CAAC;AACH,CAAC","sourcesContent":["import { cwd } from 'node:process';\n\nimport { git } from '@simbo/git-exec';\n\nimport { getExitCode, toRepositoryError } from './git-failure.js';\nimport { GitRepositoryError } from './git-repository-error.js';\nimport type { GitRepositoryOptions } from './git-repository.types.js';\n\n/**\n * The suffix that resolves a ref to the commit it points to.\n */\nconst PEEL_TO_COMMIT = '^{commit}';\n\n/**\n * Resolves a Git ref to the commit it points to.\n *\n * Runs `git rev-parse --verify --quiet --end-of-options <ref>^{commit}`, so that an annotated tag resolves to its\n * commit and a ref that starts with a dash is never read as an option. The resolved SHA is safe to pass on to other\n * Git commands.\n *\n * @param ref - The ref, such as a branch, a tag, `HEAD~1` or a SHA.\n * @param options - The directory of the repository.\n * @returns A promise that resolves to the full SHA of the commit.\n * @throws {GitRepositoryError} With the reason `unknown-ref` if the ref does not exist or does not point to a commit,\n * or `no-repository` if the directory is missing or Git cannot open a repository from it.\n * @throws {GitError} if Git cannot be started or is terminated.\n */\nexport async function resolveCommit(ref: string, options: GitRepositoryOptions = {}): Promise<string> {\n const { cwd: directory = cwd() } = options;\n try {\n return await git(['rev-parse', '--verify', '--quiet', '--end-of-options', `${ref}${PEEL_TO_COMMIT}`], {\n cwd: directory,\n });\n } catch (error) {\n const repositoryError = await toRepositoryError(error, directory);\n // In a repository, `rev-parse --verify` fails only for the ref, with code 1 or with a fatal error.\n throw repositoryError === error && getExitCode(error) !== undefined\n ? new GitRepositoryError(`Cannot resolve the Git ref \"${ref}\" to a commit.`, 'unknown-ref', error)\n : repositoryError;\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,76 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simbo/git-repository",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"description": "Query a Git repository: find and check the root of a working tree, resolve refs to commits, detect shallow clones and read remote URLs.",
|
|
4
|
+
"version": "1.0.1",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"git",
|
|
7
|
+
"repository",
|
|
8
|
+
"root",
|
|
9
|
+
"worktree",
|
|
10
|
+
"submodule",
|
|
11
|
+
"commit",
|
|
12
|
+
"ref",
|
|
13
|
+
"rev-parse",
|
|
14
|
+
"shallow",
|
|
15
|
+
"remote",
|
|
16
|
+
"utility",
|
|
17
|
+
"typescript"
|
|
18
|
+
],
|
|
19
|
+
"author": "Simon Lepel <simbo@simbo.de> (https://simbo.de/)",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"homepage": "https://github.com/simbo/packages/tree/main/packages/git-repository",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/simbo/packages.git",
|
|
25
|
+
"directory": "packages/git-repository"
|
|
26
|
+
},
|
|
27
|
+
"bugs": "https://github.com/simbo/packages/issues",
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": "^24.15.0 || >=26.0.0"
|
|
30
|
+
},
|
|
31
|
+
"type": "module",
|
|
32
|
+
"sideEffects": false,
|
|
33
|
+
"exports": {
|
|
34
|
+
".": "./dist/index.js"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"CHANGELOG.md",
|
|
38
|
+
"dist",
|
|
39
|
+
"LICENSE.md",
|
|
40
|
+
"README.md"
|
|
41
|
+
],
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@simbo/accessible": "3.0.1",
|
|
44
|
+
"@simbo/git-exec": "1.1.1"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@arethetypeswrong/cli": "^0.18.5",
|
|
48
|
+
"@simbo/eslint-config": "7.0.1",
|
|
49
|
+
"@simbo/git-fixture": "1.0.5",
|
|
50
|
+
"@simbo/tsconfig": "5.0.1",
|
|
51
|
+
"@types/node": "^24.19.4",
|
|
52
|
+
"@vitest/coverage-v8": "^5.1.2",
|
|
53
|
+
"@vitest/ui": "^5.1.2",
|
|
54
|
+
"eslint": "^10.16.0",
|
|
55
|
+
"jiti": "^2.7.0",
|
|
56
|
+
"publint": "^0.3.25",
|
|
57
|
+
"typescript": "^6.0.3",
|
|
58
|
+
"vitest": "^5.1.2"
|
|
59
|
+
},
|
|
60
|
+
"scripts": {
|
|
61
|
+
"build": "tsc --project tsconfig.build.json",
|
|
62
|
+
"check": "pnpm run check:eslint && pnpm run check:types",
|
|
63
|
+
"check:eslint": "eslint --config ./eslint.config.ts --max-warnings 0",
|
|
64
|
+
"check:pack": "pnpm pack --out node_modules/.cache/check-pack/package.tgz && publint node_modules/.cache/check-pack/package.tgz --strict && attw node_modules/.cache/check-pack/package.tgz --profile esm-only",
|
|
65
|
+
"check:types": "tsc --noEmit",
|
|
66
|
+
"clean": "pnpm run clean:dist && pnpm run clean:coverage && pnpm run clean:turbo && pnpm run clean:node_modules",
|
|
67
|
+
"clean:dist": "rm -rf dist",
|
|
68
|
+
"clean:node_modules": "rm -rf node_modules",
|
|
69
|
+
"clean:turbo": "rm -rf .turbo",
|
|
70
|
+
"clean:coverage": "rm -rf coverage",
|
|
71
|
+
"fix:eslint": "pnpm run check:eslint --fix",
|
|
72
|
+
"test": "vitest run --coverage",
|
|
73
|
+
"test:ui": "pnpm run test:watch --ui",
|
|
74
|
+
"test:watch": "vitest dev --coverage"
|
|
75
|
+
}
|
|
6
76
|
}
|