-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathgit.ts
221 lines (193 loc) Β· 5.82 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import type { ChangelogConfig } from "./config";
import { execCommand } from "./exec";
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;
revertedHashes: string[];
}
export interface RevertPair {
shortRevertingHash: string;
revertedHash: string;
}
export async function getLastGitTag() {
const r = await execCommand("git", ["describe", "--tags", "--abbrev=0"])
.then((r) => r.split("\n"))
.catch(() => []);
return r.at(-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"
): Promise<RawGitCommit[]> {
// https://git-scm.com/docs/pretty-formats
const r = await execCommand("git", [
"--no-pager",
"log",
`${from ? `${from}...` : ""}${to}`,
'--pretty="----%n%s|%h|%an|%ae%n%b"',
"--name-status",
]);
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;
const RevertHashRE = /This reverts commit (?<hash>[\da-f]{40})./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();
// Extract the reverted hashes.
const revertedHashes = [];
const matchedHashes = commit.body.matchAll(RevertHashRE);
for (const matchedHash of matchedHashes) {
revertedHashes.push(matchedHash.groups.hash);
}
// 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,
revertedHashes,
};
}
export function filterCommits(
commits: GitCommit[],
config: ChangelogConfig
): GitCommit[] {
const commitsWithNoDeps = commits.filter(
(c) =>
config.types[c.type] &&
!(c.type === "chore" && c.scope === "deps" && !c.isBreaking)
);
let resolvedCommits: GitCommit[] = [];
let revertWatchList: RevertPair[] = [];
for (const commit of commitsWithNoDeps) {
// Include the reverted hashes in the watch list
if (commit.revertedHashes.length > 0) {
revertWatchList.push(
...commit.revertedHashes.map(
(revertedHash) =>
({
revertedHash,
shortRevertingHash: commit.shortHash,
} as RevertPair)
)
);
}
// Find the commits which revert the current commit being evaluated
const shortRevertingHashes = revertWatchList
.filter((pair) => pair.revertedHash.startsWith(commit.shortHash))
.map((pair) => pair.shortRevertingHash);
if (shortRevertingHashes.length > 0) {
// Remove commits that reverts this current commit
resolvedCommits = resolvedCommits.filter(
(resolvedCommit) =>
!shortRevertingHashes.includes(resolvedCommit.shortHash)
);
// Unwatch reverting hashes that has been resolved
revertWatchList = revertWatchList.filter(
(watchedRevert) =>
!shortRevertingHashes.includes(watchedRevert.shortRevertingHash)
);
} else {
// If the current commit is known not to have been reverted, put it to resolved commits.
resolvedCommits = [...resolvedCommits, commit];
}
}
return resolvedCommits;
}