-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathgit.ts
153 lines (132 loc) Β· 4.15 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
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;
}
export async function getLastGitTag() {
try {
return execCommand("git describe --tags --abbrev=0")?.split("\n").at(-1);
} catch {
// Ignore
}
}
export function getCurrentGitBranch() {
return execCommand("git rev-parse --abbrev-ref HEAD");
}
export function getCurrentGitTag() {
return execCommand("git tag --points-at HEAD");
}
export function getCurrentGitRef() {
return getCurrentGitTag() || getCurrentGitBranch();
}
export function getGitRemoteURL(cwd: string, remote = "origin") {
return execCommand(`git --work-tree="${cwd}" remote get-url "${remote}"`);
}
export async function getCurrentGitStatus() {
return execCommand("git status --porcelain");
}
export async function getGitDiff(
from: string | undefined,
to = "HEAD",
includePaths?: string[]
): Promise<RawGitCommit[]> {
// https://git-scm.com/docs/pretty-formats
const r = execCommand(
`git --no-pager log "${from ? `${from}...` : ""}${to}" --pretty="----%n%s|%h|%an|%ae%n%b" --name-status${includePaths ? ` -- ${includePaths.join(" ")}` : ""}`
);
console.log(`git --no-pager log "${from ? `${from}...` : ""}${to}" --pretty="----%n%s|%h|%an|%ae%n%b" --name-status${includePaths ? ` -- ${includePaths.join(" ")}` : ""}`)
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 =
/(?<emoji>:.+:|(\uD83C[\uDF00-\uDFFF])|(\uD83D[\uDC00-\uDE4F\uDE80-\uDEFF])|[\u2600-\u2B55])?( *)?(?<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;
const hasBreakingBody = /breaking change:/i.test(commit.body);
let scope = match.groups.scope || "";
scope = config.scopeMap[scope] || scope;
const isBreaking = Boolean(match.groups.breaking || hasBreakingBody);
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,
};
}