@sous-io/sous 0.2.16 → 0.2.17
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/docs/markdown/commands.md +23 -7
- package/docs/markdown/repositories-authoring.md +52 -11
- package/docs/markdown/repositories-file-formats.md +20 -0
- package/docs/markdown/repositories-providers.md +20 -10
- package/package.json +1 -1
- package/recipes/core/sous-skills/sous.recipe.yaml +8 -1
- package/src/commands/repo/release.ts +41 -0
- package/src/commands/repo/submit.ts +245 -35
- package/src/lib/repos/formats/common.ts +20 -0
- package/src/lib/repos/formats/recipe-manifest.ts +7 -0
- package/src/lib/repos/formats/repo-manifest.ts +8 -0
- package/src/lib/repos/providers/base.ts +33 -1
- package/src/lib/repos/providers/github.ts +275 -3
- package/src/lib/repos/providers/provider.ts +119 -3
- package/src/lib/repos/release/changelog.ts +448 -0
- package/src/lib/repos/release/git-state.ts +101 -15
- package/src/lib/repos/release/index.ts +2 -0
- package/src/lib/repos/release/submissions.ts +214 -0
- package/src/lib/repos/release/submit-checkout.ts +271 -0
- package/src/lib/repos/release/submit-questions.ts +153 -0
- package/src/lib/repos/release/submit-service.ts +581 -174
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The changelog `sous repo submit` writes under a proposal's description.
|
|
3
|
+
*
|
|
4
|
+
* A maintainer reviewing a proposal needs to know what merging it does to the
|
|
5
|
+
* people who subscribe to the repository: which recipes appear, disappear or
|
|
6
|
+
* change version, which will be released as a patch because their files
|
|
7
|
+
* changed without a version raise, which namespaces come and go, and which
|
|
8
|
+
* variables change. None of that is in a commit message, and all of it can be
|
|
9
|
+
* read from the manifests, so sous reads them: the ones the change carries,
|
|
10
|
+
* compared with the ones on the default branch.
|
|
11
|
+
*
|
|
12
|
+
* The changelog explains; it never refuses. Whether a change is acceptable is
|
|
13
|
+
* the repository's own checks' business, so a change that affects subscribers
|
|
14
|
+
* is described here, with a warning where one is due, and never blocked.
|
|
15
|
+
*
|
|
16
|
+
* Reading the default branch's manifests goes through git and the injectable
|
|
17
|
+
* runner; everything after that is pure, so the comparison is tested without a
|
|
18
|
+
* repository at all.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import path from "node:path";
|
|
22
|
+
import semver from "semver";
|
|
23
|
+
import {
|
|
24
|
+
MANIFEST_EXTENSIONS,
|
|
25
|
+
RECIPE_MANIFEST_BASENAME,
|
|
26
|
+
REPO_MANIFEST_BASENAME,
|
|
27
|
+
} from "../formats/common.js";
|
|
28
|
+
import {
|
|
29
|
+
parseRecipeManifest,
|
|
30
|
+
recipeManifestKey,
|
|
31
|
+
type RecipeManifest,
|
|
32
|
+
type VariableDefinition,
|
|
33
|
+
} from "../formats/recipe-manifest.js";
|
|
34
|
+
import { parseRepoManifest, type RepoManifest } from "../formats/repo-manifest.js";
|
|
35
|
+
import { parseJsoncText, parseYamlText } from "../load-manifest.js";
|
|
36
|
+
import type { RunOptions } from "../providers/git.js";
|
|
37
|
+
import { pathsInside } from "./submissions.js";
|
|
38
|
+
import { readFileAtTag } from "./tags.js";
|
|
39
|
+
import type { RepoValidation } from "./validate.js";
|
|
40
|
+
|
|
41
|
+
/** The manifests as they stood at one commit. */
|
|
42
|
+
export type ManifestSnapshot = {
|
|
43
|
+
/** The repo manifest. */
|
|
44
|
+
repo: RepoManifest;
|
|
45
|
+
/** Every recipe manifest that could be read, keyed by recipe key. */
|
|
46
|
+
recipes: Map<string, { path: string; manifest: RecipeManifest }>;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** One recipe that appeared or disappeared. */
|
|
50
|
+
export type RecipeEntry = { key: string; version: string };
|
|
51
|
+
|
|
52
|
+
/** One recipe whose declared version moved. */
|
|
53
|
+
export type VersionChange = { key: string; from: string; to: string };
|
|
54
|
+
|
|
55
|
+
/** One recipe whose files changed while its version stayed where it was. */
|
|
56
|
+
export type UnraisedChange = { key: string; version: string; next: string };
|
|
57
|
+
|
|
58
|
+
/** One variable that was added, removed or changed. */
|
|
59
|
+
export type VariableChange = {
|
|
60
|
+
/** The recipe that declares it. */
|
|
61
|
+
recipe: string;
|
|
62
|
+
/** The variable's name. */
|
|
63
|
+
name: string;
|
|
64
|
+
/** What happened to it. */
|
|
65
|
+
change: "added" | "removed" | "changed";
|
|
66
|
+
/** The fields that changed, for a changed variable. */
|
|
67
|
+
fields?: string[];
|
|
68
|
+
/** True when the change usually breaks a subscriber: a removal, or a tighter rule. */
|
|
69
|
+
breaking: boolean;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/** Everything merging a change would do, as the manifests describe it. */
|
|
73
|
+
export type Changelog = {
|
|
74
|
+
/** The branch the change was compared with. */
|
|
75
|
+
baseBranch: string;
|
|
76
|
+
/** False when there was nothing to compare with; every list is then empty. */
|
|
77
|
+
compared: boolean;
|
|
78
|
+
namespacesAdded: string[];
|
|
79
|
+
namespacesRemoved: string[];
|
|
80
|
+
recipesAdded: RecipeEntry[];
|
|
81
|
+
recipesRemoved: RecipeEntry[];
|
|
82
|
+
versionChanges: VersionChange[];
|
|
83
|
+
unraised: UnraisedChange[];
|
|
84
|
+
variables: VariableChange[];
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Reads the repo manifest and every recipe manifest it lists as they stood at
|
|
89
|
+
* one commit. Returns undefined when that commit holds no repo manifest sous
|
|
90
|
+
* can read; a recipe manifest it cannot read is left out rather than failing
|
|
91
|
+
* the whole comparison.
|
|
92
|
+
*
|
|
93
|
+
* @param rootDir - The repository's root directory.
|
|
94
|
+
* @param commit - The commit to read at.
|
|
95
|
+
* @param options - The command runner to use.
|
|
96
|
+
*/
|
|
97
|
+
export async function readManifestsAt(
|
|
98
|
+
rootDir: string,
|
|
99
|
+
commit: string,
|
|
100
|
+
options: RunOptions = {}
|
|
101
|
+
): Promise<ManifestSnapshot | undefined> {
|
|
102
|
+
const repoRaw = await readManifestAt(rootDir, commit, "", REPO_MANIFEST_BASENAME, options);
|
|
103
|
+
if (repoRaw === undefined) return undefined;
|
|
104
|
+
|
|
105
|
+
let repo: RepoManifest;
|
|
106
|
+
try {
|
|
107
|
+
repo = parseRepoManifest(repoRaw.value, `${repoRaw.file} at ${commit.slice(0, 12)}`);
|
|
108
|
+
} catch {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const recipes = new Map<string, { path: string; manifest: RecipeManifest }>();
|
|
113
|
+
for (const recipePath of repo.recipes) {
|
|
114
|
+
const raw = await readManifestAt(rootDir, commit, recipePath, RECIPE_MANIFEST_BASENAME, options);
|
|
115
|
+
if (raw === undefined) continue;
|
|
116
|
+
try {
|
|
117
|
+
const manifest = parseRecipeManifest(raw.value, `${raw.file} at ${commit.slice(0, 12)}`);
|
|
118
|
+
recipes.set(recipeManifestKey(manifest), { path: recipePath, manifest });
|
|
119
|
+
} catch {
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return { repo, recipes };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The manifests the working tree holds, in the same shape a commit's are read
|
|
129
|
+
* into, so the two can be compared.
|
|
130
|
+
*
|
|
131
|
+
* @param validation - The validated repository.
|
|
132
|
+
*/
|
|
133
|
+
export function snapshotOf(validation: RepoValidation): ManifestSnapshot {
|
|
134
|
+
const recipes = new Map<string, { path: string; manifest: RecipeManifest }>();
|
|
135
|
+
for (const recipe of validation.recipes) {
|
|
136
|
+
recipes.set(recipe.key, { path: recipe.path, manifest: recipe.manifest });
|
|
137
|
+
}
|
|
138
|
+
return { repo: validation.manifest, recipes };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Compares the manifests a change carries with the ones on the default branch.
|
|
143
|
+
*
|
|
144
|
+
* @param input.baseBranch - The branch the change is compared with, for the record.
|
|
145
|
+
* @param input.base - The manifests on that branch, or undefined when there was nothing to read.
|
|
146
|
+
* @param input.head - The manifests the change carries.
|
|
147
|
+
* @param input.changedPaths - Every path the change touched, relative to the repository root.
|
|
148
|
+
*/
|
|
149
|
+
export function buildChangelog(input: {
|
|
150
|
+
baseBranch: string;
|
|
151
|
+
base: ManifestSnapshot | undefined;
|
|
152
|
+
head: ManifestSnapshot;
|
|
153
|
+
changedPaths: ReadonlyArray<string>;
|
|
154
|
+
}): Changelog {
|
|
155
|
+
const { baseBranch, base, head, changedPaths } = input;
|
|
156
|
+
const changelog: Changelog = {
|
|
157
|
+
baseBranch,
|
|
158
|
+
compared: base !== undefined,
|
|
159
|
+
namespacesAdded: [],
|
|
160
|
+
namespacesRemoved: [],
|
|
161
|
+
recipesAdded: [],
|
|
162
|
+
recipesRemoved: [],
|
|
163
|
+
versionChanges: [],
|
|
164
|
+
unraised: [],
|
|
165
|
+
variables: [],
|
|
166
|
+
};
|
|
167
|
+
if (base === undefined) return changelog;
|
|
168
|
+
|
|
169
|
+
const baseNamespaces = Object.keys(base.repo.namespaces);
|
|
170
|
+
const headNamespaces = Object.keys(head.repo.namespaces);
|
|
171
|
+
changelog.namespacesAdded = headNamespaces.filter((name) => !baseNamespaces.includes(name)).sort();
|
|
172
|
+
changelog.namespacesRemoved = baseNamespaces
|
|
173
|
+
.filter((name) => !headNamespaces.includes(name))
|
|
174
|
+
.sort();
|
|
175
|
+
|
|
176
|
+
for (const [key, entry] of sortedEntries(head.recipes)) {
|
|
177
|
+
const before = base.recipes.get(key);
|
|
178
|
+
if (before === undefined) {
|
|
179
|
+
changelog.recipesAdded.push({ key, version: entry.manifest.version });
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const from = before.manifest.version;
|
|
184
|
+
const to = entry.manifest.version;
|
|
185
|
+
if (from !== to) {
|
|
186
|
+
changelog.versionChanges.push({ key, from, to });
|
|
187
|
+
} else if (pathsInside(entry.path, changedPaths).length > 0) {
|
|
188
|
+
changelog.unraised.push({ key, version: to, next: semver.inc(to, "patch") ?? to });
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
changelog.variables.push(
|
|
192
|
+
...compareVariables(key, before.manifest.variables ?? [], entry.manifest.variables ?? [])
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
for (const [key, entry] of sortedEntries(base.recipes)) {
|
|
197
|
+
if (!head.recipes.has(key)) {
|
|
198
|
+
changelog.recipesRemoved.push({ key, version: entry.manifest.version });
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return changelog;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** True when the changelog found nothing that merging would change. */
|
|
206
|
+
export function changelogIsEmpty(changelog: Changelog): boolean {
|
|
207
|
+
return (
|
|
208
|
+
changelog.namespacesAdded.length === 0 &&
|
|
209
|
+
changelog.namespacesRemoved.length === 0 &&
|
|
210
|
+
changelog.recipesAdded.length === 0 &&
|
|
211
|
+
changelog.recipesRemoved.length === 0 &&
|
|
212
|
+
changelog.versionChanges.length === 0 &&
|
|
213
|
+
changelog.unraised.length === 0 &&
|
|
214
|
+
changelog.variables.length === 0
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** The warning a breaking variable change carries. */
|
|
219
|
+
export const BREAKING_VARIABLE_WARNING =
|
|
220
|
+
"Removing a variable or tightening its validation is usually a major change: a " +
|
|
221
|
+
"subscriber's stored answer may no longer be read, or no longer be accepted.";
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* The changelog as Markdown, the way it is appended to a proposal's body and a
|
|
225
|
+
* commit message. Every section is left out when it has nothing to say.
|
|
226
|
+
*
|
|
227
|
+
* @param changelog - The comparison to describe.
|
|
228
|
+
*/
|
|
229
|
+
export function renderChangelog(changelog: Changelog): string {
|
|
230
|
+
const lines = ["## What merging this changes", ""];
|
|
231
|
+
|
|
232
|
+
if (!changelog.compared) {
|
|
233
|
+
lines.push(
|
|
234
|
+
`Sous could not compare this change with the branch '${changelog.baseBranch}', because ` +
|
|
235
|
+
"this checkout holds no copy of it, so no changelog was generated."
|
|
236
|
+
);
|
|
237
|
+
return lines.join("\n");
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
lines.push(
|
|
241
|
+
`Generated by \`sous repo submit\` from the manifests this change carries, compared with ` +
|
|
242
|
+
`the branch '${changelog.baseBranch}'.`
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
if (changelogIsEmpty(changelog)) {
|
|
246
|
+
lines.push("", "Merging changes no recipe, namespace, version or variable.");
|
|
247
|
+
return lines.join("\n");
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const section = (title: string, entries: string[]) => {
|
|
251
|
+
if (entries.length === 0) return;
|
|
252
|
+
lines.push("", `**${title}**`, "", ...entries.map((entry) => `- ${entry}`));
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
section(
|
|
256
|
+
"Recipes added",
|
|
257
|
+
changelog.recipesAdded.map((entry) => `\`${entry.key}\`, at version ${entry.version}`)
|
|
258
|
+
);
|
|
259
|
+
section(
|
|
260
|
+
"Recipes retired",
|
|
261
|
+
changelog.recipesRemoved.map(
|
|
262
|
+
(entry) => `\`${entry.key}\`, last at version ${entry.version}`
|
|
263
|
+
)
|
|
264
|
+
);
|
|
265
|
+
section(
|
|
266
|
+
"Version changes",
|
|
267
|
+
changelog.versionChanges.map(
|
|
268
|
+
(entry) => `\`${entry.key}\`: ${entry.from} becomes ${entry.to}`
|
|
269
|
+
)
|
|
270
|
+
);
|
|
271
|
+
section(
|
|
272
|
+
"Changed without a version raise",
|
|
273
|
+
changelog.unraised.map(
|
|
274
|
+
(entry) =>
|
|
275
|
+
`\`${entry.key}\`: its files changed and its version is still ${entry.version}, so ` +
|
|
276
|
+
`merging releases it as ${entry.next}`
|
|
277
|
+
)
|
|
278
|
+
);
|
|
279
|
+
section("Namespaces added", changelog.namespacesAdded.map((name) => `\`${name}\``));
|
|
280
|
+
section("Namespaces removed", changelog.namespacesRemoved.map((name) => `\`${name}\``));
|
|
281
|
+
section("Variables", changelog.variables.map(describeVariableChange));
|
|
282
|
+
|
|
283
|
+
if (changelog.variables.some((entry) => entry.breaking)) {
|
|
284
|
+
lines.push("", `**Warning:** ${BREAKING_VARIABLE_WARNING}`);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return lines.join("\n");
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Composes a proposal's body: the contributor's own description, then the
|
|
292
|
+
* changelog sous generated.
|
|
293
|
+
*
|
|
294
|
+
* @param description - What the contributor wrote.
|
|
295
|
+
* @param changelog - The comparison to append.
|
|
296
|
+
*/
|
|
297
|
+
export function composeProposalBody(description: string, changelog: Changelog): string {
|
|
298
|
+
return `${description.trim()}\n\n${renderChangelog(changelog)}\n`;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// --- Variables ----------------------------------------------------------------------------------
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Compares one recipe's variable definitions before and after a change.
|
|
305
|
+
*
|
|
306
|
+
* @param recipe - The recipe key.
|
|
307
|
+
* @param before - The definitions on the default branch.
|
|
308
|
+
* @param after - The definitions the change carries.
|
|
309
|
+
*/
|
|
310
|
+
export function compareVariables(
|
|
311
|
+
recipe: string,
|
|
312
|
+
before: ReadonlyArray<VariableDefinition>,
|
|
313
|
+
after: ReadonlyArray<VariableDefinition>
|
|
314
|
+
): VariableChange[] {
|
|
315
|
+
const changes: VariableChange[] = [];
|
|
316
|
+
const beforeByName = new Map(before.map((definition) => [definition.name, definition]));
|
|
317
|
+
const afterByName = new Map(after.map((definition) => [definition.name, definition]));
|
|
318
|
+
|
|
319
|
+
for (const definition of after) {
|
|
320
|
+
const previous = beforeByName.get(definition.name);
|
|
321
|
+
if (previous === undefined) {
|
|
322
|
+
changes.push({ recipe, name: definition.name, change: "added", breaking: false });
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
const fields = changedFields(previous, definition);
|
|
326
|
+
if (fields.length === 0) continue;
|
|
327
|
+
changes.push({
|
|
328
|
+
recipe,
|
|
329
|
+
name: definition.name,
|
|
330
|
+
change: "changed",
|
|
331
|
+
fields,
|
|
332
|
+
breaking: isTightened(previous, definition),
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
for (const definition of before) {
|
|
337
|
+
if (!afterByName.has(definition.name)) {
|
|
338
|
+
changes.push({ recipe, name: definition.name, change: "removed", breaking: true });
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return changes;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* True when a changed definition accepts less than it did: a different type, a
|
|
347
|
+
* newly required answer, or a validation rule that rejects something the old
|
|
348
|
+
* one accepted. A changed pattern counts, since sous cannot prove one regular
|
|
349
|
+
* expression accepts everything another did.
|
|
350
|
+
*
|
|
351
|
+
* isTightened({ ..., validate: { maxLength: 10 } }, { ..., validate: { maxLength: 5 } });
|
|
352
|
+
* // -> true
|
|
353
|
+
*
|
|
354
|
+
* @param before - The definition on the default branch.
|
|
355
|
+
* @param after - The definition the change carries.
|
|
356
|
+
*/
|
|
357
|
+
export function isTightened(before: VariableDefinition, after: VariableDefinition): boolean {
|
|
358
|
+
if (before.type !== after.type) return true;
|
|
359
|
+
if (!before.required && after.required) return true;
|
|
360
|
+
|
|
361
|
+
const was = before.validate ?? {};
|
|
362
|
+
const now = after.validate ?? {};
|
|
363
|
+
|
|
364
|
+
if (now.pattern !== undefined && now.pattern !== was.pattern) return true;
|
|
365
|
+
if (raised(was.minLength, now.minLength)) return true;
|
|
366
|
+
if (lowered(was.maxLength, now.maxLength)) return true;
|
|
367
|
+
if (raised(was.min, now.min)) return true;
|
|
368
|
+
if (lowered(was.max, now.max)) return true;
|
|
369
|
+
if (now.enum !== undefined) {
|
|
370
|
+
if (was.enum === undefined) return true;
|
|
371
|
+
if (was.enum.some((option) => !now.enum!.includes(option))) return true;
|
|
372
|
+
}
|
|
373
|
+
return false;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** True when a lower bound was added or moved up. */
|
|
377
|
+
function raised(was: number | undefined, now: number | undefined): boolean {
|
|
378
|
+
return now !== undefined && (was === undefined || now > was);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** True when an upper bound was added or moved down. */
|
|
382
|
+
function lowered(was: number | undefined, now: number | undefined): boolean {
|
|
383
|
+
return now !== undefined && (was === undefined || now < was);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/** The top-level fields of a definition that differ, by name, in a stable order. */
|
|
387
|
+
function changedFields(before: VariableDefinition, after: VariableDefinition): string[] {
|
|
388
|
+
const keys = new Set([...Object.keys(before), ...Object.keys(after)]);
|
|
389
|
+
const fields: string[] = [];
|
|
390
|
+
for (const key of [...keys].sort()) {
|
|
391
|
+
const was = (before as Record<string, unknown>)[key];
|
|
392
|
+
const now = (after as Record<string, unknown>)[key];
|
|
393
|
+
if (JSON.stringify(was) !== JSON.stringify(now)) fields.push(key);
|
|
394
|
+
}
|
|
395
|
+
return fields;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/** One variable change, as a changelog line. */
|
|
399
|
+
function describeVariableChange(entry: VariableChange): string {
|
|
400
|
+
const subject = `\`${entry.recipe}\`: the variable \`${entry.name}\``;
|
|
401
|
+
if (entry.change === "added") return `${subject} was added`;
|
|
402
|
+
if (entry.change === "removed") return `${subject} was removed (usually a major change)`;
|
|
403
|
+
const fields = (entry.fields ?? []).map((field) => `\`${field}\``).join(", ");
|
|
404
|
+
return (
|
|
405
|
+
`${subject} changed its ${fields}` +
|
|
406
|
+
(entry.breaking ? " and now accepts less than before (usually a major change)" : "")
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// --- Reading at a commit ------------------------------------------------------------------------
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Reads one manifest at a commit, trying each supported file name in turn.
|
|
414
|
+
*
|
|
415
|
+
* @param rootDir - The repository's root directory.
|
|
416
|
+
* @param commit - The commit to read at.
|
|
417
|
+
* @param folder - The folder holding it, relative to the repository root ("" for the root).
|
|
418
|
+
* @param basename - The manifest's name without an extension.
|
|
419
|
+
* @param options - The command runner to use.
|
|
420
|
+
*/
|
|
421
|
+
async function readManifestAt(
|
|
422
|
+
rootDir: string,
|
|
423
|
+
commit: string,
|
|
424
|
+
folder: string,
|
|
425
|
+
basename: string,
|
|
426
|
+
options: RunOptions
|
|
427
|
+
): Promise<{ file: string; value: unknown } | undefined> {
|
|
428
|
+
for (const extension of MANIFEST_EXTENSIONS) {
|
|
429
|
+
const file = folder.length === 0 ? `${basename}${extension}` : path.posix.join(folder, `${basename}${extension}`);
|
|
430
|
+
const text = await readFileAtTag(rootDir, commit, file, options);
|
|
431
|
+
if (text === undefined) continue;
|
|
432
|
+
try {
|
|
433
|
+
const value =
|
|
434
|
+
extension === ".yaml" || extension === ".yml"
|
|
435
|
+
? parseYamlText(text, file)
|
|
436
|
+
: parseJsoncText(text, file);
|
|
437
|
+
return { file, value };
|
|
438
|
+
} catch {
|
|
439
|
+
return undefined;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return undefined;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/** A map's entries, sorted by key, so the changelog reads the same every time. */
|
|
446
|
+
function sortedEntries<T>(map: Map<string, T>): Array<[string, T]> {
|
|
447
|
+
return [...map.entries()].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
448
|
+
}
|
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
* branch is checked out, which branch is the default one, and where does
|
|
7
7
|
* `origin` point. A release makes exactly one commit of its own (its version
|
|
8
8
|
* bumps and its index, through `commitPaths`) and refuses while anything else
|
|
9
|
-
* is uncommitted;
|
|
10
|
-
* to
|
|
9
|
+
* is uncommitted; a submission commits only when `--commit` asks it to
|
|
10
|
+
* (`commitEverything`). Everything else here is read to refuse politely rather
|
|
11
|
+
* than to fix anything.
|
|
11
12
|
*
|
|
12
13
|
* Every function takes the injectable command runner, so tests never spawn git
|
|
13
14
|
* unless they mean to.
|
|
@@ -240,10 +241,11 @@ export async function hasCommitIdentity(
|
|
|
240
241
|
/**
|
|
241
242
|
* Stages exactly the given paths and commits them.
|
|
242
243
|
*
|
|
243
|
-
* This is
|
|
244
|
+
* This is how `sous repo release` commits on an author's behalf, and it is
|
|
244
245
|
* deliberately narrow: a release writes version bumps and an index, and those
|
|
245
246
|
* are the only paths it stages. Anything else in the working tree is left
|
|
246
|
-
* exactly as it was.
|
|
247
|
+
* exactly as it was. The only other commit sous makes is `commitEverything`,
|
|
248
|
+
* for `sous repo submit --commit`.
|
|
247
249
|
*
|
|
248
250
|
* @param rootDir - The repository's root directory.
|
|
249
251
|
* @param paths - The paths to stage, relative to the repository root.
|
|
@@ -300,8 +302,18 @@ export async function createBranch(
|
|
|
300
302
|
await runGit(["checkout", "-b", branch], { cwd: rootDir, run: options.run });
|
|
301
303
|
}
|
|
302
304
|
|
|
305
|
+
/** What a push did: sent new commits, or found the remote already had them. */
|
|
306
|
+
export type PushOutcome = "updated" | "up-to-date";
|
|
307
|
+
|
|
303
308
|
/**
|
|
304
|
-
* Pushes one branch to a remote, setting it as the branch's upstream
|
|
309
|
+
* Pushes one branch to a remote, setting it as the branch's upstream, and says
|
|
310
|
+
* whether anything was sent.
|
|
311
|
+
*
|
|
312
|
+
* The push is never forced. When the remote branch holds commits the local one
|
|
313
|
+
* lacks, git refuses, and its own explanation is what the caller receives: git
|
|
314
|
+
* is the authority on whether a push is safe, so nothing here second-guesses it.
|
|
315
|
+
* Whether anything was sent is read from git's machine-readable report, where a
|
|
316
|
+
* ref that was already current is flagged with `=`.
|
|
305
317
|
*
|
|
306
318
|
* @param rootDir - The repository's root directory.
|
|
307
319
|
* @param remote - The remote to push to.
|
|
@@ -313,36 +325,110 @@ export async function pushBranch(
|
|
|
313
325
|
remote: string,
|
|
314
326
|
branch: string,
|
|
315
327
|
options: RunOptions = {}
|
|
316
|
-
): Promise<
|
|
317
|
-
await runGit(["push", "--set-upstream", remote, branch], {
|
|
328
|
+
): Promise<PushOutcome> {
|
|
329
|
+
const report = await runGit(["push", "--porcelain", "--set-upstream", remote, branch], {
|
|
318
330
|
cwd: rootDir,
|
|
319
331
|
run: options.run,
|
|
320
332
|
});
|
|
333
|
+
return pushReportIsUpToDate(report) ? "up-to-date" : "updated";
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* True when git's machine-readable push report says every ref it pushed was
|
|
338
|
+
* already current on the remote.
|
|
339
|
+
*
|
|
340
|
+
* pushReportIsUpToDate("To origin\n=\trefs/heads/a:refs/heads/a\t[up to date]\nDone");
|
|
341
|
+
* // -> true
|
|
342
|
+
*
|
|
343
|
+
* @param report - What `git push --porcelain` printed.
|
|
344
|
+
*/
|
|
345
|
+
export function pushReportIsUpToDate(report: string): boolean {
|
|
346
|
+
const refLines = report.split("\n").filter((line) => /^[ +\-*!=]\t/.test(line));
|
|
347
|
+
return refLines.length > 0 && refLines.every((line) => line.startsWith("=\t"));
|
|
321
348
|
}
|
|
322
349
|
|
|
323
350
|
/**
|
|
324
|
-
*
|
|
325
|
-
* It is the default title for a proposed change, which is what a contributor
|
|
326
|
-
* would have typed anyway.
|
|
351
|
+
* True when a local branch of that name exists.
|
|
327
352
|
*
|
|
328
353
|
* @param rootDir - The repository's root directory.
|
|
354
|
+
* @param branch - The branch name.
|
|
329
355
|
* @param options - The command runner to use.
|
|
330
356
|
*/
|
|
331
|
-
export async function
|
|
357
|
+
export async function branchExists(
|
|
332
358
|
rootDir: string,
|
|
359
|
+
branch: string,
|
|
333
360
|
options: RunOptions = {}
|
|
334
|
-
): Promise<
|
|
361
|
+
): Promise<boolean> {
|
|
335
362
|
try {
|
|
336
|
-
|
|
363
|
+
await runGit(["rev-parse", "--verify", "--quiet", `refs/heads/${branch}`], {
|
|
337
364
|
cwd: rootDir,
|
|
338
365
|
run: options.run,
|
|
339
366
|
});
|
|
340
|
-
return
|
|
367
|
+
return true;
|
|
341
368
|
} catch {
|
|
342
|
-
return
|
|
369
|
+
return false;
|
|
343
370
|
}
|
|
344
371
|
}
|
|
345
372
|
|
|
373
|
+
/**
|
|
374
|
+
* Checks out an existing branch. Git refuses when uncommitted changes would be
|
|
375
|
+
* overwritten, and that refusal reaches the caller unchanged.
|
|
376
|
+
*
|
|
377
|
+
* @param rootDir - The repository's root directory.
|
|
378
|
+
* @param branch - The branch to check out.
|
|
379
|
+
* @param options - The command runner to use.
|
|
380
|
+
*/
|
|
381
|
+
export async function switchBranch(
|
|
382
|
+
rootDir: string,
|
|
383
|
+
branch: string,
|
|
384
|
+
options: RunOptions = {}
|
|
385
|
+
): Promise<void> {
|
|
386
|
+
await runGit(["switch", branch], { cwd: rootDir, run: options.run });
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Every path the commits since `since` changed, comparing that commit with HEAD.
|
|
391
|
+
*
|
|
392
|
+
* @param rootDir - The repository's root directory.
|
|
393
|
+
* @param since - The commit to compare HEAD with.
|
|
394
|
+
* @param options - The command runner to use.
|
|
395
|
+
*/
|
|
396
|
+
export async function pathsChangedSince(
|
|
397
|
+
rootDir: string,
|
|
398
|
+
since: string,
|
|
399
|
+
options: RunOptions = {}
|
|
400
|
+
): Promise<string[]> {
|
|
401
|
+
const changed = await runGit(["diff", "--name-only", since, "HEAD"], {
|
|
402
|
+
cwd: rootDir,
|
|
403
|
+
run: options.run,
|
|
404
|
+
});
|
|
405
|
+
return changed.length === 0 ? [] : changed.split("\n").filter((line) => line.length > 0);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Stages everything the working tree holds (edits, deletions and untracked
|
|
410
|
+
* files alike) and commits it with the given message.
|
|
411
|
+
*
|
|
412
|
+
* This is the second place sous commits on an author's behalf, and it runs only
|
|
413
|
+
* for `sous repo submit --commit`, after the contributor has seen every path it
|
|
414
|
+
* stages and agreed to it.
|
|
415
|
+
*
|
|
416
|
+
* @param rootDir - The repository's root directory.
|
|
417
|
+
* @param message - The commit message.
|
|
418
|
+
* @param options - The command runner to use.
|
|
419
|
+
*/
|
|
420
|
+
export async function commitEverything(
|
|
421
|
+
rootDir: string,
|
|
422
|
+
message: string,
|
|
423
|
+
options: RunOptions = {}
|
|
424
|
+
): Promise<void> {
|
|
425
|
+
await runGit(["add", "--all"], { cwd: rootDir, run: options.run });
|
|
426
|
+
await runGit(["commit", "--quiet", "--message", message], {
|
|
427
|
+
cwd: rootDir,
|
|
428
|
+
run: options.run,
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
|
|
346
432
|
/**
|
|
347
433
|
* The branch name sous proposes for a submission, stamped with the minute it
|
|
348
434
|
* was made so two submissions from one checkout never collide.
|