bstack 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -5
- package/dist/bstack.js +717 -0
- package/package.json +15 -10
- package/src/checkout.ts +0 -67
- package/src/cli.ts +0 -100
- package/src/command.ts +0 -61
- package/src/git.ts +0 -197
- package/src/github.ts +0 -187
- package/src/identity.ts +0 -88
- package/src/model.ts +0 -44
- package/src/reporter.ts +0 -11
- package/src/state.ts +0 -52
- package/src/sync.ts +0 -251
package/src/identity.ts
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import type { Commit } from "./model";
|
|
3
|
-
|
|
4
|
-
const trailerPattern = /^Bstack-Id:\s*(\S+)\s*$/gim;
|
|
5
|
-
|
|
6
|
-
export function readChangeId(message: string): string | undefined {
|
|
7
|
-
const matches = [...message.matchAll(trailerPattern)];
|
|
8
|
-
if (matches.length > 1) {
|
|
9
|
-
throw new Error("A commit contains more than one Bstack-Id trailer");
|
|
10
|
-
}
|
|
11
|
-
return matches[0]?.[1];
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function addChangeId(message: string, changeId: string): string {
|
|
15
|
-
const trimmed = message.trimEnd();
|
|
16
|
-
return `${trimmed}\n\nBstack-Id: ${changeId}\n`;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function newChangeId(): string {
|
|
20
|
-
return randomUUID().replaceAll("-", "");
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function parseRawCommit(oid: string, raw: string): Commit {
|
|
24
|
-
const boundary = raw.indexOf("\n\n");
|
|
25
|
-
if (boundary === -1) {
|
|
26
|
-
throw new Error(`Commit ${oid} has an invalid object format`);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const headers = raw.slice(0, boundary).split("\n");
|
|
30
|
-
const treeLine = headers.find((line) => line.startsWith("tree "));
|
|
31
|
-
const parents = headers.filter((line) => line.startsWith("parent "));
|
|
32
|
-
if (!treeLine || parents.length !== 1) {
|
|
33
|
-
throw new Error(
|
|
34
|
-
`Commit ${oid} must have exactly one parent; merge and root commits are not supported`,
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
if (headers.some((line) => line.startsWith("gpgsig "))) {
|
|
38
|
-
throw new Error(
|
|
39
|
-
`Commit ${oid} is signed. bstack cannot add an identity trailer without replacing its signature`,
|
|
40
|
-
);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const message = raw.slice(boundary + 2);
|
|
44
|
-
return {
|
|
45
|
-
oid,
|
|
46
|
-
tree: treeLine.slice("tree ".length),
|
|
47
|
-
parent: parents[0]!.slice("parent ".length),
|
|
48
|
-
message,
|
|
49
|
-
headers,
|
|
50
|
-
changeId: readChangeId(message),
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export function rewriteCommit(
|
|
55
|
-
commit: Commit,
|
|
56
|
-
parent: string,
|
|
57
|
-
message: string,
|
|
58
|
-
): string {
|
|
59
|
-
const rewrittenHeaders: string[] = [];
|
|
60
|
-
let replacedParent = false;
|
|
61
|
-
|
|
62
|
-
for (const header of commit.headers) {
|
|
63
|
-
if (header.startsWith("parent ")) {
|
|
64
|
-
if (!replacedParent) {
|
|
65
|
-
rewrittenHeaders.push(`parent ${parent}`);
|
|
66
|
-
replacedParent = true;
|
|
67
|
-
}
|
|
68
|
-
continue;
|
|
69
|
-
}
|
|
70
|
-
rewrittenHeaders.push(header);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return `${rewrittenHeaders.join("\n")}\n\n${message}`;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function splitCommitMessage(message: string): {
|
|
77
|
-
subject: string;
|
|
78
|
-
body: string;
|
|
79
|
-
} {
|
|
80
|
-
const withoutIdentity = message
|
|
81
|
-
.split("\n")
|
|
82
|
-
.filter((line) => !/^Bstack-Id:\s*\S+\s*$/i.test(line))
|
|
83
|
-
.join("\n")
|
|
84
|
-
.trim();
|
|
85
|
-
const [subject = "Untitled change", ...bodyLines] =
|
|
86
|
-
withoutIdentity.split("\n");
|
|
87
|
-
return { subject, body: bodyLines.join("\n").trim() };
|
|
88
|
-
}
|
package/src/model.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
export type Commit = {
|
|
2
|
-
oid: string;
|
|
3
|
-
tree: string;
|
|
4
|
-
parent: string;
|
|
5
|
-
message: string;
|
|
6
|
-
headers: readonly string[];
|
|
7
|
-
changeId: string | undefined;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export type Change = {
|
|
11
|
-
id: string;
|
|
12
|
-
oid: string;
|
|
13
|
-
subject: string;
|
|
14
|
-
body: string;
|
|
15
|
-
remoteBranch: string;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export type PullRequest = {
|
|
19
|
-
number: number;
|
|
20
|
-
url: string;
|
|
21
|
-
state: "OPEN" | "CLOSED" | "MERGED";
|
|
22
|
-
title: string;
|
|
23
|
-
body: string;
|
|
24
|
-
isDraft: boolean;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export type StoredChange = {
|
|
28
|
-
id: string;
|
|
29
|
-
remoteBranch: string;
|
|
30
|
-
pullRequest: number;
|
|
31
|
-
url: string;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export type StoredStack = {
|
|
35
|
-
remote: string;
|
|
36
|
-
base: string;
|
|
37
|
-
stackNumber?: number;
|
|
38
|
-
changes: StoredChange[];
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
export type BstackState = {
|
|
42
|
-
schemaVersion: 1;
|
|
43
|
-
stacks: StoredStack[];
|
|
44
|
-
};
|
package/src/reporter.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export interface Reporter {
|
|
2
|
-
progress(message: string): void;
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
export class ConsoleReporter implements Reporter {
|
|
6
|
-
constructor(private readonly enabled = true) {}
|
|
7
|
-
|
|
8
|
-
progress(message: string): void {
|
|
9
|
-
if (this.enabled) process.stderr.write(`[bstack] ${message}\n`);
|
|
10
|
-
}
|
|
11
|
-
}
|
package/src/state.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
2
|
-
import { dirname } from "node:path";
|
|
3
|
-
import type { BstackState, StoredStack } from "./model";
|
|
4
|
-
|
|
5
|
-
const emptyState = (): BstackState => ({ schemaVersion: 1, stacks: [] });
|
|
6
|
-
|
|
7
|
-
export class StateStore {
|
|
8
|
-
constructor(private readonly path: string) {}
|
|
9
|
-
|
|
10
|
-
read(): BstackState {
|
|
11
|
-
try {
|
|
12
|
-
const parsed = JSON.parse(readFileSync(this.path, "utf8")) as unknown;
|
|
13
|
-
if (!isState(parsed))
|
|
14
|
-
throw new Error(`Unsupported bstack state in ${this.path}`);
|
|
15
|
-
return parsed;
|
|
16
|
-
} catch (error) {
|
|
17
|
-
if (isMissingFile(error)) return emptyState();
|
|
18
|
-
throw error;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
write(state: BstackState): void {
|
|
23
|
-
mkdirSync(dirname(this.path), { recursive: true });
|
|
24
|
-
const temporary = `${this.path}.tmp`;
|
|
25
|
-
writeFileSync(temporary, `${JSON.stringify(state, null, 2)}\n`);
|
|
26
|
-
renameSync(temporary, this.path);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
findByChangeIds(
|
|
30
|
-
state: BstackState,
|
|
31
|
-
ids: ReadonlySet<string>,
|
|
32
|
-
): StoredStack | undefined {
|
|
33
|
-
const matches = state.stacks.filter((stack) =>
|
|
34
|
-
stack.changes.some((change) => ids.has(change.id)),
|
|
35
|
-
);
|
|
36
|
-
if (matches.length > 1)
|
|
37
|
-
throw new Error(
|
|
38
|
-
"The current commits match more than one stored bstack stack",
|
|
39
|
-
);
|
|
40
|
-
return matches[0];
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function isMissingFile(error: unknown): boolean {
|
|
45
|
-
return error instanceof Error && "code" in error && error.code === "ENOENT";
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function isState(value: unknown): value is BstackState {
|
|
49
|
-
if (typeof value !== "object" || value === null) return false;
|
|
50
|
-
const candidate = value as { schemaVersion?: unknown; stacks?: unknown };
|
|
51
|
-
return candidate.schemaVersion === 1 && Array.isArray(candidate.stacks);
|
|
52
|
-
}
|
package/src/sync.ts
DELETED
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
import type { GitRepository } from "./git";
|
|
2
|
-
import type { GitHubPlatform } from "./github";
|
|
3
|
-
import type { BstackState, Change, PullRequest, StoredStack } from "./model";
|
|
4
|
-
import type { Reporter } from "./reporter";
|
|
5
|
-
import { StateStore } from "./state";
|
|
6
|
-
|
|
7
|
-
export type SyncOptions = {
|
|
8
|
-
base: string | undefined;
|
|
9
|
-
remote: string | undefined;
|
|
10
|
-
open: boolean;
|
|
11
|
-
dryRun: boolean;
|
|
12
|
-
reporter: Reporter;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export type SyncResult = {
|
|
16
|
-
base: string;
|
|
17
|
-
remote: string;
|
|
18
|
-
rewritten: boolean;
|
|
19
|
-
changes: Array<Change & { pullRequest?: PullRequest }>;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export function syncStack(
|
|
23
|
-
repository: GitRepository,
|
|
24
|
-
github: GitHubPlatform,
|
|
25
|
-
options: SyncOptions,
|
|
26
|
-
): SyncResult {
|
|
27
|
-
const { reporter } = options;
|
|
28
|
-
reporter.progress("Checking the repository and GitHub prerequisites");
|
|
29
|
-
repository.assertReady();
|
|
30
|
-
github.assertReady();
|
|
31
|
-
const remote = repository.resolveRemote(options.remote);
|
|
32
|
-
const base = options.base ?? github.defaultBranch();
|
|
33
|
-
reporter.progress(
|
|
34
|
-
`Using ${remote} as the remote and ${base} as the stack base`,
|
|
35
|
-
);
|
|
36
|
-
reporter.progress(`Fetching ${remote}/${base}`);
|
|
37
|
-
const remoteBase = repository.fetchBase(remote, base);
|
|
38
|
-
const baseOid = repository.mergeBase("HEAD", remoteBase);
|
|
39
|
-
const commits = repository.commitsSince(baseOid);
|
|
40
|
-
if (commits.length === 0)
|
|
41
|
-
throw new Error(`No commits found between ${base} and HEAD`);
|
|
42
|
-
reporter.progress(
|
|
43
|
-
`Found ${commits.length} local change${commits.length === 1 ? "" : "s"}`,
|
|
44
|
-
);
|
|
45
|
-
|
|
46
|
-
const rewritten = commits.some((commit) => commit.changeId === undefined);
|
|
47
|
-
if (rewritten) {
|
|
48
|
-
reporter.progress(
|
|
49
|
-
options.dryRun
|
|
50
|
-
? "Stable change IDs would be added to the commits"
|
|
51
|
-
: "Adding stable change IDs to the commits",
|
|
52
|
-
);
|
|
53
|
-
} else {
|
|
54
|
-
reporter.progress("All commits already have stable change IDs");
|
|
55
|
-
}
|
|
56
|
-
const changes = repository.ensureChangeIds(commits, options.dryRun);
|
|
57
|
-
if (options.dryRun) {
|
|
58
|
-
reporter.progress(
|
|
59
|
-
"Dry run complete; no commits or remote branches were changed",
|
|
60
|
-
);
|
|
61
|
-
return { base, remote, rewritten, changes };
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
reporter.progress("Reading the previous stack state");
|
|
65
|
-
const store = new StateStore(repository.statePath());
|
|
66
|
-
const state = store.read();
|
|
67
|
-
const previous = store.findByChangeIds(
|
|
68
|
-
state,
|
|
69
|
-
new Set(changes.map((change) => change.id)),
|
|
70
|
-
);
|
|
71
|
-
const evolution = analyzeEvolution(previous, changes, github);
|
|
72
|
-
|
|
73
|
-
reporter.progress(
|
|
74
|
-
`Publishing ${changes.length} protected remote ref${changes.length === 1 ? "" : "s"}`,
|
|
75
|
-
);
|
|
76
|
-
repository.pushChanges(remote, changes);
|
|
77
|
-
|
|
78
|
-
reporter.progress("Looking up existing pull requests");
|
|
79
|
-
const existing = changes.map((change) =>
|
|
80
|
-
github.pullRequestForBranch(change.remoteBranch),
|
|
81
|
-
);
|
|
82
|
-
let pullRequests: PullRequest[];
|
|
83
|
-
if (changes.length === 1) {
|
|
84
|
-
if (evolution.kind === "partial") {
|
|
85
|
-
reporter.progress(
|
|
86
|
-
"Updating this down-stack prefix while preserving higher pull requests",
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
reporter.progress(
|
|
90
|
-
existing[0]
|
|
91
|
-
? "Using the existing pull request"
|
|
92
|
-
: "Creating a pull request",
|
|
93
|
-
);
|
|
94
|
-
pullRequests = [
|
|
95
|
-
existing[0] ?? github.createPullRequest(changes[0]!, base, options.open),
|
|
96
|
-
];
|
|
97
|
-
} else {
|
|
98
|
-
if (evolution.kind === "full") {
|
|
99
|
-
reporter.progress(
|
|
100
|
-
`Linking ${changes.length} pull requests as a native GitHub stack`,
|
|
101
|
-
);
|
|
102
|
-
github.linkStack(
|
|
103
|
-
changes.map((change) => change.remoteBranch),
|
|
104
|
-
base,
|
|
105
|
-
remote,
|
|
106
|
-
options.open,
|
|
107
|
-
);
|
|
108
|
-
} else if (evolution.kind === "append") {
|
|
109
|
-
reporter.progress(
|
|
110
|
-
`Appending ${evolution.branches.length} pull request${evolution.branches.length === 1 ? "" : "s"} to stack #${evolution.stackNumber}`,
|
|
111
|
-
);
|
|
112
|
-
github.appendToStack(
|
|
113
|
-
evolution.stackNumber,
|
|
114
|
-
evolution.branches,
|
|
115
|
-
remote,
|
|
116
|
-
options.open,
|
|
117
|
-
);
|
|
118
|
-
} else if (evolution.kind === "partial") {
|
|
119
|
-
reporter.progress(
|
|
120
|
-
"Updating this down-stack prefix while preserving higher pull requests",
|
|
121
|
-
);
|
|
122
|
-
} else {
|
|
123
|
-
reporter.progress(
|
|
124
|
-
"The native GitHub stack already has the correct members",
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
pullRequests = changes.map((change, index) => {
|
|
128
|
-
const pr =
|
|
129
|
-
existing[index] ?? github.pullRequestForBranch(change.remoteBranch);
|
|
130
|
-
if (!pr)
|
|
131
|
-
throw new Error(
|
|
132
|
-
`GitHub did not return a PR for ${change.remoteBranch}`,
|
|
133
|
-
);
|
|
134
|
-
return pr;
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
reporter.progress("Synchronizing pull request titles and descriptions");
|
|
139
|
-
for (const [index, pr] of pullRequests.entries()) {
|
|
140
|
-
github.editPullRequest(pr, changes[index]!);
|
|
141
|
-
reporter.progress(`PR #${pr.number}: ${changes[index]!.subject}`);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const stackNumber =
|
|
145
|
-
previous?.stackNumber ??
|
|
146
|
-
(pullRequests.length > 1
|
|
147
|
-
? github.stackNumberForPullRequest(pullRequests[0]!.number)
|
|
148
|
-
: undefined);
|
|
149
|
-
const synchronizedChanges = changes.map((change, index) => ({
|
|
150
|
-
id: change.id,
|
|
151
|
-
remoteBranch: change.remoteBranch,
|
|
152
|
-
pullRequest: pullRequests[index]!.number,
|
|
153
|
-
url: pullRequests[index]!.url,
|
|
154
|
-
}));
|
|
155
|
-
const storedChanges =
|
|
156
|
-
evolution.kind === "partial" && previous
|
|
157
|
-
? [
|
|
158
|
-
...synchronizedChanges,
|
|
159
|
-
...previous.changes.slice(evolution.previousOffset + changes.length),
|
|
160
|
-
]
|
|
161
|
-
: synchronizedChanges;
|
|
162
|
-
const stored: StoredStack = {
|
|
163
|
-
remote,
|
|
164
|
-
base,
|
|
165
|
-
...(stackNumber === undefined ? {} : { stackNumber }),
|
|
166
|
-
changes: storedChanges,
|
|
167
|
-
};
|
|
168
|
-
writeUpdatedState(store, state, previous, stored);
|
|
169
|
-
reporter.progress("Saved the local stack state");
|
|
170
|
-
|
|
171
|
-
return {
|
|
172
|
-
base,
|
|
173
|
-
remote,
|
|
174
|
-
rewritten,
|
|
175
|
-
changes: changes.map((change, index) => ({
|
|
176
|
-
...change,
|
|
177
|
-
pullRequest: pullRequests[index]!,
|
|
178
|
-
})),
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
type Evolution =
|
|
183
|
-
| { kind: "full" }
|
|
184
|
-
| { kind: "skip" }
|
|
185
|
-
| { kind: "partial"; previousOffset: number }
|
|
186
|
-
| { kind: "append"; stackNumber: number; branches: string[] };
|
|
187
|
-
|
|
188
|
-
function analyzeEvolution(
|
|
189
|
-
previous: StoredStack | undefined,
|
|
190
|
-
changes: readonly Change[],
|
|
191
|
-
github: GitHubPlatform,
|
|
192
|
-
): Evolution {
|
|
193
|
-
if (!previous) return { kind: "full" };
|
|
194
|
-
const previousIds = previous.changes.map((change) => change.id);
|
|
195
|
-
const currentIds = changes.map((change) => change.id);
|
|
196
|
-
|
|
197
|
-
const firstCurrentIndex = previousIds.indexOf(currentIds[0]!);
|
|
198
|
-
if (firstCurrentIndex === -1) {
|
|
199
|
-
throw new Error(
|
|
200
|
-
"The current commits do not continue the previously submitted stack",
|
|
201
|
-
);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
const removedPrefix = previous.changes.slice(0, firstCurrentIndex);
|
|
205
|
-
for (const removed of removedPrefix) {
|
|
206
|
-
if (github.pullRequest(removed.pullRequest).state !== "MERGED") {
|
|
207
|
-
throw new Error(
|
|
208
|
-
"Submitted commits may only disappear from the bottom after their pull requests are merged",
|
|
209
|
-
);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
const surviving = previousIds.slice(firstCurrentIndex);
|
|
214
|
-
const sharedLength = Math.min(surviving.length, currentIds.length);
|
|
215
|
-
for (let index = 0; index < sharedLength; index++) {
|
|
216
|
-
if (surviving[index] !== currentIds[index]) {
|
|
217
|
-
throw new Error(
|
|
218
|
-
"Reordering or removing submitted commits is not supported yet. Restore the original order before syncing",
|
|
219
|
-
);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
if (currentIds.length < surviving.length) {
|
|
223
|
-
return { kind: "partial", previousOffset: firstCurrentIndex };
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
const appended = changes.slice(surviving.length);
|
|
227
|
-
if (removedPrefix.length === 0) return { kind: "full" };
|
|
228
|
-
if (appended.length === 0) return { kind: "skip" };
|
|
229
|
-
if (previous.stackNumber === undefined) {
|
|
230
|
-
throw new Error(
|
|
231
|
-
"Cannot append after a merge because the native GitHub stack number is missing from local state",
|
|
232
|
-
);
|
|
233
|
-
}
|
|
234
|
-
return {
|
|
235
|
-
kind: "append",
|
|
236
|
-
stackNumber: previous.stackNumber,
|
|
237
|
-
branches: appended.map((change) => change.remoteBranch),
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
function writeUpdatedState(
|
|
242
|
-
store: StateStore,
|
|
243
|
-
state: BstackState,
|
|
244
|
-
previous: StoredStack | undefined,
|
|
245
|
-
updated: StoredStack,
|
|
246
|
-
): void {
|
|
247
|
-
const stacks = previous
|
|
248
|
-
? state.stacks.map((stack) => (stack === previous ? updated : stack))
|
|
249
|
-
: [...state.stacks, updated];
|
|
250
|
-
store.write({ schemaVersion: 1, stacks });
|
|
251
|
-
}
|