Repository navigation
Expand file tree
/
Copy pathgit.ts
More file actions
225 lines (210 loc) · 6.87 KB
/
Copy pathgit.ts
File metadata and controls
225 lines (210 loc) · 6.87 KB
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
222
223
224
225
import { access } from "node:fs/promises";
import { join } from "node:path";
import { $ } from "execa";
import type { ReleaseOpts } from "./main";
export async function configureVcs(repoRoot: string): Promise<boolean> {
try {
await access(join(repoRoot, ".jj"));
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
return false;
}
// If .jj exists, failures here indicate a broken jj workspace. Falling back to
// Git could release a different revision, especially in non-colocated repos.
await $({ cwd: repoRoot })`jj git root`;
return true;
}
export async function getCurrentRevision(repoRoot: string): Promise<string> {
if (await configureVcs(repoRoot)) {
const { stdout: empty } = await $({
cwd: repoRoot,
})`jj log -r @ --no-graph -T empty`;
const revision = empty.trim() === "true" ? "@-" : "@";
const { stdout } = await $({
cwd: repoRoot,
})`jj log -r ${revision} --no-graph -T commit_id`;
return stdout.trim();
}
const { stdout } = await $({ cwd: repoRoot })`git rev-parse HEAD`;
return stdout.trim();
}
export async function getCurrentBookmark(repoRoot: string): Promise<string> {
if (await configureVcs(repoRoot)) {
const bookmarkedAncestor = "latest(ancestors(@) & bookmarks())";
const { stdout } = await $({
cwd: repoRoot,
})`jj log -r ${bookmarkedAncestor} --no-graph -T ${'local_bookmarks.map(|bookmark| bookmark.name()).join("\\n") ++ "\\n"'}`;
const bookmarks = stdout
.split("\n")
.map((bookmark) => bookmark.trim())
.filter(Boolean);
if (bookmarks.length === 0)
throw new Error(
"The release revision is not associated with a jj bookmark",
);
if (bookmarks.length > 1)
throw new Error(
`The release revision has multiple jj bookmarks (${bookmarks.join(", ")}); specify a single release bookmark by deleting or moving the others`,
);
return bookmarks[0];
}
const { stdout } = await $({
cwd: repoRoot,
})`git rev-parse --abbrev-ref HEAD`;
return stdout.trim();
}
export async function commitAndPush(opts: ReleaseOpts, push: boolean) {
if (await configureVcs(opts.repoRoot)) {
const { stdout: summary } = await $({
cwd: opts.repoRoot,
})`jj diff --summary -r @`;
if (summary.trim()) {
await $({
stdio: "inherit",
cwd: opts.repoRoot,
})`jj commit -m ${`chore(release): update version to ${opts.version}`}`;
}
if (push) {
const bookmark = await getCurrentBookmark(opts.repoRoot);
const revision = await getCurrentRevision(opts.repoRoot);
const { stdout: gitDir } = await $({ cwd: opts.repoRoot })`jj git root`;
const gitOptions = {
cwd: opts.repoRoot,
env: { GIT_DIR: gitDir.trim() },
};
const remoteRef = `refs/remotes/origin/${bookmark}`;
const remote = await $({
...gitOptions,
reject: false,
})`git rev-parse -q --verify ${remoteRef}`;
if (remote.exitCode === 0) {
// jj pushes use lease semantics. Releases must also be fast-forward,
// even when a freshly initialized workspace has not tracked origin.
await $(
gitOptions,
)`git merge-base --is-ancestor ${remoteRef} ${revision}`;
await $({
stdio: "inherit",
cwd: opts.repoRoot,
})`jj bookmark track ${bookmark} --remote origin`;
}
await $({
stdio: "inherit",
cwd: opts.repoRoot,
})`jj bookmark set ${bookmark} -r ${revision}`;
await $({
stdio: "inherit",
cwd: opts.repoRoot,
})`jj git push --remote origin --bookmark ${bookmark}`;
}
return;
}
if (!push) {
await $({ stdio: "inherit", cwd: opts.repoRoot })`git add .`;
const { stdout: staged } = await $({
cwd: opts.repoRoot,
})`git status --porcelain`;
if (staged.trim())
await $({
stdio: "inherit",
cwd: opts.repoRoot,
})`git commit -m ${`chore(release): update version to ${opts.version}`}`;
} else {
const bookmark = await getCurrentBookmark(opts.repoRoot);
if (bookmark === "main") {
await $({ stdio: "inherit", cwd: opts.repoRoot })`git push`;
} else {
await $({
stdio: "inherit",
cwd: opts.repoRoot,
})`gt submit --no-edit --publish`;
}
}
}
export async function validateGit(opts: ReleaseOpts) {
// Validate there's no uncommitted changes
const result = (await configureVcs(opts.repoRoot))
? await $({ cwd: opts.repoRoot })`jj diff --summary -r @`
: await $({ cwd: opts.repoRoot })`git status --porcelain`;
const status = result.stdout;
if (status.trim().length > 0) {
throw new Error(
"There are uncommitted changes. Please commit or stash them.",
);
}
}
export async function createAndPushTag(opts: ReleaseOpts) {
console.log(`Creating tag v${opts.version}...`);
try {
const gitDir = (await configureVcs(opts.repoRoot))
? (await $({ cwd: opts.repoRoot })`jj git root`).stdout.trim()
: undefined;
const gitOptions = { cwd: opts.repoRoot, env: { GIT_DIR: gitDir } };
const tag = `v${opts.version}`;
const existing = await $({
...gitOptions,
reject: false,
})`git rev-parse -q --verify ${`refs/tags/${tag}^{commit}`}`;
const target = await $(
gitOptions,
)`git rev-parse ${`${opts.commit}^{commit}`}`;
if (
existing.exitCode === 0 &&
existing.stdout.trim() !== target.stdout.trim()
) {
throw new Error(`${tag} already exists at a different commit`);
}
if (existing.exitCode !== 0)
await $(gitOptions)`git tag ${tag} ${opts.commit}`;
await $({
...gitOptions,
stdio: "inherit",
})`git push origin refs/tags/${tag}`;
console.log(`✅ Tag v${opts.version} created and pushed`);
} catch (err) {
console.error("❌ Failed to create or push tag");
throw err;
}
}
export async function createGitHubRelease(opts: ReleaseOpts) {
console.log("Creating GitHub release...");
try {
const tagName = `v${opts.version}`;
console.log(`Looking for existing release for ${opts.version}`);
// Check if a release with this version name already exists
const { stdout: releaseJson } = await $({
cwd: opts.repoRoot,
})`gh release list --json name,tagName`;
const releases = JSON.parse(releaseJson);
const existingRelease = releases.find((r: any) => r.name === opts.version);
if (existingRelease) {
console.log(
`Updating release ${opts.version} to point to new tag ${tagName}`,
);
await $({
stdio: "inherit",
cwd: opts.repoRoot,
})`gh release edit ${existingRelease.tagName} --tag ${tagName}`;
} else {
console.log(
`Creating new release ${opts.version} pointing to tag ${tagName}`,
);
await $({
stdio: "inherit",
cwd: opts.repoRoot,
})`gh release create ${tagName} --title ${opts.version} --generate-notes`;
// Check if this is a pre-release (contains -rc. or similar)
if (opts.version.includes("-")) {
await $({
stdio: "inherit",
cwd: opts.repoRoot,
})`gh release edit ${tagName} --prerelease`;
}
}
console.log("✅ GitHub release created/updated");
} catch (err) {
console.error("❌ Failed to create GitHub release");
console.warn("! You may need to create the release manually");
throw err;
}
}