-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathgit.ts
164 lines (143 loc) Β· 4.03 KB
/
git.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import type { ChangelogConfig } from "./config";
export interface GitCommitAuthor {
name: string;
email: string;
}
export interface RawGitCommit {
message: string;
body: string;
shortHash: string;
author: GitCommitAuthor;
}
export interface Reference {
type: "hash" | "issue" | "pull-request";
value: string;
}
export interface GitCommit extends RawGitCommit {
description: string;
type: string;
scope: string;
references: Reference[];
authors: GitCommitAuthor[];
isBreaking: boolean;
}
export async function getLastGitTag() {
const r = await execCommand("git", ["describe", "--tags", "--abbrev=0"])
.then((r) => r.split("\n"))
.catch(() => []);
return r[r.length - 1];
}
export async function getCurrentGitBranch() {
return await execCommand("git", ["rev-parse", "--abbrev-ref", "HEAD"]);
}
export async function getCurrentGitTag() {
return await execCommand("git", ["tag", "--points-at", "HEAD"]);
}
export async function getCurrentGitRef() {
return (await getCurrentGitTag()) || (await getCurrentGitBranch());
}
export async function getGitRemoteURL(cwd: string, remote = "origin") {
return await execCommand("git", [
`--work-tree=${cwd}`,
"remote",
"get-url",
remote,
]);
}
export async function getGitDiff(
from: string | undefined,
to = "HEAD",
dir?: string
): Promise<RawGitCommit[]> {
const args = [
"--no-pager",
"log",
`${from ? `${from}...` : ""}${to}`,
'--pretty="----%n%s|%h|%an|%ae%n%b"',
"--name-status",
];
if (dir) {
args.push("--", dir);
}
// https://git-scm.com/docs/pretty-formats
const r = await execCommand("git", args.filter(Boolean));
return r
.split("----\n")
.splice(1)
.map((line) => {
const [firstLine, ..._body] = line.split("\n");
const [message, shortHash, authorName, authorEmail] =
firstLine.split("|");
const r: RawGitCommit = {
message,
shortHash,
author: { name: authorName, email: authorEmail },
body: _body.join("\n"),
};
return r;
});
}
export function parseCommits(
commits: RawGitCommit[],
config: ChangelogConfig
): GitCommit[] {
return commits
.map((commit) => parseGitCommit(commit, config))
.filter(Boolean);
}
// https://www.conventionalcommits.org/en/v1.0.0/
// https://regex101.com/r/FSfNvA/1
const ConventionalCommitRegex =
/(?<type>[a-z]+)(\((?<scope>.+)\))?(?<breaking>!)?: (?<description>.+)/i;
const CoAuthoredByRegex = /co-authored-by:\s*(?<name>.+)(<(?<email>.+)>)/gim;
const PullRequestRE = /\([ a-z]*(#\d+)\s*\)/gm;
const IssueRE = /(#\d+)/gm;
export function parseGitCommit(
commit: RawGitCommit,
config: ChangelogConfig
): GitCommit | null {
const match = commit.message.match(ConventionalCommitRegex);
if (!match) {
return null;
}
const type = match.groups.type;
let scope = match.groups.scope || "";
scope = config.scopeMap[scope] || scope;
const isBreaking = Boolean(match.groups.breaking);
let description = match.groups.description;
// Extract references from message
const references: Reference[] = [];
for (const m of description.matchAll(PullRequestRE)) {
references.push({ type: "pull-request", value: m[1] });
}
for (const m of description.matchAll(IssueRE)) {
if (!references.some((i) => i.value === m[1])) {
references.push({ type: "issue", value: m[1] });
}
}
references.push({ value: commit.shortHash, type: "hash" });
// Remove references and normalize
description = description.replace(PullRequestRE, "").trim();
// Find all authors
const authors: GitCommitAuthor[] = [commit.author];
for (const match of commit.body.matchAll(CoAuthoredByRegex)) {
authors.push({
name: (match.groups.name || "").trim(),
email: (match.groups.email || "").trim(),
});
}
return {
...commit,
authors,
description,
type,
scope,
references,
isBreaking,
};
}
async function execCommand(cmd: string, args: string[]) {
const { execa } = await import("execa");
const res = await execa(cmd, args);
return res.stdout;
}