-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
Copy pathlocalGetDiff.ts
34 lines (28 loc) · 995 Bytes
/
localGetDiff.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { debug } from "../../debug"
import { spawn } from "child_process"
const d = debug("localGetDiff")
const useCommittedDiffArgs = (base: string, head: string) => [
"diff",
"--src-prefix='a/' --dst-prefix='b/'",
`${base}...${head}`,
]
const useStagingChanges = () => ["diff", "--src-prefix='a/' --dst-prefix='b/'", "--staged"]
export const localGetDiff = (base: string, head: string, staging: boolean = false) =>
new Promise<string>((done) => {
const args = staging ? useStagingChanges() : useCommittedDiffArgs(base, head)
let stdout = ""
const child = spawn("git", args, { env: process.env })
d("> git", args.join(" "))
child.stdout.on("data", (chunk) => {
stdout += chunk
})
child.stderr.on("data", (data) => {
console.error(`Could not get diff from git between ${base} and ${head}`)
throw new Error(data.toString())
})
child.on("close", function (code) {
if (code === 0) {
done(stdout)
}
})
})