@jonmilner/local-ci 0.14.1 → 0.16.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 +4 -2
- package/dist/cli.js +130 -15
- package/dist/config.d.ts +10 -2
- package/dist/config.js +27 -3
- package/dist/push.d.ts +11 -0
- package/dist/push.js +21 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,7 +21,7 @@ That writes `pre-push` and `commit-msg` shims, each one line that calls local-ci
|
|
|
21
21
|
## Commands
|
|
22
22
|
|
|
23
23
|
- `local-ci hooks install`: writes executable `#!/bin/sh` `pre-push` and `commit-msg` shims into the hooks directory: `core.hooksPath`, resolved against the repo root, with a Vite+ (or husky) `<dir>/_` dispatcher mapped to `<dir>`, and an absolute path into the main checkout mapped onto the current worktree. Each shim is one line: `pnpm exec local-ci pre-push "$@"` and `pnpm exec local-ci commit-msg "$1"` in a repo with a `package.json`, a global `local-ci …` without one, or `"local-ci": { "exec": "<command>" }` in `package.json` in place of `pnpm exec local-ci` (this repo runs local-ci from source that way). An existing shell hook keeps its other lines and gains the shim line, at the end, only when no line already runs `local-ci pre-push` (or `commit-msg`), so a line with an env prefix such as `LOCAL_CI_COMMAND=… local-ci pre-push "$@"` counts. It prints each file it wrote, or `hooks up to date`; a second run changes nothing. It exits 1 without `core.hooksPath` or with one outside the repo's tracked files (including `.git/hooks`), and for an existing hook in another language (a `node` or `python` shebang), naming the line to add by hand. Run it by hand and commit the result; `prepare` does not run it.
|
|
24
|
-
- `local-ci pre-push`: the hook. It first checks that both hook files are in place, as `local-ci hooks install` would leave them, and refuses the push otherwise (``hooks out of date: <file> must run local-ci; run `<command> hooks install` and commit the result``), so a repo's hooks can't drift. For each commit pushed to a branch (deletes and tags are skipped) that has not already passed, runs the check command. The pushed commit must be `HEAD` with a clean working tree (`git status --porcelain` empty), so the result reflects the commit; otherwise the push is refused. A failing check blocks the push. When every commit passes, it starts `local-ci post <sha>` in the background, aimed at the pushed remote and told the pid of the `git` process doing the push (`LOCAL_CI_PUSH_PID`), and lets the push go ahead. For an ssh remote, it first warns when ssh to its host sends no keepalive (see [SSH keepalive](#ssh-keepalive)), and a run of 30 seconds or more ends by printing how long it ran and, for each pushed ref, the `git ls-remote <remote> <ref>` that should print the pushed sha.
|
|
24
|
+
- `local-ci pre-push`: the hook. It first checks that both hook files are in place, as `local-ci hooks install` would leave them, and refuses the push otherwise (``hooks out of date: <file> must run local-ci; run `<command> hooks install` and commit the result``), so a repo's hooks can't drift. Then, before any check runs, it refuses a push whose commit, pushed to a branch other than the remote's default, conflicts with that default branch's latest commit, since a check on a head that must still take a merge is wasted: it fetches the default branch (the remote's `refs/remotes/<remote>/HEAD`, else GitHub's `defaultBranchRef` for the pushed repo) into `refs/remotes/<remote>/<default>` and runs `git merge-tree --write-tree` on the two, with no checkout, and the refusal names each conflicting commit and file and the fix (`<short-sha> conflicts with origin/main in: <files>`, then merge with `git merge origin/main`, resolve, commit and push again). `LOCAL_CI_ALLOW_CONFLICT=1 git push` lets such a push through on purpose and runs the check as usual, so `--no-verify` stays for emergencies. Pushes to the default branch, tags and deletes skip this probe, and one that can't run (a push to a URL rather than a named remote, no default branch found, a failed fetch, a `git` command still running after 30 seconds, a `merge-tree` error such as unrelated histories; git's own credential prompt is turned off, and the timeout caps any ssh prompt) prints `local-ci: <why>; skipping the conflict check` and lets the push go on. For each commit pushed to a branch (deletes and tags are skipped) that has not already passed, runs the check command. The pushed commit must be `HEAD` with a clean working tree (`git status --porcelain` empty), so the result reflects the commit; otherwise the push is refused. A failing check blocks the push. When every commit passes, it starts `local-ci post <sha>` in the background, aimed at the pushed remote and told the pid of the `git` process doing the push (`LOCAL_CI_PUSH_PID`), and lets the push go ahead. For an ssh remote, it first warns when ssh to its host sends no keepalive (see [SSH keepalive](#ssh-keepalive)), and a run of 30 seconds or more ends by printing how long it ran and, for each pushed ref, the `git ls-remote <remote> <ref>` that should print the pushed sha.
|
|
25
25
|
- `local-ci commit-msg <file>`: lints the commit message in `<file>` with `@jonmilner/commitlint-config`, for a git `commit-msg` hook (git passes `.git/COMMIT_EDITMSG`). `local-ci commit-msg -` reads the message from stdin, for PR titles. Comment lines (starting with `#`) and everything after a `# ------------------------ >8 ------------------------` scissors line are stripped first; merge commits, `fixup!`/`squash!` and reverts are skipped. Each error or warning goes to stderr as `commit-msg: <message> [<rule>]`, followed by the offending header; it exits 1 when there are errors (warnings alone exit 0), and 2 when no file is given. It makes no network, git or GitHub calls.
|
|
26
26
|
- `local-ci post <ref>`: resolves `<ref>` (a full or short sha, `HEAD`, a branch) to a local commit's full sha, failing with `<ref> is not a commit in this repo` when it names none, and with `<sha> has no passing check` when that commit never passed. When started by `pre-push`, first waits for the pushing `git` to exit (polling each second, up to 60 minutes, logging if it gives up), since git sends the push only after the whole hook exits, so a slow step after local-ci in the hook can't run out the clock; run by hand, it skips that wait. It then waits up to 3 minutes for GitHub to have the commit, posts status `check` = `success` (`<command> passed on <hostname>`), then posts `pr-title` on it for an open PR that contains it (waiting up to 30s for GitHub to link them). A PR opened after the push, or a retitled one, needs `local-ci pr-title` run by hand.
|
|
27
27
|
- `local-ci pr-title [<pr>]`: lints the PR title (the current branch's PR by default) with `@jonmilner/commitlint-config` and posts status `pr-title` on its head commit, `failure` with the first error when the title does not pass.
|
|
@@ -72,7 +72,9 @@ Host github.com
|
|
|
72
72
|
|
|
73
73
|
Logs, the record of passed commits and the release lock live in `~/.local/state/local-ci/<repo>.log`, `<repo>.passed` and `<repo>.release.lock` (`$XDG_STATE_HOME` if set). A lock whose process has died is taken over.
|
|
74
74
|
|
|
75
|
-
|
|
75
|
+
A pass is recorded by commit and by tree: `<repo>.passed` gets a `<sha>\t<command>` line and a `tree:<tree>\t<command as JSON>` line (`git rev-parse <sha>^{tree}`; JSON keeps a command over several lines on one line). A commit that has not passed but whose tree passed under the same check command (a squash or merge commit with the PR head's files, or a reworded commit) counts as passed without running the check: `pre-push` and `status` skip it, record a pass for its sha, and print and log `<short-sha>: <command> already passed on tree <short-tree>; reused without running`. A different tree or command runs the check. Unlike a commit record, a tree record counts only for a clean `HEAD`, so the command it matches is the one that commit's `package.json` names. It assumes the check reads only the commit's files: a check that reads the commit message or history (a commitlint of `HEAD`, a diff from a merge base) can pass on one commit and fail on another with the same tree, and its result is reused anyway. Records written before tree lines existed still match their sha, but no tree.
|
|
76
|
+
|
|
77
|
+
One check command runs at a time on the machine, across every repo, so overlapping checks can't starve each other's tests: `pre-push`, `status`, the `status` runs `renovate` starts, and `local-ci run` each take `check.lock` in the same directory around the command they run (not the status posts). Nothing else takes it: a bare `pnpm check` or `pnpm test` runs beside every other check, so start a full check by hand with `local-ci run -- pnpm check`. The lock only runs checks one at a time; it does not wait for memory pressure or swap to clear. A run that finds it held prints `local-ci: waiting for the check lock, held by pid <pid> (<repo> <short-sha>)` (`<repo> <command>` for `local-ci run`, with the command cut to 60 characters), again whenever another run takes it first, and polls every second until the holder exits or dies; Ctrl-C stops the wait. Checks don't queue, so the next one to poll after a release goes first; background jobs wait in arrival order (see [below](#taking-the-check-lock-from-your-own-scripts)). After the wait it skips a commit that passed meanwhile and re-applies the clean-tree rule. The lock goes when the check passes, fails or is killed by a signal. Ctrl-C, SIGTERM, SIGHUP or SIGQUIT (Ctrl-\\) to local-ci passes the signal to the check's whole process group (so a check script's children get it too), waits for the check to exit, then releases the lock and dies of the signal, so a check never outlives its lock. A lock whose holder died (SIGKILL, a crash) or that predates the last boot is taken over by the next run. A local-ci check nested inside a check (one that pushes, say) sees its ancestor holds the lock and runs without waiting. A check that finds a background job's lock (see below) runs beside the job instead of waiting and leaves that lock alone, printing `local-ci: running beside background job pid <pid> (<holder>), without the check lock`; it holds `check.lock.beside` meanwhile, so another check waits for it rather than overlapping, and signals still stop it and release that lock as above. Each check run appends its duration and outcome to the log when it ends, e.g. `[<end time>] abc1234: pnpm check passed in 71.2s` (or `failed in`, or `did not finish after` when the check is killed by a signal or can't start); a commit that already passed is skipped and logs nothing (one that reuses a pass on its tree logs that instead, see above). Ctrl-C stops the check, which logs `did not finish after`.
|
|
76
78
|
|
|
77
79
|
### Taking the check lock from your own scripts
|
|
78
80
|
|
package/dist/cli.js
CHANGED
|
@@ -6,14 +6,14 @@ import { basename, dirname, join, relative, resolve } from "node:path";
|
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
7
7
|
import { heldCheckLock, lockCheck, withCheckLock } from "./check-lock.js";
|
|
8
8
|
import { commitMsg } from "./commit-msg.js";
|
|
9
|
-
import { appendLog, checkRunMessage, logLine, passedCommand, recordPass, releaseEnabled, resolveCommand, shimCommand, statePaths, } from "./config.js";
|
|
9
|
+
import { appendLog, checkRunMessage, logLine, passedCommand, recordPass, releaseEnabled, resolveCommand, shimCommand, statePaths, treePassed, treeReuseMessage, } from "./config.js";
|
|
10
10
|
import { exec, run } from "./exec.js";
|
|
11
11
|
import { assertGhAuth, checkStatus, postStatus, prTitleStatus, viewPr, waitForCommit, waitForOpenPr, } from "./github.js";
|
|
12
12
|
import { hooksDir, installHooks, missingHooks } from "./hooks.js";
|
|
13
13
|
import { installRenovateAgent, renovateLog, rotateLog, uninstallRenovateAgent } from "./launchd.js";
|
|
14
14
|
import { waitForExit } from "./lock.js";
|
|
15
15
|
import { processTable } from "./processes.js";
|
|
16
|
-
import { cannotCheckInPlace, githubRepo, nearestGit, parsePrePush, shasToCheck, slowHookWarning, } from "./push.js";
|
|
16
|
+
import { ALLOW_CONFLICT, cannotCheckInPlace, conflictedFiles, conflictMessage, githubRepo, nearestGit, parsePrePush, shasToCheck, shasToProbe, slowHookWarning, } from "./push.js";
|
|
17
17
|
import { release, releaseCheck, takeEnvToken } from "./release.js";
|
|
18
18
|
import { projectsDir, renovate } from "./renovate.js";
|
|
19
19
|
import { configuredSshCommand, keepaliveWarning, sshHost } from "./ssh.js";
|
|
@@ -81,19 +81,50 @@ async function assertCheckable(sha) {
|
|
|
81
81
|
async function runCheck(current, sha) {
|
|
82
82
|
if (passedCommand(current.passed, sha))
|
|
83
83
|
return;
|
|
84
|
+
// A tree reuse waits for the clean-tree rule too, so the command it matches is the one `sha`'s
|
|
85
|
+
// own package.json names.
|
|
84
86
|
await assertCheckable(sha);
|
|
87
|
+
const tree = await git("rev-parse", `${sha}^{tree}`);
|
|
88
|
+
if (reuseTreePass(current, sha, tree))
|
|
89
|
+
return;
|
|
85
90
|
const unlock = await lockCheck(`${current.name} ${sha.slice(0, 7)}`, { file: current.checkLock });
|
|
86
91
|
try {
|
|
87
|
-
// Another run may have passed it, or the tree changed, while this one waited.
|
|
92
|
+
// Another run may have passed it, or the working tree changed, while this one waited.
|
|
88
93
|
if (passedCommand(current.passed, sha))
|
|
89
94
|
return;
|
|
90
95
|
await assertCheckable(sha);
|
|
91
|
-
|
|
96
|
+
if (reuseTreePass(current, sha, tree))
|
|
97
|
+
return;
|
|
98
|
+
await runLocked(current, sha, tree);
|
|
92
99
|
}
|
|
93
100
|
finally {
|
|
94
101
|
unlock();
|
|
95
102
|
}
|
|
96
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* When the check command already passed on another commit with the same `tree` (a merge commit
|
|
106
|
+
* with the PR head's files, say), records a pass for `sha`, so `post` finds it, logs the reuse and
|
|
107
|
+
* returns true.
|
|
108
|
+
*/
|
|
109
|
+
function reuseTreePass({ root, passed, log: logFile }, sha, tree) {
|
|
110
|
+
const command = resolveCommand(root);
|
|
111
|
+
if (!treePassed(passed, tree, command))
|
|
112
|
+
return false;
|
|
113
|
+
recordPass(passed, sha, command);
|
|
114
|
+
const message = treeReuseMessage(sha, command, tree);
|
|
115
|
+
err(`local-ci: ${message}`);
|
|
116
|
+
logQuietly(logFile, message);
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
/** Appends to the repo's log; a log that can't be written must not change the check's outcome. */
|
|
120
|
+
function logQuietly(file, message) {
|
|
121
|
+
try {
|
|
122
|
+
appendLog(file, message);
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
err(`local-ci: could not log "${message}": ${error instanceof Error ? error.message : String(error)}`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
97
128
|
/** Runs a program under the check lock, attached to it so a signal to local-ci reaches it. */
|
|
98
129
|
async function runHeld(file, args, cwd) {
|
|
99
130
|
const held = heldCheckLock();
|
|
@@ -115,7 +146,7 @@ async function runHeld(file, args, cwd) {
|
|
|
115
146
|
return { ...result, interrupted: held?.signal };
|
|
116
147
|
}
|
|
117
148
|
/** Runs the check command under the check lock, logging its duration and recording a pass. */
|
|
118
|
-
async function runLocked({ root, passed, log: logFile }, sha) {
|
|
149
|
+
async function runLocked({ root, passed, log: logFile }, sha, tree) {
|
|
119
150
|
const command = resolveCommand(root);
|
|
120
151
|
err(`local-ci: running ${command} on ${sha.slice(0, 7)}`);
|
|
121
152
|
const started = performance.now();
|
|
@@ -124,13 +155,7 @@ async function runLocked({ root, passed, log: logFile }, sha) {
|
|
|
124
155
|
const { interrupted } = result;
|
|
125
156
|
const status = interrupted ? null : result.status;
|
|
126
157
|
// Every run's duration goes in the log, so a faster check can be measured against a slower one.
|
|
127
|
-
|
|
128
|
-
try {
|
|
129
|
-
appendLog(logFile, checkRunMessage(sha, command, status, performance.now() - started));
|
|
130
|
-
}
|
|
131
|
-
catch (error) {
|
|
132
|
-
err(`local-ci: could not log the run: ${error instanceof Error ? error.message : String(error)}`);
|
|
133
|
-
}
|
|
158
|
+
logQuietly(logFile, checkRunMessage(sha, command, status, performance.now() - started));
|
|
134
159
|
// A spawn error or a signal means the check never finished, so it is not a failure to post.
|
|
135
160
|
if (status === null) {
|
|
136
161
|
const cause = interrupted ?? result.error ?? result.signal;
|
|
@@ -138,7 +163,7 @@ async function runLocked({ root, passed, log: logFile }, sha) {
|
|
|
138
163
|
}
|
|
139
164
|
if (status !== 0)
|
|
140
165
|
throw new CheckFailed(command, sha);
|
|
141
|
-
recordPass(passed, sha, command);
|
|
166
|
+
recordPass(passed, sha, command, tree);
|
|
142
167
|
}
|
|
143
168
|
/**
|
|
144
169
|
* Runs `-- <command>` under the check lock, so a full check started by hand waits its turn with
|
|
@@ -185,12 +210,104 @@ async function runHooks([subcommand, ...extra]) {
|
|
|
185
210
|
const changes = installHooks(root, dir, command);
|
|
186
211
|
out(changes.length ? changes.join("\n") : "hooks up to date");
|
|
187
212
|
}
|
|
213
|
+
/** How long a conflict-probe git command may run before the probe gives up and lets the push go on. */
|
|
214
|
+
const PROBE_TIMEOUT_MS = 30_000;
|
|
215
|
+
/**
|
|
216
|
+
* Runs git for the conflict probe, never prompting and never hanging the push: a credential prompt
|
|
217
|
+
* fails at once and a stalled fetch is killed after PROBE_TIMEOUT_MS.
|
|
218
|
+
*/
|
|
219
|
+
const probeGit = (args) => spawnSync("git", args, {
|
|
220
|
+
encoding: "utf8",
|
|
221
|
+
env: { ...process.env, GIT_TERMINAL_PROMPT: "0" },
|
|
222
|
+
maxBuffer: 16 * 1024 * 1024,
|
|
223
|
+
timeout: PROBE_TIMEOUT_MS,
|
|
224
|
+
});
|
|
225
|
+
/** Why a probe git command failed: its stderr, else the spawn error or signal, else its exit status. */
|
|
226
|
+
const failure = (result) => result.stderr?.trim() || result.error?.message || result.signal || `exit ${result.status}`;
|
|
227
|
+
const skipConflictCheck = (why) => err(`local-ci: ${why}; skipping the conflict check`);
|
|
228
|
+
/** The remote's default branch: its `HEAD` symref, else GitHub's for `ghRepo`, else undefined. */
|
|
229
|
+
async function defaultBranch(remote, ghRepo) {
|
|
230
|
+
const head = await git("symbolic-ref", "--quiet", "--short", `refs/remotes/${remote}/HEAD`).catch(() => "");
|
|
231
|
+
if (head.startsWith(`${remote}/`))
|
|
232
|
+
return head.slice(remote.length + 1);
|
|
233
|
+
if (!ghRepo)
|
|
234
|
+
return undefined;
|
|
235
|
+
const name = await exec("gh", [
|
|
236
|
+
"repo",
|
|
237
|
+
"view",
|
|
238
|
+
ghRepo,
|
|
239
|
+
"--json",
|
|
240
|
+
"defaultBranchRef",
|
|
241
|
+
"--jq",
|
|
242
|
+
".defaultBranchRef.name",
|
|
243
|
+
]).catch(() => "");
|
|
244
|
+
return name.trim() || undefined;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Refuses the push, naming the files and the fix, when a commit pushed to a branch other than the
|
|
248
|
+
* default conflicts with the remote's latest default branch, so no check runs on a head that has
|
|
249
|
+
* to change. A probe that can't run warns and lets the push go on.
|
|
250
|
+
*/
|
|
251
|
+
async function assertNoConflict(refs, shas, remote, ghRepo) {
|
|
252
|
+
if (process.env[ALLOW_CONFLICT] === "1" || !shas.length)
|
|
253
|
+
return;
|
|
254
|
+
const branch = await defaultBranch(remote, ghRepo);
|
|
255
|
+
if (!branch) {
|
|
256
|
+
skipConflictCheck(`could not find ${remote}'s default branch`);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
const probed = shasToProbe(refs, branch);
|
|
260
|
+
if (!probed.length)
|
|
261
|
+
return;
|
|
262
|
+
// A push to a URL has no `<remote>/<branch>` to merge, so the fix the refusal names can't work.
|
|
263
|
+
if (probeGit(["config", "--get", `remote.${remote}.url`]).status !== 0) {
|
|
264
|
+
skipConflictCheck(`${remote} is not a named remote`);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
// Into the tracking ref the refusal tells you to merge, whatever `remote.<name>.fetch` covers.
|
|
268
|
+
const tracking = `refs/remotes/${remote}/${branch}`;
|
|
269
|
+
const fetched = probeGit([
|
|
270
|
+
"fetch",
|
|
271
|
+
"--quiet",
|
|
272
|
+
"--no-tags",
|
|
273
|
+
remote,
|
|
274
|
+
`+refs/heads/${branch}:${tracking}`,
|
|
275
|
+
]);
|
|
276
|
+
if (fetched.status !== 0) {
|
|
277
|
+
skipConflictCheck(`could not fetch ${remote} ${branch} (${failure(fetched)})`);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
const base = resolveCommit(tracking);
|
|
281
|
+
const conflicts = [];
|
|
282
|
+
for (const sha of probed) {
|
|
283
|
+
const result = probeGit([
|
|
284
|
+
"merge-tree",
|
|
285
|
+
"--write-tree",
|
|
286
|
+
"--name-only",
|
|
287
|
+
"--no-messages",
|
|
288
|
+
sha,
|
|
289
|
+
base,
|
|
290
|
+
]);
|
|
291
|
+
// merge-tree exits 1 for a conflict; anything else but 0 means it could not merge at all.
|
|
292
|
+
if (result.status === 1)
|
|
293
|
+
conflicts.push({ sha, files: conflictedFiles(result.stdout) });
|
|
294
|
+
else if (result.status !== 0) {
|
|
295
|
+
skipConflictCheck(`git merge-tree failed on ${sha.slice(0, 7)} (${failure(result)})`);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (conflicts.length)
|
|
299
|
+
throw new Error(conflictMessage(remote, branch, conflicts));
|
|
300
|
+
}
|
|
188
301
|
async function prePush(remote, remoteUrl) {
|
|
189
302
|
const current = await repo();
|
|
190
303
|
await assertHooks(current.root);
|
|
191
304
|
await assertGhAuth(exec);
|
|
192
305
|
const refs = parsePrePush(readFileSync(0, "utf8"));
|
|
193
306
|
const shas = shasToCheck(refs);
|
|
307
|
+
// Post to the remote being pushed, not whichever one gh would pick.
|
|
308
|
+
const ghRepo = remoteUrl ? githubRepo(remoteUrl) : undefined;
|
|
309
|
+
// Before any check, since a check on a head that must still take a merge is wasted.
|
|
310
|
+
await assertNoConflict(refs, shas, remote ?? "origin", ghRepo);
|
|
194
311
|
// git holds an ssh connection open, idle, while this hook runs; an https push holds none.
|
|
195
312
|
const host = remoteUrl ? sshHost(remoteUrl) : undefined;
|
|
196
313
|
if (shas.length && host) {
|
|
@@ -202,8 +319,6 @@ async function prePush(remote, remoteUrl) {
|
|
|
202
319
|
// Check every commit before posting any, so a refused push posts nothing.
|
|
203
320
|
for (const sha of shas)
|
|
204
321
|
await runCheck(current, sha);
|
|
205
|
-
// Post to the remote being pushed, not whichever one gh would pick.
|
|
206
|
-
const ghRepo = remoteUrl ? githubRepo(remoteUrl) : undefined;
|
|
207
322
|
const env = ghRepo ? { ...process.env, GH_REPO: ghRepo } : { ...process.env };
|
|
208
323
|
// git sends the push only once this hook exits, so the poster waits on git before GitHub.
|
|
209
324
|
const gitPid = nearestGit(processTable(), process.pid);
|
package/dist/config.d.ts
CHANGED
|
@@ -28,7 +28,15 @@ export declare function appendLog(file: string, message: string, now?: Date): vo
|
|
|
28
28
|
* A null `status` (a spawn error or a signal) means the check never finished.
|
|
29
29
|
*/
|
|
30
30
|
export declare function checkRunMessage(sha: string, command: string, status: number | null, ms: number): string;
|
|
31
|
-
/**
|
|
32
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Records that `command` passed at `sha`, one `<sha>\t<command>` line per commit, plus a
|
|
33
|
+
* `tree:<tree>\t<json command>` line when the commit's tree is given, so another commit with the
|
|
34
|
+
* same files can reuse the pass. JSON keeps a command written over several lines on one line.
|
|
35
|
+
*/
|
|
36
|
+
export declare function recordPass(file: string, sha: string, command: string, tree?: string): void;
|
|
33
37
|
/** The command that last passed at `sha`, or undefined if it was never checked. */
|
|
34
38
|
export declare function passedCommand(file: string, sha: string): string | undefined;
|
|
39
|
+
/** Whether `command` passed on a commit whose tree is `tree`. Sha-only records never match. */
|
|
40
|
+
export declare function treePassed(file: string, tree: string, command: string): boolean;
|
|
41
|
+
/** The log line for a commit that reused a pass on the same tree instead of running the check. */
|
|
42
|
+
export declare function treeReuseMessage(sha: string, command: string, tree: string): string;
|
package/dist/config.js
CHANGED
|
@@ -65,12 +65,23 @@ export function checkRunMessage(sha, command, status, ms) {
|
|
|
65
65
|
const outcome = status === null
|
|
66
66
|
? `did not finish after ${seconds}`
|
|
67
67
|
: `${status === 0 ? "passed" : "failed"} in ${seconds}`;
|
|
68
|
-
|
|
68
|
+
return runMessage(sha, command, outcome);
|
|
69
|
+
}
|
|
70
|
+
/** `<short-sha>: <command> <outcome>`, one line per run even for a command over several lines. */
|
|
71
|
+
function runMessage(sha, command, outcome) {
|
|
69
72
|
return `${sha.slice(0, 7)}: ${command.replace(/\s+/g, " ")} ${outcome}`;
|
|
70
73
|
}
|
|
71
|
-
/**
|
|
72
|
-
|
|
74
|
+
/** Starts a pass record's tree line; it never matches a sha, so older readers skip the line. */
|
|
75
|
+
const TREE_PREFIX = "tree:";
|
|
76
|
+
/**
|
|
77
|
+
* Records that `command` passed at `sha`, one `<sha>\t<command>` line per commit, plus a
|
|
78
|
+
* `tree:<tree>\t<json command>` line when the commit's tree is given, so another commit with the
|
|
79
|
+
* same files can reuse the pass. JSON keeps a command written over several lines on one line.
|
|
80
|
+
*/
|
|
81
|
+
export function recordPass(file, sha, command, tree) {
|
|
73
82
|
appendFileSync(file, `${sha}\t${command}\n`);
|
|
83
|
+
if (tree)
|
|
84
|
+
appendFileSync(file, `${treeLine(tree, command)}\n`);
|
|
74
85
|
}
|
|
75
86
|
/** The command that last passed at `sha`, or undefined if it was never checked. */
|
|
76
87
|
export function passedCommand(file, sha) {
|
|
@@ -84,3 +95,16 @@ export function passedCommand(file, sha) {
|
|
|
84
95
|
}
|
|
85
96
|
return command;
|
|
86
97
|
}
|
|
98
|
+
/** Whether `command` passed on a commit whose tree is `tree`. Sha-only records never match. */
|
|
99
|
+
export function treePassed(file, tree, command) {
|
|
100
|
+
if (!existsSync(file))
|
|
101
|
+
return false;
|
|
102
|
+
return readFileSync(file, "utf8").split("\n").includes(treeLine(tree, command));
|
|
103
|
+
}
|
|
104
|
+
function treeLine(tree, command) {
|
|
105
|
+
return `${TREE_PREFIX}${tree}\t${JSON.stringify(command)}`;
|
|
106
|
+
}
|
|
107
|
+
/** The log line for a commit that reused a pass on the same tree instead of running the check. */
|
|
108
|
+
export function treeReuseMessage(sha, command, tree) {
|
|
109
|
+
return runMessage(sha, command, `already passed on tree ${tree.slice(0, 7)}; reused without running`);
|
|
110
|
+
}
|
package/dist/push.d.ts
CHANGED
|
@@ -16,6 +16,17 @@ export declare const SLOW_HOOK_MS = 30000;
|
|
|
16
16
|
export declare function slowHookWarning(elapsedMs: number, remote: string, refs: PushedRef[]): string | undefined;
|
|
17
17
|
/** The distinct commits pushed to branches. Deletes and tags are skipped. */
|
|
18
18
|
export declare function shasToCheck(refs: PushedRef[]): string[];
|
|
19
|
+
/** The distinct commits pushed to branches other than `defaultBranch`, the only ones that can conflict with it. */
|
|
20
|
+
export declare function shasToProbe(refs: PushedRef[], defaultBranch: string): string[];
|
|
21
|
+
/** The conflicted paths in `git merge-tree --write-tree --name-only --no-messages` output: every line after the tree. */
|
|
22
|
+
export declare function conflictedFiles(output: string): string[];
|
|
23
|
+
/** The env var that lets a push through though its head conflicts with the default branch. */
|
|
24
|
+
export declare const ALLOW_CONFLICT = "LOCAL_CI_ALLOW_CONFLICT";
|
|
25
|
+
/** The refusal for pushed commits that conflict with `<remote>/<branch>`, naming the files, the fix and the override. */
|
|
26
|
+
export declare function conflictMessage(remote: string, branch: string, conflicts: {
|
|
27
|
+
files: string[];
|
|
28
|
+
sha: string;
|
|
29
|
+
}[]): string;
|
|
19
30
|
/**
|
|
20
31
|
* Why `sha` can't be checked in place, or null when it can: it must be HEAD, with a clean
|
|
21
32
|
* working tree, so the result reflects the commit rather than uncommitted files.
|
package/dist/push.js
CHANGED
|
@@ -36,6 +36,27 @@ export function shasToCheck(refs) {
|
|
|
36
36
|
.map((ref) => ref.localSha);
|
|
37
37
|
return [...new Set(shas)];
|
|
38
38
|
}
|
|
39
|
+
/** The distinct commits pushed to branches other than `defaultBranch`, the only ones that can conflict with it. */
|
|
40
|
+
export function shasToProbe(refs, defaultBranch) {
|
|
41
|
+
return shasToCheck(refs.filter((ref) => ref.remoteRef !== `refs/heads/${defaultBranch}`));
|
|
42
|
+
}
|
|
43
|
+
/** The conflicted paths in `git merge-tree --write-tree --name-only --no-messages` output: every line after the tree. */
|
|
44
|
+
export function conflictedFiles(output) {
|
|
45
|
+
return output.split("\n").slice(1).filter(Boolean);
|
|
46
|
+
}
|
|
47
|
+
/** The env var that lets a push through though its head conflicts with the default branch. */
|
|
48
|
+
export const ALLOW_CONFLICT = "LOCAL_CI_ALLOW_CONFLICT";
|
|
49
|
+
/** The refusal for pushed commits that conflict with `<remote>/<branch>`, naming the files, the fix and the override. */
|
|
50
|
+
export function conflictMessage(remote, branch, conflicts) {
|
|
51
|
+
const base = `${remote}/${branch}`;
|
|
52
|
+
return [
|
|
53
|
+
...conflicts.flatMap(({ sha, files }) => [
|
|
54
|
+
`${sha.slice(0, 7)} conflicts with ${base} in:`,
|
|
55
|
+
...files.map((file) => ` ${file}`),
|
|
56
|
+
]),
|
|
57
|
+
`Merge it in first (\`git merge ${base}\`), resolve the conflicts, commit and push again, so the check runs on the head that will merge. To push anyway: \`${ALLOW_CONFLICT}=1 git push\`.`,
|
|
58
|
+
].join("\n");
|
|
59
|
+
}
|
|
39
60
|
/**
|
|
40
61
|
* Why `sha` can't be checked in place, or null when it can: it must be HEAD, with a clean
|
|
41
62
|
* working tree, so the result reflects the commit rather than uncommitted files.
|