@jonmilner/local-ci 0.14.0 → 0.15.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 CHANGED
@@ -21,14 +21,14 @@ 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.
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.
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.
28
28
  - `local-ci status [<sha>]`: posts `check` for a commit (default `HEAD`) that already passed, or runs the check first. When the check command fails, it posts `check` = `failure` (`<command> failed on <hostname>`) and exits non-zero; a later passing run posts `success` over it. A dirty tree, a commit that is not `HEAD`, or a `gh` error posts nothing. Use it after a push that skipped hooks (`--no-verify`) or a lost background poster.
29
29
  - `local-ci run -- <command>`: runs `<command>` under the machine-wide check lock (see [State](#state)), for a full check started by hand or from a script: `local-ci run -- pnpm check`. It waits its turn with every other check, and they wait for it; like any check, it runs beside a background job rather than waiting for one (see [State](#state)). The arguments after `--` go to the program as they are, with no shell (`local-ci run -- sh -c 'a && b'` for a shell line). Stdin is `/dev/null` and the command has no controlling terminal, so anything that prompts on `/dev/tty` fails; output goes straight through. It exits with the command's status, 128 plus the signal number if a signal killed the command, or 1 if the command can't start. It works outside a git repo, needs no `gh` login, posts no status and records no pass. Inside a check that already holds the lock it runs at once, so a repo's `check` script can wrap itself in `local-ci run` without deadlocking its own pre-push.
30
30
  - `local-ci release`: publishes the repo's packages from this machine. Off unless `package.json` has `"local-ci": { "release": true }`; otherwise it prints `release not enabled` and exits 0. It needs a clean working tree, then fetches `origin` and takes a lock, so a second run exits with `release in progress`. In a temporary worktree at `origin`'s default branch:
31
- 1. With pending changesets, it creates `release/<short-sha>`, runs the root `version-packages` script if `package.json` has one (for steps such as a size-history entry that belong in the version PR) or `pnpm changeset version` otherwise, commits `chore: version packages`, pushes (the hook runs the check and posts `check`), opens the PR, posts `pr-title`, waits for both statuses, and squash-merges it.
31
+ 1. With pending changesets, it creates `release/<short-sha>`, runs the root `version-packages` script if `package.json` has one (for steps such as a size-history entry that belong in the version PR) or `pnpm changeset version` otherwise, commits `chore: version packages`, pushes (the hook runs the check and posts `check`; the push adds `-o ServerAliveInterval=15` to the ssh command git would run (an OpenSSH one), so a long check can't drop it, see [SSH keepalive](#ssh-keepalive)), opens the PR, posts `pr-title`, waits for both statuses, and squash-merges it.
32
32
  2. At the merge commit (or the default branch, with no changesets pending), it runs `local-ci status`, then `pnpm release` with the npm token, then pushes the tags `changeset publish` created.
33
33
  3. It checks each published `name@version` with `npm view`, waiting up to 5 minutes in all for the registry to list them. A timeout or `npm view` error here exits non-zero, but the publish already ran: check with the `npm view` command the error gives before rerunning.
34
34
 
@@ -57,11 +57,24 @@ That writes `pre-push` and `commit-msg` shims, each one line that calls local-ci
57
57
 
58
58
  A repo without a `package.json` sets `LOCAL_CI_COMMAND`, for example on the hook's line: `LOCAL_CI_COMMAND=./scripts/check.sh local-ci pre-push "$@"`.
59
59
 
60
+ ## SSH keepalive
61
+
62
+ git opens the push connection before it runs the `pre-push` hook and sends the pack only after the hook exits, so the connection sits idle while the hook waits for the check lock and runs the check. GitHub closes an ssh connection that idles too long (drops were seen after hooks of 36 seconds and more), printing `Connection to github.com closed by remote host.`; git then exits 141 (SIGPIPE) and the ref on GitHub never moves, though the hook passed. Keep the connection alive in `~/.ssh/config`:
63
+
64
+ ```
65
+ Host github.com
66
+ ServerAliveInterval 15
67
+ ```
68
+
69
+ `local-ci pre-push` runs the ssh command git would (`$GIT_SSH_COMMAND`, `core.sshCommand`, `$GIT_SSH`, else `ssh`) with `-G <host>` for an ssh remote, which prints the effective config, that command's own `-F` and `-o` options included, without connecting, and warns, naming these lines, when it shows no `ServerAliveInterval`. It checks nothing for an https remote, or when that command is not OpenSSH (its program isn't `ssh`, such as plink). `local-ci release` adds `-o ServerAliveInterval=15` to that command for its own pushes (an OpenSSH one only), so it needs no config. After a hook of 30 seconds or more on an ssh remote, timed from local-ci's start, confirm the push landed with the `git ls-remote` command the hook prints; if it shows the old sha, push again.
70
+
60
71
  ## State
61
72
 
62
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.
63
74
 
64
- 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. Ctrl-C stops the check, which logs `did not finish after`.
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`.
65
78
 
66
79
  ### Taking the check lock from your own scripts
67
80
 
package/dist/cli.js CHANGED
@@ -6,16 +6,17 @@ 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 } from "./push.js";
16
+ import { cannotCheckInPlace, githubRepo, nearestGit, parsePrePush, shasToCheck, slowHookWarning, } from "./push.js";
17
17
  import { release, releaseCheck, takeEnvToken } from "./release.js";
18
18
  import { projectsDir, renovate } from "./renovate.js";
19
+ import { configuredSshCommand, keepaliveWarning, sshHost } from "./ssh.js";
19
20
  import { lintTitle } from "./title.js";
20
21
  const USAGE = `Usage:
21
22
  local-ci pre-push run the check for each pushed commit (git pre-push hook)
@@ -80,19 +81,50 @@ async function assertCheckable(sha) {
80
81
  async function runCheck(current, sha) {
81
82
  if (passedCommand(current.passed, sha))
82
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.
83
86
  await assertCheckable(sha);
87
+ const tree = await git("rev-parse", `${sha}^{tree}`);
88
+ if (reuseTreePass(current, sha, tree))
89
+ return;
84
90
  const unlock = await lockCheck(`${current.name} ${sha.slice(0, 7)}`, { file: current.checkLock });
85
91
  try {
86
- // 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.
87
93
  if (passedCommand(current.passed, sha))
88
94
  return;
89
95
  await assertCheckable(sha);
90
- await runLocked(current, sha);
96
+ if (reuseTreePass(current, sha, tree))
97
+ return;
98
+ await runLocked(current, sha, tree);
91
99
  }
92
100
  finally {
93
101
  unlock();
94
102
  }
95
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
+ }
96
128
  /** Runs a program under the check lock, attached to it so a signal to local-ci reaches it. */
97
129
  async function runHeld(file, args, cwd) {
98
130
  const held = heldCheckLock();
@@ -114,7 +146,7 @@ async function runHeld(file, args, cwd) {
114
146
  return { ...result, interrupted: held?.signal };
115
147
  }
116
148
  /** Runs the check command under the check lock, logging its duration and recording a pass. */
117
- async function runLocked({ root, passed, log: logFile }, sha) {
149
+ async function runLocked({ root, passed, log: logFile }, sha, tree) {
118
150
  const command = resolveCommand(root);
119
151
  err(`local-ci: running ${command} on ${sha.slice(0, 7)}`);
120
152
  const started = performance.now();
@@ -123,13 +155,7 @@ async function runLocked({ root, passed, log: logFile }, sha) {
123
155
  const { interrupted } = result;
124
156
  const status = interrupted ? null : result.status;
125
157
  // Every run's duration goes in the log, so a faster check can be measured against a slower one.
126
- // A log that can't be written must not change the check's outcome.
127
- try {
128
- appendLog(logFile, checkRunMessage(sha, command, status, performance.now() - started));
129
- }
130
- catch (error) {
131
- err(`local-ci: could not log the run: ${error instanceof Error ? error.message : String(error)}`);
132
- }
158
+ logQuietly(logFile, checkRunMessage(sha, command, status, performance.now() - started));
133
159
  // A spawn error or a signal means the check never finished, so it is not a failure to post.
134
160
  if (status === null) {
135
161
  const cause = interrupted ?? result.error ?? result.signal;
@@ -137,7 +163,7 @@ async function runLocked({ root, passed, log: logFile }, sha) {
137
163
  }
138
164
  if (status !== 0)
139
165
  throw new CheckFailed(command, sha);
140
- recordPass(passed, sha, command);
166
+ recordPass(passed, sha, command, tree);
141
167
  }
142
168
  /**
143
169
  * Runs `-- <command>` under the check lock, so a full check started by hand waits its turn with
@@ -184,11 +210,20 @@ async function runHooks([subcommand, ...extra]) {
184
210
  const changes = installHooks(root, dir, command);
185
211
  out(changes.length ? changes.join("\n") : "hooks up to date");
186
212
  }
187
- async function prePush(remoteUrl) {
213
+ async function prePush(remote, remoteUrl) {
188
214
  const current = await repo();
189
215
  await assertHooks(current.root);
190
216
  await assertGhAuth(exec);
191
- const shas = shasToCheck(parsePrePush(readFileSync(0, "utf8")));
217
+ const refs = parsePrePush(readFileSync(0, "utf8"));
218
+ const shas = shasToCheck(refs);
219
+ // git holds an ssh connection open, idle, while this hook runs; an https push holds none.
220
+ const host = remoteUrl ? sshHost(remoteUrl) : undefined;
221
+ if (shas.length && host) {
222
+ const configured = await configuredSshCommand(exec, current.root);
223
+ const warning = await keepaliveWarning(exec, host, process.env, configured);
224
+ if (warning)
225
+ err(warning);
226
+ }
192
227
  // Check every commit before posting any, so a refused push posts nothing.
193
228
  for (const sha of shas)
194
229
  await runCheck(current, sha);
@@ -196,9 +231,9 @@ async function prePush(remoteUrl) {
196
231
  const ghRepo = remoteUrl ? githubRepo(remoteUrl) : undefined;
197
232
  const env = ghRepo ? { ...process.env, GH_REPO: ghRepo } : { ...process.env };
198
233
  // git sends the push only once this hook exits, so the poster waits on git before GitHub.
199
- const git = nearestGit(processTable(), process.pid);
200
- if (git)
201
- env[PUSH_PID] = String(git);
234
+ const gitPid = nearestGit(processTable(), process.pid);
235
+ if (gitPid)
236
+ env[PUSH_PID] = String(gitPid);
202
237
  else
203
238
  delete env[PUSH_PID];
204
239
  const fd = openSync(current.log, "a");
@@ -213,6 +248,12 @@ async function prePush(remoteUrl) {
213
248
  closeSync(fd);
214
249
  if (shas.length)
215
250
  err(`local-ci: passed; posting statuses in the background (${current.log})`);
251
+ // Timed from process start, so node's own startup counts toward the connection's idle time.
252
+ if (remote && host) {
253
+ const slow = slowHookWarning(performance.now(), remote, refs);
254
+ if (slow)
255
+ err(slow);
256
+ }
216
257
  }
217
258
  async function post({ passed }, sha) {
218
259
  const command = passedCommand(passed, sha);
@@ -318,7 +359,7 @@ async function runRenovate([flag, ...extra]) {
318
359
  }
319
360
  async function main([command, ...args]) {
320
361
  if (command === "pre-push")
321
- return prePush(args[1]);
362
+ return prePush(args[0], args[1]);
322
363
  if (command === "commit-msg") {
323
364
  if (!args[0] || args.length > 1) {
324
365
  err(USAGE);
@@ -343,6 +384,7 @@ async function main([command, ...args]) {
343
384
  if (command === "post" || command === "pr-title" || command === "status") {
344
385
  await assertGhAuth(exec);
345
386
  }
387
+ // oxlint-disable-next-line typescript/switch-exhaustiveness-check -- default prints usage for any other command
346
388
  switch (command) {
347
389
  case "post":
348
390
  if (!args[0])
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
- /** Records that `command` passed at `sha`, one `<sha>\t<command>` line per commit. */
32
- export declare function recordPass(file: string, sha: string, command: string): void;
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
- // One line per run, even for a command written over several lines.
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
- /** Records that `command` passed at `sha`, one `<sha>\t<command>` line per commit. */
72
- export function recordPass(file, sha, command) {
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
@@ -6,6 +6,14 @@ export interface PushedRef {
6
6
  }
7
7
  /** Parses the `<local ref> <local sha> <remote ref> <remote sha>` lines git feeds a pre-push hook. */
8
8
  export declare function parsePrePush(input: string): PushedRef[];
9
+ /** A hook run at least this long may have outlived the remote's idle timeout (the shortest drop seen came after 36s). */
10
+ export declare const SLOW_HOOK_MS = 30000;
11
+ /**
12
+ * The warning to print after a hook that ran `elapsedMs`, at least SLOW_HOOK_MS: an ssh remote may
13
+ * have dropped the connection git opened before the hook, so it names the `git ls-remote` that
14
+ * shows whether each pushed ref landed.
15
+ */
16
+ export declare function slowHookWarning(elapsedMs: number, remote: string, refs: PushedRef[]): string | undefined;
9
17
  /** The distinct commits pushed to branches. Deletes and tags are skipped. */
10
18
  export declare function shasToCheck(refs: PushedRef[]): string[];
11
19
  /**
package/dist/push.js CHANGED
@@ -13,6 +13,22 @@ export function parsePrePush(input) {
13
13
  return { localRef, localSha, remoteRef, remoteSha };
14
14
  });
15
15
  }
16
+ /** A hook run at least this long may have outlived the remote's idle timeout (the shortest drop seen came after 36s). */
17
+ export const SLOW_HOOK_MS = 30_000;
18
+ /**
19
+ * The warning to print after a hook that ran `elapsedMs`, at least SLOW_HOOK_MS: an ssh remote may
20
+ * have dropped the connection git opened before the hook, so it names the `git ls-remote` that
21
+ * shows whether each pushed ref landed.
22
+ */
23
+ export function slowHookWarning(elapsedMs, remote, refs) {
24
+ const pushed = refs.filter((ref) => !ZERO_SHA.test(ref.localSha));
25
+ if (elapsedMs < SLOW_HOOK_MS || !pushed.length)
26
+ return undefined;
27
+ return [
28
+ `local-ci: this hook ran ${Math.round(elapsedMs / 1000)}s, long enough for the remote to drop git's connection; if git then fails or prints "closed by remote host", the push did not land. Confirm with:`,
29
+ ...pushed.map((ref) => ` git ls-remote ${remote} ${ref.remoteRef} # should print ${ref.localSha}`),
30
+ ].join("\n");
31
+ }
16
32
  /** The distinct commits pushed to branches. Deletes and tags are skipped. */
17
33
  export function shasToCheck(refs) {
18
34
  const shas = refs
package/dist/release.js CHANGED
@@ -5,6 +5,7 @@ import { basename, dirname, join } from "node:path";
5
5
  import { run } from "./exec.js";
6
6
  import { defaultSleep, waitForStatuses } from "./github.js";
7
7
  import { takeLock } from "./lock.js";
8
+ import { configuredSshCommand, keepaliveEnv } from "./ssh.js";
8
9
  /** The npm token's name, both as an environment variable and in the `secret` store. */
9
10
  export const TOKEN_NAME = "NPM_TOKEN_JONMILNER";
10
11
  /** How many days ahead of the token's expiry a release starts warning. */
@@ -370,9 +371,12 @@ export async function release(exec, context) {
370
371
  return track(exec(file, args));
371
372
  };
372
373
  const stepGit = gitWith(step);
374
+ // Set once the worktree exists, so its pushes keep ssh alive and GitHub can't drop one while the
375
+ // pre-push hook waits for the check lock and runs the check.
376
+ let stepEnv;
373
377
  const stepRun = async (file, args, cwd) => {
374
378
  await live();
375
- run(file, args, cwd);
379
+ run(file, args, cwd, stepEnv);
376
380
  };
377
381
  // Set once the run is cleaning up on its own, so a signal then doesn't turn a success into an exit.
378
382
  let finishing = false;
@@ -411,6 +415,7 @@ export async function release(exec, context) {
411
415
  // Unguarded, like the token read below, so the fallback can't swallow an interrupt.
412
416
  const base = await defaultBranch(git, root);
413
417
  await stepGit(root, "worktree", "add", "--detach", dir, `origin/${base}`);
418
+ stepEnv = keepaliveEnv(process.env, await configuredSshCommand(exec, dir));
414
419
  const { changesets: pending, current, packages: unpublishedNow } = await plan(step, dir);
415
420
  let packages = unpublishedNow;
416
421
  if (!pending.length && !packages.length) {
package/dist/ssh.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ import type { Exec } from "./exec.ts";
2
+ /**
3
+ * Seconds between ssh keepalives. git opens the push connection before the pre-push hook runs and
4
+ * sends the pack only after it exits, so without keepalives GitHub drops a connection that idles
5
+ * through a long hook.
6
+ */
7
+ export declare const KEEPALIVE_SECONDS = 15;
8
+ /** The host of an ssh remote URL (`ssh://[user@]host[:port]/path` or scp-like `[user@]host:path`), or undefined for any other. */
9
+ export declare function sshHost(url: string): string | undefined;
10
+ /** The ServerAliveInterval `ssh -G` printed, 0 when it names none. */
11
+ export declare function serverAliveInterval(sshConfig: string): number;
12
+ /** `core.sshCommand` in the repo at `cwd`, or "" when it is unset. */
13
+ export declare function configuredSshCommand(exec: Exec, cwd: string): Promise<string>;
14
+ /**
15
+ * `env` with GIT_SSH_COMMAND set to the ssh command git would run plus a keepalive, so a push
16
+ * survives a long pre-push hook whatever the user's ssh config says. `configured` is
17
+ * `core.sshCommand`. A command that is not OpenSSH is left alone. An earlier
18
+ * `-o ServerAliveInterval` in the command still wins, as ssh takes the first value it reads.
19
+ */
20
+ export declare function keepaliveEnv(env: NodeJS.ProcessEnv, configured?: string): NodeJS.ProcessEnv;
21
+ /**
22
+ * The warning to print when pushing over ssh to `host` with no keepalive, judged by running the
23
+ * ssh command git would (`env`, `configured` as in keepaliveEnv) with `-G`, which prints the
24
+ * effective config, its own `-F` and `-o` options included, without connecting. Undefined when a
25
+ * keepalive is in place, or for a command that is not OpenSSH or whose `-G` fails, since those
26
+ * can't be judged.
27
+ */
28
+ export declare function keepaliveWarning(exec: Exec, host: string, env: NodeJS.ProcessEnv, configured?: string): Promise<string | undefined>;
package/dist/ssh.js ADDED
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Seconds between ssh keepalives. git opens the push connection before the pre-push hook runs and
3
+ * sends the pack only after it exits, so without keepalives GitHub drops a connection that idles
4
+ * through a long hook.
5
+ */
6
+ export const KEEPALIVE_SECONDS = 15;
7
+ /** The host of an ssh remote URL (`ssh://[user@]host[:port]/path` or scp-like `[user@]host:path`), or undefined for any other. */
8
+ export function sshHost(url) {
9
+ const full = /^(?:git\+)?ssh(?:\+git)?:\/\/(?:[^@/]+@)?(\[[^\]/]+\]|[^:/]+)/.exec(url);
10
+ if (full)
11
+ return full[1].replace(/^\[(.*)\]$/, "$1");
12
+ if (url.includes("://"))
13
+ return undefined;
14
+ // git reads scp-like syntax only when no slash comes before the first colon.
15
+ return /^(?:[^@/:]+@)?([^/:]+):/.exec(url)?.[1];
16
+ }
17
+ /** The ServerAliveInterval `ssh -G` printed, 0 when it names none. */
18
+ export function serverAliveInterval(sshConfig) {
19
+ return Number(/^serveraliveinterval\s+(\d+)\s*$/im.exec(sshConfig)?.[1] ?? 0);
20
+ }
21
+ /** `core.sshCommand` in the repo at `cwd`, or "" when it is unset. */
22
+ export async function configuredSshCommand(exec, cwd) {
23
+ // `git config --get` exits 1 when the key is unset.
24
+ return (await exec("git", ["-C", cwd, "config", "--get", "core.sshCommand"]).catch(() => "")).trim();
25
+ }
26
+ /**
27
+ * The ssh command git runs, in git's own order: $GIT_SSH_COMMAND, `configured` (core.sshCommand),
28
+ * $GIT_SSH, then ssh. Undefined unless it is OpenSSH (its program is `ssh`), since another program
29
+ * such as plink takes neither `-o` nor `-G`.
30
+ */
31
+ function openSshCommand(env, configured) {
32
+ const command = env.GIT_SSH_COMMAND || configured || env.GIT_SSH || "ssh";
33
+ return /^(?:\S*\/)?ssh(?:\s|$)/.test(command) ? command : undefined;
34
+ }
35
+ /**
36
+ * `env` with GIT_SSH_COMMAND set to the ssh command git would run plus a keepalive, so a push
37
+ * survives a long pre-push hook whatever the user's ssh config says. `configured` is
38
+ * `core.sshCommand`. A command that is not OpenSSH is left alone. An earlier
39
+ * `-o ServerAliveInterval` in the command still wins, as ssh takes the first value it reads.
40
+ */
41
+ export function keepaliveEnv(env, configured) {
42
+ const command = openSshCommand(env, configured);
43
+ if (!command)
44
+ return env;
45
+ return { ...env, GIT_SSH_COMMAND: `${command} -o ServerAliveInterval=${KEEPALIVE_SECONDS}` };
46
+ }
47
+ /**
48
+ * The warning to print when pushing over ssh to `host` with no keepalive, judged by running the
49
+ * ssh command git would (`env`, `configured` as in keepaliveEnv) with `-G`, which prints the
50
+ * effective config, its own `-F` and `-o` options included, without connecting. Undefined when a
51
+ * keepalive is in place, or for a command that is not OpenSSH or whose `-G` fails, since those
52
+ * can't be judged.
53
+ */
54
+ export async function keepaliveWarning(exec, host, env, configured) {
55
+ const command = openSshCommand(env, configured);
56
+ if (!command)
57
+ return undefined;
58
+ // git runs the command through a shell too.
59
+ const config = await exec("sh", ["-c", `${command} -G "$1"`, "sh", host]).catch(() => undefined);
60
+ if (config === undefined || serverAliveInterval(config) > 0)
61
+ return undefined;
62
+ return [
63
+ `local-ci: ssh to ${host} sends no keepalive, so ${host} can drop this push while the check runs; add to ~/.ssh/config:`,
64
+ ` Host ${host}`,
65
+ ` ServerAliveInterval ${KEEPALIVE_SECONDS}`,
66
+ ].join("\n");
67
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jonmilner/local-ci",
3
- "version": "0.14.0",
3
+ "version": "0.15.0",
4
4
  "description": "Run a repo's check in a pre-push hook and post the result as GitHub commit statuses",
5
5
  "license": "MIT",
6
6
  "repository": {