|
1 | | -import { debug } from "../../debug" |
2 | | -import JSON5 from "json5" |
3 | | - |
4 | | -import { spawn } from "child_process" |
| 1 | +import { logGitCommits } from "./localLogGitCommits" |
5 | 2 | import { GitCommit } from "../../dsl/Commit" |
6 | 3 |
|
7 | | -const d = debug("localGetDiff") |
8 | | - |
9 | | -const sha = "%H" |
10 | | -const parents = "%p" |
11 | | -const authorName = "%an" |
12 | | -const authorEmail = "%ae" |
13 | | -const authorDate = "%ai" |
14 | | -const committerName = "%cn" |
15 | | -const committerEmail = "%ce" |
16 | | -const committerDate = "%ci" |
17 | | -const message = "%f" // this is subject, not message, so it'll only be one line |
18 | | - |
19 | | -const author = `"author": {"name": "${authorName}", "email": "${authorEmail}", "date": "${authorDate}" }` |
20 | | -const committer = `"committer": {"name": "${committerName}", "email": "${committerEmail}", "date": "${committerDate}" }` |
21 | | -export const formatJSON = `{ "sha": "${sha}", "parents": "${parents}", ${author}, ${committer}, "message": "${message}"},` |
22 | | - |
23 | | -export const localGetCommits = (base: string, head: string) => |
24 | | - new Promise<GitCommit[]>((resolve, reject) => { |
25 | | - const args = ["log", `${base}...${head}`, `--pretty=format:${formatJSON}`] |
26 | | - const child = spawn("git", args, { env: process.env }) |
27 | | - d("> git", args.join(" ")) |
28 | | - const commits: GitCommit[] = [] |
29 | | - |
30 | | - child.stdout.on("data", async (chunk: Buffer) => { |
31 | | - const data = chunk.toString() |
32 | | - // remove trailing comma, and wrap into an array |
33 | | - const asJSONString = `[${data.substring(0, data.length - 1)}]` |
34 | | - const parsedCommits = JSON5.parse(asJSONString) |
35 | | - const realCommits = parsedCommits.map((c: any) => ({ |
36 | | - ...c, |
37 | | - parents: c.parents.split(" "), |
38 | | - url: "fake.danger.systems/" + c.sha, |
39 | | - })) |
40 | | - |
41 | | - commits.push(...realCommits) |
42 | | - }) |
43 | | - |
44 | | - child.stderr.on("end", () => resolve(commits)) |
45 | | - |
46 | | - const errorParts: string[] = [] |
47 | | - |
48 | | - child.stderr.on("data", (chunk: Buffer) => errorParts.push(chunk.toString())) |
49 | | - |
50 | | - child.on("close", code => { |
51 | | - if (code !== 0) { |
52 | | - console.error(`Could not get commits from git between ${base} and ${head}`) |
53 | | - reject(new Error(errorParts.join(""))) |
54 | | - } else { |
55 | | - resolve(commits) |
56 | | - } |
57 | | - }) |
58 | | - }) |
| 4 | +export const localGetCommits = (base: string, head: string) => { |
| 5 | + const options = { |
| 6 | + number: 100, |
| 7 | + branch: `${base}...${head}`, |
| 8 | + fields: [ |
| 9 | + "hash" as "hash", |
| 10 | + "abbrevParentHashes" as "abbrevParentHashes", |
| 11 | + "treeHash" as "treeHash", |
| 12 | + "authorName" as "authorName", |
| 13 | + "authorEmail" as "authorEmail", |
| 14 | + "authorDate" as "authorDate", |
| 15 | + "committerName" as "committerName", |
| 16 | + "committerEmail" as "committerEmail", |
| 17 | + "committerDate" as "committerDate", |
| 18 | + "subject" as "subject", |
| 19 | + ], |
| 20 | + } |
| 21 | + |
| 22 | + const commits: GitCommit[] = logGitCommits(options).map(commit => ({ |
| 23 | + sha: commit.hash, |
| 24 | + author: { |
| 25 | + name: commit.authorName, |
| 26 | + email: commit.authorEmail, |
| 27 | + date: commit.authorDate, |
| 28 | + }, |
| 29 | + committer: { |
| 30 | + name: commit.committerName, |
| 31 | + email: commit.committerEmail, |
| 32 | + date: commit.committerDate, |
| 33 | + }, |
| 34 | + message: commit.subject, |
| 35 | + tree: commit.treeHash, |
| 36 | + url: "fake.danger.systems/" + commit.hash, |
| 37 | + })) |
| 38 | + |
| 39 | + return commits |
| 40 | +} |
0 commit comments