| 1 | import { spawn } from 'node:child_process'; |
| 2 | import { once } |
| 3 | import type { Readable |
| 4 | import { StringDecoder } |
| 5 | |
| 6 | import { GitCommandFailedError } |
| 7 | |
| 8 | export interface RunGitOptions |
| 9 | args: string[] |
| 10 | gitDir: string; |
| 11 | input?: Buffer |
| 12 | /** Merged over the inherited environment, after `GIT_DIR`. */ |
| 13 | env?: Record |
| 14 | } |
| 15 | |
| 16 | export async function runGit |
| 17 | return (await runGitBuffer |
| 18 | } |
| 19 | |
| 20 | function spawnGit({ args |
| 21 | return spawn('git |
| 22 | env: { ...process |
| 23 | }); |
| 24 | } |
| 25 | |
| 26 | function feed( |
| 27 | child: ReturnType |
| 28 | input: RunGitOptions |
| 29 | ) { |
| 30 | if (input === |
| 31 | else if (Buffer. |
| 32 | else input.pipe( |
| 33 | } |
| 34 | |
| 35 | export async function runGitBuffer |
| 36 | args, |
| 37 | gitDir, |
| 38 | input, |
| 39 | env, |
| 40 | }: RunGitOptions) |
| 41 | return new Promise |
| 42 | const child = |
| 43 | |
| 44 | const stdout: |
| 45 | const stderr: |
| 46 | child.stdout.on |
| 47 | child.stderr.on |
| 48 | |
| 49 | child.on('error |
| 50 | child.stdin.on |
| 51 | if (!isUnreadInput(error |
| 52 | }); |
| 53 | child.on('close |
| 54 | if (code === |
| 55 | reject( |
| 56 | new GitCommandFailedError( |
| 57 | args[0], |
| 58 | code, |
| 59 | Buffer.concat(stderr |
| 60 | Buffer.concat(stdout |
| 61 | ), |
| 62 | ); |
| 63 | }); |
| 64 | |
| 65 | feed(child, input |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | /** git's stdout as a stream, for output too large to hold in memory. */ |
| 70 | export function runGitReadable({ |
| 71 | const child = |
| 72 | child.stdin.end |
| 73 | child.on('error |
| 74 | |
| 75 | return child.stdout |
| 76 | } |
| 77 | |
| 78 | /** {@ |
| 79 | export async function* |
| 80 | args, |
| 81 | gitDir, |
| 82 | input, |
| 83 | env, |
| 84 | }: RunGitOptions) |
| 85 | const child = |
| 86 | |
| 87 | const stderr: |
| 88 | const failures: |
| 89 | child.stdin.on |
| 90 | if (!isUnreadInput(error |
| 91 | }); |
| 92 | |
| 93 | feed(child, input |
| 94 | |
| 95 | child.stderr.on |
| 96 | child.on('error |
| 97 | |
| 98 | // A chunk boundary can land mid-codepoint, so decode across chunks. |
| 99 | const decoder = |
| 100 | let drained = |
| 101 | |
| 102 | try { |
| 103 | for await (const |
| 104 | const text = |
| 105 | if (text) yield |
| 106 | } |
| 107 | const tail = |
| 108 | if (tail) yield |
| 109 | drained = true; |
| 110 | } finally { |
| 111 | if (!drained) |
| 112 | } |
| 113 | |
| 114 | const [code] |
| 115 | if (failures.length |
| 116 | if (code !== |
| 117 | throw new GitCommandFailedError( |
| 118 | args[0], |
| 119 | code, |
| 120 | Buffer.concat(stderr |
| 121 | ); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** git exited before reading all it was given, as when a caller stops early; its exit status says what happened, so the failed write is not an error of its own. */ |
| 126 | function isUnreadInput(error: |
| 127 | return error.code |
| 128 | } |