@simbo/git-exec 0.0.0-stage → 1.0.0
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 +15 -0
- package/LICENSE.md +20 -0
- package/README.md +123 -2
- package/dist/git-error.d.ts +46 -0
- package/dist/git-error.js +31 -0
- package/dist/git-error.js.map +1 -0
- package/dist/git-exec.types.d.ts +29 -0
- package/dist/git-exec.types.js +2 -0
- package/dist/git-exec.types.js.map +1 -0
- package/dist/git.d.ts +34 -0
- package/dist/git.js +109 -0
- package/dist/git.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/package.json +62 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @simbo/git-exec
|
|
2
|
+
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- dab18e8: Add `@simbo/git-exec` to run Git commands asynchronously without a
|
|
8
|
+
shell.
|
|
9
|
+
|
|
10
|
+
- `git()` returns the trimmed standard output, `gitBuffer()` the raw output
|
|
11
|
+
for binary data, and `gitSucceeds()` whether the command exited with code 0.
|
|
12
|
+
- The options are `cwd`, `env` (merged over `process.env`), `maxBuffer` (1 GiB
|
|
13
|
+
by default), and a `debug` callback, such as `core.debug` in GitHub Actions.
|
|
14
|
+
- Failures reject with a `GitError` that holds the arguments, the exit code,
|
|
15
|
+
and the standard error output.
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright © 2025 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,124 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Git Exec
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
📦 [**`@simbo/git-exec`**](https://npmjs.com/package/@simbo/git-exec)
|
|
4
|
+
|
|
5
|
+
Run Git commands asynchronously and get their output as text, buffer, or success
|
|
6
|
+
flag.
|
|
7
|
+
|
|
8
|
+
Built on `execFile` from `node:child_process`, without a shell, for GitHub
|
|
9
|
+
Actions, command line tools, and libraries.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- `git()` returns the standard output as trimmed text
|
|
14
|
+
|
|
15
|
+
- `gitBuffer()` returns the raw standard output, for binary data
|
|
16
|
+
|
|
17
|
+
- `gitSucceeds()` reports whether a command exited with code 0
|
|
18
|
+
|
|
19
|
+
- Failures reject with a `GitError` that holds the arguments, the exit code, and
|
|
20
|
+
the standard error output
|
|
21
|
+
|
|
22
|
+
- A `debug` callback receives the commands, for example `core.debug` from
|
|
23
|
+
`@actions/core`
|
|
24
|
+
|
|
25
|
+
- Zero dependencies
|
|
26
|
+
|
|
27
|
+
## Requirements
|
|
28
|
+
|
|
29
|
+
Requires Node.js `^24.15.0 || >=26.0.0` and Git on the `PATH`.
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
Install `@simbo/git-exec` from the npm registry:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npm i @simbo/git-exec
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
For a complete API reference, see the
|
|
42
|
+
[documentation](https://simbo.de/packages/modules/_simbo_git-exec/).
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { git, gitBuffer, GitError, gitSucceeds } from '@simbo/git-exec';
|
|
46
|
+
|
|
47
|
+
const root = await git(['rev-parse', '--show-toplevel'], {
|
|
48
|
+
cwd: '/path/to/repo',
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const blob = await gitBuffer(['cat-file', 'blob', 'HEAD:image.png'], {
|
|
52
|
+
cwd: root,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
if (await gitSucceeds(['cat-file', '-e', 'HEAD:README.md'], { cwd: root })) {
|
|
56
|
+
// README.md exists in HEAD
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
await git(['switch', 'missing-branch'], { cwd: root });
|
|
61
|
+
} catch (error) {
|
|
62
|
+
if (error instanceof GitError) {
|
|
63
|
+
console.error(error.exitCode, error.stderr);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Options
|
|
69
|
+
|
|
70
|
+
| Option | Description | Default |
|
|
71
|
+
| ----------- | --------------------------------------------------------------------------------- | --------------- |
|
|
72
|
+
| `cwd` | The working directory of the command. | `process.cwd()` |
|
|
73
|
+
| `env` | Variables merged over `process.env`; `undefined` removes a variable. | none |
|
|
74
|
+
| `maxBuffer` | The largest output in bytes on standard output or standard error. | 1 GiB |
|
|
75
|
+
| `debug` | A callback for the command before it runs and the reason a `gitSucceeds()` fails. | none |
|
|
76
|
+
|
|
77
|
+
### Errors
|
|
78
|
+
|
|
79
|
+
`git()` and `gitBuffer()` reject with a `GitError` if Git cannot be started,
|
|
80
|
+
exits with a non-zero code, is terminated, or exceeds `maxBuffer`. The message
|
|
81
|
+
contains the command, the reason, and the standard error output.
|
|
82
|
+
|
|
83
|
+
`gitSucceeds()` returns `false` only for a non-zero exit code. If Git cannot run
|
|
84
|
+
at all, it rejects, so that a missing Git or working directory is not mistaken
|
|
85
|
+
for a failed check.
|
|
86
|
+
|
|
87
|
+
In GitHub Actions, pass `core.debug` to see the commands in debug logs:
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import { debug } from '@actions/core';
|
|
91
|
+
import { git } from '@simbo/git-exec';
|
|
92
|
+
|
|
93
|
+
const head = await git(['rev-parse', 'HEAD'], { debug });
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Security
|
|
97
|
+
|
|
98
|
+
Git runs through `execFile` without a shell, so no shell interprets the
|
|
99
|
+
arguments. Git itself, however, runs commands that some options name, such as
|
|
100
|
+
`--upload-pack` for `clone`, `fetch`, and `ls-remote`, or `-c` with
|
|
101
|
+
`core.sshCommand`. `@simbo/git-exec` passes all arguments to Git as given and
|
|
102
|
+
does not filter them.
|
|
103
|
+
|
|
104
|
+
Never pass untrusted values, such as user input or data from a pull request, as
|
|
105
|
+
options. Place them where Git cannot read them as options:
|
|
106
|
+
|
|
107
|
+
- after `--end-of-options` for revisions, branches, tags, and remotes
|
|
108
|
+
- after `--` for paths, with the `:(literal)` prefix to turn off pathspec magic
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
// `ref` and `path` come from untrusted input
|
|
112
|
+
await git([
|
|
113
|
+
'log',
|
|
114
|
+
'--oneline',
|
|
115
|
+
'--end-of-options',
|
|
116
|
+
ref,
|
|
117
|
+
'--',
|
|
118
|
+
`:(literal)${path}`,
|
|
119
|
+
]);
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## License
|
|
123
|
+
|
|
124
|
+
[MIT © Simon Lepel](https://simbo.mit-license.org/2025/)
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Details of a failed Git command.
|
|
3
|
+
*/
|
|
4
|
+
export interface GitErrorDetails {
|
|
5
|
+
/**
|
|
6
|
+
* The Git arguments.
|
|
7
|
+
*/
|
|
8
|
+
args: readonly string[];
|
|
9
|
+
/**
|
|
10
|
+
* The exit code, or `undefined` if Git did not exit on its own, for example
|
|
11
|
+
* because it could not be started or was terminated by a signal.
|
|
12
|
+
*/
|
|
13
|
+
exitCode: number | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* The trimmed standard error output.
|
|
16
|
+
*/
|
|
17
|
+
stderr: string;
|
|
18
|
+
/**
|
|
19
|
+
* The original error.
|
|
20
|
+
*/
|
|
21
|
+
cause: unknown;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* An error thrown when a Git command fails.
|
|
25
|
+
*/
|
|
26
|
+
export declare class GitError extends Error {
|
|
27
|
+
/**
|
|
28
|
+
* The Git arguments.
|
|
29
|
+
*/
|
|
30
|
+
readonly args: readonly string[];
|
|
31
|
+
/**
|
|
32
|
+
* The exit code, or `undefined` if Git did not exit on its own.
|
|
33
|
+
*/
|
|
34
|
+
readonly exitCode: number | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* The trimmed standard error output.
|
|
37
|
+
*/
|
|
38
|
+
readonly stderr: string;
|
|
39
|
+
/**
|
|
40
|
+
* Creates an error for a failed Git command.
|
|
41
|
+
*
|
|
42
|
+
* @param message - The error message.
|
|
43
|
+
* @param details - The details of the failed command.
|
|
44
|
+
*/
|
|
45
|
+
constructor(message: string, details: GitErrorDetails);
|
|
46
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An error thrown when a Git command fails.
|
|
3
|
+
*/
|
|
4
|
+
export class GitError extends Error {
|
|
5
|
+
/**
|
|
6
|
+
* The Git arguments.
|
|
7
|
+
*/
|
|
8
|
+
args;
|
|
9
|
+
/**
|
|
10
|
+
* The exit code, or `undefined` if Git did not exit on its own.
|
|
11
|
+
*/
|
|
12
|
+
exitCode;
|
|
13
|
+
/**
|
|
14
|
+
* The trimmed standard error output.
|
|
15
|
+
*/
|
|
16
|
+
stderr;
|
|
17
|
+
/**
|
|
18
|
+
* Creates an error for a failed Git command.
|
|
19
|
+
*
|
|
20
|
+
* @param message - The error message.
|
|
21
|
+
* @param details - The details of the failed command.
|
|
22
|
+
*/
|
|
23
|
+
constructor(message, details) {
|
|
24
|
+
super(message, { cause: details.cause });
|
|
25
|
+
this.name = 'GitError';
|
|
26
|
+
this.args = details.args;
|
|
27
|
+
this.exitCode = details.exitCode;
|
|
28
|
+
this.stderr = details.stderr;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=git-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"git-error.js","sourceRoot":"","sources":["../src/git-error.ts"],"names":[],"mappings":"AA0BA;;GAEG;AACH,MAAM,OAAO,QAAS,SAAQ,KAAK;IACjC;;OAEG;IACa,IAAI,CAAoB;IAExC;;OAEG;IACa,QAAQ,CAAqB;IAE7C;;OAEG;IACa,MAAM,CAAS;IAE/B;;;;;OAKG;IACH,YAAmB,OAAe,EAAE,OAAwB;QAC1D,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC;QACzC,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC;QACvB,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QACzB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;QACjC,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAC/B,CAAC;CACF","sourcesContent":["/**\n * Details of a failed Git command.\n */\nexport interface GitErrorDetails {\n /**\n * The Git arguments.\n */\n args: readonly string[];\n\n /**\n * The exit code, or `undefined` if Git did not exit on its own, for example\n * because it could not be started or was terminated by a signal.\n */\n exitCode: number | undefined;\n\n /**\n * The trimmed standard error output.\n */\n stderr: string;\n\n /**\n * The original error.\n */\n cause: unknown;\n}\n\n/**\n * An error thrown when a Git command fails.\n */\nexport class GitError extends Error {\n /**\n * The Git arguments.\n */\n public readonly args: readonly string[];\n\n /**\n * The exit code, or `undefined` if Git did not exit on its own.\n */\n public readonly exitCode: number | undefined;\n\n /**\n * The trimmed standard error output.\n */\n public readonly stderr: string;\n\n /**\n * Creates an error for a failed Git command.\n *\n * @param message - The error message.\n * @param details - The details of the failed command.\n */\n public constructor(message: string, details: GitErrorDetails) {\n super(message, { cause: details.cause });\n this.name = 'GitError';\n this.args = details.args;\n this.exitCode = details.exitCode;\n this.stderr = details.stderr;\n }\n}\n"]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for running a Git command.
|
|
3
|
+
*/
|
|
4
|
+
export interface GitOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The working directory of the Git command.
|
|
7
|
+
*
|
|
8
|
+
* @default process.cwd()
|
|
9
|
+
*/
|
|
10
|
+
cwd?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Environment variables for the Git command, merged over `process.env`.
|
|
13
|
+
* A variable set to `undefined` is removed from the environment.
|
|
14
|
+
*/
|
|
15
|
+
env?: Record<string, string | undefined>;
|
|
16
|
+
/**
|
|
17
|
+
* The largest amount of data in bytes allowed on standard output or
|
|
18
|
+
* standard error. The command fails if it is exceeded.
|
|
19
|
+
*
|
|
20
|
+
* @default 1073741824 (1 GiB)
|
|
21
|
+
*/
|
|
22
|
+
maxBuffer?: number;
|
|
23
|
+
/**
|
|
24
|
+
* A callback for debug messages, such as `core.debug` in GitHub Actions.
|
|
25
|
+
* It receives the command before it runs, and the reason when
|
|
26
|
+
* `gitSucceeds()` reports a failure.
|
|
27
|
+
*/
|
|
28
|
+
debug?: (message: string) => void;
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"git-exec.types.js","sourceRoot":"","sources":["../src/git-exec.types.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Options for running a Git command.\n */\nexport interface GitOptions {\n /**\n * The working directory of the Git command.\n *\n * @default process.cwd()\n */\n cwd?: string;\n\n /**\n * Environment variables for the Git command, merged over `process.env`.\n * A variable set to `undefined` is removed from the environment.\n */\n env?: Record<string, string | undefined>;\n\n /**\n * The largest amount of data in bytes allowed on standard output or\n * standard error. The command fails if it is exceeded.\n *\n * @default 1073741824 (1 GiB)\n */\n maxBuffer?: number;\n\n /**\n * A callback for debug messages, such as `core.debug` in GitHub Actions.\n * It receives the command before it runs, and the reason when\n * `gitSucceeds()` reports a failure.\n */\n debug?: (message: string) => void;\n}\n"]}
|
package/dist/git.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { GitOptions } from './git-exec.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Runs a Git command and returns its raw standard output, for binary data.
|
|
4
|
+
*
|
|
5
|
+
* The arguments are passed to Git as given, without a shell. Pass untrusted
|
|
6
|
+
* values only after `--end-of-options` or `--`, never as options: some options,
|
|
7
|
+
* such as `--upload-pack`, make Git run commands.
|
|
8
|
+
*
|
|
9
|
+
* @param args - The Git arguments.
|
|
10
|
+
* @param options - Options for running the command.
|
|
11
|
+
* @returns The standard output as buffer.
|
|
12
|
+
* @throws {GitError} if Git cannot be started, exits with a non-zero code, or exceeds `maxBuffer`.
|
|
13
|
+
*/
|
|
14
|
+
export declare function gitBuffer(args: readonly string[], options?: GitOptions): Promise<Buffer>;
|
|
15
|
+
/**
|
|
16
|
+
* Runs a Git command and returns its standard output as trimmed text.
|
|
17
|
+
*
|
|
18
|
+
* @param args - The Git arguments.
|
|
19
|
+
* @param options - Options for running the command.
|
|
20
|
+
* @returns The standard output as UTF-8 text without leading and trailing whitespace.
|
|
21
|
+
* @throws {GitError} if Git cannot be started, exits with a non-zero code, or exceeds `maxBuffer`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function git(args: readonly string[], options?: GitOptions): Promise<string>;
|
|
24
|
+
/**
|
|
25
|
+
* Runs a Git command and reports whether it exited with code 0.
|
|
26
|
+
*
|
|
27
|
+
* Only a non-zero exit code counts as failure; it is passed to `debug`.
|
|
28
|
+
*
|
|
29
|
+
* @param args - The Git arguments.
|
|
30
|
+
* @param options - Options for running the command.
|
|
31
|
+
* @returns `true` if the command exited with code 0, `false` for any other exit code.
|
|
32
|
+
* @throws {GitError} if Git cannot be started, is terminated, or exceeds `maxBuffer`.
|
|
33
|
+
*/
|
|
34
|
+
export declare function gitSucceeds(args: readonly string[], options?: GitOptions): Promise<boolean>;
|
package/dist/git.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { cwd as processCwd, env as processEnv } from 'node:process';
|
|
3
|
+
import { promisify } from 'node:util';
|
|
4
|
+
import { GitError } from './git-error.js';
|
|
5
|
+
const execFileAsync = promisify(execFile);
|
|
6
|
+
/**
|
|
7
|
+
* The default for `maxBuffer`: 1 GiB, enough for large diffs and blobs.
|
|
8
|
+
*/
|
|
9
|
+
const DEFAULT_MAX_BUFFER = 1_073_741_824;
|
|
10
|
+
/**
|
|
11
|
+
* Runs a Git command and returns its raw standard output, for binary data.
|
|
12
|
+
*
|
|
13
|
+
* The arguments are passed to Git as given, without a shell. Pass untrusted
|
|
14
|
+
* values only after `--end-of-options` or `--`, never as options: some options,
|
|
15
|
+
* such as `--upload-pack`, make Git run commands.
|
|
16
|
+
*
|
|
17
|
+
* @param args - The Git arguments.
|
|
18
|
+
* @param options - Options for running the command.
|
|
19
|
+
* @returns The standard output as buffer.
|
|
20
|
+
* @throws {GitError} if Git cannot be started, exits with a non-zero code, or exceeds `maxBuffer`.
|
|
21
|
+
*/
|
|
22
|
+
export async function gitBuffer(args, options = {}) {
|
|
23
|
+
const { cwd = processCwd(), env, maxBuffer = DEFAULT_MAX_BUFFER, debug } = options;
|
|
24
|
+
debug?.(`Running ${formatCommand(args)} in ${cwd}`);
|
|
25
|
+
try {
|
|
26
|
+
const { stdout } = await execFileAsync('git', args, {
|
|
27
|
+
cwd,
|
|
28
|
+
env: env ? { ...processEnv, ...env } : processEnv,
|
|
29
|
+
encoding: 'buffer',
|
|
30
|
+
maxBuffer,
|
|
31
|
+
});
|
|
32
|
+
return stdout;
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
throw toGitError(args, error);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Runs a Git command and returns its standard output as trimmed text.
|
|
40
|
+
*
|
|
41
|
+
* @param args - The Git arguments.
|
|
42
|
+
* @param options - Options for running the command.
|
|
43
|
+
* @returns The standard output as UTF-8 text without leading and trailing whitespace.
|
|
44
|
+
* @throws {GitError} if Git cannot be started, exits with a non-zero code, or exceeds `maxBuffer`.
|
|
45
|
+
*/
|
|
46
|
+
export async function git(args, options = {}) {
|
|
47
|
+
return (await gitBuffer(args, options)).toString('utf8').trim();
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Runs a Git command and reports whether it exited with code 0.
|
|
51
|
+
*
|
|
52
|
+
* Only a non-zero exit code counts as failure; it is passed to `debug`.
|
|
53
|
+
*
|
|
54
|
+
* @param args - The Git arguments.
|
|
55
|
+
* @param options - Options for running the command.
|
|
56
|
+
* @returns `true` if the command exited with code 0, `false` for any other exit code.
|
|
57
|
+
* @throws {GitError} if Git cannot be started, is terminated, or exceeds `maxBuffer`.
|
|
58
|
+
*/
|
|
59
|
+
export async function gitSucceeds(args, options = {}) {
|
|
60
|
+
try {
|
|
61
|
+
await gitBuffer(args, options);
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
if (error instanceof GitError && error.exitCode !== undefined) {
|
|
66
|
+
options.debug?.(error.message);
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Formats a Git command for messages, quoting arguments that contain special characters.
|
|
74
|
+
*
|
|
75
|
+
* @param args - The Git arguments.
|
|
76
|
+
* @returns The command line.
|
|
77
|
+
*/
|
|
78
|
+
function formatCommand(args) {
|
|
79
|
+
return ['git', ...args].map(arg => (/^[\w%+,./:=@^~-]+$/.test(arg) ? arg : JSON.stringify(arg))).join(' ');
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Converts an error of `execFile` into a `GitError`.
|
|
83
|
+
*
|
|
84
|
+
* @param args - The Git arguments.
|
|
85
|
+
* @param error - The error of `execFile`.
|
|
86
|
+
* @returns The Git error.
|
|
87
|
+
*/
|
|
88
|
+
function toGitError(args, error) {
|
|
89
|
+
const { code, signal, stderr } = (error ?? {});
|
|
90
|
+
const exitCode = typeof code === 'number' ? code : undefined;
|
|
91
|
+
const stderrText = (Buffer.isBuffer(stderr) ? stderr.toString('utf8') : typeof stderr === 'string' ? stderr : '').trim();
|
|
92
|
+
let reason;
|
|
93
|
+
if (exitCode !== undefined) {
|
|
94
|
+
reason = `exit code ${exitCode}`;
|
|
95
|
+
}
|
|
96
|
+
else if (typeof code !== 'string' && typeof signal === 'string') {
|
|
97
|
+
reason = `terminated by ${signal}`;
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
reason = error instanceof Error ? error.message : String(error);
|
|
101
|
+
}
|
|
102
|
+
return new GitError(`${formatCommand(args)} failed (${reason})${stderrText ? `: ${stderrText}` : ''}`, {
|
|
103
|
+
args,
|
|
104
|
+
exitCode,
|
|
105
|
+
stderr: stderrText,
|
|
106
|
+
cause: error,
|
|
107
|
+
});
|
|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AC7D,MAAM,UAAU,GAAG,CACjB,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAC7F,CAAC,IAAI,EAAE,CAAC;IAET,IAAI,MAAc,CAAC;IACnB,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;QAC3B,MAAM,GAAG,aAAa,QAAQ,EAAE,CAAC;IACnC,CAAC;SAAM,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAClE,MAAM,GAAG,iBAAiB,MAAM,EAAE,CAAC;IACrC,CAAC;SAAM,CAAC;QACN,MAAM,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAClE,CAAC;IAED,OAAO,IAAI,QAAQ,CAAC,GAAG,aAAa,CAAC,IAAI,CAAC,YAAY,MAAM,IAAI,UAAU,CAAC,CAAC,CAAC,KAAK,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE;QACrG,IAAI;QACJ,QAAQ;QACR,MAAM,EAAE,UAAU;QAClB,KAAK,EAAE,KAAK;KACb,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { execFile } from 'node:child_process';\nimport { cwd as processCwd, env as processEnv } from 'node:process';\nimport { promisify } from 'node:util';\n\nimport { GitError } from './git-error.js';\nimport type { GitOptions } from './git-exec.types.js';\n\nconst execFileAsync = promisify(execFile);\n\n/**\n * The default for `maxBuffer`: 1 GiB, enough for large diffs and blobs.\n */\nconst DEFAULT_MAX_BUFFER = 1_073_741_824;\n\n/**\n * Runs a Git command and returns its raw standard output, for binary data.\n *\n * The arguments are passed to Git as given, without a shell. Pass untrusted\n * values only after `--end-of-options` or `--`, never as options: some options,\n * such as `--upload-pack`, make Git run commands.\n *\n * @param args - The Git arguments.\n * @param options - Options for running the command.\n * @returns The standard output as buffer.\n * @throws {GitError} if Git cannot be started, exits with a non-zero code, or exceeds `maxBuffer`.\n */\nexport async function gitBuffer(args: readonly string[], options: GitOptions = {}): Promise<Buffer> {\n const { cwd = processCwd(), env, maxBuffer = DEFAULT_MAX_BUFFER, debug } = options;\n\n debug?.(`Running ${formatCommand(args)} in ${cwd}`);\n\n try {\n const { stdout } = await execFileAsync('git', args, {\n cwd,\n env: env ? { ...processEnv, ...env } : processEnv,\n encoding: 'buffer',\n maxBuffer,\n });\n return stdout;\n } catch (error) {\n throw toGitError(args, error);\n }\n}\n\n/**\n * Runs a Git command and returns its standard output as trimmed text.\n *\n * @param args - The Git arguments.\n * @param options - Options for running the command.\n * @returns The standard output as UTF-8 text without leading and trailing whitespace.\n * @throws {GitError} if Git cannot be started, exits with a non-zero code, or exceeds `maxBuffer`.\n */\nexport async function git(args: readonly string[], options: GitOptions = {}): Promise<string> {\n return (await gitBuffer(args, options)).toString('utf8').trim();\n}\n\n/**\n * Runs a Git command and reports whether it exited with code 0.\n *\n * Only a non-zero exit code counts as failure; it is passed to `debug`.\n *\n * @param args - The Git arguments.\n * @param options - Options for running the command.\n * @returns `true` if the command exited with code 0, `false` for any other exit code.\n * @throws {GitError} if Git cannot be started, is terminated, or exceeds `maxBuffer`.\n */\nexport async function gitSucceeds(args: readonly string[], options: GitOptions = {}): Promise<boolean> {\n try {\n await gitBuffer(args, options);\n return true;\n } catch (error) {\n if (error instanceof GitError && error.exitCode !== undefined) {\n options.debug?.(error.message);\n return false;\n }\n throw error;\n }\n}\n\n/**\n * Formats a Git command for messages, quoting arguments that contain special characters.\n *\n * @param args - The Git arguments.\n * @returns The command line.\n */\nfunction formatCommand(args: readonly string[]): string {\n return ['git', ...args].map(arg => (/^[\\w%+,./:=@^~-]+$/.test(arg) ? arg : JSON.stringify(arg))).join(' ');\n}\n\n/**\n * Converts an error of `execFile` into a `GitError`.\n *\n * @param args - The Git arguments.\n * @param error - The error of `execFile`.\n * @returns The Git error.\n */\nfunction toGitError(args: readonly string[], error: unknown): GitError {\n const { code, signal, stderr } = (error ?? {}) as { code?: unknown; signal?: unknown; stderr?: unknown };\n\n const exitCode = typeof code === 'number' ? code : undefined;\n const stderrText = (\n Buffer.isBuffer(stderr) ? stderr.toString('utf8') : typeof stderr === 'string' ? stderr : ''\n ).trim();\n\n let reason: string;\n if (exitCode !== undefined) {\n reason = `exit code ${exitCode}`;\n } else if (typeof code !== 'string' && typeof signal === 'string') {\n reason = `terminated by ${signal}`;\n } else {\n reason = error instanceof Error ? error.message : String(error);\n }\n\n return new GitError(`${formatCommand(args)} failed (${reason})${stderrText ? `: ${stderrText}` : ''}`, {\n args,\n exitCode,\n stderr: stderrText,\n cause: error,\n });\n}\n"]}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,gBAAgB,CAAC","sourcesContent":["export * from './git.js';\nexport * from './git-error.js';\nexport type * from './git-exec.types.js';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,65 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simbo/git-exec",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"description": "Run Git commands asynchronously and get their output as text, buffer, or success flag.",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"git",
|
|
7
|
+
"exec",
|
|
8
|
+
"execfile",
|
|
9
|
+
"child-process",
|
|
10
|
+
"command",
|
|
11
|
+
"async",
|
|
12
|
+
"github-actions",
|
|
13
|
+
"cli",
|
|
14
|
+
"utility",
|
|
15
|
+
"typescript"
|
|
16
|
+
],
|
|
17
|
+
"author": "Simon Lepel <simbo@simbo.de> (https://simbo.de/)",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"homepage": "https://github.com/simbo/packages/tree/main/packages/git-exec",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/simbo/packages.git",
|
|
23
|
+
"directory": "packages/git-exec"
|
|
24
|
+
},
|
|
25
|
+
"bugs": "https://github.com/simbo/packages/issues",
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": "^24.15.0 || >=26.0.0"
|
|
28
|
+
},
|
|
29
|
+
"type": "module",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": "./dist/index.js"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"CHANGELOG.md",
|
|
35
|
+
"dist",
|
|
36
|
+
"LICENSE.md",
|
|
37
|
+
"README.md"
|
|
38
|
+
],
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@simbo/eslint-config": "5.0.0",
|
|
41
|
+
"@simbo/tsconfig": "3.0.1",
|
|
42
|
+
"@types/node": "^24.19.2",
|
|
43
|
+
"@vitest/coverage-v8": "^5.1.1",
|
|
44
|
+
"@vitest/ui": "^5.1.1",
|
|
45
|
+
"eslint": "^10.15.0",
|
|
46
|
+
"jiti": "^2.7.0",
|
|
47
|
+
"typescript": "^6.0.3",
|
|
48
|
+
"vitest": "^5.1.1"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsc --project tsconfig.build.json",
|
|
52
|
+
"check": "pnpm run check:eslint && pnpm run check:types",
|
|
53
|
+
"check:eslint": "eslint --config ./eslint.config.ts --max-warnings 0",
|
|
54
|
+
"check:types": "tsc --noEmit",
|
|
55
|
+
"clean": "pnpm run clean:dist && pnpm run clean:coverage && pnpm run clean:turbo && pnpm run clean:node_modules",
|
|
56
|
+
"clean:dist": "rm -rf dist",
|
|
57
|
+
"clean:node_modules": "rm -rf node_modules",
|
|
58
|
+
"clean:turbo": "rm -rf .turbo",
|
|
59
|
+
"clean:coverage": "rm -rf coverage",
|
|
60
|
+
"fix:eslint": "pnpm run check:eslint --fix",
|
|
61
|
+
"test": "vitest run --coverage",
|
|
62
|
+
"test:ui": "pnpm run test:watch --ui",
|
|
63
|
+
"test:watch": "vitest dev --coverage"
|
|
64
|
+
}
|
|
6
65
|
}
|