@feigi/fleet-ctl 0.0.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/.claude-plugin/plugin.json +9 -0
- package/LICENSE +202 -0
- package/NOTICE +18 -0
- package/agents/fleet-finisher.agent.md +7 -0
- package/agents/fleet-implementer-alt.agent.md +210 -0
- package/agents/fleet-implementer.agent.md +210 -0
- package/agents/fleet-merge-bot.agent.md +7 -0
- package/agents/fleet-review-comments.agent.md +40 -0
- package/agents/fleet-review-correctness.agent.md +43 -0
- package/agents/fleet-review-runner.agent.md +73 -0
- package/agents/fleet-review-silent-failure.agent.md +49 -0
- package/agents/fleet-review-simplify.agent.md +46 -0
- package/agents/fleet-review-snapshot.agent.md +24 -0
- package/agents/fleet-review-tests.agent.md +46 -0
- package/agents/fleet-review-types.agent.md +41 -0
- package/agents/fleet-review-verifier.agent.md +29 -0
- package/commands/review-and-fix.md +98 -0
- package/commands/run-merge-bot.md +357 -0
- package/commands/run-team-help.md +17 -0
- package/package.json +23 -0
- package/scripts/agent-test-dir-prose.test.mjs +59 -0
- package/scripts/agent-test.test.mjs +1636 -0
- package/scripts/ambient-git-vars-mjs-prose.test.mjs +354 -0
- package/scripts/ambient-git-vars-prose.test.mjs +192 -0
- package/scripts/ancestry-check-position-prose.test.mjs +175 -0
- package/scripts/apply-ruleset.test.mjs +420 -0
- package/scripts/arg-header-probes-prose.test.mjs +393 -0
- package/scripts/arg.mjs +600 -0
- package/scripts/arg.test.mjs +1354 -0
- package/scripts/board-cli.test.mjs +694 -0
- package/scripts/board-identity.test.mjs +429 -0
- package/scripts/board-liveness-banner.test.mjs +119 -0
- package/scripts/board-prev-shape.test.mjs +229 -0
- package/scripts/board-tryparse-null.test.mjs +119 -0
- package/scripts/board.html +471 -0
- package/scripts/board.mjs +2037 -0
- package/scripts/board.test.mjs +3086 -0
- package/scripts/brief-after-cut-prose.test.mjs +53 -0
- package/scripts/candidates-exit3-prose.test.mjs +302 -0
- package/scripts/candidates.mjs +674 -0
- package/scripts/candidates.test.mjs +2101 -0
- package/scripts/caveat-unification-prose.test.mjs +98 -0
- package/scripts/ci-comment-rot-prose.test.mjs +2025 -0
- package/scripts/ci-completes-premise-prose.test.mjs +257 -0
- package/scripts/ci-state-prose.test.mjs +113 -0
- package/scripts/ci-state.mjs +761 -0
- package/scripts/ci-state.test.mjs +1640 -0
- package/scripts/ci-vacuous-green.test.mjs +368 -0
- package/scripts/citation-sweep-prose.test.mjs +1572 -0
- package/scripts/claim-lifecycle.test.mjs +185 -0
- package/scripts/claim-ticket.sh +417 -0
- package/scripts/claim-ticket.test.mjs +1121 -0
- package/scripts/closing-keyword-prose.test.mjs +193 -0
- package/scripts/cockpit-launch-idempotent-prose.test.mjs +165 -0
- package/scripts/cockpit-tick-nonblocking.test.mjs +210 -0
- package/scripts/compute-board.mjs +579 -0
- package/scripts/compute-board.test.mjs +1060 -0
- package/scripts/compute-spend.mjs +329 -0
- package/scripts/compute-spend.test.mjs +451 -0
- package/scripts/cross-repo-citation-prose.test.mjs +156 -0
- package/scripts/cwd-isolation-pins.mjs +83 -0
- package/scripts/dedupe-guard-prose.test.mjs +243 -0
- package/scripts/derive-testcmd.sh +239 -0
- package/scripts/derive-testcmd.test.mjs +380 -0
- package/scripts/diff-stats.mjs +231 -0
- package/scripts/diff-stats.test.mjs +408 -0
- package/scripts/dispatch-block-golden-prose.test.mjs +1024 -0
- package/scripts/dispatch-block-pins-prose.test.mjs +615 -0
- package/scripts/drop-merged-label.sh +173 -0
- package/scripts/drop-merged-label.test.mjs +480 -0
- package/scripts/expected-jobs-prose.test.mjs +80 -0
- package/scripts/filing-bar-seat-prose.test.mjs +242 -0
- package/scripts/filing-claim-bar-prose.test.mjs +166 -0
- package/scripts/finisher-dispatch-premise-prose.test.mjs +312 -0
- package/scripts/finisher-halt-prose.test.mjs +106 -0
- package/scripts/finisher-name-prose.test.mjs +134 -0
- package/scripts/finisher-own-tree-prose.test.mjs +285 -0
- package/scripts/finisher-pin-race-prose.test.mjs +339 -0
- package/scripts/fix-applier-correction-rules-prose.test.mjs +201 -0
- package/scripts/fix-applier-result-file-prose.test.mjs +100 -0
- package/scripts/fleet-bootstrap +155 -0
- package/scripts/fleet-heartbeat.mjs +272 -0
- package/scripts/fleet-heartbeat.test.mjs +450 -0
- package/scripts/fleet-provenance +207 -0
- package/scripts/fleet-provenance.test.mjs +64 -0
- package/scripts/fleet-run +148 -0
- package/scripts/fleet-run.test.mjs +90 -0
- package/scripts/fleet-state.mjs +400 -0
- package/scripts/fleet-state.test.mjs +373 -0
- package/scripts/fleet-tick-prose.test.mjs +299 -0
- package/scripts/fleet-tick.mjs +832 -0
- package/scripts/fleet-tick.test.mjs +1332 -0
- package/scripts/force-with-lease-reason-prose.test.mjs +184 -0
- package/scripts/frontmatter-allowlist.json +64 -0
- package/scripts/frontmatter-check.mjs +256 -0
- package/scripts/frontmatter-check.test.mjs +436 -0
- package/scripts/git-env.mjs +124 -0
- package/scripts/git-env.test.mjs +149 -0
- package/scripts/git-status-untracked-mode-sweep.test.mjs +216 -0
- package/scripts/gone-walk.test.mjs +213 -0
- package/scripts/gp-sep-invariant-prose.test.mjs +188 -0
- package/scripts/head-moved-after-label-prose.test.mjs +233 -0
- package/scripts/immutable-body-claim-prose.test.mjs +413 -0
- package/scripts/implementer-model-tier.test.mjs +643 -0
- package/scripts/inflight-citation-prose.test.mjs +144 -0
- package/scripts/inflight-exit2-prose.test.mjs +81 -0
- package/scripts/inflight-probe-guard-prose.test.mjs +109 -0
- package/scripts/inflight-scan-scope-prose.test.mjs +57 -0
- package/scripts/inflight.sh +1106 -0
- package/scripts/inflight.test.mjs +2984 -0
- package/scripts/injection-control-prose.test.mjs +404 -0
- package/scripts/install-root-audit.test.mjs +464 -0
- package/scripts/instrument-check-prose.test.mjs +235 -0
- package/scripts/instruments.sh +394 -0
- package/scripts/instruments.test.mjs +787 -0
- package/scripts/issue-tracker-prose.test.mjs +71 -0
- package/scripts/json.sh +248 -0
- package/scripts/json.test.mjs +364 -0
- package/scripts/ledger-dispatch.test.mjs +516 -0
- package/scripts/ledger-grammar.mjs +85 -0
- package/scripts/ledger-grammar.test.mjs +142 -0
- package/scripts/ledger-read-require-file.test.mjs +197 -0
- package/scripts/ledger.mjs +1753 -0
- package/scripts/ledger.test.mjs +3554 -0
- package/scripts/line-distance-prose.test.mjs +245 -0
- package/scripts/liveness-rationale-prose.test.mjs +83 -0
- package/scripts/locale-pin-prose.test.mjs +189 -0
- package/scripts/member-outcome-prose.test.mjs +167 -0
- package/scripts/member-outcomes-header.test.mjs +148 -0
- package/scripts/member-outcomes-prose.test.mjs +63 -0
- package/scripts/member-outcomes.mjs +231 -0
- package/scripts/member-outcomes.test.mjs +507 -0
- package/scripts/member-prompt-prose.test.mjs +161 -0
- package/scripts/member-record.mjs +605 -0
- package/scripts/member-record.test.mjs +516 -0
- package/scripts/member-write-guard.mjs +279 -0
- package/scripts/member-write-guard.test.mjs +211 -0
- package/scripts/merge-commit-probe-prose.test.mjs +122 -0
- package/scripts/merge-gate.mjs +262 -0
- package/scripts/merge-gate.test.mjs +613 -0
- package/scripts/muted-git-guard-sweep.test.mjs +493 -0
- package/scripts/net-ssh-precedence-prose.test.mjs +91 -0
- package/scripts/net-watchdog-prose.test.mjs +79 -0
- package/scripts/net.sh +409 -0
- package/scripts/net.test.mjs +211 -0
- package/scripts/no-ci-gate-prose.test.mjs +108 -0
- package/scripts/no-undo-audit.sh +1134 -0
- package/scripts/no-undo-audit.test.mjs +3965 -0
- package/scripts/no-wave.test.mjs +138 -0
- package/scripts/node-floor-sweep.test.mjs +611 -0
- package/scripts/pointer-target-prose.test.mjs +186 -0
- package/scripts/pr-overlap.mjs +417 -0
- package/scripts/pr-overlap.test.mjs +695 -0
- package/scripts/printf-die-sweep.test.mjs +384 -0
- package/scripts/prompt-renderer.mjs +39 -0
- package/scripts/prompt-renderer.test.mjs +68 -0
- package/scripts/prose-pin.mjs +808 -0
- package/scripts/prose-pin.test.mjs +1034 -0
- package/scripts/prove-merge.sh +228 -0
- package/scripts/prove-merge.test.mjs +995 -0
- package/scripts/pull-table-relabel-prose.test.mjs +60 -0
- package/scripts/quiet-payload-prose.test.mjs +222 -0
- package/scripts/reap.sh +1768 -0
- package/scripts/reap.test.mjs +4395 -0
- package/scripts/reaping-prose.test.mjs +159 -0
- package/scripts/refuter-scratch-prose.test.mjs +135 -0
- package/scripts/release-label-required-prose.test.mjs +71 -0
- package/scripts/release-label-split-prose.test.mjs +169 -0
- package/scripts/release-ticket.sh +1716 -0
- package/scripts/release-ticket.test.mjs +5106 -0
- package/scripts/renovate-release-contract.test.mjs +379 -0
- package/scripts/repo-root.mjs +544 -0
- package/scripts/repo-root.test.mjs +873 -0
- package/scripts/reporter-output-rule-prose.test.mjs +208 -0
- package/scripts/review-agent-report-only.test.mjs +49 -0
- package/scripts/review-core-cwd-isolation.test.mjs +374 -0
- package/scripts/review-core-reads.test.mjs +478 -0
- package/scripts/review-core-refuter-scratch.test.mjs +148 -0
- package/scripts/review-core-snapshot-path.test.mjs +777 -0
- package/scripts/review-core-specialist-scratch.test.mjs +189 -0
- package/scripts/review-core-testcmd.test.mjs +288 -0
- package/scripts/review-core-unrun.test.mjs +530 -0
- package/scripts/review-core-unverified-discriminant.test.mjs +208 -0
- package/scripts/review-core.mjs +1028 -0
- package/scripts/review-eval-resolver-path.test.mjs +55 -0
- package/scripts/review-eval.mjs +197 -0
- package/scripts/review-host-fixture.mjs +73 -0
- package/scripts/review-in-run-retry.test.mjs +187 -0
- package/scripts/review-no-vendor-dispatch.test.mjs +45 -0
- package/scripts/review-path-default.test.mjs +796 -0
- package/scripts/review-runner-spawns.test.mjs +93 -0
- package/scripts/review-runner.test.mjs +155 -0
- package/scripts/review-tier-audit.test.mjs +140 -0
- package/scripts/run-artifact-pr-prose.test.mjs +341 -0
- package/scripts/run-merge-bot-prose.test.mjs +1391 -0
- package/scripts/scratch-partition-invariant-prose.test.mjs +74 -0
- package/scripts/scripts-path-citation-sweep.test.mjs +73 -0
- package/scripts/section-between-guard-prose.test.mjs +378 -0
- package/scripts/select-dimensions.test.mjs +674 -0
- package/scripts/shared-refusal.test.mjs +388 -0
- package/scripts/shell-traps-prose.test.mjs +507 -0
- package/scripts/shortlist.mjs +355 -0
- package/scripts/shortlist.test.mjs +366 -0
- package/scripts/slow-transport.mjs +72 -0
- package/scripts/snapshot-repo.test.mjs +772 -0
- package/scripts/snapshot-runner-audience-prose.test.mjs +129 -0
- package/scripts/spend-view.test.mjs +312 -0
- package/scripts/staleness-qualifier-prose.test.mjs +251 -0
- package/scripts/staleness-verdict-prose.test.mjs +142 -0
- package/scripts/staleness.mjs +414 -0
- package/scripts/staleness.test.mjs +893 -0
- package/scripts/stalled-member-liveness-prose.test.mjs +219 -0
- package/scripts/stash-prohibition-prose.test.mjs +313 -0
- package/scripts/strip-comments.mjs +44 -0
- package/scripts/tier-check.mjs +468 -0
- package/scripts/tier-check.test.mjs +726 -0
- package/scripts/tier-guard-gate-prose.test.mjs +216 -0
- package/scripts/tier-outcomes-header.test.mjs +586 -0
- package/scripts/tier-outcomes.mjs +441 -0
- package/scripts/tier-outcomes.test.mjs +342 -0
- package/scripts/tier-roles.mjs +250 -0
- package/scripts/tier-roles.test.mjs +316 -0
- package/scripts/tracker-block-copy-prose.test.mjs +288 -0
- package/scripts/triage-labels-gloss-prose.test.mjs +97 -0
- package/scripts/unattended-git-sweep.test.mjs +142 -0
- package/scripts/verify-sha-exit2-prose.test.mjs +269 -0
- package/scripts/verify-sha.sh +117 -0
- package/scripts/verify-sha.test.mjs +693 -0
- package/scripts/watcher-degraded-block.test.mjs +372 -0
- package/scripts/watcher-key-prose.test.mjs +106 -0
- package/scripts/within-run-pair-prose.test.mjs +208 -0
- package/scripts/worktree-audit.sh +482 -0
- package/scripts/worktree-audit.test.mjs +1407 -0
- package/scripts/worktree-head-prose.test.mjs +276 -0
- package/scripts/worktree-listing-sweep.test.mjs +217 -0
- package/scripts/worktree-model-prose.test.mjs +216 -0
- package/scripts/worktree.sh +470 -0
- package/skills/next-ticket/SKILL.md +138 -0
- package/skills/run-team/SKILL.md +3603 -0
- package/skills/run-team/references/ci-and-staleness.md +75 -0
- package/skills/run-team/references/correction-tickets.md +37 -0
- package/skills/run-team/references/isolation.md +23 -0
- package/skills/run-team/references/member-lifecycle.md +73 -0
- package/skills/run-team/references/reaping.md +73 -0
- package/skills/sizing-a-ticket/SKILL.md +39 -0
|
@@ -0,0 +1,3965 @@
|
|
|
1
|
+
// Regression gate for no-undo-audit.sh, all three of its outcomes. Zero deps:
|
|
2
|
+
// `node --test plugin/scripts/no-undo-audit.test.mjs`.
|
|
3
|
+
//
|
|
4
|
+
// The audit answers one question — is this worktree safe to rebase — and the
|
|
5
|
+
// only thing that makes it refuse is uncommitted work in the worktree. It used
|
|
6
|
+
// to also refuse on a nonzero repo-global stash count, which is unrelated to
|
|
7
|
+
// that question: a rebase never consumes a pre-existing entry, so the count
|
|
8
|
+
// refused every run in a repo holding any entry while proving nothing about the
|
|
9
|
+
// branch. Both directions are pinned, because a fix for a false refusal is one
|
|
10
|
+
// keystroke from deleting the true one.
|
|
11
|
+
//
|
|
12
|
+
// The refusal is only half of it. A clean worktree exits 0 while still
|
|
13
|
+
// answering "what would a careless resolution eat", and everything below the
|
|
14
|
+
// `conflicts[] and atRisk[]` banner pins that half — the half that had no
|
|
15
|
+
// coverage at all while this file called itself the regression gate.
|
|
16
|
+
//
|
|
17
|
+
// Exit 2 is its own outcome: the question could not be answered. Its rule is
|
|
18
|
+
// that no payload is emitted, because a payload is an answer, and every exit-2
|
|
19
|
+
// test asserts that as well as the code. Reported safe on a question never
|
|
20
|
+
// asked is the one failure this script exists to prevent, so a shape it cannot
|
|
21
|
+
// answer must exit 2 rather than exit 0 with an empty `atRisk`.
|
|
22
|
+
//
|
|
23
|
+
// Real git throughout: a shell script that reasons about git state can only be
|
|
24
|
+
// tested against real git state.
|
|
25
|
+
|
|
26
|
+
import { test } from "node:test";
|
|
27
|
+
import assert from "node:assert/strict";
|
|
28
|
+
import { execFileSync, spawnSync } from "node:child_process";
|
|
29
|
+
import { chmodSync, copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
|
|
30
|
+
import { tmpdir } from "node:os";
|
|
31
|
+
import { dirname, join } from "node:path";
|
|
32
|
+
import { fileURLToPath } from "node:url";
|
|
33
|
+
import { between, phrase, stripHashGutter } from "./prose-pin.mjs";
|
|
34
|
+
|
|
35
|
+
const SCRIPT = fileURLToPath(new URL("./no-undo-audit.sh", import.meta.url));
|
|
36
|
+
|
|
37
|
+
// Pin identity and cut the developer's ~/.gitconfig out of the fixtures, so a
|
|
38
|
+
// local pull.rebase or hook cannot change what these repos look like. BASE_REF
|
|
39
|
+
// is unset because the fleet harness is exactly the caller that would have it
|
|
40
|
+
// set, and inheriting it would point every fixture at a local main while the
|
|
41
|
+
// suite stayed green.
|
|
42
|
+
const ENV = {
|
|
43
|
+
...process.env,
|
|
44
|
+
BASE_REF: undefined,
|
|
45
|
+
GIT_DIR: undefined,
|
|
46
|
+
GIT_WORK_TREE: undefined,
|
|
47
|
+
GIT_TEMPLATE_DIR: undefined,
|
|
48
|
+
GIT_INDEX_FILE: undefined,
|
|
49
|
+
GIT_AUTHOR_NAME: "t",
|
|
50
|
+
GIT_AUTHOR_EMAIL: "t@example.com",
|
|
51
|
+
GIT_COMMITTER_NAME: "t",
|
|
52
|
+
GIT_COMMITTER_EMAIL: "t@example.com",
|
|
53
|
+
GIT_CONFIG_GLOBAL: "/dev/null",
|
|
54
|
+
GIT_CONFIG_SYSTEM: "/dev/null",
|
|
55
|
+
// GIT_CONFIG_GLOBAL alone does not isolate: these two carry config in through
|
|
56
|
+
// a separate door and outrank the files. A suite run from inside a git hook
|
|
57
|
+
// inherits whatever set them, which is a false red nobody can reproduce by
|
|
58
|
+
// hand.
|
|
59
|
+
GIT_CONFIG_COUNT: undefined,
|
|
60
|
+
GIT_CONFIG_PARAMETERS: undefined,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const git = (cwd, ...args) =>
|
|
64
|
+
execFileSync("git", args, { cwd, env: ENV, encoding: "utf8" }).trim();
|
|
65
|
+
|
|
66
|
+
/** Bare origin + clone with `main` and a pushed feature branch, checked out. */
|
|
67
|
+
function repo(t, branch = "fix/1-thing", prefix = "no-undo-audit-") {
|
|
68
|
+
const root = mkdtempSync(join(tmpdir(), prefix));
|
|
69
|
+
t.after(() => rmSync(root, { recursive: true, force: true }));
|
|
70
|
+
const origin = join(root, "origin.git");
|
|
71
|
+
const w = join(root, "w");
|
|
72
|
+
execFileSync("git", ["-c", "init.defaultBranch=main", "init", "-q", "--bare", origin], { env: ENV });
|
|
73
|
+
execFileSync("git", ["clone", "-q", origin, w], { env: ENV });
|
|
74
|
+
writeFileSync(join(w, "f.txt"), "root\n");
|
|
75
|
+
git(w, "add", "f.txt");
|
|
76
|
+
git(w, "commit", "-q", "-m", "root");
|
|
77
|
+
git(w, "branch", "-M", "main");
|
|
78
|
+
git(w, "push", "-q", "-u", "origin", "main");
|
|
79
|
+
git(w, "checkout", "-q", "-b", branch);
|
|
80
|
+
writeFileSync(join(w, "g.txt"), "branch work\n");
|
|
81
|
+
git(w, "add", "g.txt");
|
|
82
|
+
git(w, "commit", "-q", "-m", "branch work");
|
|
83
|
+
git(w, "push", "-q", "-u", "origin", branch);
|
|
84
|
+
return { w, branch };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Leave a stash entry behind without leaving the worktree dirty. */
|
|
88
|
+
function stashSomething(w, name = "h.txt") {
|
|
89
|
+
writeFileSync(join(w, name), "stashed\n");
|
|
90
|
+
git(w, "add", name);
|
|
91
|
+
git(w, "stash", "push", "-q", "-m", `pre-existing ${name}`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Add/add conflict on `path`, with main holding the later commit that made it. */
|
|
95
|
+
// Two decoys make `atRisk` discriminating rather than merely nonempty. Without
|
|
96
|
+
// them every main commit since the fork touches the conflicting path and the
|
|
97
|
+
// root commit touches nothing else, so "filtered by path", "filtered by range"
|
|
98
|
+
// and "not filtered at all" all return exactly one line — and dropping either
|
|
99
|
+
// filter from the script leaves the suite green. DECOY_OLD is on the path but
|
|
100
|
+
// before the fork; DECOY_NEW is after the fork but on another file.
|
|
101
|
+
// `branch` and `prefix` pass straight through to `repo`, so a case can plant a
|
|
102
|
+
// marker in the branch name or in the worktree path — the two values #431's
|
|
103
|
+
// escape block renders, and the only two a content-selected shim can address
|
|
104
|
+
// there. Both default through `repo`'s own defaults, so every existing caller
|
|
105
|
+
// is unchanged.
|
|
106
|
+
function conflictRepo(t, path, branch, prefix) {
|
|
107
|
+
const c = repo(t, branch, prefix);
|
|
108
|
+
git(c.w, "checkout", "-q", "main");
|
|
109
|
+
writeFileSync(join(c.w, path), "older, already shared\n");
|
|
110
|
+
git(c.w, "add", "--", `:(literal)${path}`);
|
|
111
|
+
git(c.w, "commit", "-q", "-m", "DECOY_OLD before the fork");
|
|
112
|
+
git(c.w, "push", "-q", "origin", "main");
|
|
113
|
+
git(c.w, "checkout", "-q", c.branch);
|
|
114
|
+
git(c.w, "merge", "-q", "main", "-m", "carry main into the branch");
|
|
115
|
+
writeFileSync(join(c.w, path), "branch side\n");
|
|
116
|
+
git(c.w, "add", "--", `:(literal)${path}`);
|
|
117
|
+
git(c.w, "commit", "-q", "-m", "branch edits the file");
|
|
118
|
+
git(c.w, "push", "-q", "origin", c.branch);
|
|
119
|
+
git(c.w, "checkout", "-q", "main");
|
|
120
|
+
writeFileSync(join(c.w, "decoy.txt"), "untouched by the conflict\n");
|
|
121
|
+
git(c.w, "add", "--", "decoy.txt");
|
|
122
|
+
git(c.w, "commit", "-q", "-m", "DECOY_NEW after the fork");
|
|
123
|
+
writeFileSync(join(c.w, path), "MAIN SIDE\n");
|
|
124
|
+
git(c.w, "add", "--", `:(literal)${path}`);
|
|
125
|
+
git(c.w, "commit", "-q", "-m", "MAIN COMMIT AT RISK");
|
|
126
|
+
git(c.w, "push", "-q", "origin", "main");
|
|
127
|
+
git(c.w, "checkout", "-q", c.branch);
|
|
128
|
+
return c;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Both sides add `path`; nothing else. Used where the decoys would be noise. */
|
|
132
|
+
function bareConflictRepo(t, path) {
|
|
133
|
+
const c = repo(t);
|
|
134
|
+
writeFileSync(join(c.w, path), "branch side\n");
|
|
135
|
+
git(c.w, "add", "--", `:(literal)${path}`);
|
|
136
|
+
git(c.w, "commit", "-q", "-m", "branch edits the file");
|
|
137
|
+
git(c.w, "push", "-q", "origin", c.branch);
|
|
138
|
+
git(c.w, "checkout", "-q", "main");
|
|
139
|
+
writeFileSync(join(c.w, path), "MAIN SIDE\n");
|
|
140
|
+
git(c.w, "add", "--", `:(literal)${path}`);
|
|
141
|
+
git(c.w, "commit", "-q", "-m", "MAIN COMMIT AT RISK");
|
|
142
|
+
git(c.w, "push", "-q", "origin", "main");
|
|
143
|
+
git(c.w, "checkout", "-q", c.branch);
|
|
144
|
+
return c;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Both sides add a path spelled as a `printf` FORMAT, plus `plain.txt`, so
|
|
149
|
+
* merge-tree reports TWO conflicting paths, and main's later commit is
|
|
150
|
+
* `MAIN COMMIT AT RISK` — the same shape `conflictRepo` builds, reached without
|
|
151
|
+
* ever naming the path in JavaScript.
|
|
152
|
+
*
|
|
153
|
+
* The name has to be produced by the SHELL. `execFileSync` re-encodes every JS
|
|
154
|
+
* string as UTF-8, so a JS `"\xFF"` arrives as the two bytes `\303\277` — valid
|
|
155
|
+
* UTF-8, which reproduces nothing. `printf 'b\377ad.txt'` is the only way to get
|
|
156
|
+
* the raw byte across, and it keeps this file pure ASCII.
|
|
157
|
+
*
|
|
158
|
+
* The commits go in through a THROWAWAY `GIT_INDEX_FILE` and are pushed straight
|
|
159
|
+
* to origin, so neither the real index nor the filesystem ever has to hold the
|
|
160
|
+
* name — APFS refuses it outright (`touch $(printf 'b\377ad.txt')` → `Illegal
|
|
161
|
+
* byte sequence`, status 1), which is what makes `--cacheinfo` load-bearing
|
|
162
|
+
* rather than a shortcut. Nothing needs checking out either: the audit reads
|
|
163
|
+
* `origin/main` and `origin/<branch>` and compares neither against local HEAD,
|
|
164
|
+
* so the local branch stays one commit behind and `status --porcelain` stays
|
|
165
|
+
* clean. That is also why no sparse-checkout is needed to keep it clean.
|
|
166
|
+
*/
|
|
167
|
+
function byteConflictRepo(t, printfPath) {
|
|
168
|
+
const c = repo(t);
|
|
169
|
+
execFileSync("sh", ["-c", `
|
|
170
|
+
set -eu
|
|
171
|
+
w=$1; idx=$2; branch=$3; p=$(printf "$4")
|
|
172
|
+
cd "$w"
|
|
173
|
+
side() {
|
|
174
|
+
parent=$(git rev-parse "$1")
|
|
175
|
+
blob=$(printf '%s\\n' "$2" | git hash-object -w --stdin)
|
|
176
|
+
GIT_INDEX_FILE=$idx git read-tree "$parent"
|
|
177
|
+
GIT_INDEX_FILE=$idx git update-index --add --cacheinfo "100644,$blob,$p" --cacheinfo "100644,$blob,plain.txt"
|
|
178
|
+
tree=$(GIT_INDEX_FILE=$idx git write-tree)
|
|
179
|
+
git commit-tree "$tree" -p "$parent" -m "$3"
|
|
180
|
+
}
|
|
181
|
+
git push -q origin "$(side "origin/$branch" 'branch side' 'branch edits the file')":"refs/heads/$branch"
|
|
182
|
+
git push -q origin "$(side origin/main 'MAIN SIDE' 'MAIN COMMIT AT RISK')":refs/heads/main
|
|
183
|
+
`, "sh", c.w, join(c.w, "..", "idx"), c.branch, printfPath], { env: ENV, encoding: "utf8" });
|
|
184
|
+
return c;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Add/add conflicts on 20 paths, introduced on main by TWO distinct commits
|
|
189
|
+
* that INTERLEAVE through the pathspec list: the even-numbered paths come
|
|
190
|
+
* from one, the odd-numbered from the other, and the audit hands them to
|
|
191
|
+
* `git log` in sorted order. So a batch — always a contiguous run of that
|
|
192
|
+
* list — holds paths from both commits and reports both, and the
|
|
193
|
+
* concatenation across batches is `B A B A …` rather than `A A B B`.
|
|
194
|
+
*
|
|
195
|
+
* Every part of that shape is load-bearing, and one commit pinned none of it:
|
|
196
|
+
* with a single commit each batch emits the same line, so all duplicates are
|
|
197
|
+
* adjacent and a merely-ADJACENT dedupe (`uniq`) passes; interleaved, `uniq`
|
|
198
|
+
* has nothing adjacent to collapse and returns one line per batch. The two
|
|
199
|
+
* subjects share a first word so a dedupe keyed on `$2` — the subject's first
|
|
200
|
+
* word rather than `$1`, the SHA that is the actual commit identity —
|
|
201
|
+
* collapses them to one and fails. And two DISTINCT commits are what pin that
|
|
202
|
+
* the dedupe drops only true duplicates: both must survive the split.
|
|
203
|
+
*
|
|
204
|
+
* Returns `paths` so the test asserts against the fixture's own count instead
|
|
205
|
+
* of a literal repeated at the call site.
|
|
206
|
+
*
|
|
207
|
+
* Real ARG_MAX needs thousands of ordinary-length paths to split on its own —
|
|
208
|
+
* the exact figure is ambient-environment-size dependent, since xargs' budget
|
|
209
|
+
* is the limit minus the inherited environment — so `withSplitXargs` below
|
|
210
|
+
* forces the split at 20 instead.
|
|
211
|
+
*/
|
|
212
|
+
function manyConflictsTwoCommits(t) {
|
|
213
|
+
const c = repo(t);
|
|
214
|
+
const paths = Array.from({ length: 20 }, (_, i) => `conflict-${String(i).padStart(2, "0")}.txt`);
|
|
215
|
+
const evens = paths.filter((_, i) => i % 2 === 0);
|
|
216
|
+
const odds = paths.filter((_, i) => i % 2 === 1);
|
|
217
|
+
git(c.w, "checkout", "-q", "main");
|
|
218
|
+
for (const [subject, batch] of [["MAIN COMMIT AT RISK, even paths", evens], ["MAIN COMMIT AT RISK, odd paths", odds]]) {
|
|
219
|
+
for (const p of batch) writeFileSync(join(c.w, p), "MAIN SIDE\n");
|
|
220
|
+
git(c.w, "add", "--", ...batch);
|
|
221
|
+
git(c.w, "commit", "-q", "-m", subject);
|
|
222
|
+
}
|
|
223
|
+
git(c.w, "push", "-q", "origin", "main");
|
|
224
|
+
git(c.w, "checkout", "-q", c.branch);
|
|
225
|
+
for (const p of paths) writeFileSync(join(c.w, p), "branch side\n");
|
|
226
|
+
git(c.w, "add", "--", ...paths);
|
|
227
|
+
git(c.w, "commit", "-q", "-m", "branch edits every file");
|
|
228
|
+
git(c.w, "push", "-q", "origin", c.branch);
|
|
229
|
+
return { ...c, paths };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Shadows `xargs` on PATH with a wrapper that inserts `-s size` ahead of
|
|
234
|
+
* whatever args the script passes, forcing the ARG_MAX split #148 describes
|
|
235
|
+
* on an ordinary small fixture instead of a ~1 MiB pathspec list — the same
|
|
236
|
+
* technique the ticket used to measure the bug. Both wrappers resolve the real
|
|
237
|
+
* binary once up front — outside the shadowed PATH, so the lookup cannot
|
|
238
|
+
* recurse into the wrapper — and then `exec` it, so nothing else changes.
|
|
239
|
+
*
|
|
240
|
+
* `git` is shadowed too, and only to COUNT the batches: it records every
|
|
241
|
+
* invocation carrying a `:(literal)` pathspec, which is the one call xargs
|
|
242
|
+
* drives, so the count IS the number of batches. Without it the split is an
|
|
243
|
+
* unasserted side condition and the test is only conditionally a test — an
|
|
244
|
+
* xargs that clamps or ignores a small `-s` runs the whole fixture in one
|
|
245
|
+
* batch, and then every assertion below passes with the dedupe DELETED,
|
|
246
|
+
* measured end to end. `batches()` is what makes that platform fail red
|
|
247
|
+
* instead of green-on-nothing.
|
|
248
|
+
*/
|
|
249
|
+
function withSplitXargs(t, size = 300) {
|
|
250
|
+
const bin = mkdtempSync(join(tmpdir(), "no-undo-audit-xargs-"));
|
|
251
|
+
t.after(() => rmSync(bin, { recursive: true, force: true }));
|
|
252
|
+
const log = join(bin, "batches");
|
|
253
|
+
const shim = (name, body) => {
|
|
254
|
+
const real = execFileSync("sh", ["-c", `command -v ${name}`], { encoding: "utf8" }).trim();
|
|
255
|
+
writeFileSync(join(bin, name), `#!/bin/sh\n${body}exec ${real} "$@"\n`);
|
|
256
|
+
chmodSync(join(bin, name), 0o755);
|
|
257
|
+
};
|
|
258
|
+
shim("xargs", `set -- -s ${size} "$@"\n`);
|
|
259
|
+
shim("git", `case " $* " in *':(literal)'*) echo x >>"${log}" ;; esac\n`);
|
|
260
|
+
return {
|
|
261
|
+
path: `${bin}:${process.env.PATH}`,
|
|
262
|
+
batches: () => (existsSync(log) ? readFileSync(log, "utf8").split("\n").length - 1 : 0),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* A wrapper factory for the two shims below: resolves the real binary once, up
|
|
268
|
+
* front and outside the shadowed PATH so the lookup cannot recurse into the
|
|
269
|
+
* wrapper, then writes an executable of the given body. Same technique as
|
|
270
|
+
* `withSplitXargs`, kept separate because these two need a whole script rather
|
|
271
|
+
* than a prefix ahead of a trailing `exec`.
|
|
272
|
+
*/
|
|
273
|
+
function shimDir(t, prefix) {
|
|
274
|
+
const bin = mkdtempSync(join(tmpdir(), prefix));
|
|
275
|
+
t.after(() => rmSync(bin, { recursive: true, force: true }));
|
|
276
|
+
return {
|
|
277
|
+
bin,
|
|
278
|
+
real: (name) => execFileSync("sh", ["-c", `command -v ${name}`], { encoding: "utf8" }).trim(),
|
|
279
|
+
write: (name, body) => {
|
|
280
|
+
writeFileSync(join(bin, name), `#!/bin/sh\n${body}`);
|
|
281
|
+
chmodSync(join(bin, name), 0o755);
|
|
282
|
+
},
|
|
283
|
+
path: () => `${bin}:${process.env.PATH}`,
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Makes the file holding merge-tree's `-z` output unreadable at the moment the
|
|
289
|
+
* audit reads it, and not one step earlier: the `git` wrapper chmods it only
|
|
290
|
+
* after the real `merge-tree` has written and exited, so the emptiness guard
|
|
291
|
+
* that sits between the write and the read still sees a non-empty file and
|
|
292
|
+
* passes. That ordering is the whole point — the fault has to land on the stage
|
|
293
|
+
* that READS, which is the stage a pipeline's exit status does not report.
|
|
294
|
+
*
|
|
295
|
+
* `chmod 000` rather than `rm` for the same reason: a removed file fails the
|
|
296
|
+
* emptiness guard instead, and the test would then be green on a refusal raised
|
|
297
|
+
* by a different guard about a different fault, proving nothing about this one.
|
|
298
|
+
*
|
|
299
|
+
* The `mktemp` wrapper exists only to learn the name of a temporary that is
|
|
300
|
+
* otherwise private to the script. It records the FIRST one, which is the file
|
|
301
|
+
* merge-tree writes; a later temporary is left alone.
|
|
302
|
+
*/
|
|
303
|
+
function withUnreadableMergeTreeOutput(t) {
|
|
304
|
+
const s = shimDir(t, "no-undo-audit-mtout-");
|
|
305
|
+
const recorded = join(s.bin, "first-temp-path");
|
|
306
|
+
s.write("mktemp", `f=$(${s.real("mktemp")} "$@") || exit $?
|
|
307
|
+
[ -e "${recorded}" ] || printf '%s\\n' "$f" >"${recorded}"
|
|
308
|
+
printf '%s\\n' "$f"
|
|
309
|
+
`);
|
|
310
|
+
s.write("git", `case " $* " in
|
|
311
|
+
*" merge-tree "*)
|
|
312
|
+
${s.real("git")} "$@"
|
|
313
|
+
rc=$?
|
|
314
|
+
chmod 000 "$(cat "${recorded}")"
|
|
315
|
+
exit $rc ;;
|
|
316
|
+
esac
|
|
317
|
+
exec ${s.real("git")} "$@"
|
|
318
|
+
`);
|
|
319
|
+
return s.path();
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Fails every `mktemp` after the first, leaving merge-tree's own temporary
|
|
324
|
+
* intact so the run reaches the point where a second one is asked for. A full
|
|
325
|
+
* TMPDIR or a TMPDIR that has gone missing does this for real.
|
|
326
|
+
*
|
|
327
|
+
* The path the first call handed back is recorded, not merely the fact that a
|
|
328
|
+
* call happened, because the abort this injects is also the abort that has to
|
|
329
|
+
* clean up after itself: the caller reads `firstTemp` to check the file is gone
|
|
330
|
+
* once the run has exited. Recording it is what lets the assertion name a real
|
|
331
|
+
* path rather than trusting the trap.
|
|
332
|
+
*/
|
|
333
|
+
function withLaterMktempFailing(t) {
|
|
334
|
+
const s = shimDir(t, "no-undo-audit-mktemp-");
|
|
335
|
+
const firstTemp = join(s.bin, "first-temp-path");
|
|
336
|
+
s.write("mktemp", `if [ -e "${firstTemp}" ]; then
|
|
337
|
+
echo "mktemp: failed to create file" >&2
|
|
338
|
+
exit 1
|
|
339
|
+
fi
|
|
340
|
+
f=$(${s.real("mktemp")} "$@") || exit $?
|
|
341
|
+
printf '%s\n' "$f" >"${firstTemp}"
|
|
342
|
+
printf '%s\n' "$f"
|
|
343
|
+
`);
|
|
344
|
+
return { path: s.path(), firstTemp };
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Add/add conflicts on enough paths, each named long enough, that merge-tree's PROSE tail — the
|
|
349
|
+
* `Auto-merging`/`CONFLICT` section that follows the empty record, and the part
|
|
350
|
+
* of the output the audit deliberately never wants — is larger than a pipe
|
|
351
|
+
* buffer.
|
|
352
|
+
*
|
|
353
|
+
* That size is the whole fixture. The reader stops at the empty record by
|
|
354
|
+
* design, so on any conflicted run it leaves the tail unread; once the tail no
|
|
355
|
+
* longer fits in the buffer, whatever is upstream of the reader is still trying
|
|
356
|
+
* to write when the reader goes away. A `tr | tr | awk` reads back
|
|
357
|
+
* `141 141 0 0` there — two stages killed by SIGPIPE on a run whose answer is
|
|
358
|
+
* completely CORRECT. Anything that adopts a prefix stage's status as the
|
|
359
|
+
* pipeline's own, `set -o pipefail` most obviously, converts exactly this run
|
|
360
|
+
* into a refusal. So the fixture is a control: it must keep answering.
|
|
361
|
+
*
|
|
362
|
+
* One main commit, not `manyConflictsTwoCommits`' two — the dedupe across xargs
|
|
363
|
+
* batches is that fixture's claim and not this one's.
|
|
364
|
+
*/
|
|
365
|
+
function longTailConflictRepo(t) {
|
|
366
|
+
const c = repo(t);
|
|
367
|
+
const paths = Array.from({ length: 200 }, (_, i) => `${String(i).padStart(3, "0")}-${"x".repeat(220)}.txt`);
|
|
368
|
+
git(c.w, "checkout", "-q", "main");
|
|
369
|
+
for (const p of paths) writeFileSync(join(c.w, p), "MAIN SIDE\n");
|
|
370
|
+
git(c.w, "add", "--", ...paths);
|
|
371
|
+
git(c.w, "commit", "-q", "-m", "MAIN COMMIT AT RISK");
|
|
372
|
+
git(c.w, "push", "-q", "origin", "main");
|
|
373
|
+
git(c.w, "checkout", "-q", c.branch);
|
|
374
|
+
for (const p of paths) writeFileSync(join(c.w, p), "branch side\n");
|
|
375
|
+
git(c.w, "add", "--", ...paths);
|
|
376
|
+
git(c.w, "commit", "-q", "-m", "branch edits every file");
|
|
377
|
+
git(c.w, "push", "-q", "origin", c.branch);
|
|
378
|
+
return { ...c, paths };
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** Bytes of merge-tree's output after the empty record — the part left unread. */
|
|
382
|
+
function unreadTailBytes({ w, branch }) {
|
|
383
|
+
const mt = spawnSync("git", ["-C", w, "merge-tree", "--write-tree", "--name-only", "-z", "origin/main", `origin/${branch}`],
|
|
384
|
+
{ env: ENV, maxBuffer: 1 << 27 });
|
|
385
|
+
const end = mt.stdout.indexOf(Buffer.from([0, 0]));
|
|
386
|
+
assert.notEqual(end, -1, "merge-tree must emit the empty record that ends the filename section");
|
|
387
|
+
return mt.stdout.length - (end + 2);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* A linked worktree NESTED inside the clone, `.worktrees/` gitignored — the
|
|
392
|
+
* fleet's own layout, and the only one where breaking the linkage is dangerous:
|
|
393
|
+
* an enclosing repo is standing by to answer in the worktree's place, and being
|
|
394
|
+
* clean it answers "nothing uncommitted here". A worktree with no repo above it
|
|
395
|
+
* has nothing to walk up to, so git fails there and the script already refuses.
|
|
396
|
+
* `precious.txt` is the uncommitted work that exists nowhere else.
|
|
397
|
+
*
|
|
398
|
+
* The directory is made BEFORE `worktree add`, not left for `git` to create
|
|
399
|
+
* it: a leaf `git` creates itself comes back reprecomposed under
|
|
400
|
+
* `core.precomposeunicode` even when `name` is passed decomposed (measured),
|
|
401
|
+
* so a caller wanting an on-disk NFD leaf (#2095) needs the directory to
|
|
402
|
+
* already exist — `git` then uses it as-is, byte for byte.
|
|
403
|
+
*/
|
|
404
|
+
function nestedWorktree(t, name = "9-x") {
|
|
405
|
+
const branch = "fix/9-nested";
|
|
406
|
+
const c = repo(t);
|
|
407
|
+
writeFileSync(join(c.w, ".gitignore"), ".worktrees/\n");
|
|
408
|
+
git(c.w, "add", ".gitignore");
|
|
409
|
+
git(c.w, "commit", "-q", "-m", "ignore the nested worktree");
|
|
410
|
+
const w = join(c.w, ".worktrees", name);
|
|
411
|
+
mkdirSync(w, { recursive: true });
|
|
412
|
+
git(c.w, "worktree", "add", "-q", "-b", branch, w);
|
|
413
|
+
git(w, "push", "-q", "-u", "origin", branch);
|
|
414
|
+
writeFileSync(join(w, "precious.txt"), "work that exists nowhere else\n");
|
|
415
|
+
return { parent: c.w, w, branch };
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* `nestedWorktree` plus a SECOND linked worktree under the same parent,
|
|
420
|
+
* named `8-y` to match the ticket's own repro. `sibling` is where it lives;
|
|
421
|
+
* `siblingAdmin` is its admin dir — the thing #189's spoof names in place of
|
|
422
|
+
* $wt's own.
|
|
423
|
+
*/
|
|
424
|
+
function nestedWorktreePair(t) {
|
|
425
|
+
const c = nestedWorktree(t);
|
|
426
|
+
const sibling = join(c.parent, ".worktrees", "8-y");
|
|
427
|
+
git(c.parent, "worktree", "add", "-q", "-b", "fix/8-sibling", sibling);
|
|
428
|
+
// Asked of git itself, not built with `join`: on macOS `tmpdir()` sits under
|
|
429
|
+
// a `/var` that is itself a symlink to `/private/var`, and git's own
|
|
430
|
+
// `--git-dir` answers with the resolved form. A hand-joined path would
|
|
431
|
+
// still WORK if spoofed into a `.git` file — the filesystem resolves either
|
|
432
|
+
// spelling — but comparing it against a later `--git-dir` call in a test
|
|
433
|
+
// assertion needs the same spelling git itself produces.
|
|
434
|
+
const siblingAdmin = git(sibling, "rev-parse", "--git-dir");
|
|
435
|
+
return { ...c, sibling, siblingAdmin };
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// The payload is parsed here rather than at the call site: "the audit passed and
|
|
439
|
+
// then the caller crashed on its own stdout" is a distinct outcome from "the
|
|
440
|
+
// audit refused", and a test cannot tell them apart if the parse throws inside
|
|
441
|
+
// the helper that also reports the exit code.
|
|
442
|
+
// `cwd` defaults to `$wt`, which is what every fixture predating #376 wants.
|
|
443
|
+
// The real caller runs from its own directory; the cwd-independence test below
|
|
444
|
+
// passes that in rather than relying on the two happening to coincide.
|
|
445
|
+
const audit = ({ w, branch }, env = ENV, cwd = w) => {
|
|
446
|
+
const r = spawnSync("sh", [SCRIPT, w, branch], { cwd, env, encoding: "utf8" });
|
|
447
|
+
const out = r.stdout.trim();
|
|
448
|
+
let json = null;
|
|
449
|
+
let jsonError = null;
|
|
450
|
+
if (out) {
|
|
451
|
+
try {
|
|
452
|
+
json = JSON.parse(out);
|
|
453
|
+
} catch (e) {
|
|
454
|
+
jsonError = e;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return { ...r, json, jsonError };
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Runs the audit with its own stderr piped to a reader that exits after one
|
|
462
|
+
* line — a genuine SIGPIPE (#1571), not the closed-fd-before-launch shape
|
|
463
|
+
* `2>&-` gives the tests above it: here the descriptor is live and writable
|
|
464
|
+
* right up until the reader goes away mid run.
|
|
465
|
+
*
|
|
466
|
+
* `spawnSync`'s own `status`/`signal` fields cannot carry this: node reads
|
|
467
|
+
* the child's stdio through its OWN pipes, and destroying node's end races
|
|
468
|
+
* the child's writes without ever reproducing a real reader closing a real
|
|
469
|
+
* pipe (measured — it left the unfixed script un-killed as often as not).
|
|
470
|
+
* A `head -1` in a real shell pipeline is what actually closes the
|
|
471
|
+
* descriptor, and what is left to report the audit's exit code is `sh`
|
|
472
|
+
* itself, the same way an unattended caller would read it — `$?` — not
|
|
473
|
+
* node's process object. `printf … >&3` carries that `$?` around the pipe
|
|
474
|
+
* that would otherwise discard it: `3>&1` on the outer group aliases fd 3 to
|
|
475
|
+
* the descriptor `spawnSync` is already reading as `r.stdout`, and the
|
|
476
|
+
* pipeline's left-hand brace group inherits that alias directly — `$0`,
|
|
477
|
+
* `$1`, `$2` are already this `sh -c`'s own positionals, so nothing needs
|
|
478
|
+
* re-passing into a second shell.
|
|
479
|
+
*/
|
|
480
|
+
function earlyClosingStderrReaderStatus({ w, branch }, cwd = w) {
|
|
481
|
+
const wrapper = '{ { sh "$0" "$1" "$2" 2>&1 >/dev/null; printf "RC=%s" "$?" >&3; } '
|
|
482
|
+
+ '| head -1 >/dev/null; } 3>&1';
|
|
483
|
+
const r = spawnSync("sh", ["-c", wrapper, SCRIPT, w, branch], { cwd, env: ENV, encoding: "utf8" });
|
|
484
|
+
const m = /^RC=(\d+)$/.exec(r.stdout);
|
|
485
|
+
assert.ok(m, `wrapper must report the audit's own exit code; got ${JSON.stringify(r)}`);
|
|
486
|
+
return Number(m[1]);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/** `atRisk` with the abbreviated SHA stripped, so a test can pin the exact set. */
|
|
490
|
+
const subjects = (r) => r.json.atRisk.map((l) => l.replace(/^\S+ /, ""));
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* The audit's own stash line, pulled out of stderr whole. Pinned as a literal
|
|
494
|
+
* rather than a substring match: #304 appends git's diagnostic to this line,
|
|
495
|
+
* and the thing that must not happen is a separator appended with nothing
|
|
496
|
+
* after it — which every `/unknown/` match above would still pass.
|
|
497
|
+
*/
|
|
498
|
+
const UNKNOWN_LINE =
|
|
499
|
+
" stash entries (repo-global, not gated): unknown — the list came back empty but refs/stash is not absent (an unreadable ref or reflog, or a ref pointing at a missing object)";
|
|
500
|
+
/**
|
|
501
|
+
* #376's line, and deliberately NOT the one above: there `refs/stash` is
|
|
502
|
+
* present and unreadable, here it is gone while its reflog is not. Reusing
|
|
503
|
+
* UNKNOWN_LINE would tell the operator "refs/stash is not absent" about a
|
|
504
|
+
* state whose whole shape is that it IS, and send them to `ls -l` on a file
|
|
505
|
+
* that no longer exists.
|
|
506
|
+
*/
|
|
507
|
+
const ORPHAN_LINE =
|
|
508
|
+
" stash entries (repo-global, not gated): unknown — refs/stash is absent but its reflog is not, and still names entries no ref points at";
|
|
509
|
+
/**
|
|
510
|
+
* #482's line, and the third of the three: the two above are reached with an
|
|
511
|
+
* EMPTY list, this one with entries printed and a nonzero rc. Literal for the
|
|
512
|
+
* same reason they are — `/unknown/` passes on any wording, so a reword naming
|
|
513
|
+
* the wrong call as the one that failed would go unseen. git's own diagnostic
|
|
514
|
+
* is appended after it, so what is pinned here is the prefix, not the line.
|
|
515
|
+
*/
|
|
516
|
+
const RC_FAILED_LINE =
|
|
517
|
+
" stash entries (repo-global, not gated): unknown — the list call itself failed, so what it printed cannot be read as a count";
|
|
518
|
+
/**
|
|
519
|
+
* #570's line, and the fourth. Of the three above only ORPHAN_LINE is reached
|
|
520
|
+
* with the reflog path in hand: the resolution is guarded on an empty list AND
|
|
521
|
+
* an absent ref, so UNKNOWN_LINE (ref present) and RC_FAILED_LINE (entries
|
|
522
|
+
* printed) never ask for the path at all — measured. This one is reached
|
|
523
|
+
* because asking for it FAILED. So it says only that the reflog could not be
|
|
524
|
+
* reached and nothing about what it holds — ORPHAN_LINE's claim that the
|
|
525
|
+
* reflog "still names entries no ref points at" is a claim about contents this
|
|
526
|
+
* state has not read and cannot make.
|
|
527
|
+
*/
|
|
528
|
+
const UNREACHED_LINE =
|
|
529
|
+
" stash entries (repo-global, not gated): unknown — the reflog path could not be resolved, so the reflog could not be read";
|
|
530
|
+
const stashLine = (r) => r.stderr.split("\n").find((l) => l.includes("stash entries (repo-global"));
|
|
531
|
+
/**
|
|
532
|
+
* `stashLine`'s sibling: every matching line, not just the first. `.find()`
|
|
533
|
+
* above answers "what is the first sentence", and nothing built on it can
|
|
534
|
+
* therefore see a SECOND stash-entry line appear — so a future edit that
|
|
535
|
+
* breaks the if/elif/elif/else below into independent `if`s, letting two
|
|
536
|
+
* arms fire for one state, would go unnoticed. The design rule for that
|
|
537
|
+
* chain is one sentence per state — each `elif` branch names its own state
|
|
538
|
+
* and prints exactly one line for it (no-undo-audit.sh:424-431, 490-505);
|
|
539
|
+
* the length-1 assertions below are what actually enforces it. #1210.
|
|
540
|
+
*/
|
|
541
|
+
const stashLines = (r) => r.stderr.split("\n").filter((l) => l.includes("stash entries (repo-global"));
|
|
542
|
+
|
|
543
|
+
// `$wt` is caller-supplied and reaches the operator through a step header.
|
|
544
|
+
// Under `#!/bin/sh` an `echo` operand expands escapes, so a worktree whose
|
|
545
|
+
// name holds `\c` truncated that header and the next stderr line landed on
|
|
546
|
+
// top of it. No corrupt repo needed — which makes this a strictly more
|
|
547
|
+
// reachable instance of the same hazard as the stash line's, and the reason
|
|
548
|
+
// `printf` is used at both sites.
|
|
549
|
+
test("a worktree path holding a backslash escape reaches the operator whole", (t) => {
|
|
550
|
+
const c = repo(t, "fix/1-thing", "no-undo-audit-back\\clue-");
|
|
551
|
+
assert.match(c.w, /back\\clue/, "fixture must actually put a `\\c` in the path");
|
|
552
|
+
|
|
553
|
+
const r = audit(c);
|
|
554
|
+
assert.equal(r.status, 0, `fixture must be clean; got ${r.status} ${r.stderr}`);
|
|
555
|
+
assert.ok(
|
|
556
|
+
r.stderr.includes(`$ git -C ${c.w} status --porcelain`),
|
|
557
|
+
"`echo` truncates the header at the `\\c` — it must name the worktree verbatim",
|
|
558
|
+
);
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
// The same hazard on the refusal path. `die` interpolates `$wt` into 11 of
|
|
562
|
+
// its messages and is the single place they all route through, so one `printf`
|
|
563
|
+
// covers every one of them.
|
|
564
|
+
test("a die message naming an unreachable worktree keeps the path whole", (t) => {
|
|
565
|
+
const c = repo(t);
|
|
566
|
+
const notARepo = `${c.w}-back\\clue-notarepo`;
|
|
567
|
+
mkdirSync(notARepo);
|
|
568
|
+
|
|
569
|
+
const r = audit({ w: notARepo, branch: c.branch });
|
|
570
|
+
assert.equal(r.status, 2, `a non-worktree is unanswerable, not a refusal; got ${r.status} ${r.stderr}`);
|
|
571
|
+
assert.ok(
|
|
572
|
+
r.stderr.includes(`${notARepo} is not a git worktree`),
|
|
573
|
+
`\`echo\` truncates the refusal at the \`\\c\`; got ${JSON.stringify(r.stderr)}`,
|
|
574
|
+
);
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
// `$porcelain` carries the uncommitted file list — the audit's whole subject,
|
|
578
|
+
// and the lines it prints when it REFUSES. A filename is free to hold `\c`.
|
|
579
|
+
test("an uncommitted path holding a backslash escape survives the refusal listing", (t) => {
|
|
580
|
+
const c = repo(t);
|
|
581
|
+
writeFileSync(join(c.w, "back\\clue.txt"), "work that exists nowhere else\n");
|
|
582
|
+
|
|
583
|
+
const r = audit(c);
|
|
584
|
+
assert.equal(r.status, 1, `uncommitted work must refuse; got ${r.status} ${r.stderr}`);
|
|
585
|
+
// git C-quotes a path holding a backslash, so the bytes on the wire are `\\`.
|
|
586
|
+
// `echo` collapses that pair to one, silently rewriting the quoted path into
|
|
587
|
+
// a different one — corruption rather than truncation here, but on the very
|
|
588
|
+
// line the refusal prints. `-z` turns the same quoting OFF for the conflict
|
|
589
|
+
// list below, which is why that one truncates outright instead.
|
|
590
|
+
assert.ok(
|
|
591
|
+
r.stderr.includes('?? "back\\\\clue.txt"'),
|
|
592
|
+
`the C-quoted path must keep its doubled backslash; got ${JSON.stringify(r.stderr)}`,
|
|
593
|
+
);
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
test("a pre-existing stash does not refuse a clean worktree", (t) => {
|
|
597
|
+
const c = repo(t);
|
|
598
|
+
stashSomething(c.w);
|
|
599
|
+
assert.equal(git(c.w, "status", "--porcelain"), "", "fixture must leave the worktree clean");
|
|
600
|
+
assert.equal(git(c.w, "stash", "list").split("\n").filter(Boolean).length, 1, "fixture must leave one stash");
|
|
601
|
+
|
|
602
|
+
const r = audit(c);
|
|
603
|
+
assert.equal(r.status, 0, `a stash the rebase will not consume must not refuse; got ${r.status} ${r.stderr}`);
|
|
604
|
+
assert.equal(r.json.clean, true);
|
|
605
|
+
assert.equal(r.json.stash, 1, "the count is still reported, just not gated on");
|
|
606
|
+
assert.doesNotMatch(r.stderr, /REFUSED/);
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
test("several pre-existing stashes still do not refuse", (t) => {
|
|
610
|
+
const c = repo(t);
|
|
611
|
+
stashSomething(c.w, "h1.txt");
|
|
612
|
+
stashSomething(c.w, "h2.txt");
|
|
613
|
+
stashSomething(c.w, "h3.txt");
|
|
614
|
+
|
|
615
|
+
const r = audit(c);
|
|
616
|
+
assert.equal(r.status, 0, "the refusal must not scale with the count either");
|
|
617
|
+
assert.equal(r.json.stash, 3);
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
test("a dirty worktree still refuses, and names the worktree not the stash", (t) => {
|
|
621
|
+
const c = repo(t);
|
|
622
|
+
writeFileSync(join(c.w, "uncommitted.txt"), "work that exists nowhere else\n");
|
|
623
|
+
|
|
624
|
+
const r = audit(c);
|
|
625
|
+
assert.equal(r.status, 1, "uncommitted work is the whole point of this audit");
|
|
626
|
+
assert.equal(r.json.clean, false);
|
|
627
|
+
assert.match(r.stderr, /REFUSED/);
|
|
628
|
+
assert.match(r.stderr, /commit the worktree before rebasing/);
|
|
629
|
+
// The old message told the caller to clear the stash list and forbade
|
|
630
|
+
// `git stash drop` in the same refusal. Removing the gate must remove the
|
|
631
|
+
// instruction, or the contradiction outlives the bug.
|
|
632
|
+
assert.doesNotMatch(r.stderr, /stash-list-clear/);
|
|
633
|
+
// `git stash`, not `git stash drop`: stashing to clear a dirty worktree now
|
|
634
|
+
// leaves `clean` true, so this line is the only thing in the repo forbidding
|
|
635
|
+
// a maneuver nothing detects. Narrowing it back to `drop` reads like a
|
|
636
|
+
// consistency fix and silently reopens the hole.
|
|
637
|
+
assert.match(r.stderr, /`git stash` to make a rebase start/);
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
// #730 (see reap.sh's branch sweep for the full explanation) — a bare
|
|
641
|
+
// `--porcelain` reads `clean` over a dirty tree under
|
|
642
|
+
// `status.showUntrackedFiles = no`. Load-bearing here beyond a reap: the
|
|
643
|
+
// merge bot leans on this audit to authorize a REBASE, and the work a
|
|
644
|
+
// rebase replays over may exist nowhere else.
|
|
645
|
+
test("a dirty worktree still refuses under status.showUntrackedFiles=no (#730)", (t) => {
|
|
646
|
+
const c = repo(t);
|
|
647
|
+
writeFileSync(join(c.w, "uncommitted.txt"), "work that exists nowhere else\n");
|
|
648
|
+
git(c.w, "config", "status.showUntrackedFiles", "no");
|
|
649
|
+
assert.equal(git(c.w, "status", "--porcelain"), "",
|
|
650
|
+
"fixture: the config must really silence the unpinned probe, or this test measures nothing");
|
|
651
|
+
|
|
652
|
+
const r = audit(c);
|
|
653
|
+
assert.equal(r.status, 1, `a silenced probe must not become a clean verdict; got ${r.status} ${r.stderr}`);
|
|
654
|
+
assert.equal(r.json.clean, false);
|
|
655
|
+
assert.match(r.stderr, /REFUSED/);
|
|
656
|
+
assert.match(r.stderr, /commit the worktree before rebasing/);
|
|
657
|
+
// Exit 1 is the refusal that means dirty; exit 2 means unanswerable. A fix
|
|
658
|
+
// that turned the silenced answer into a refusal-to-answer would satisfy a
|
|
659
|
+
// bare "not 0" and report the wrong thing about a worktree git can read.
|
|
660
|
+
assert.match(r.stderr, /uncommitted\.txt/, "the file git could only see with the mode pinned must be named");
|
|
661
|
+
});
|
|
662
|
+
|
|
663
|
+
test("a dirty worktree refuses with an empty stash stack", (t) => {
|
|
664
|
+
const c = repo(t);
|
|
665
|
+
writeFileSync(join(c.w, "uncommitted.txt"), "work\n");
|
|
666
|
+
assert.equal(git(c.w, "stash", "list"), "", "fixture must leave no stash");
|
|
667
|
+
|
|
668
|
+
const r = audit(c);
|
|
669
|
+
assert.equal(r.status, 1, "clean is the sole gate — it must fire on its own");
|
|
670
|
+
assert.equal(r.json.stash, 0);
|
|
671
|
+
});
|
|
672
|
+
|
|
673
|
+
test("a clean worktree with no stash passes", (t) => {
|
|
674
|
+
const c = repo(t);
|
|
675
|
+
const r = audit(c);
|
|
676
|
+
assert.equal(r.status, 0);
|
|
677
|
+
assert.equal(r.json.clean, true);
|
|
678
|
+
assert.equal(r.json.stash, 0);
|
|
679
|
+
});
|
|
680
|
+
|
|
681
|
+
// #147: `git stash list` prints nothing at rc 0 when the reflog behind
|
|
682
|
+
// `refs/stash` cannot be read — no error to catch, so this used to report
|
|
683
|
+
// `stash: 0`, indistinguishable from the case right above, where there really
|
|
684
|
+
// is nothing. `show-ref` still finds `refs/stash` here (rc 0), which is what
|
|
685
|
+
// tells the two apart.
|
|
686
|
+
test("stash entries present but the reflog is unreadable reports unknown, not zero", (t) => {
|
|
687
|
+
if (process.getuid?.() === 0) return t.skip("root reads a 000 file regardless");
|
|
688
|
+
const c = repo(t);
|
|
689
|
+
stashSomething(c.w);
|
|
690
|
+
assert.equal(git(c.w, "stash", "list").split("\n").filter(Boolean).length, 1, "fixture must leave one stash");
|
|
691
|
+
chmodSync(join(c.w, ".git", "logs", "refs", "stash"), 0o000);
|
|
692
|
+
|
|
693
|
+
const r = audit(c);
|
|
694
|
+
assert.equal(r.status, 0, `unknown must not gate the audit; got ${r.status} ${r.stderr}`);
|
|
695
|
+
assert.equal(r.json.stash, null, "an unreadable reflog must report unknown, not the zero this used to silently report");
|
|
696
|
+
assert.match(r.stderr, /unknown/);
|
|
697
|
+
});
|
|
698
|
+
|
|
699
|
+
// The third trap, and the one a `rev-parse --verify` cross-check cannot see:
|
|
700
|
+
// the `refs/stash` FILE itself unreadable. The list comes back empty at rc 0
|
|
701
|
+
// exactly as above, but this time the ref does not resolve either — so
|
|
702
|
+
// rev-parse takes the genuinely-empty branch and prints the confident `0`
|
|
703
|
+
// that #147 exists to remove. `show-ref` returns 1 only for a ref that is
|
|
704
|
+
// genuinely ABSENT, and 128 for one that is there but unreadable, which is
|
|
705
|
+
// what keeps the two apart.
|
|
706
|
+
test("stash entries present but refs/stash is unreadable reports unknown, not zero", (t) => {
|
|
707
|
+
if (process.getuid?.() === 0) return t.skip("root reads a 000 file regardless");
|
|
708
|
+
const c = repo(t);
|
|
709
|
+
stashSomething(c.w);
|
|
710
|
+
assert.equal(git(c.w, "stash", "list").split("\n").filter(Boolean).length, 1, "fixture must leave one stash");
|
|
711
|
+
chmodSync(join(c.w, ".git", "refs", "stash"), 0o000);
|
|
712
|
+
|
|
713
|
+
const r = audit(c);
|
|
714
|
+
assert.equal(r.status, 0, `unknown must not gate the audit; got ${r.status} ${r.stderr}`);
|
|
715
|
+
assert.equal(r.json.stash, null, "an unreadable refs/stash must report unknown, not the zero a rev-parse cross-check reports");
|
|
716
|
+
assert.match(r.stderr, /unknown/);
|
|
717
|
+
});
|
|
718
|
+
|
|
719
|
+
// The second, different trap: a corrupted stash ref (missing object) makes
|
|
720
|
+
// `git stash list` itself fail — `fatal: bad object refs/stash`, rc 1 — but
|
|
721
|
+
// the pipeline's exit status was always `wc`'s, never git's, so an rc capture
|
|
722
|
+
// on the old pipeline would not have caught this either. `show-ref` fails
|
|
723
|
+
// here at rc 128 rather than the rc 1 that means genuinely absent, so the
|
|
724
|
+
// same cross-check catches this case too.
|
|
725
|
+
test("a corrupted stash ref reports unknown, not zero", (t) => {
|
|
726
|
+
const c = repo(t);
|
|
727
|
+
stashSomething(c.w);
|
|
728
|
+
const sha = git(c.w, "rev-parse", "refs/stash");
|
|
729
|
+
rmSync(join(c.w, ".git", "objects", sha.slice(0, 2), sha.slice(2)));
|
|
730
|
+
|
|
731
|
+
const r = audit(c);
|
|
732
|
+
assert.equal(r.status, 0, `unknown must not gate the audit; got ${r.status} ${r.stderr}`);
|
|
733
|
+
assert.equal(r.json.stash, null, "a corrupted stash object must report unknown, not zero");
|
|
734
|
+
assert.match(r.stderr, /unknown/);
|
|
735
|
+
});
|
|
736
|
+
|
|
737
|
+
// #304: of the states above, exactly one has git saying anything —
|
|
738
|
+
// `fatal: bad object refs/stash`. The counting pipeline's `2>/dev/null` threw
|
|
739
|
+
// it away, so the operator got the guess in place of the answer git had
|
|
740
|
+
// already named. Appended, never substituted: in the other states git is
|
|
741
|
+
// silent, and the generic line is all there is.
|
|
742
|
+
test("a corrupted stash ref passes git's own diagnostic through to the operator", (t) => {
|
|
743
|
+
const c = repo(t);
|
|
744
|
+
stashSomething(c.w);
|
|
745
|
+
const sha = git(c.w, "rev-parse", "refs/stash");
|
|
746
|
+
rmSync(join(c.w, ".git", "objects", sha.slice(0, 2), sha.slice(2)));
|
|
747
|
+
|
|
748
|
+
const r = audit(c);
|
|
749
|
+
assert.equal(r.status, 0, `unknown must not gate the audit; got ${r.status} ${r.stderr}`);
|
|
750
|
+
assert.equal(r.json.stash, null, "the diagnostic is stderr only — the payload still says unknown");
|
|
751
|
+
// The whole line, not `startsWith` plus a substring match: those two leave
|
|
752
|
+
// the text between them unconstrained, so `msg="$msg$diag"` — separator
|
|
753
|
+
// dropped entirely — satisfies both, as does any other joiner.
|
|
754
|
+
assert.equal(
|
|
755
|
+
stashLine(r),
|
|
756
|
+
`${UNKNOWN_LINE} — fatal: bad object refs/stash`,
|
|
757
|
+
"git named the fault; the audit must append it to the generic line, ` — ` and all, not substitute for it and not run it together",
|
|
758
|
+
);
|
|
759
|
+
assert.equal(stashLines(r).length, 1, "exactly one stash-entry line — a broken elif chain would let a second one through, #1210");
|
|
760
|
+
});
|
|
761
|
+
|
|
762
|
+
// #376: the fourth `show-ref` state, and the only one that used to print a
|
|
763
|
+
// number. The three above all leave `refs/stash` resolvable, so they land on
|
|
764
|
+
// rc 0 or rc 128 and trip the `-ne 1` guard. Delete the ref FILE and leave
|
|
765
|
+
// `.git/logs/refs/stash` behind and `show-ref` exits **1** — the exact rc that
|
|
766
|
+
// guard defines as genuine absence — while both stash commits are still named
|
|
767
|
+
// in the reflog and still reachable. `git stash list` is empty at rc 0, so
|
|
768
|
+
// nothing contradicts the `0`, and `0` is the value the runbook reads as
|
|
769
|
+
// nothing to look at before an irreversible rebase.
|
|
770
|
+
test("a deleted refs/stash with an intact reflog reports unknown, not zero", (t) => {
|
|
771
|
+
const c = repo(t);
|
|
772
|
+
stashSomething(c.w, "h1.txt");
|
|
773
|
+
stashSomething(c.w, "h2.txt");
|
|
774
|
+
rmSync(join(c.w, ".git", "refs", "stash")); // the ref file only — the reflog is untouched
|
|
775
|
+
|
|
776
|
+
// The three conditions that make this indistinguishable from an empty stash
|
|
777
|
+
// by everything the script asked before this change.
|
|
778
|
+
const reflog = readFileSync(join(c.w, ".git", "logs", "refs", "stash"), "utf8").split("\n").filter(Boolean);
|
|
779
|
+
assert.equal(reflog.length, 2, "fixture must leave both reflog entries behind");
|
|
780
|
+
const showRef = spawnSync("git", ["show-ref", "refs/stash"], { cwd: c.w, env: ENV, encoding: "utf8" });
|
|
781
|
+
assert.equal(showRef.status, 1, "the whole defect is this rc — the same one a genuinely empty stash gives");
|
|
782
|
+
assert.equal(git(c.w, "stash", "list"), "", "and the list agrees with it, at rc 0");
|
|
783
|
+
|
|
784
|
+
const r = audit(c);
|
|
785
|
+
assert.equal(r.status, 0, `an orphaned stash reflog is a report, not a refusal; got ${r.status} ${r.stderr}`);
|
|
786
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
787
|
+
assert.equal(r.json.stash, null, "two recoverable stash commits are still named in the reflog — `0` is not a claim the audit can make");
|
|
788
|
+
assert.equal(stashLine(r), ORPHAN_LINE);
|
|
789
|
+
assert.equal(stashLines(r).length, 1, "exactly one stash-entry line — a broken elif chain would let a second one through, #1210");
|
|
790
|
+
});
|
|
791
|
+
|
|
792
|
+
// The reflog path has to come from the repo's REAL gitdir. `$wt` is routinely a
|
|
793
|
+
// linked worktree — the fleet's own layout, which is where this script actually
|
|
794
|
+
// runs — and there `.git` is a FILE, so `$wt/.git/logs/refs/stash` reaches
|
|
795
|
+
// nothing. A probe built on that path reads "no reflog", takes the accept
|
|
796
|
+
// branch, and prints the same confident `0` this fix exists to remove, in the
|
|
797
|
+
// one layout that matters. The stash stack is repo-global, so the entries are
|
|
798
|
+
// created in the parent and seen from the worktree.
|
|
799
|
+
test("the orphaned-reflog probe resolves against the shared gitdir, not $wt/.git", (t) => {
|
|
800
|
+
const c = nestedWorktree(t);
|
|
801
|
+
rmSync(join(c.w, "precious.txt")); // this fixture is otherwise dirty, and dirty refuses at exit 1
|
|
802
|
+
stashSomething(c.parent, "h1.txt");
|
|
803
|
+
rmSync(join(c.parent, ".git", "refs", "stash"));
|
|
804
|
+
|
|
805
|
+
assert.ok(!existsSync(join(c.w, ".git", "logs", "refs", "stash")), "a naive $wt/.git path must find nothing here — that is what this test discriminates");
|
|
806
|
+
assert.ok(existsSync(join(c.parent, ".git", "logs", "refs", "stash")), "the reflog lives in the shared gitdir");
|
|
807
|
+
assert.equal(git(c.w, "status", "--porcelain"), "", "fixture must leave the worktree clean");
|
|
808
|
+
|
|
809
|
+
const r = audit(c);
|
|
810
|
+
assert.equal(r.status, 0, `got ${r.status} ${r.stderr}`);
|
|
811
|
+
assert.equal(r.json.stash, null, "the reflog is one directory up, and the probe has to follow git there");
|
|
812
|
+
assert.equal(stashLine(r), ORPHAN_LINE);
|
|
813
|
+
assert.equal(stashLines(r).length, 1, "exactly one stash-entry line — a broken elif chain would let a second one through, #1210");
|
|
814
|
+
});
|
|
815
|
+
|
|
816
|
+
// Every other fixture in this file spawns the script with `cwd === $wt`, which
|
|
817
|
+
// is not how it is called: `run-merge-bot.md` runs `no-undo-audit.sh <worktree>
|
|
818
|
+
// <branch>` from wherever the operator's shell already sits. `--git-path`
|
|
819
|
+
// answers relative to `-C` in a main checkout, so a probe that keeps that
|
|
820
|
+
// answer looks for the reflog under the CALLER's cwd, finds nothing, takes the
|
|
821
|
+
// accept branch and prints the confident `0` this fix exists to remove — in the
|
|
822
|
+
// one invocation that actually happens. Pinned as behaviour, not mechanism: the
|
|
823
|
+
// probe has to resolve from anywhere, and how the path is made absolute is
|
|
824
|
+
// git's business, not this test's.
|
|
825
|
+
test("the orphaned-reflog probe resolves from a cwd that is not $wt", (t) => {
|
|
826
|
+
const c = repo(t);
|
|
827
|
+
stashSomething(c.w, "h1.txt");
|
|
828
|
+
rmSync(join(c.w, ".git", "refs", "stash")); // the ref file only — the reflog is untouched
|
|
829
|
+
|
|
830
|
+
const r = audit(c, ENV, tmpdir());
|
|
831
|
+
assert.equal(r.status, 0, `got ${r.status} ${r.stderr}`);
|
|
832
|
+
assert.equal(r.json.stash, null, "the reflog is under $wt — resolved against the caller's cwd instead, it reads as absent and reports the `0` #376 removes");
|
|
833
|
+
assert.equal(stashLine(r), ORPHAN_LINE);
|
|
834
|
+
assert.equal(stashLines(r).length, 1, "exactly one stash-entry line — a broken elif chain would let a second one through, #1210");
|
|
835
|
+
});
|
|
836
|
+
|
|
837
|
+
// #570: the reflog path is resolved lazily, and resolving it needs search
|
|
838
|
+
// permission on every ancestor directory. An unsearchable `logs/refs` makes
|
|
839
|
+
// that call fail, and the `|| die` behind it turned a fault in a field the
|
|
840
|
+
// script declares "reported, not gated" into a refusal of the WHOLE audit —
|
|
841
|
+
// exit 2 with a zero-byte payload, withholding the `clean`, `conflicts` and
|
|
842
|
+
// `atRisk` answers the audit exists to give before an irreversible rebase.
|
|
843
|
+
// The suite already pins that rule twice above, with `unknown must not gate
|
|
844
|
+
// the audit`, for the unreadable-reflog FILE and unreadable-ref FILE faults;
|
|
845
|
+
// the unsearchable DIRECTORY is the same class.
|
|
846
|
+
//
|
|
847
|
+
// Both rows reach the probe: it is entered on an empty list plus a genuinely
|
|
848
|
+
// absent ref, which is what a never-stashed repo looks like and what an
|
|
849
|
+
// orphaned reflog looks like. So the common state — a repo that never stashed
|
|
850
|
+
// at all — is one of them, and needed no stash history to refuse.
|
|
851
|
+
//
|
|
852
|
+
// The die's stated rationale was that a failure here means the repo went away
|
|
853
|
+
// mid-run. These fixtures falsify it: the repository is entirely present, and
|
|
854
|
+
// the worktree status and three earlier revision resolutions have already
|
|
855
|
+
// succeeded on it. A repo that really goes away still refuses from the steps
|
|
856
|
+
// that need it — the status and merge-tree probes both die on their own.
|
|
857
|
+
for (const [why, prepare] of [
|
|
858
|
+
["a repository that never stashed", () => {}],
|
|
859
|
+
["a stash ref deleted while its reflog survived", (w) => { stashSomething(w); rmSync(join(w, ".git", "refs", "stash")); }],
|
|
860
|
+
]) {
|
|
861
|
+
test(`an unsearchable reflog directory reports unknown rather than refusing the whole audit — ${why}, #570`, (t) => {
|
|
862
|
+
if (process.getuid?.() === 0) return t.skip("root searches a 000 directory regardless");
|
|
863
|
+
const c = repo(t);
|
|
864
|
+
prepare(c.w);
|
|
865
|
+
const dir = join(c.w, ".git", "logs", "refs");
|
|
866
|
+
mkdirSync(dir, { recursive: true });
|
|
867
|
+
assert.equal(git(c.w, "stash", "list"), "", "both fixtures must leave the list empty — that is what reaches the probe");
|
|
868
|
+
chmodSync(dir, 0o000);
|
|
869
|
+
const probe = spawnSync("git", ["-C", c.w, "rev-parse", "--path-format=absolute", "--git-path", "logs/refs/stash"], { env: ENV, encoding: "utf8" });
|
|
870
|
+
assert.notEqual(probe.status, 0, "fixture must actually defeat the path resolution — that failure is the whole subject");
|
|
871
|
+
|
|
872
|
+
const r = audit(c);
|
|
873
|
+
// Restored here, not in a `t.after`: `repo` registers its own teardown
|
|
874
|
+
// first and node runs them in that order, so an rmSync that cannot recurse
|
|
875
|
+
// into a 000 directory fires before any later hook could reopen it.
|
|
876
|
+
chmodSync(dir, 0o755);
|
|
877
|
+
assert.equal(r.status, 0, `unknown must not gate the audit; got ${r.status} ${r.stderr}`);
|
|
878
|
+
assert.equal(r.jsonError, null, `payload must parse — this used to be zero bytes; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
879
|
+
assert.equal(r.json.stash, null, "the probe could not look, so a number is not a claim it can make");
|
|
880
|
+
assert.equal(stashLine(r), UNREACHED_LINE);
|
|
881
|
+
assert.equal(stashLines(r).length, 1, "exactly one stash-entry line — the ticket's own measured mutation (elif -> fi/if) doubles this state, #1210");
|
|
882
|
+
// The other half of the ruling: this state must not borrow the sentence
|
|
883
|
+
// that asserts what the reflog CONTAINS.
|
|
884
|
+
assert.notEqual(stashLine(r), ORPHAN_LINE);
|
|
885
|
+
// Degrading must not fall through to the confident `0` either: with the
|
|
886
|
+
// path unresolved, the `-s` test that guards the orphan branch is false
|
|
887
|
+
// against an empty string and the trailing branch prints the count.
|
|
888
|
+
assert.doesNotMatch(r.stderr, /stash entries \(repo-global, not gated\): 0$/m);
|
|
889
|
+
// git's own `fatal:` naming the path and the errno is the operator's whole
|
|
890
|
+
// lead on WHICH directory to reopen — the audit's own sentence names none.
|
|
891
|
+
// Unpinned, a `2>/dev/null` on that `rev-parse` deletes it silently: the
|
|
892
|
+
// mutation leaves every other assertion here green (measured).
|
|
893
|
+
assert.match(
|
|
894
|
+
r.stderr,
|
|
895
|
+
/fatal: .*logs\/refs\/stash.*Permission denied/,
|
|
896
|
+
"git's unwrapped diagnostic is the only thing naming the directory",
|
|
897
|
+
);
|
|
898
|
+
// The audit's real subject still answers, which is the point of degrading.
|
|
899
|
+
assert.equal(r.json.clean, true);
|
|
900
|
+
assert.deepEqual(r.json.conflicts, []);
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
// The control that keeps the guard above honest in the other direction, and
|
|
905
|
+
// the one state it must NOT reach. Not by the count: an unsearchable
|
|
906
|
+
// `logs/refs` empties `git stash list` too — it prints nothing at rc 0
|
|
907
|
+
// (measured) — so `$stash` is 0 here exactly as in the rows above. What keeps
|
|
908
|
+
// this state off #570's probe is `show-ref refs/stash` still answering rc 0,
|
|
909
|
+
// which fails the probe's `sr_rc = 1` guard, so an unsearchable directory
|
|
910
|
+
// lands on the existing empty-list unknown line, unchanged.
|
|
911
|
+
test("a healthy stash under an unsearchable reflog directory keeps the sentence it already printed, #570", (t) => {
|
|
912
|
+
if (process.getuid?.() === 0) return t.skip("root searches a 000 directory regardless");
|
|
913
|
+
const c = repo(t);
|
|
914
|
+
stashSomething(c.w);
|
|
915
|
+
const dir = join(c.w, ".git", "logs", "refs");
|
|
916
|
+
chmodSync(dir, 0o000);
|
|
917
|
+
|
|
918
|
+
const r = audit(c);
|
|
919
|
+
chmodSync(dir, 0o755); // see the sibling above — `repo`'s teardown runs before any `t.after` here
|
|
920
|
+
assert.equal(r.status, 0, `unknown must not gate the audit; got ${r.status} ${r.stderr}`);
|
|
921
|
+
assert.equal(r.json.stash, null);
|
|
922
|
+
assert.equal(stashLine(r), UNKNOWN_LINE, "this state never reaches #570's probe — its line must not move");
|
|
923
|
+
assert.equal(stashLines(r).length, 1, "exactly one stash-entry line — a broken elif chain would let a second one through, #1210");
|
|
924
|
+
});
|
|
925
|
+
|
|
926
|
+
// The other half of #376, and the more expensive one to get wrong. This script
|
|
927
|
+
// gates an irreversible action; an operator who sees `unknown` on every run
|
|
928
|
+
// stops reading it, and then the three states above go unread too. So the
|
|
929
|
+
// accept case is pinned explicitly, across every way a stack empties honestly.
|
|
930
|
+
//
|
|
931
|
+
// Measured, git 2.50.1: `pop`, `drop`, `clear` and `update-ref -d` each remove
|
|
932
|
+
// `.git/logs/refs/stash` outright — there is no lifecycle that empties the
|
|
933
|
+
// stack and leaves the reflog behind, which is why the probe can be this
|
|
934
|
+
// blunt. The zero-byte row is not reachable that way; it is here because
|
|
935
|
+
// `-s` and `-e` differ on exactly it, and a probe testing mere existence
|
|
936
|
+
// would turn it into `unknown` for nothing.
|
|
937
|
+
for (const [why, prepare] of [
|
|
938
|
+
["never stashed at all", () => {}],
|
|
939
|
+
// `pop` restores the entry staged, so the fixture has to put the worktree
|
|
940
|
+
// back itself — the audit refuses a dirty one at exit 1 before it ever
|
|
941
|
+
// reaches the stash line.
|
|
942
|
+
["stashed and popped", (w) => { stashSomething(w); git(w, "stash", "pop", "-q"); git(w, "rm", "-q", "-f", "h.txt"); }],
|
|
943
|
+
["stashed and dropped", (w) => { stashSomething(w); git(w, "stash", "drop", "-q"); }],
|
|
944
|
+
["stashed twice and cleared", (w) => { stashSomething(w, "h1.txt"); stashSomething(w, "h2.txt"); git(w, "stash", "clear"); }],
|
|
945
|
+
["the ref deleted with update-ref, which takes the reflog with it", (w) => { stashSomething(w); git(w, "update-ref", "-d", "refs/stash"); }],
|
|
946
|
+
["a zero-byte reflog left behind with no ref", (w) => { stashSomething(w); git(w, "stash", "clear"); mkdirSync(join(w, ".git", "logs", "refs"), { recursive: true }); writeFileSync(join(w, ".git", "logs", "refs", "stash"), ""); }],
|
|
947
|
+
]) {
|
|
948
|
+
test(`an honestly empty stash still reports a confident zero — ${why}`, (t) => {
|
|
949
|
+
const c = repo(t);
|
|
950
|
+
prepare(c.w);
|
|
951
|
+
assert.equal(git(c.w, "status", "--porcelain"), "", "fixture must leave the worktree clean");
|
|
952
|
+
assert.equal(git(c.w, "stash", "list"), "", "fixture must leave no stash");
|
|
953
|
+
|
|
954
|
+
const r = audit(c);
|
|
955
|
+
assert.equal(r.status, 0, `got ${r.status} ${r.stderr}`);
|
|
956
|
+
assert.equal(r.json.stash, 0, "nothing is recoverable here — reporting `unknown` would be a worse bug than the one #376 fixes");
|
|
957
|
+
assert.equal(stashLine(r), " stash entries (repo-global, not gated): 0");
|
|
958
|
+
assert.equal(stashLines(r).length, 1, "exactly one stash-entry line — a broken elif chain would let a second one through, #1210");
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
// #306: CHARACTERIZATION TEST, not a spec. The three tests above close every
|
|
963
|
+
// unreadable-reflog case that fails LOUDLY enough for the `show-ref`
|
|
964
|
+
// cross-check to notice. A reflog that is merely TRUNCATED — some lines
|
|
965
|
+
// gone, the rest still parses — is the one case left open, named in-line,
|
|
966
|
+
// right above the stash-counting pipeline this exercises, as "a remaining
|
|
967
|
+
// ceiling" (grep the phrase rather than a line number — those drift). This
|
|
968
|
+
// pins that known undercount so a later change to the block cannot silently
|
|
969
|
+
// move it. It does NOT assert desired behavior, and no detection logic is
|
|
970
|
+
// being added here — that was ruled out on the issue. If the gap is ever
|
|
971
|
+
// closed for real, this test goes red and whoever closed it deletes it
|
|
972
|
+
// deliberately; that is the point, not a regression.
|
|
973
|
+
test("a truncated-but-parseable stash reflog reports the too-low count as exact — pins a known ceiling, not desired behaviour, #306", (t) => {
|
|
974
|
+
// The citation above is an anchor, not decoration: the phrase it sends the
|
|
975
|
+
// next reader to grep for has to still be in the script.
|
|
976
|
+
assert.match(readFileSync(SCRIPT, "utf8"), /a remaining ceiling/, "no-undo-audit.sh no longer names the ceiling this test cites by phrase");
|
|
977
|
+
const c = repo(t);
|
|
978
|
+
stashSomething(c.w, "h1.txt");
|
|
979
|
+
stashSomething(c.w, "h2.txt");
|
|
980
|
+
stashSomething(c.w, "h3.txt");
|
|
981
|
+
assert.equal(git(c.w, "stash", "list").split("\n").filter(Boolean).length, 3, "fixture must leave three stashes");
|
|
982
|
+
|
|
983
|
+
// Drop the oldest reflog line — one per stash push, oldest first. The
|
|
984
|
+
// remaining lines still parse, so `stash list` resolves the ref and
|
|
985
|
+
// returns a shorter-but-nonempty list instead of failing loudly.
|
|
986
|
+
const reflogPath = join(c.w, ".git", "logs", "refs", "stash");
|
|
987
|
+
const lines = readFileSync(reflogPath, "utf8").split("\n").filter(Boolean);
|
|
988
|
+
writeFileSync(reflogPath, lines.slice(1).join("\n") + "\n");
|
|
989
|
+
|
|
990
|
+
// The two conditions that make the undercount invisible to the script's own
|
|
991
|
+
// cross-check: the list call itself does not fail, and `show-ref` still
|
|
992
|
+
// finds the ref. Neither signature the three tests above rely on fires.
|
|
993
|
+
const list = spawnSync("git", ["stash", "list"], { cwd: c.w, env: ENV, encoding: "utf8" });
|
|
994
|
+
assert.equal(list.status, 0, `truncation must not make the list call itself fail; got ${list.status} ${list.stderr}`);
|
|
995
|
+
assert.equal(list.stdout.split("\n").filter(Boolean).length, 2, "fixture must leave a nonempty, undercounted list");
|
|
996
|
+
const showRef = spawnSync("git", ["show-ref", "refs/stash"], { cwd: c.w, env: ENV, encoding: "utf8" });
|
|
997
|
+
assert.equal(showRef.status, 0, `refs/stash must still resolve — this is why the cross-check sees no disagreement; got ${showRef.status} ${showRef.stderr}`);
|
|
998
|
+
|
|
999
|
+
const r = audit(c);
|
|
1000
|
+
assert.equal(r.status, 0, `a truncated reflog must not gate the audit; got ${r.status} ${r.stderr}`);
|
|
1001
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
1002
|
+
assert.equal(r.json.stash, 2, "known-wrong: the true count is 3, and this pins the undercount printing as exact rather than `unknown`");
|
|
1003
|
+
assert.doesNotMatch(r.stderr, /unknown/, "the cross-check misses this state, which is the whole ceiling");
|
|
1004
|
+
assert.equal(stashLine(r), " stash entries (repo-global, not gated): 2", "the operator-facing line must print the undercount as a plain number");
|
|
1005
|
+
assert.equal(stashLines(r).length, 1, "exactly one stash-entry line — a broken elif chain would let a second one through, #1210");
|
|
1006
|
+
});
|
|
1007
|
+
|
|
1008
|
+
// #482: one shape of that ceiling, and the one that is not a ceiling any more.
|
|
1009
|
+
// Only this half of it — a MISSING non-tip object is skipped in silence at rc 0
|
|
1010
|
+
// and stays under #306, which is why this fixture corrupts rather than removes.
|
|
1011
|
+
// Corrupt the loose object behind a NON-TIP entry and the reflog stays
|
|
1012
|
+
// intact, so `stash list` resolves the ref, prints the entries it could read,
|
|
1013
|
+
// names the fault and exits 1 — a nonempty list at a nonzero rc, which is the
|
|
1014
|
+
// one shape none of the `show-ref` states above produce. The cross-check sees
|
|
1015
|
+
// no disagreement, so the rc is the whole signal, and the old pipeline gave
|
|
1016
|
+
// `wc`'s status instead of git's. The count printed was short by exactly the
|
|
1017
|
+
// entries git refused to read.
|
|
1018
|
+
test("a corrupt loose object behind a non-tip stash entry reports unknown, not the count git could not finish, #482", (t) => {
|
|
1019
|
+
const c = repo(t);
|
|
1020
|
+
stashSomething(c.w, "h1.txt");
|
|
1021
|
+
stashSomething(c.w, "h2.txt");
|
|
1022
|
+
stashSomething(c.w, "h3.txt");
|
|
1023
|
+
const sha = git(c.w, "rev-parse", "refs/stash@{2}"); // the OLDEST entry, not the tip
|
|
1024
|
+
const obj = join(c.w, ".git", "objects", sha.slice(0, 2), sha.slice(2));
|
|
1025
|
+
chmodSync(obj, 0o644); // loose objects are mode 444
|
|
1026
|
+
writeFileSync(obj, "junk\n");
|
|
1027
|
+
|
|
1028
|
+
// The signature that makes this state its own: git fails, and it fails
|
|
1029
|
+
// AFTER printing entries. Measured here rather than asserted in prose,
|
|
1030
|
+
// because both halves are what the fix reads and neither is obvious.
|
|
1031
|
+
const list = spawnSync("git", ["stash", "list"], { cwd: c.w, env: ENV, encoding: "utf8" });
|
|
1032
|
+
assert.equal(list.status, 1, `a corrupt non-tip object must make the list call fail; got ${list.status} ${list.stderr}`);
|
|
1033
|
+
assert.equal(list.stdout.split("\n").filter(Boolean).length, 2, "fixture must leave a nonempty, undercounted list — an empty one lands on the show-ref branch instead");
|
|
1034
|
+
const showRef = spawnSync("git", ["show-ref", "refs/stash"], { cwd: c.w, env: ENV, encoding: "utf8" });
|
|
1035
|
+
assert.equal(showRef.status, 0, `refs/stash must still resolve — this is why the cross-check cannot see it; got ${showRef.status} ${showRef.stderr}`);
|
|
1036
|
+
const reflog = readFileSync(join(c.w, ".git", "logs", "refs", "stash"), "utf8").split("\n").filter(Boolean);
|
|
1037
|
+
assert.equal(reflog.length, 3, "the reflog is intact — the true count is 3, which is what makes the printed 2 a lie rather than a limit");
|
|
1038
|
+
|
|
1039
|
+
const r = audit(c);
|
|
1040
|
+
assert.equal(r.status, 0, `unknown must not gate the audit; got ${r.status} ${r.stderr}`);
|
|
1041
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
1042
|
+
assert.equal(r.json.stash, null, "a list call that failed is not a count — the payload must say unknown, never the short number");
|
|
1043
|
+
assert.equal(stashLine(r).slice(0, RC_FAILED_LINE.length), RC_FAILED_LINE, "the operator-facing line must name THIS cause, not just say unknown — the other two say the list came back empty, which this state is not");
|
|
1044
|
+
assert.match(stashLine(r), /fatal: loose object \S+ .* is corrupt/, "git named the fault outright; the audit must pass it through rather than guess");
|
|
1045
|
+
assert.equal(stashLines(r).length, 1, "exactly one stash-entry line — a broken elif chain would let a second one through, #1210");
|
|
1046
|
+
});
|
|
1047
|
+
|
|
1048
|
+
// A stash object that is CORRUPT rather than missing reaches the same branch —
|
|
1049
|
+
// list empty at rc 1, show-ref rc 0 — but git answers in SEVEN lines there, and
|
|
1050
|
+
// a bad `objects/info/alternates` both adds three more and puts a backslash in
|
|
1051
|
+
// them. That fixture is what makes the two hazards of `msg="$msg — $diag";
|
|
1052
|
+
// echo "$msg"` observable at once: unfolded newlines put git's text at column 0
|
|
1053
|
+
// where only the audit's own `$ git ...` step headers belong, and `echo` under
|
|
1054
|
+
// `#!/bin/sh` eats the operator's line from the `\c` onward.
|
|
1055
|
+
//
|
|
1056
|
+
// The alternates path also makes git's LATER calls complain at column 0, which
|
|
1057
|
+
// is why the fold assertion below is scoped to the diagnostic's own text rather
|
|
1058
|
+
// than to every unindented line.
|
|
1059
|
+
test("a multi-line diagnostic holding a backslash arrives folded and whole", (t) => {
|
|
1060
|
+
const c = repo(t);
|
|
1061
|
+
stashSomething(c.w);
|
|
1062
|
+
const sha = git(c.w, "rev-parse", "refs/stash");
|
|
1063
|
+
const obj = join(c.w, ".git", "objects", sha.slice(0, 2), sha.slice(2));
|
|
1064
|
+
chmodSync(obj, 0o644);
|
|
1065
|
+
writeFileSync(obj, "junk\n");
|
|
1066
|
+
|
|
1067
|
+
// The rc both comments name for this state, measured rather than asserted in
|
|
1068
|
+
// prose (#494). `stash list` FAILS here, unlike the states it stays silent
|
|
1069
|
+
// about — and the counting pipeline takes `wc`'s status, so the branch still
|
|
1070
|
+
// fires and the rc is discarded. That discard is why a wrong rc in the prose
|
|
1071
|
+
// above could sit here for as long as it did without a test noticing.
|
|
1072
|
+
const list = spawnSync("git", ["stash", "list"], { cwd: c.w, env: ENV, encoding: "utf8" });
|
|
1073
|
+
assert.equal(list.status, 1, `a corrupt tip object must make the list call itself fail; got ${list.status} ${list.stderr}`);
|
|
1074
|
+
|
|
1075
|
+
mkdirSync(join(c.w, ".git", "objects", "info"), { recursive: true });
|
|
1076
|
+
writeFileSync(join(c.w, ".git", "objects", "info", "alternates"), "/no\\clue/objects\n");
|
|
1077
|
+
|
|
1078
|
+
const r = audit(c);
|
|
1079
|
+
assert.equal(r.json.stash, null, "a corrupt (not missing) stash object must reach the unknown branch too");
|
|
1080
|
+
assert.match(stashLine(r), /fatal: loose object \S+ .* is corrupt/, "git named the fault; the audit must not drop it");
|
|
1081
|
+
assert.match(
|
|
1082
|
+
stashLine(r),
|
|
1083
|
+
/\/no\\clue\/objects/,
|
|
1084
|
+
"`echo` expands the `\\c` and truncates the line there — the path must arrive verbatim",
|
|
1085
|
+
);
|
|
1086
|
+
assert.equal(stashLines(r).length, 1, "exactly one stash-entry line — a broken elif chain would let a second one through, #1210");
|
|
1087
|
+
const atColumn0 = r.stderr.split("\n").filter((l) => /^\S/.test(l) && /loose object|unable to unpack|inflate/.test(l));
|
|
1088
|
+
assert.deepEqual(atColumn0, [], "git's diagnostic belongs folded into the audit's own indented line, never at column 0");
|
|
1089
|
+
});
|
|
1090
|
+
|
|
1091
|
+
// Its own test, not a line inside the behaviour test above: `assert` aborts the
|
|
1092
|
+
// whole test function, so a prose drift ahead of `audit(c)` would pre-empt this
|
|
1093
|
+
// file's only coverage of the #304 fold and `printf` hazards, and report the
|
|
1094
|
+
// comment instead of the behaviour. Measured — with the fold dropped AND the
|
|
1095
|
+
// comment reverted, the fold regression went unnamed and only the comment was
|
|
1096
|
+
// reported.
|
|
1097
|
+
test("no-undo-audit.sh's own comment names the rc the fixture above measures", () => {
|
|
1098
|
+
// Bounded to the paragraph under test, not matched against the whole file: an
|
|
1099
|
+
// unbounded end lets a later, unrelated occurrence of the phrase satisfy this
|
|
1100
|
+
// after the real clause is deleted. Measured — that false green reproduces
|
|
1101
|
+
// against an unbounded match and reds here. The gutter comes off before
|
|
1102
|
+
// `phrase`'s wrap-tolerant `\s+`, because the clause is hard-wrapped and it is
|
|
1103
|
+
// the `#`, not whitespace, that sits at the break.
|
|
1104
|
+
const paragraph = between(
|
|
1105
|
+
stripHashGutter(readFileSync(SCRIPT, "utf8")),
|
|
1106
|
+
"A stash object that is CORRUPT",
|
|
1107
|
+
"`printf`, not `echo`",
|
|
1108
|
+
"no-undo-audit.sh",
|
|
1109
|
+
);
|
|
1110
|
+
assert.match(
|
|
1111
|
+
paragraph,
|
|
1112
|
+
phrase("list empty at rc 1"),
|
|
1113
|
+
"no-undo-audit.sh states this same rc in its own comment; the two must not drift apart again",
|
|
1114
|
+
);
|
|
1115
|
+
});
|
|
1116
|
+
|
|
1117
|
+
// The other half of #304, and the half a careless append breaks: the `stash
|
|
1118
|
+
// list` this branch captures says nothing in either permission state, so the
|
|
1119
|
+
// line must come out exactly as it did before — no trailing separator, no
|
|
1120
|
+
// empty parenthetical, nothing dangling where the diagnostic would have gone.
|
|
1121
|
+
// One test for both, because it is one behaviour: an empty capture appends
|
|
1122
|
+
// nothing. Git as a whole is NOT silent in both — see the narrowing below.
|
|
1123
|
+
test("the states `stash list` is silent about print the unknown line unchanged", (t) => {
|
|
1124
|
+
if (process.getuid?.() === 0) return t.skip("root reads a 000 file regardless");
|
|
1125
|
+
for (const path of [[".git", "logs", "refs", "stash"], [".git", "refs", "stash"]]) {
|
|
1126
|
+
const c = repo(t);
|
|
1127
|
+
stashSomething(c.w);
|
|
1128
|
+
chmodSync(join(c.w, ...path), 0o000);
|
|
1129
|
+
|
|
1130
|
+
const r = audit(c);
|
|
1131
|
+
assert.equal(r.json.stash, null, `${path.join("/")}: fixture must reach the unknown branch`);
|
|
1132
|
+
assert.equal(stashLine(r), UNKNOWN_LINE, `${path.join("/")}: git said nothing, so nothing may be appended`);
|
|
1133
|
+
assert.equal(stashLines(r).length, 1, `${path.join("/")}: exactly one stash-entry line — a broken elif chain would let a second one through, #1210`);
|
|
1134
|
+
// Reflog only. The `stash list` this branch captures is silent in both
|
|
1135
|
+
// states, but git as a whole is not: in the `refs/stash` case the
|
|
1136
|
+
// `show-ref` cross-check above prints `fatal: git show-ref: bad ref
|
|
1137
|
+
// refs/stash (0000…)`, and the audit only looks silent there because that
|
|
1138
|
+
// call runs under `>/dev/null 2>&1` (#481). Asserting no `fatal:` over
|
|
1139
|
+
// that state would convert an unstated ceiling into an invariant, and make
|
|
1140
|
+
// the follow-up edit a passing test.
|
|
1141
|
+
if (path.includes("logs")) {
|
|
1142
|
+
assert.doesNotMatch(r.stderr, /fatal:|warning:/, "the unreadable reflog is the one state git says nothing about at all");
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
});
|
|
1146
|
+
|
|
1147
|
+
// The count stays reported-only, even at "unknown" — the dirty check is the
|
|
1148
|
+
// sole gate, and an unreadable reflog must not mask it either.
|
|
1149
|
+
test("a dirty worktree refuses even when the stash is unknown", (t) => {
|
|
1150
|
+
if (process.getuid?.() === 0) return t.skip("root reads a 000 file regardless");
|
|
1151
|
+
const c = repo(t);
|
|
1152
|
+
stashSomething(c.w);
|
|
1153
|
+
chmodSync(join(c.w, ".git", "logs", "refs", "stash"), 0o000);
|
|
1154
|
+
writeFileSync(join(c.w, "uncommitted.txt"), "work\n");
|
|
1155
|
+
|
|
1156
|
+
const r = audit(c);
|
|
1157
|
+
assert.equal(r.status, 1, "unknown must not mask the dirty check, in either direction");
|
|
1158
|
+
assert.equal(r.json.clean, false);
|
|
1159
|
+
assert.equal(r.json.stash, null);
|
|
1160
|
+
});
|
|
1161
|
+
|
|
1162
|
+
// `clean` is now the sole gate, so a `git status` that fails must not read as a
|
|
1163
|
+
// clean worktree. It used to have an accidental backstop: a repo holding any
|
|
1164
|
+
// stash refused anyway, whatever `status` did. That backstop left with the gate.
|
|
1165
|
+
test("a git status that fails is unanswerable (2), never clean (0)", (t) => {
|
|
1166
|
+
if (process.getuid?.() === 0) return t.skip("root reads a 000 file regardless");
|
|
1167
|
+
const c = repo(t);
|
|
1168
|
+
writeFileSync(join(c.w, "uncommitted.txt"), "work that exists nowhere else\n");
|
|
1169
|
+
chmodSync(join(c.w, ".git", "index"), 0o000);
|
|
1170
|
+
|
|
1171
|
+
const r = audit(c);
|
|
1172
|
+
assert.equal(r.status, 2, "an unreadable index cannot answer the question — it must not answer 'clean'");
|
|
1173
|
+
assert.match(r.stderr, /cannot tell a clean worktree from a dirty one/);
|
|
1174
|
+
assert.doesNotMatch(r.stdout, /"clean":true/);
|
|
1175
|
+
});
|
|
1176
|
+
|
|
1177
|
+
test("a dirty worktree refuses even when a stash is also present", (t) => {
|
|
1178
|
+
const c = repo(t);
|
|
1179
|
+
stashSomething(c.w);
|
|
1180
|
+
writeFileSync(join(c.w, "uncommitted.txt"), "work\n");
|
|
1181
|
+
|
|
1182
|
+
const r = audit(c);
|
|
1183
|
+
assert.equal(r.status, 1, "the stash must not mask the dirty check, in either direction");
|
|
1184
|
+
assert.equal(r.json.clean, false);
|
|
1185
|
+
assert.equal(r.json.stash, 1);
|
|
1186
|
+
});
|
|
1187
|
+
|
|
1188
|
+
// ---------------------------------------------------------------------------
|
|
1189
|
+
// conflicts[] and atRisk[]. The refusal above is only half the audit: a clean
|
|
1190
|
+
// worktree still exits 0 while answering "what would a careless resolution
|
|
1191
|
+
// eat", and that answer had no coverage at all.
|
|
1192
|
+
// ---------------------------------------------------------------------------
|
|
1193
|
+
|
|
1194
|
+
// One path carries both failures, because they compound. The space made the
|
|
1195
|
+
// pathspec word-split into `has` + `space.txt`, so `atRisk` came back empty on
|
|
1196
|
+
// a branch that really was about to eat a commit — a false safe, which is the
|
|
1197
|
+
// one outcome this script exists to prevent. The quote made the payload
|
|
1198
|
+
// unparseable, on exit 0, so a caller that got as far as reading the answer
|
|
1199
|
+
// crashed instead. A path that git has to quote also proves the paths reaching
|
|
1200
|
+
// the caller are real paths and not git's C-quoted rendering of them.
|
|
1201
|
+
test("a conflicting path with a space and a quote still names the commits at risk", (t) => {
|
|
1202
|
+
const path = 'has"quote and space.txt';
|
|
1203
|
+
const c = conflictRepo(t, path);
|
|
1204
|
+
|
|
1205
|
+
const fork = git(c.w, "merge-base", "origin/main", `origin/${c.branch}`);
|
|
1206
|
+
const truth = git(c.w, "log", "--oneline", `${fork}..origin/main`, "--", path);
|
|
1207
|
+
assert.equal(truth.split("\n").filter(Boolean).length, 1, "fixture must put exactly one main commit at risk");
|
|
1208
|
+
|
|
1209
|
+
const r = audit(c);
|
|
1210
|
+
assert.equal(r.status, 0, `a clean worktree passes even with conflicts; got ${r.status} ${r.stderr}`);
|
|
1211
|
+
assert.equal(r.jsonError, null, `a passing audit must emit parseable JSON; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
1212
|
+
assert.deepEqual(r.json.conflicts, [path], "the real path, not git's C-quoted rendering of it");
|
|
1213
|
+
assert.deepEqual(subjects(r), ["MAIN COMMIT AT RISK"], `ground truth was ${truth}`);
|
|
1214
|
+
assert.match(r.stderr, /at risk: /);
|
|
1215
|
+
});
|
|
1216
|
+
|
|
1217
|
+
// Same question, asked the other way: an ordinary path must not regress while
|
|
1218
|
+
// the quoted one is being fixed.
|
|
1219
|
+
test("a plain conflicting path names the commits at risk", (t) => {
|
|
1220
|
+
const c = conflictRepo(t, "plain.txt");
|
|
1221
|
+
|
|
1222
|
+
const r = audit(c);
|
|
1223
|
+
assert.equal(r.status, 0);
|
|
1224
|
+
assert.deepEqual(r.json.conflicts, ["plain.txt"]);
|
|
1225
|
+
assert.deepEqual(subjects(r), ["MAIN COMMIT AT RISK"]);
|
|
1226
|
+
});
|
|
1227
|
+
|
|
1228
|
+
// #148: above ARG_MAX, `xargs -0` runs `git log` once per batch of the
|
|
1229
|
+
// pathspec list, and each invocation reports every commit touching ITS OWN
|
|
1230
|
+
// batch — so a commit spanning several batches used to come back once per
|
|
1231
|
+
// batch. `withSplitXargs` forces that split at 20 files instead of the
|
|
1232
|
+
// thousands real ARG_MAX needs, reproducing the ticket's own measurement
|
|
1233
|
+
// (`xargs -s 300`) without a ~1 MiB fixture.
|
|
1234
|
+
//
|
|
1235
|
+
// Sorted, because the set is the whole claim: once xargs splits, `atRisk` is
|
|
1236
|
+
// the concatenation of per-batch outputs in PATHSPEC order, not `git log`'s
|
|
1237
|
+
// reverse-chronological one, and which commit lands first depends on how the
|
|
1238
|
+
// batch boundaries fall — which depends on the length of the tmpdir path.
|
|
1239
|
+
// Asserting the emitted order would pin the environment, not the dedupe.
|
|
1240
|
+
test("two commits spanning the conflicting paths are each named once in atRisk, even when xargs splits the pathspec list into several batches", (t) => {
|
|
1241
|
+
const c = manyConflictsTwoCommits(t);
|
|
1242
|
+
const xargs = withSplitXargs(t);
|
|
1243
|
+
|
|
1244
|
+
const r = audit(c, { ...ENV, PATH: xargs.path });
|
|
1245
|
+
assert.equal(r.status, 0, `got ${r.status} ${r.stderr}`);
|
|
1246
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
1247
|
+
assert.equal(r.json.conflicts.length, c.paths.length, "fixture must put every file in conflict");
|
|
1248
|
+
assert.ok(xargs.batches() > 1, `xargs must actually split — ran git log ${xargs.batches()}x, and at 1 this test passes with the dedupe deleted`);
|
|
1249
|
+
assert.deepEqual(
|
|
1250
|
+
subjects(r).sort(),
|
|
1251
|
+
["MAIN COMMIT AT RISK, even paths", "MAIN COMMIT AT RISK, odd paths"],
|
|
1252
|
+
"each commit named once, not once per xargs batch it lands in",
|
|
1253
|
+
);
|
|
1254
|
+
});
|
|
1255
|
+
|
|
1256
|
+
// #522: this pipeline's status is `xargs`' — the last command — not that of the
|
|
1257
|
+
// `git log` xargs drives, and there is no `pipefail` in POSIX sh to change
|
|
1258
|
+
// that. So an xargs-side fault reaches the guard, and the guard used to answer
|
|
1259
|
+
// for it by naming git log, sending the reader to git for a fault git never had.
|
|
1260
|
+
// Induced with the same shim #148's test uses, at a size that leaves xargs no
|
|
1261
|
+
// room for the command line at all, so it exits nonzero without running git.
|
|
1262
|
+
// Only the audit's own line is pinned: the accompanying diagnostic is xargs'
|
|
1263
|
+
// own and its wording differs between implementations.
|
|
1264
|
+
test("an xargs-side failure listing the at-risk commits is unanswerable, and does not answer for it by naming git log", (t) => {
|
|
1265
|
+
const c = bareConflictRepo(t, "plain.txt");
|
|
1266
|
+
const xargs = withSplitXargs(t, 60);
|
|
1267
|
+
|
|
1268
|
+
const r = audit(c, { ...ENV, PATH: xargs.path });
|
|
1269
|
+
assert.equal(r.status, 2, `an at-risk list that could not be built is unanswerable, not a verdict; got ${r.status} ${r.stderr}`);
|
|
1270
|
+
assert.equal(r.stdout.trim(), "", `exit 2 emits no payload — a payload is an answer; got ${r.stdout}`);
|
|
1271
|
+
assert.match(
|
|
1272
|
+
r.stderr,
|
|
1273
|
+
/listing commits for the conflicting paths failed \(git log or xargs\)/,
|
|
1274
|
+
"the guard no longer names both commands that can produce the status it reads",
|
|
1275
|
+
);
|
|
1276
|
+
assert.doesNotMatch(
|
|
1277
|
+
r.stderr,
|
|
1278
|
+
/git log failed for the conflicting paths/,
|
|
1279
|
+
"the guard is back to blaming git log for a fault that can be xargs' own",
|
|
1280
|
+
);
|
|
1281
|
+
});
|
|
1282
|
+
|
|
1283
|
+
/**
|
|
1284
|
+
* Shadows `awk` on PATH with a wrapper that fails ONLY the dedupe call this
|
|
1285
|
+
* guard reads, and defers to the real awk otherwise — so the earlier
|
|
1286
|
+
* stash-counting `awk 'END{print NR}'` keeps working and the fault lands on
|
|
1287
|
+
* the one statement under test. Same technique as `withBrokenEscaper`, and
|
|
1288
|
+
* for its reason: the call is selected on the at-risk list flowing THROUGH
|
|
1289
|
+
* it, never on the program text handed to it. Selecting on the literal
|
|
1290
|
+
* `!seen[$1]++` looked equivalent and was not — a behaviour-preserving
|
|
1291
|
+
* rewrite to `{if(!seen[$1]++)print}` fell straight through to the real awk,
|
|
1292
|
+
* disarming the injection while the test went red as if the `|| die` had
|
|
1293
|
+
* regressed (measured).
|
|
1294
|
+
*
|
|
1295
|
+
* Input is captured to a file and replayed byte-for-byte rather than through
|
|
1296
|
+
* `printf`, which would turn the stash call's empty input into one blank line
|
|
1297
|
+
* and its `NR` from 0 into 1.
|
|
1298
|
+
*
|
|
1299
|
+
* `fired` is the residual that selecting on content cannot cover: a rewrite
|
|
1300
|
+
* routing the dedupe away from awk altogether still disarms the fault, and
|
|
1301
|
+
* without this the run would again red as a guard regression rather than as
|
|
1302
|
+
* an injection that never fired.
|
|
1303
|
+
*/
|
|
1304
|
+
function withFailingDedupeAwk(t) {
|
|
1305
|
+
const bin = mkdtempSync(join(tmpdir(), "no-undo-audit-awk-"));
|
|
1306
|
+
t.after(() => rmSync(bin, { recursive: true, force: true }));
|
|
1307
|
+
const real = execFileSync("sh", ["-c", "command -v awk"], { encoding: "utf8" }).trim();
|
|
1308
|
+
writeFileSync(join(bin, "awk"), `#!/bin/sh
|
|
1309
|
+
f="${bin}/stdin.$$"
|
|
1310
|
+
cat > "$f"
|
|
1311
|
+
if grep -qF 'MAIN COMMIT AT RISK' "$f"; then
|
|
1312
|
+
: > "${bin}/fired"
|
|
1313
|
+
echo "SHIM: forced awk failure for test" >&2
|
|
1314
|
+
exit 13
|
|
1315
|
+
fi
|
|
1316
|
+
exec ${real} "$@" < "$f"
|
|
1317
|
+
`);
|
|
1318
|
+
chmodSync(join(bin, "awk"), 0o755);
|
|
1319
|
+
return { path: `${bin}:${process.env.PATH}`, fired: join(bin, "fired") };
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
// This statement is kept separate from the xargs/git-log pipe above it (see
|
|
1323
|
+
// the comment ahead of the dedupe line in the script) precisely so an
|
|
1324
|
+
// awk-side fault is diagnosed by name instead of swallowed into that pipe's
|
|
1325
|
+
// status. Nothing forced that statement to fail before this test, so the
|
|
1326
|
+
// separation it documents was unpinned -- deleting the `|| die` left this
|
|
1327
|
+
// exact fixture green (measured): under `set -eu` a bare
|
|
1328
|
+
// `at_risk=$(... | awk ...)` still aborts on the shim's exit 13, but with
|
|
1329
|
+
// awk's own status and no named message, which is what the assertions below
|
|
1330
|
+
// discriminate from the guard actually firing.
|
|
1331
|
+
test("an awk-side failure deduplicating the at-risk commits is unanswerable, and names awk rather than the git-log/xargs pipe ahead of it", (t) => {
|
|
1332
|
+
const c = bareConflictRepo(t, "plain.txt");
|
|
1333
|
+
|
|
1334
|
+
const awk = withFailingDedupeAwk(t);
|
|
1335
|
+
const r = audit(c, { ...ENV, PATH: awk.path });
|
|
1336
|
+
assert.ok(existsSync(awk.fired),
|
|
1337
|
+
"the fault injection never fired — the at-risk list no longer flows through awk, so every assertion below is measuring an unmutated run");
|
|
1338
|
+
assert.equal(r.status, 2, `an at-risk list that could not be deduplicated is unanswerable, not a verdict; got ${r.status} ${r.stderr}`);
|
|
1339
|
+
assert.equal(r.stdout.trim(), "", `exit 2 emits no payload -- a payload is an answer; got ${r.stdout}`);
|
|
1340
|
+
assert.match(
|
|
1341
|
+
r.stderr,
|
|
1342
|
+
/awk failed deduplicating the at-risk commits/,
|
|
1343
|
+
"the guard must name awk, not fall through to a bare set -e abort",
|
|
1344
|
+
);
|
|
1345
|
+
assert.doesNotMatch(
|
|
1346
|
+
r.stderr,
|
|
1347
|
+
/listing commits for the conflicting paths failed \(git log or xargs\)/,
|
|
1348
|
+
"an awk-side fault must not be misreported as the git-log/xargs pipe ahead of it",
|
|
1349
|
+
);
|
|
1350
|
+
});
|
|
1351
|
+
|
|
1352
|
+
/**
|
|
1353
|
+
* Shadows `awk` on PATH with a wrapper that fails ONLY the stash-count call
|
|
1354
|
+
* this guard reads — selected on content flowing THROUGH it, never on the
|
|
1355
|
+
* program text handed to it, same technique and same reason as
|
|
1356
|
+
* `withFailingDedupeAwk` above. `stash@{` is git's own `stash list` line
|
|
1357
|
+
* prefix and cannot appear in the at-risk dedupe's commit-log input, so the
|
|
1358
|
+
* two awk call sites stay distinguishable without either one shadowing the
|
|
1359
|
+
* other.
|
|
1360
|
+
*/
|
|
1361
|
+
function withFailingStashCountAwk(t) {
|
|
1362
|
+
const bin = mkdtempSync(join(tmpdir(), "no-undo-audit-awk-stash-"));
|
|
1363
|
+
t.after(() => rmSync(bin, { recursive: true, force: true }));
|
|
1364
|
+
const real = execFileSync("sh", ["-c", "command -v awk"], { encoding: "utf8" }).trim();
|
|
1365
|
+
writeFileSync(join(bin, "awk"), `#!/bin/sh
|
|
1366
|
+
f="${bin}/stdin.$$"
|
|
1367
|
+
cat > "$f"
|
|
1368
|
+
if grep -qF 'stash@{' "$f"; then
|
|
1369
|
+
: > "${bin}/fired"
|
|
1370
|
+
echo "SHIM: forced awk failure for test" >&2
|
|
1371
|
+
exit 13
|
|
1372
|
+
fi
|
|
1373
|
+
exec ${real} "$@" < "$f"
|
|
1374
|
+
`);
|
|
1375
|
+
chmodSync(join(bin, "awk"), 0o755);
|
|
1376
|
+
return { path: `${bin}:${process.env.PATH}`, fired: join(bin, "fired") };
|
|
1377
|
+
}
|
|
1378
|
+
|
|
1379
|
+
// Same shape as the dedupe guard's own test above, and the fix for finding 1
|
|
1380
|
+
// of #1160's follow-up review: this was the one external-tool command
|
|
1381
|
+
// substitution left in the file with no guard, and it reproduced the EXACT
|
|
1382
|
+
// symptom this script exists to remove — a clean worktree refused at exit 1,
|
|
1383
|
+
// no payload, no cause named — because it ANSWERS a question (the stash
|
|
1384
|
+
// count) rather than only rendering one, so it belongs with the `|| die`
|
|
1385
|
+
// family below, not with `render`'s family above.
|
|
1386
|
+
test("an awk-side failure counting the stash entries is unanswerable, and names awk rather than a bare set -e abort", (t) => {
|
|
1387
|
+
const c = repo(t);
|
|
1388
|
+
stashSomething(c.w);
|
|
1389
|
+
assert.equal(git(c.w, "stash", "list").split("\n").filter(Boolean).length, 1, "fixture must leave one stash");
|
|
1390
|
+
|
|
1391
|
+
const awk = withFailingStashCountAwk(t);
|
|
1392
|
+
const r = audit(c, { ...ENV, PATH: awk.path });
|
|
1393
|
+
assert.ok(existsSync(awk.fired),
|
|
1394
|
+
"the fault injection never fired — the stash count no longer flows through awk, so every assertion below is measuring an unmutated run");
|
|
1395
|
+
assert.equal(r.status, 2, `a stash count that could not be computed is unanswerable, not a verdict; got ${r.status} ${r.stderr}`);
|
|
1396
|
+
assert.equal(r.stdout.trim(), "", `exit 2 emits no payload -- a payload is an answer; got ${r.stdout}`);
|
|
1397
|
+
assert.match(
|
|
1398
|
+
r.stderr,
|
|
1399
|
+
/awk failed counting the stash entries/,
|
|
1400
|
+
"the guard must name awk, not fall through to a bare set -e abort — the #1160 symptom this fixes",
|
|
1401
|
+
);
|
|
1402
|
+
});
|
|
1403
|
+
|
|
1404
|
+
// #583: a POSIX pipeline's status is its LAST command's, so a fault in any
|
|
1405
|
+
// earlier stage is invisible to `set -e` and to a trailing `|| die` alike. The
|
|
1406
|
+
// stage that reads merge-tree's output is the one that can fail — measured on
|
|
1407
|
+
// the locale fault #582 filed, `PIPESTATUS: 1 0 0`, the first stage exiting 1
|
|
1408
|
+
// and truncating while the two behind it exit 0 on the short input handed to
|
|
1409
|
+
// them. What came out was a SMALLER conflicts list at exit 0: a false safe from
|
|
1410
|
+
// the one tool whose job is to say whether a rebase would eat a commit.
|
|
1411
|
+
//
|
|
1412
|
+
// The fault is injected on the file rather than on any one command, so the
|
|
1413
|
+
// pin survives a change of reader: whatever reads merge-tree's output, it must
|
|
1414
|
+
// refuse when it cannot.
|
|
1415
|
+
test("merge-tree's output going unreadable at the moment it is read is unanswerable (2), never an empty conflicts list at exit 0", (t) => {
|
|
1416
|
+
const c = bareConflictRepo(t, "plain.txt");
|
|
1417
|
+
|
|
1418
|
+
const r = audit(c, { ...ENV, PATH: withUnreadableMergeTreeOutput(t) });
|
|
1419
|
+
assert.equal(r.status, 2,
|
|
1420
|
+
`a conflicts list that could not be read is unanswerable, not a verdict; got ${r.status} ${r.stderr}`);
|
|
1421
|
+
assert.equal(r.stdout.trim(), "",
|
|
1422
|
+
`exit 2 emits no payload — an empty conflicts[] here would be a false safe on a branch that really conflicts; got ${r.stdout}`);
|
|
1423
|
+
assert.match(r.stderr, /could not read git merge-tree's output \(python3\)/,
|
|
1424
|
+
"the guard names the command whose status it actually reads, the way #522 taught the at-risk guard to");
|
|
1425
|
+
});
|
|
1426
|
+
|
|
1427
|
+
// The other half of #583, and the one that rules `set -o pipefail` out even
|
|
1428
|
+
// where a shell offers it: on a large CORRECT run the reader stops at the empty
|
|
1429
|
+
// record by design and leaves merge-tree's prose tail unread, so a `tr | tr |
|
|
1430
|
+
// awk` reads back `141 141 0 0` — two stages killed by SIGPIPE with nothing
|
|
1431
|
+
// wrong. Adopting a prefix stage's status would refuse this run. It must
|
|
1432
|
+
// answer, and answer in full.
|
|
1433
|
+
test("a conflicting run whose unread tail outgrows a pipe buffer still answers, and answers in full", (t) => {
|
|
1434
|
+
const c = longTailConflictRepo(t);
|
|
1435
|
+
const tail = unreadTailBytes(c);
|
|
1436
|
+
assert.ok(tail > 65536,
|
|
1437
|
+
`the fixture must leave more unread than a pipe buffer holds, or nothing is being controlled for — left ${tail} bytes`);
|
|
1438
|
+
|
|
1439
|
+
const r = audit(c);
|
|
1440
|
+
assert.equal(r.status, 0, `a large correct run is an answer, not a refusal; got ${r.status} ${r.stderr}`);
|
|
1441
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}`);
|
|
1442
|
+
assert.deepEqual([...r.json.conflicts].sort(), [...c.paths].sort(),
|
|
1443
|
+
"every conflicting path, not the prefix that fitted before the reader stopped");
|
|
1444
|
+
assert.deepEqual(subjects(r), ["MAIN COMMIT AT RISK"]);
|
|
1445
|
+
});
|
|
1446
|
+
|
|
1447
|
+
// The at-risk half of #583. Its `|| die` reads the status of the LAST command
|
|
1448
|
+
// in the pipeline that built the pathspec list, so the `printf | sed | tr` that
|
|
1449
|
+
// once sat ahead of `xargs` could fail and leave `at_risk` short or empty at
|
|
1450
|
+
// exit 0 — a branch reported as eating nothing while it really was about to.
|
|
1451
|
+
// The fix is that the list no longer passes through them at all, and this is
|
|
1452
|
+
// the assertion that says so: break `sed` on the invocation that used to build
|
|
1453
|
+
// the pathspecs and the answer must be unchanged, because there is no longer
|
|
1454
|
+
// such an invocation. Restore the pipeline and the decoy commit comes back with
|
|
1455
|
+
// the real one, since an empty pathspec list leaves `git log` filtering by
|
|
1456
|
+
// nothing.
|
|
1457
|
+
//
|
|
1458
|
+
// `conflictRepo`, not `bareConflictRepo`: the decoys are what make an
|
|
1459
|
+
// unfiltered `git log` distinguishable from a correctly filtered one. Without
|
|
1460
|
+
// them the range holds a single commit and the broken run returns the right
|
|
1461
|
+
// answer for the wrong reason.
|
|
1462
|
+
test("breaking the sed that used to build the at-risk pathspecs changes nothing, because nothing ahead of xargs can fail unseen", (t) => {
|
|
1463
|
+
const c = conflictRepo(t, "plain.txt");
|
|
1464
|
+
|
|
1465
|
+
const r = audit(c, { ...ENV, PATH: withBrokenEscaper(t, { tool: "sed", marker: "plain.txt", selector: "literal" }) });
|
|
1466
|
+
assert.equal(r.status, 0, `nothing failed, so nothing is unanswerable; got ${r.status} ${r.stderr}`);
|
|
1467
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}`);
|
|
1468
|
+
assert.deepEqual(r.json.conflicts, ["plain.txt"]);
|
|
1469
|
+
assert.deepEqual(subjects(r), ["MAIN COMMIT AT RISK"],
|
|
1470
|
+
"the decoys are back, which means the pathspec list went through something that could fail without the guard hearing it");
|
|
1471
|
+
});
|
|
1472
|
+
|
|
1473
|
+
// A temporary the audit cannot create is a question it cannot answer, and the
|
|
1474
|
+
// exit code has to say so: bare `set -eu` would abort with mktemp's own 1,
|
|
1475
|
+
// which out of THIS script is the dirty-worktree refusal — fabricated on a
|
|
1476
|
+
// worktree already measured clean, with no payload and nothing on stderr.
|
|
1477
|
+
//
|
|
1478
|
+
// The last assertion is about the abort PATH, not the verdict, and it is the
|
|
1479
|
+
// one that needs saying: the fault lands between the two `mktemp` calls, so a
|
|
1480
|
+
// cleanup trap armed after both would not yet exist when this run dies, and the
|
|
1481
|
+
// temporary the first call created would outlive it. Exit code and message are
|
|
1482
|
+
// both correct in that world, which is why they cannot be the whole pin —
|
|
1483
|
+
// asking whether the file is gone is the only question that separates a trap
|
|
1484
|
+
// armed early enough from one armed too late.
|
|
1485
|
+
test("a temporary file the at-risk step cannot create is unanswerable (2), never the refusal that means dirty", (t) => {
|
|
1486
|
+
const c = bareConflictRepo(t, "plain.txt");
|
|
1487
|
+
|
|
1488
|
+
const m = withLaterMktempFailing(t);
|
|
1489
|
+
const r = audit(c, { ...ENV, PATH: m.path });
|
|
1490
|
+
assert.equal(r.status, 2,
|
|
1491
|
+
`the worktree is clean and was measured clean — exit 1 here would report it dirty on the strength of a full TMPDIR; got ${r.status} ${r.stderr}`);
|
|
1492
|
+
assert.equal(r.stdout.trim(), "", `exit 2 emits no payload; got ${r.stdout}`);
|
|
1493
|
+
assert.match(r.stderr, /cannot create a temporary file/,
|
|
1494
|
+
"and it names the cause rather than exiting silently");
|
|
1495
|
+
|
|
1496
|
+
const first = readFileSync(m.firstTemp, "utf8").trim();
|
|
1497
|
+
assert.ok(first, "the shim must have recorded the temporary the first call handed back, or the check below proves nothing");
|
|
1498
|
+
assert.equal(existsSync(first), false,
|
|
1499
|
+
`the temporary created before the failing call has to be cleaned up by the abort that follows it, or a run that dies here leaks one; ${first} survived`);
|
|
1500
|
+
});
|
|
1501
|
+
|
|
1502
|
+
// #146: a BS, tab, FF, CR or DEL in a conflicting path used to be replaced
|
|
1503
|
+
// with a space, so `conflicts[]` named a file that exists nowhere on disk —
|
|
1504
|
+
// this is the case the ticket itself measured. RFC 8259 gives short forms to
|
|
1505
|
+
// all four control bytes (\b \t \f \r) and DEL (\177) is not a C0 byte at
|
|
1506
|
+
// all, so all five must round-trip, and `conflictsRewritten` must say so. BS
|
|
1507
|
+
// and FF are here because the first version of this fix scrubbed them anyway.
|
|
1508
|
+
test("a conflicting path with a BS, a tab, a FF, a CR and a DEL round-trips and is not flagged rewritten", (t) => {
|
|
1509
|
+
const path = "has\bbs\ttab\fff\rcr\x7fdel.txt";
|
|
1510
|
+
const c = conflictRepo(t, path);
|
|
1511
|
+
|
|
1512
|
+
const r = audit(c);
|
|
1513
|
+
assert.equal(r.status, 0, `got ${r.status} ${r.stderr}`);
|
|
1514
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
1515
|
+
assert.deepEqual(r.json.conflicts, [path], "the real path, byte for byte — this is what a consumer pastes into `git diff --`");
|
|
1516
|
+
assert.deepEqual(r.json.conflictsRewritten, [false], "escaped or preserved, not replaced");
|
|
1517
|
+
assert.deepEqual(subjects(r), ["MAIN COMMIT AT RISK"]);
|
|
1518
|
+
});
|
|
1519
|
+
|
|
1520
|
+
// The other half: a byte with no JSON short form still has to be replaced —
|
|
1521
|
+
// unlike tab/CR/DEL, JSON has nowhere to put it — but the payload must now say
|
|
1522
|
+
// so, since the runbook step that pastes `conflicts[]` into a diff command must
|
|
1523
|
+
// skip exactly this path.
|
|
1524
|
+
// \013 (VT) rides along with \002: it is the C0 byte that looks like it has a
|
|
1525
|
+
// short form and does not — RFC 8259 lists no \v — so narrowing the scrub set
|
|
1526
|
+
// to make room for \b and \f must not take VT out with them. Unescaped in a
|
|
1527
|
+
// JSON string it is a parse error, so `jsonError` is the discriminator.
|
|
1528
|
+
test("a conflicting path with bytes that have no short form is replaced and flagged rewritten", (t) => {
|
|
1529
|
+
const path = "has\x02bell\x0bvt.txt";
|
|
1530
|
+
const c = conflictRepo(t, path);
|
|
1531
|
+
|
|
1532
|
+
const r = audit(c);
|
|
1533
|
+
assert.equal(r.status, 0, `got ${r.status} ${r.stderr}`);
|
|
1534
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
1535
|
+
assert.deepEqual(r.json.conflicts, ["has bell vt.txt"], "no short form for \\002 or \\013 — both still neutralised to a space");
|
|
1536
|
+
assert.deepEqual(r.json.conflictsRewritten, [true], "and the payload must disclose that it was");
|
|
1537
|
+
});
|
|
1538
|
+
|
|
1539
|
+
// `--` ends the options, not the pathspec magic, so a real file named
|
|
1540
|
+
// `:colon.txt` is parsed as a pathspec expression and matches nothing. Same
|
|
1541
|
+
// false safe as the space, reached by a different byte, and `:(literal)` is
|
|
1542
|
+
// what closes it.
|
|
1543
|
+
test("a conflicting path that looks like pathspec magic names the commits at risk", (t) => {
|
|
1544
|
+
const path = ":colon.txt";
|
|
1545
|
+
const c = conflictRepo(t, path);
|
|
1546
|
+
|
|
1547
|
+
const r = audit(c);
|
|
1548
|
+
assert.equal(r.status, 0, `got ${r.status} ${r.stderr}`);
|
|
1549
|
+
assert.deepEqual(r.json.conflicts, [path]);
|
|
1550
|
+
assert.deepEqual(subjects(r), ["MAIN COMMIT AT RISK"], "a leading `:` must be matched literally, not as magic");
|
|
1551
|
+
});
|
|
1552
|
+
|
|
1553
|
+
// #582: `tr` is locale-sensitive, and under a UTF-8 locale BSD tr exits 1 on a
|
|
1554
|
+
// byte that is not valid UTF-8. What that cost was measured on the `tr | tr |
|
|
1555
|
+
// awk` that used to split merge-tree's output, where the FIRST stage was the
|
|
1556
|
+
// one that failed — PIPESTATUS `1 0 0`, the second `tr` and `awk` both exiting
|
|
1557
|
+
// 0 on the short input it handed them — so the real status sat in a non-final
|
|
1558
|
+
// slot where `set -e` could not see it (the pipeline's status was awk's) and
|
|
1559
|
+
// where the line carried no `|| die` of its own either. `conflicts` came back
|
|
1560
|
+
// holding the single truncated entry `b`, `plain.txt` was dropped from the list
|
|
1561
|
+
// entirely, `atRisk` was `[]`, and the audit exited 0: a false safe from the one
|
|
1562
|
+
// tool whose whole job is to say whether a rebase would eat a commit, which is
|
|
1563
|
+
// the outcome the comment above `conflicts=` says must be exit 2.
|
|
1564
|
+
// `export LC_ALL=C` is the fix, and it is the convention inflight.sh already
|
|
1565
|
+
// follows, both globally and per site.
|
|
1566
|
+
//
|
|
1567
|
+
// Past tense throughout, because that mechanism is no longer here to reproduce:
|
|
1568
|
+
// #583 replaced the split with a single byte-oriented reader whose status
|
|
1569
|
+
// nothing discards, so the pin is not what stands between that byte and a false
|
|
1570
|
+
// safe on this path any more. The assertions below are unchanged by that —
|
|
1571
|
+
// they assert the correct ANSWER, which is what both the split and the reader
|
|
1572
|
+
// owe.
|
|
1573
|
+
//
|
|
1574
|
+
// WHAT KILLS THE MUTANT, and the difference matters to anyone tidying the
|
|
1575
|
+
// script. Measured on this tree with the pin deleted: the reader parses both
|
|
1576
|
+
// conflicting paths correctly whatever the locale, and the kill lands further
|
|
1577
|
+
// down, in the `conflict: ` render that prints them to stderr. `sed` exits on
|
|
1578
|
+
// the byte — `sed: RE error: illegal byte sequence` on `b\377ad.txt` — and
|
|
1579
|
+
// emits nothing, so neither conflict line reaches stderr.
|
|
1580
|
+
//
|
|
1581
|
+
// It used to produce a second kill beside that one, and it no longer does. The
|
|
1582
|
+
// render was a bare pipeline, so `set -eu` took sed's own 1 — which out of
|
|
1583
|
+
// this script is the dirty-worktree refusal, fabricated on a worktree the
|
|
1584
|
+
// audit had already printed `clean` for — and the status assertion below
|
|
1585
|
+
// caught the mutant on that. #1160 put the render behind a guard, so the
|
|
1586
|
+
// verdict no longer moves when it fails, and the status, payload and
|
|
1587
|
+
// `subjects()` assertions below now all stay GREEN under the mutant.
|
|
1588
|
+
//
|
|
1589
|
+
// So the kill is the pair of STDERR assertions at the end of this test and
|
|
1590
|
+
// nothing else. Measured both ways at #1160's own commit: with the pin, both
|
|
1591
|
+
// conflicting paths are named; with it deleted, `no-undo-audit: could not
|
|
1592
|
+
// render the conflicting-path list to stderr` stands in their place, at the
|
|
1593
|
+
// same exit 0 over the same payload. Do not drop them to tidy the test —
|
|
1594
|
+
// on this script nothing else observes the pin.
|
|
1595
|
+
//
|
|
1596
|
+
// The locale goes in as `LANG`, with `LC_ALL` explicitly UNSET, and both halves
|
|
1597
|
+
// are load-bearing. Explicit rather than inherited, because a suite that takes
|
|
1598
|
+
// whatever the runner happens to export pins nothing at all — same reasoning,
|
|
1599
|
+
// and same technique, as inflight.test.mjs' own locale test. `LANG` rather than
|
|
1600
|
+
// `LC_ALL`, because passing `LC_ALL` in puts it in the child's environment
|
|
1601
|
+
// BEFORE the script runs, and a POSIX shell keeps a variable's export attribute
|
|
1602
|
+
// once it is already there: a plain `LC_ALL=C` with the `export` keyword
|
|
1603
|
+
// dropped would still reach `tr`, and these tests would go on passing against a
|
|
1604
|
+
// script that exports nothing. Measured both ways — under
|
|
1605
|
+
// `{ LC_ALL: "en_US.UTF-8" }` that mutant survives, under this shape it dies.
|
|
1606
|
+
// It is also the honest shape: the ambient failure is an operator's
|
|
1607
|
+
// `LANG=en_US.UTF-8` with `LC_ALL` unset.
|
|
1608
|
+
//
|
|
1609
|
+
// Platform ceiling, stated rather than hidden: BSD tr — macOS, the fleet's own
|
|
1610
|
+
// platform — rejects the byte, while GNU tr is byte-oriented and accepts it, so
|
|
1611
|
+
// on Linux the unpatched script already answers correctly and this test passes
|
|
1612
|
+
// with or without the pin. Measured under a full GNU toolchain (coreutils 9.11,
|
|
1613
|
+
// gnu-sed 4.10, gawk 5.4.1 ahead of PATH): the whole file stays green with
|
|
1614
|
+
// `export LC_ALL=C` deleted. This test asserts the correct ANSWER, which is
|
|
1615
|
+
// right on both platforms; only the macOS run kills the mutant. `ci.yml` runs
|
|
1616
|
+
// ubuntu-latest, so the half CI can check is the source assertion in
|
|
1617
|
+
// locale-pin-prose.test.mjs, not this one.
|
|
1618
|
+
test("a conflicting path holding an invalid-UTF-8 byte still names every conflict and every commit at risk under an ambient UTF-8 locale", (t) => {
|
|
1619
|
+
const c = byteConflictRepo(t, "b\\377ad.txt");
|
|
1620
|
+
|
|
1621
|
+
const r = audit(c, { ...ENV, LANG: "en_US.UTF-8", LC_ALL: undefined });
|
|
1622
|
+
assert.equal(r.status, 0, `a clean worktree passes even with conflicts; got ${r.status} ${r.stderr}`);
|
|
1623
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
1624
|
+
assert.equal(r.json.conflicts.length, 2,
|
|
1625
|
+
`both conflicting paths, not a list truncated at the bad byte; got ${JSON.stringify(r.json.conflicts)}`);
|
|
1626
|
+
assert.equal(r.json.conflicts[1], "plain.txt",
|
|
1627
|
+
"the path AFTER the bad one is what truncation drops, and dropping it silently is the false safe");
|
|
1628
|
+
assert.match(r.json.conflicts[0], /^b.ad\.txt$/, "the bad path arrives whole, not cut down to `b`");
|
|
1629
|
+
assert.deepEqual(subjects(r), ["MAIN COMMIT AT RISK"],
|
|
1630
|
+
"and the commit a careless resolution would eat is named, rather than atRisk: [] at exit 0");
|
|
1631
|
+
// The kill, per the block above. Matched on `.` rather than on the byte:
|
|
1632
|
+
// `audit()` decodes the child's stderr as UTF-8 and substitutes U+FFFD for
|
|
1633
|
+
// it on the way in, the same lossy read #613 measured on stdout.
|
|
1634
|
+
assert.match(r.stderr, /^ {4}conflict: b.ad\.txt$/m,
|
|
1635
|
+
"the render must name the bad path — with `export LC_ALL=C` deleted, sed exits on the byte and emits nothing");
|
|
1636
|
+
assert.match(r.stderr, /^ {4}conflict: plain\.txt$/m,
|
|
1637
|
+
"and the path behind it, which sed's failure takes with it whether it aborts the audit or not");
|
|
1638
|
+
});
|
|
1639
|
+
|
|
1640
|
+
// #613: the test above parses the payload through `audit()`'s
|
|
1641
|
+
// `encoding: "utf8"`, and Node decodes a child's stdout as UTF-8 LOSSILY on
|
|
1642
|
+
// the way in — the same silent U+FFFD substitution `jq` performs — so it
|
|
1643
|
+
// cannot tell "the script emitted valid UTF-8" from "the script emitted a raw
|
|
1644
|
+
// invalid byte and Node papered over it before JSON.parse ever saw it". This
|
|
1645
|
+
// reads the raw bytes instead and checks them against the two consumers the
|
|
1646
|
+
// issue measured: `jq`, which substitutes U+FFFD silently at exit 0
|
|
1647
|
+
// (corrupting the data without saying so), and `python3 json.load`, a strict
|
|
1648
|
+
// parser that refuses the payload outright with `UnicodeDecodeError`. Both
|
|
1649
|
+
// must now accept the SAME bytes the script wrote, and the field naming which
|
|
1650
|
+
// element lost a byte must say so honestly rather than silently.
|
|
1651
|
+
test("a conflicting path holding an invalid-UTF-8 byte round-trips as valid, parseable UTF-8 JSON, with the fault flagged rather than hidden (#613)", (t) => {
|
|
1652
|
+
const c = byteConflictRepo(t, "b\\377ad.txt");
|
|
1653
|
+
|
|
1654
|
+
const r = spawnSync("sh", [SCRIPT, c.w, c.branch], { env: ENV, encoding: "buffer" });
|
|
1655
|
+
assert.equal(r.status, 0,
|
|
1656
|
+
`a clean worktree passes even with conflicts; got ${r.status} ${r.stderr?.toString("utf8")}`);
|
|
1657
|
+
const raw = r.stdout;
|
|
1658
|
+
|
|
1659
|
+
// Round-trip validity: re-encoding what a lossy UTF-8 decode produces
|
|
1660
|
+
// reproduces the same bytes only when the buffer was already valid UTF-8.
|
|
1661
|
+
assert.deepEqual(Buffer.from(raw.toString("utf8"), "utf8"), raw,
|
|
1662
|
+
"the audit's own stdout is not valid UTF-8 — a raw invalid byte reached the payload");
|
|
1663
|
+
|
|
1664
|
+
// `jq -e '.'` cannot pin the #613 bug on its own — it is one of the two
|
|
1665
|
+
// LENIENT consumers the issue names, silently substituting U+FFFD at exit 0
|
|
1666
|
+
// on a raw invalid byte just like Node's own decode above, so it would
|
|
1667
|
+
// exit 0 against the pre-fix payload too. The round-trip check above and
|
|
1668
|
+
// the strict `python3 json.load` below are what actually pin validity;
|
|
1669
|
+
// this only confirms a real downstream consumer of this payload (several
|
|
1670
|
+
// fleet scripts pipe conflicts/atRisk through jq) can parse it as
|
|
1671
|
+
// well-formed JSON at all.
|
|
1672
|
+
const jq = spawnSync("jq", ["-e", "."], { input: raw });
|
|
1673
|
+
assert.equal(jq.status, 0, `jq must accept the payload as well-formed JSON; stderr: ${jq.stderr?.toString("utf8")}`);
|
|
1674
|
+
|
|
1675
|
+
const py = spawnSync("python3", ["-c", "import json,sys; json.load(sys.stdin.buffer)"], { input: raw });
|
|
1676
|
+
assert.equal(py.status, 0,
|
|
1677
|
+
`a strict UTF-8 JSON parser must accept the payload; stderr: ${py.stderr?.toString("utf8")}`);
|
|
1678
|
+
|
|
1679
|
+
const json = JSON.parse(raw.toString("utf8"));
|
|
1680
|
+
assert.equal(json.conflicts.length, 2, "both conflicting paths are present");
|
|
1681
|
+
assert.match(json.conflicts[0], /^b.ad\.txt$/, "the bad path arrives whole, not cut down to `b`");
|
|
1682
|
+
assert.equal(json.conflicts[0].includes("�"), true,
|
|
1683
|
+
"the fault must render as U+FFFD, not the original invalid byte and not silence");
|
|
1684
|
+
assert.equal(json.conflictsRewritten[0], true,
|
|
1685
|
+
"the path that lost a byte to U+FFFD must be flagged rewritten — a caller must not treat it as the real path");
|
|
1686
|
+
assert.equal(json.conflictsRewritten[1], false, "the untouched path must not be flagged");
|
|
1687
|
+
});
|
|
1688
|
+
|
|
1689
|
+
// The other half of the same pin, and the half a fix-only suite never covers:
|
|
1690
|
+
// what does `export LC_ALL=C` now REFUSE? It makes every `tr`, `sed` and `awk`
|
|
1691
|
+
// in the script byte-oriented, so a path of legitimate multi-byte UTF-8 must
|
|
1692
|
+
// still round-trip whole and must NOT be flagged rewritten. Every scrub set in
|
|
1693
|
+
// this script is \001-\037 and every byte of a multi-byte UTF-8 sequence is
|
|
1694
|
+
// >= \200 — the script's own jstr comment says so — so the pin cannot reach it.
|
|
1695
|
+
// This is the test that keeps that true.
|
|
1696
|
+
//
|
|
1697
|
+
// Built through the same shell-side `printf` as the invalid case rather than
|
|
1698
|
+
// through `conflictRepo`: written to disk, the name goes through the
|
|
1699
|
+
// filesystem's Unicode normalisation, and `caf\303\251.txt` (NFC) can come back
|
|
1700
|
+
// NFD — a byte-for-byte assertion failing for a reason that has nothing to do
|
|
1701
|
+
// with this script.
|
|
1702
|
+
test("a conflicting path of valid multi-byte UTF-8 round-trips whole under the pinned locale and is not flagged rewritten", (t) => {
|
|
1703
|
+
const c = byteConflictRepo(t, "caf\\303\\251.txt");
|
|
1704
|
+
|
|
1705
|
+
const r = audit(c, { ...ENV, LANG: "en_US.UTF-8", LC_ALL: undefined });
|
|
1706
|
+
assert.equal(r.status, 0, `got ${r.status} ${r.stderr}`);
|
|
1707
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
1708
|
+
assert.deepEqual(r.json.conflicts, ["café.txt", "plain.txt"],
|
|
1709
|
+
"byte for byte — this is what a consumer pastes into `git diff --`");
|
|
1710
|
+
assert.deepEqual(r.json.conflictsRewritten, [false, false],
|
|
1711
|
+
"a byte >= \\200 is outside every scrub set, and pinning the locale must not change that");
|
|
1712
|
+
assert.deepEqual(subjects(r), ["MAIN COMMIT AT RISK"]);
|
|
1713
|
+
});
|
|
1714
|
+
|
|
1715
|
+
// A commit subject is free text, so it reaches the payload with whatever the
|
|
1716
|
+
// author typed. `\` matters as much as `"`: escaping the quote first and the
|
|
1717
|
+
// backslash second turns `\` into `\\` twice over, so the order is load-bearing
|
|
1718
|
+
// and only a subject holding both can tell a correct pipeline from that one.
|
|
1719
|
+
// The \x01 rides along because git stores control bytes in a subject happily and
|
|
1720
|
+
// JSON forbids them unescaped — dropping the scrub leaves the payload
|
|
1721
|
+
// unparseable, which is this PR's own defect class one byte over.
|
|
1722
|
+
// `$conflicts` and `$at_risk` are the last two operands carrying caller text,
|
|
1723
|
+
// and `$at_risk` is the most reachable of the whole class: it holds
|
|
1724
|
+
// `git log --oneline` output, so an ordinary commit subject is enough — no
|
|
1725
|
+
// corrupt repo, no exotic filename. It is also the audit's most consequential
|
|
1726
|
+
// line, the commits a careless resolution deletes. One fixture pins both.
|
|
1727
|
+
test("a backslash escape in a conflicting path and in an at-risk subject reaches the operator whole", (t) => {
|
|
1728
|
+
const c = conflictRepo(t, "back\\clue.txt");
|
|
1729
|
+
git(c.w, "checkout", "-q", "main");
|
|
1730
|
+
writeFileSync(join(c.w, "back\\clue.txt"), "MAIN AGAIN\n");
|
|
1731
|
+
git(c.w, "commit", "-q", "-am", "fix: the \\connection retry");
|
|
1732
|
+
git(c.w, "push", "-q", "origin", "main");
|
|
1733
|
+
git(c.w, "checkout", "-q", c.branch);
|
|
1734
|
+
|
|
1735
|
+
const r = audit(c);
|
|
1736
|
+
assert.equal(r.status, 0, `fixture must be clean; got ${r.status} ${r.stderr}`);
|
|
1737
|
+
assert.match(r.stderr, /^ conflict: back\\clue\.txt$/m, "`echo` truncates the conflict line at the `\\c`");
|
|
1738
|
+
assert.match(r.stderr, /^ at risk: \S+ fix: the \\connection retry$/m, "`echo` truncates the at-risk line at the `\\c` — an ordinary commit subject is enough to lose it");
|
|
1739
|
+
});
|
|
1740
|
+
|
|
1741
|
+
test("a quote, a backslash and a control byte in a commit subject keep the payload parseable", (t) => {
|
|
1742
|
+
const c = conflictRepo(t, "plain.txt");
|
|
1743
|
+
git(c.w, "checkout", "-q", "main");
|
|
1744
|
+
writeFileSync(join(c.w, "plain.txt"), "MAIN AGAIN\n");
|
|
1745
|
+
git(c.w, "commit", "-q", "-am", 'fix: the "quoted" back\\slash \x01 case');
|
|
1746
|
+
git(c.w, "push", "-q", "origin", "main");
|
|
1747
|
+
git(c.w, "checkout", "-q", c.branch);
|
|
1748
|
+
assert.match(git(c.w, "log", "-1", "--pretty=%s", "origin/main"), /\x01/, "fixture must keep the control byte in the subject");
|
|
1749
|
+
|
|
1750
|
+
const r = audit(c);
|
|
1751
|
+
assert.equal(r.status, 0);
|
|
1752
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
1753
|
+
assert.ok(
|
|
1754
|
+
r.json.atRisk.some((l) => l.includes('the "quoted" back\\slash case')),
|
|
1755
|
+
`the subject must survive escaping verbatim, the control byte scrubbed to a space; got ${JSON.stringify(r.json.atRisk)}`,
|
|
1756
|
+
);
|
|
1757
|
+
assert.deepEqual(r.json.atRiskRewritten, [true, false], "\\001 has no JSON short form — the payload must disclose the scrub");
|
|
1758
|
+
});
|
|
1759
|
+
|
|
1760
|
+
// #146: BS, tab, FF, CR and DEL in a commit subject get the opposite treatment
|
|
1761
|
+
// from \x01 above — four now have JSON short forms and the fifth is not a C0
|
|
1762
|
+
// byte at all, so all five must round-trip in atRisk[] too, and none may flag
|
|
1763
|
+
// the entry as rewritten.
|
|
1764
|
+
test("a BS, a tab, a FF, a CR and a DEL in a commit subject round-trip in atRisk without being flagged rewritten", (t) => {
|
|
1765
|
+
const c = conflictRepo(t, "plain.txt");
|
|
1766
|
+
git(c.w, "checkout", "-q", "main");
|
|
1767
|
+
writeFileSync(join(c.w, "plain.txt"), "MAIN AGAIN\n");
|
|
1768
|
+
git(c.w, "commit", "-q", "-am", "fix: has\bbs\ttab\fff\rcr\x7fdel case");
|
|
1769
|
+
git(c.w, "push", "-q", "origin", "main");
|
|
1770
|
+
git(c.w, "checkout", "-q", c.branch);
|
|
1771
|
+
|
|
1772
|
+
const r = audit(c);
|
|
1773
|
+
assert.equal(r.status, 0);
|
|
1774
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
1775
|
+
assert.ok(
|
|
1776
|
+
r.json.atRisk.some((l) => l.includes("has\bbs\ttab\fff\rcr\x7fdel case")),
|
|
1777
|
+
`all five bytes must survive verbatim; got ${JSON.stringify(r.json.atRisk)}`,
|
|
1778
|
+
);
|
|
1779
|
+
assert.deepEqual(r.json.atRiskRewritten, [false, false]);
|
|
1780
|
+
});
|
|
1781
|
+
|
|
1782
|
+
// git accepts `"` in a ref name and every byte but NUL and `/` in a path
|
|
1783
|
+
// component, so both of these are names a caller can really hand over. `\` is
|
|
1784
|
+
// rejected in a ref but legal in a path, which is why the backslash rides on
|
|
1785
|
+
// the worktree.
|
|
1786
|
+
test("a quote in the branch and a backslash in the worktree path keep the payload parseable", (t) => {
|
|
1787
|
+
const c = repo(t, 'fix/1-say"hi', 'no-undo-audit-back\\slash-say"hi-');
|
|
1788
|
+
|
|
1789
|
+
const r = audit(c);
|
|
1790
|
+
assert.equal(r.status, 0, `got ${r.status} ${r.stderr}`);
|
|
1791
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
1792
|
+
assert.equal(r.json.branch, 'fix/1-say"hi');
|
|
1793
|
+
assert.equal(r.json.worktree, c.w);
|
|
1794
|
+
assert.equal(r.json.branchRewritten, false);
|
|
1795
|
+
assert.equal(r.json.worktreeRewritten, false);
|
|
1796
|
+
});
|
|
1797
|
+
|
|
1798
|
+
// #146, the scalar fields: git forbids control bytes in a ref outright, so
|
|
1799
|
+
// `branch` cannot carry one — but `worktree` is a filesystem path, the same
|
|
1800
|
+
// vector as the conflicting-path cases above. Tab, CR and DEL must round-trip
|
|
1801
|
+
// there too, and a byte with no short form must still be replaced and flagged.
|
|
1802
|
+
test("a BS, a tab, a FF, a CR and a DEL in the worktree path round-trip and are not flagged rewritten", (t) => {
|
|
1803
|
+
const c = repo(t, "fix/1-thing", "no-undo-audit-has\bbs\ttab\fff\rcr\x7fdel-");
|
|
1804
|
+
|
|
1805
|
+
const r = audit(c);
|
|
1806
|
+
assert.equal(r.status, 0, `got ${r.status} ${r.stderr}`);
|
|
1807
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
1808
|
+
assert.equal(r.json.worktree, c.w, "BS, tab, FF, CR and DEL must all survive intact");
|
|
1809
|
+
assert.equal(r.json.worktreeRewritten, false);
|
|
1810
|
+
});
|
|
1811
|
+
|
|
1812
|
+
// The multi-line half of #146. `wt` is argv $1, a filesystem path, and a
|
|
1813
|
+
// directory name may hold a newline where a ref may not — so this is the one
|
|
1814
|
+
// input json.sh's `jstr` receives as more than one line from any live caller.
|
|
1815
|
+
// It is
|
|
1816
|
+
// what the `:a;$!N;$!ba` slurp and the `s/\n/\\n/g` rule exist for: without
|
|
1817
|
+
// the slurp sed cycles once per LINE and the LF rule never sees the byte, so
|
|
1818
|
+
// the payload carries a raw newline inside a JSON string and no parser accepts
|
|
1819
|
+
// it. Deleting either half left the whole suite green before this test.
|
|
1820
|
+
//
|
|
1821
|
+
// The same value cannot be driven through release-ticket.sh or inflight.sh:
|
|
1822
|
+
// release-ticket reaches `awk -v b="refs/heads/$branch"` first and awk refuses
|
|
1823
|
+
// a newline in a -v value (rc 2, before any receipt); its only other
|
|
1824
|
+
// interpolated message is the pair of `halt` calls that already flatten git's
|
|
1825
|
+
// stderr with `tr '\n' ' '`; and inflight builds every evidence string from
|
|
1826
|
+
// line-oriented git output. (No line numbers: #129 tracks five citations in
|
|
1827
|
+
// these files that have already drifted, one of them mid-review here.)
|
|
1828
|
+
// Since #119 there is one `jstr`, in json.sh, rather than three copies: those
|
|
1829
|
+
// two callers reach the shared slurp with values that can never be multi-line,
|
|
1830
|
+
// so this test is still the only place a real value exercises it.
|
|
1831
|
+
test("a newline in the worktree path round-trips as an escaped \\n", (t) => {
|
|
1832
|
+
const c = repo(t, "fix/1-thing", "no-undo-audit-has\nnewline-");
|
|
1833
|
+
|
|
1834
|
+
const r = audit(c);
|
|
1835
|
+
assert.equal(r.status, 0, `got ${r.status} ${r.stderr}`);
|
|
1836
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
1837
|
+
assert.equal(r.json.worktree, c.w, "the newline arrives escaped, and the path comes back byte for byte");
|
|
1838
|
+
assert.equal(r.json.worktreeRewritten, false, "escaped, not replaced — \\012 is not in the scrub set");
|
|
1839
|
+
});
|
|
1840
|
+
|
|
1841
|
+
test("bytes with no short form in the worktree path are replaced and flagged rewritten", (t) => {
|
|
1842
|
+
// \013 (VT) alongside \002 for the same reason as the conflicting-path case:
|
|
1843
|
+
// RFC 8259 has no \v, so VT must stay in the scrub set after \b and \f left it.
|
|
1844
|
+
const c = repo(t, "fix/1-thing", "no-undo-audit-has\x02bell\x0bvt-");
|
|
1845
|
+
|
|
1846
|
+
const r = audit(c);
|
|
1847
|
+
assert.equal(r.status, 0, `got ${r.status} ${r.stderr}`);
|
|
1848
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
1849
|
+
assert.equal(r.json.worktree, c.w.replace("\x02", " ").replace("\x0b", " "), "no short form for \\002 or \\013 — both still neutralised to a space");
|
|
1850
|
+
assert.equal(r.json.worktreeRewritten, true, "and the payload must disclose that it was");
|
|
1851
|
+
});
|
|
1852
|
+
|
|
1853
|
+
// ---------------------------------------------------------------------------
|
|
1854
|
+
// Exit 2. Every one of these is "the question could not be answered"; none of
|
|
1855
|
+
// them may reach the payload, because a payload is an answer.
|
|
1856
|
+
// ---------------------------------------------------------------------------
|
|
1857
|
+
|
|
1858
|
+
// `rev-parse --verify` resolves a tag to an object of ANY type, so a tag on a
|
|
1859
|
+
// blob walks straight past it and into merge-tree, which refuses to merge it.
|
|
1860
|
+
// git 2.50.1 spends exit 1 on that refusal — the same code it spends on "ran
|
|
1861
|
+
// fine, found conflicts" — so the exit code alone cannot tell them apart and
|
|
1862
|
+
// the audit used to print `"conflicts":[],"atRisk":[]` and exit 0. Safe, on a
|
|
1863
|
+
// question it never asked.
|
|
1864
|
+
// The section split reads an empty line as the end of the filename list, so a
|
|
1865
|
+
// path holding a literal newline manufactures that marker: the list comes back
|
|
1866
|
+
// short — sometimes empty — and the audit exits 0 saying nothing is at risk.
|
|
1867
|
+
// git C-quoted such a path before `-z`, which at least emitted JSON the caller
|
|
1868
|
+
// choked on, so answering "safe" here would be a strict downgrade. A shell
|
|
1869
|
+
// variable cannot hold NUL, so the only honest answer is that there isn't one.
|
|
1870
|
+
test("a conflicting path containing a newline is unanswerable (2), never safe (0)", (t) => {
|
|
1871
|
+
const c = bareConflictRepo(t, "lead\nline.txt");
|
|
1872
|
+
writeFileSync(join(c.w, "zz.txt"), "branch side\n");
|
|
1873
|
+
git(c.w, "add", "--", "zz.txt");
|
|
1874
|
+
git(c.w, "commit", "-q", "-m", "branch adds a second file");
|
|
1875
|
+
git(c.w, "push", "-q", "origin", c.branch);
|
|
1876
|
+
git(c.w, "checkout", "-q", "main");
|
|
1877
|
+
writeFileSync(join(c.w, "zz.txt"), "MAIN SIDE\n");
|
|
1878
|
+
git(c.w, "add", "--", "zz.txt");
|
|
1879
|
+
git(c.w, "commit", "-q", "-m", "MAIN ALSO AT RISK");
|
|
1880
|
+
git(c.w, "push", "-q", "origin", "main");
|
|
1881
|
+
git(c.w, "checkout", "-q", c.branch);
|
|
1882
|
+
|
|
1883
|
+
const r = audit(c);
|
|
1884
|
+
assert.equal(r.status, 2, `got ${r.status} with stdout ${r.stdout}`);
|
|
1885
|
+
assert.equal(r.stdout, "", "an unanswerable audit must not emit a payload");
|
|
1886
|
+
assert.match(r.stderr, /contains a newline/);
|
|
1887
|
+
});
|
|
1888
|
+
|
|
1889
|
+
// merge-tree refuses unrelated histories at exit 128. Note what this does NOT
|
|
1890
|
+
// pin: that refusal also writes nothing, so `[ -s ]` alone catches it and the
|
|
1891
|
+
// `mt_rc` half of the guard can be deleted with the suite staying green
|
|
1892
|
+
// (measured). No reachable input produces a bad exit code AND output, so the
|
|
1893
|
+
// code check is there for a git that prints the tree OID and then fails.
|
|
1894
|
+
test("unrelated histories are unanswerable (2), never safe (0)", (t) => {
|
|
1895
|
+
const c = repo(t);
|
|
1896
|
+
git(c.w, "checkout", "-q", "--orphan", "orphan");
|
|
1897
|
+
git(c.w, "rm", "-rq", "--cached", ".");
|
|
1898
|
+
writeFileSync(join(c.w, "o.txt"), "no shared ancestor\n");
|
|
1899
|
+
git(c.w, "add", "--", "o.txt");
|
|
1900
|
+
git(c.w, "commit", "-q", "-m", "orphan root");
|
|
1901
|
+
git(c.w, "push", "-q", "origin", "orphan");
|
|
1902
|
+
git(c.w, "checkout", "-qf", "main");
|
|
1903
|
+
git(c.w, "clean", "-qfd");
|
|
1904
|
+
|
|
1905
|
+
const r = audit({ w: c.w, branch: "orphan" });
|
|
1906
|
+
assert.equal(r.status, 2, `got ${r.status} with stdout ${r.stdout}`);
|
|
1907
|
+
assert.equal(r.stdout, "", "an unanswerable audit must not emit a payload");
|
|
1908
|
+
assert.match(r.stderr, /cannot determine conflicts/);
|
|
1909
|
+
});
|
|
1910
|
+
|
|
1911
|
+
test("a BASE_REF that dereferences to a blob is unanswerable (2), never safe (0)", (t) => {
|
|
1912
|
+
const c = repo(t);
|
|
1913
|
+
const blob = git(c.w, "hash-object", "-w", "f.txt");
|
|
1914
|
+
// Named under refs/remotes/ rather than as a tag, so it clears the
|
|
1915
|
+
// accept-list (#1565) and reaches the qualify step unchanged, exercising
|
|
1916
|
+
// rev-parse resolving to the wrong TYPE rather than the wrong ref.
|
|
1917
|
+
git(c.w, "update-ref", "refs/remotes/origin/blobtag", blob);
|
|
1918
|
+
assert.equal(git(c.w, "rev-parse", "--verify", "--quiet", "refs/remotes/origin/blobtag"), blob, "fixture must pass the rev-parse guard");
|
|
1919
|
+
|
|
1920
|
+
const r = audit(c, { ...ENV, BASE_REF: "refs/remotes/origin/blobtag" });
|
|
1921
|
+
assert.equal(r.status, 2, `merge-tree could not answer; got ${r.status} with stdout ${r.stdout}`);
|
|
1922
|
+
assert.doesNotMatch(r.stdout, /"conflicts"/, "an unanswerable audit must not emit a payload");
|
|
1923
|
+
assert.match(r.stderr, /cannot determine conflicts/);
|
|
1924
|
+
});
|
|
1925
|
+
|
|
1926
|
+
test("a local ref shadowing `origin/main` does not read a real add/add conflict as none (#1565)", (t) => {
|
|
1927
|
+
// Same class release-ticket.sh:243 and reap.sh:174 already found in the same
|
|
1928
|
+
// default — worktree-audit.sh is not a carrier of this fix and still
|
|
1929
|
+
// measures the bare shorthand. `origin/main` is a SHORTHAND, and git
|
|
1930
|
+
// resolves a shorthand through its own disambiguation order (gitrevisions:
|
|
1931
|
+
// refs/<name>, refs/tags/<name>, refs/heads/<name>, refs/remotes/<name>,
|
|
1932
|
+
// …), in which refs/remotes/origin/main comes LAST. A local TAG literally
|
|
1933
|
+
// named `origin/main` outranks the real remote-tracking branch, so `git
|
|
1934
|
+
// merge-tree` and `git merge-base`/`git log` against the bare shorthand
|
|
1935
|
+
// below answer about the tag's target instead, at rc 0 — git's own
|
|
1936
|
+
// `warning: refname 'origin/main' is ambiguous.` on stderr is the only
|
|
1937
|
+
// tell, and this script reads none of it.
|
|
1938
|
+
const path = "conflict.txt";
|
|
1939
|
+
const c = bareConflictRepo(t, path);
|
|
1940
|
+
// The tag points at the FORK point — one commit behind main's real tip —
|
|
1941
|
+
// rather than at main's tip. The fork point never touched `path`, so a
|
|
1942
|
+
// merge-tree probe against the tag sees only the branch's ADD and reports
|
|
1943
|
+
// a clean merge; the real origin/main tip added the same path on its own
|
|
1944
|
+
// side, which is the add/add conflict this fixture exists to carry.
|
|
1945
|
+
const fork = git(c.w, "rev-parse", "main~1");
|
|
1946
|
+
git(c.w, "tag", "origin/main", fork);
|
|
1947
|
+
assert.equal(git(c.w, "rev-parse", "origin/main"), fork, "fixture: the shorthand now resolves to the fork point, not main's tip");
|
|
1948
|
+
assert.notEqual(git(c.w, "rev-parse", "refs/remotes/origin/main"), fork, "fixture: the real upstream is still main's later commit");
|
|
1949
|
+
|
|
1950
|
+
const r = audit(c);
|
|
1951
|
+
assert.equal(r.status, 0, `a clean worktree passes even with conflicts; got ${r.status} ${r.stderr}`);
|
|
1952
|
+
assert.equal(r.jsonError, null, `a passing audit must emit parseable JSON; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
1953
|
+
assert.deepEqual(r.json.conflicts, [path], "must measure the merge-tree probe against refs/remotes/origin/main, not the shadowing tag");
|
|
1954
|
+
assert.deepEqual(subjects(r), ["MAIN COMMIT AT RISK"], "must measure the at-risk commit list against refs/remotes/origin/main, not the shadowing tag");
|
|
1955
|
+
});
|
|
1956
|
+
|
|
1957
|
+
test("a local ref shadowing `origin/$branch` does not read a real add/add conflict as none (#1576)", (t) => {
|
|
1958
|
+
// The branch-side twin of the test above. #1565 qualified only the base
|
|
1959
|
+
// side into $base_rev; the branch side stayed the bare `origin/$branch`
|
|
1960
|
+
// shorthand, so the identical false safe was still reachable by shadowing
|
|
1961
|
+
// the BRANCH's name instead of the base's. A local TAG literally named
|
|
1962
|
+
// `origin/<branch>` outranks the real remote-tracking branch in git's own
|
|
1963
|
+
// disambiguation order (refs/tags/<name> before refs/remotes/<name>), so
|
|
1964
|
+
// `git merge-tree` and `git merge-base` against the bare shorthand below
|
|
1965
|
+
// would answer about the tag's target instead, at rc 0.
|
|
1966
|
+
const path = "conflict.txt";
|
|
1967
|
+
const c = bareConflictRepo(t, path);
|
|
1968
|
+
// The tag points at the branch's PRIOR commit — one commit behind the
|
|
1969
|
+
// branch's real tip, before it added `path` — rather than at the branch's
|
|
1970
|
+
// real tip. A merge-tree probe against the tag then sees main's ADD of
|
|
1971
|
+
// `path` against a branch state that never touched `path` at all: a clean
|
|
1972
|
+
// merge, not the add/add conflict this fixture exists to carry.
|
|
1973
|
+
const fork = git(c.w, "rev-parse", `${c.branch}~1`);
|
|
1974
|
+
git(c.w, "tag", `origin/${c.branch}`, fork);
|
|
1975
|
+
assert.equal(
|
|
1976
|
+
git(c.w, "rev-parse", `origin/${c.branch}`),
|
|
1977
|
+
fork,
|
|
1978
|
+
"fixture: the shorthand now resolves to the branch's prior commit, not its real tip",
|
|
1979
|
+
);
|
|
1980
|
+
assert.notEqual(
|
|
1981
|
+
git(c.w, "rev-parse", `refs/remotes/origin/${c.branch}`),
|
|
1982
|
+
fork,
|
|
1983
|
+
"fixture: the real upstream is still the branch's later commit",
|
|
1984
|
+
);
|
|
1985
|
+
|
|
1986
|
+
const r = audit(c);
|
|
1987
|
+
assert.equal(r.status, 0, `a clean worktree passes even with conflicts; got ${r.status} ${r.stderr}`);
|
|
1988
|
+
assert.equal(r.jsonError, null, `a passing audit must emit parseable JSON; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
1989
|
+
assert.deepEqual(r.json.conflicts, [path], "must measure the merge-tree probe against refs/remotes/origin/$branch, not the shadowing tag");
|
|
1990
|
+
assert.deepEqual(subjects(r), ["MAIN COMMIT AT RISK"], "must measure the at-risk commit list against refs/remotes/origin/$branch, not the shadowing tag");
|
|
1991
|
+
});
|
|
1992
|
+
|
|
1993
|
+
test("a local ref shadowing `origin/$branch` does not skew the merge-base call's at-risk range (#1576)", (t) => {
|
|
1994
|
+
// The test above proves only that the merge-tree call reads
|
|
1995
|
+
// refs/remotes/origin/$branch, not the shadowing tag: reverting just the
|
|
1996
|
+
// merge-base call's `$branch_rev` argument back to the bare `origin/$branch`
|
|
1997
|
+
// leaves it green, because merge-tree already used the qualified ref there
|
|
1998
|
+
// and the shadowing tag it plants happens to share the real branch's own
|
|
1999
|
+
// fork point with main. This fixture instead points the shadowing tag at
|
|
2000
|
+
// main's OWN tip, so a merge-base computed against the tag collapses the
|
|
2001
|
+
// at-risk range to nothing (`git merge-base main main` == main, so `git log
|
|
2002
|
+
// main..main` is empty) while a merge-base against the real
|
|
2003
|
+
// refs/remotes/origin/$branch still returns the true fork point and a
|
|
2004
|
+
// non-empty at-risk list — the exact false "clean" #1576 exists to close.
|
|
2005
|
+
const path = "conflict.txt";
|
|
2006
|
+
const c = bareConflictRepo(t, path);
|
|
2007
|
+
const mainTip = git(c.w, "rev-parse", "refs/remotes/origin/main");
|
|
2008
|
+
git(c.w, "tag", `origin/${c.branch}`, mainTip);
|
|
2009
|
+
assert.equal(
|
|
2010
|
+
git(c.w, "rev-parse", `origin/${c.branch}`),
|
|
2011
|
+
mainTip,
|
|
2012
|
+
"fixture: the shorthand now resolves to main's own tip, not the branch",
|
|
2013
|
+
);
|
|
2014
|
+
assert.notEqual(
|
|
2015
|
+
git(c.w, "rev-parse", `refs/remotes/origin/${c.branch}`),
|
|
2016
|
+
mainTip,
|
|
2017
|
+
"fixture: the real upstream is still the branch's own tip",
|
|
2018
|
+
);
|
|
2019
|
+
|
|
2020
|
+
const r = audit(c);
|
|
2021
|
+
assert.equal(r.status, 0, `a clean worktree passes even with conflicts; got ${r.status} ${r.stderr}`);
|
|
2022
|
+
assert.equal(r.jsonError, null, `a passing audit must emit parseable JSON; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
2023
|
+
assert.deepEqual(r.json.conflicts, [path], "the merge-tree call already reads refs/remotes/origin/$branch — unaffected by this fixture");
|
|
2024
|
+
assert.deepEqual(
|
|
2025
|
+
subjects(r),
|
|
2026
|
+
["MAIN COMMIT AT RISK"],
|
|
2027
|
+
"must compute the merge-base against refs/remotes/origin/$branch, not the shadowing tag — a merge-base against the tag collapses the at-risk range to nothing",
|
|
2028
|
+
);
|
|
2029
|
+
});
|
|
2030
|
+
|
|
2031
|
+
test("every unanswerable precondition exits 2 and emits no payload", (t) => {
|
|
2032
|
+
const c = repo(t);
|
|
2033
|
+
const plain = mkdtempSync(join(tmpdir(), "no-undo-audit-plain-"));
|
|
2034
|
+
t.after(() => rmSync(plain, { recursive: true, force: true }));
|
|
2035
|
+
|
|
2036
|
+
const cases = [
|
|
2037
|
+
["too few arguments", [c.w], ENV, /usage:/],
|
|
2038
|
+
["too many arguments", [c.w, c.branch, "extra"], ENV, /usage:/],
|
|
2039
|
+
["worktree does not exist", [join(c.w, "nope"), c.branch], ENV, /does not exist/],
|
|
2040
|
+
["not a git worktree", [plain, c.branch], ENV, /is not a git worktree/],
|
|
2041
|
+
["branch never pushed", [c.w, "never-pushed"], ENV, /origin\/never-pushed does not resolve/],
|
|
2042
|
+
["BASE_REF does not resolve", [c.w, c.branch], { ...ENV, BASE_REF: "origin/no-such-ref" }, /does not resolve/],
|
|
2043
|
+
["BASE_REF outside the remote-tracking namespace", [c.w, c.branch], { ...ENV, BASE_REF: "refs/heads/main" }, /BASE_REF must be spelled origin\/<branch> or refs\/remotes\/<path>, got 'refs\/heads\/main'/],
|
|
2044
|
+
// Both spellings of the audited branch, because both clear the accept-list
|
|
2045
|
+
// and both make every measurement ask whether the branch conflicts with
|
|
2046
|
+
// ITSELF — `conflicts: []`, `atRisk: []` at exit 0 on a worktree that
|
|
2047
|
+
// really does conflict with origin/main. A guard narrowed to the shorthand
|
|
2048
|
+
// would leave the qualified half of that reachable, so both are pinned.
|
|
2049
|
+
["BASE_REF names the audited branch", [c.w, c.branch], { ...ENV, BASE_REF: `origin/${c.branch}` }, new RegExp(`BASE_REF must not name the audited branch, got 'origin/${c.branch}'`)],
|
|
2050
|
+
["BASE_REF names the audited branch, qualified", [c.w, c.branch], { ...ENV, BASE_REF: `refs/remotes/origin/${c.branch}` }, new RegExp(`BASE_REF must not name the audited branch, got 'refs/remotes/origin/${c.branch}'`)],
|
|
2051
|
+
];
|
|
2052
|
+
|
|
2053
|
+
for (const [why, args, env, re] of cases) {
|
|
2054
|
+
const r = spawnSync("sh", [SCRIPT, ...args], { cwd: c.w, env, encoding: "utf8" });
|
|
2055
|
+
assert.equal(r.status, 2, `${why}: expected 2, got ${r.status} — ${r.stderr}`);
|
|
2056
|
+
assert.match(r.stderr, re, why);
|
|
2057
|
+
assert.equal(r.stdout, "", `${why}: an unanswerable audit must not emit a payload`);
|
|
2058
|
+
}
|
|
2059
|
+
});
|
|
2060
|
+
|
|
2061
|
+
// ---------------------------------------------------------------------------
|
|
2062
|
+
// Whose worktree is the answer about. Exit 2 again for the refusals, but the
|
|
2063
|
+
// question is upstream of every check above: the script has to be looking at
|
|
2064
|
+
// the tree it was handed. The passing case closes the block, because a guard
|
|
2065
|
+
// that establishes identity is one keystroke from refusing every real worktree.
|
|
2066
|
+
// ---------------------------------------------------------------------------
|
|
2067
|
+
|
|
2068
|
+
// The `is not a git worktree` case in the preconditions above passes a plain
|
|
2069
|
+
// directory with no repo ANYWHERE above it, so `rev-parse --git-dir` fails and
|
|
2070
|
+
// the script refuses. That is the harmless half. These are the other half:
|
|
2071
|
+
// `rev-parse --git-dir` WALKS UP, so with an enclosing repo present the gate
|
|
2072
|
+
// passes at rc 0 having resolved a git dir that is not this worktree's, and
|
|
2073
|
+
// `status --porcelain` then answers for that repo — empty, at rc 0, because the
|
|
2074
|
+
// enclosing repo is clean and `.worktrees/` is gitignored. `clean:true` for a
|
|
2075
|
+
// tree the script never looked at, with the work still sitting on disk. The
|
|
2076
|
+
// `die` on a failing status is the wrong side of this: the command SUCCEEDS,
|
|
2077
|
+
// it just answers about somewhere else.
|
|
2078
|
+
//
|
|
2079
|
+
// Both assert the refusal lands BEFORE the audit reports anything. Exit 2 alone
|
|
2080
|
+
// would not pin it — a script that audits, prints "clean", and refuses
|
|
2081
|
+
// afterwards has already put the wrong answer on the caller's screen.
|
|
2082
|
+
function refusedAsUnknownBeforeAnySay(c) {
|
|
2083
|
+
assert.ok(existsSync(join(c.w, "precious.txt")), "fixture: the uncommitted work must still be on disk");
|
|
2084
|
+
assert.equal(git(c.parent, "status", "--porcelain"), "", "fixture: a CLEAN enclosing repo is what makes the leak answer 'clean'");
|
|
2085
|
+
assert.doesNotThrow(
|
|
2086
|
+
() => git(c.w, "rev-parse", "--git-dir"),
|
|
2087
|
+
"fixture: the script's own gate must still pass here, or this test pins nothing",
|
|
2088
|
+
);
|
|
2089
|
+
assert.equal(git(c.w, "status", "--porcelain"), "", "fixture: git answers for the enclosing repo — the manufactured clean this must refuse");
|
|
2090
|
+
|
|
2091
|
+
const r = audit(c);
|
|
2092
|
+
assert.equal(r.status, 2, `got ${r.status} with stdout ${r.stdout}`);
|
|
2093
|
+
assert.equal(r.stdout, "", "an unanswerable audit must not emit a payload");
|
|
2094
|
+
assert.doesNotMatch(r.stderr, /status --porcelain/, "the refusal must land before the audit runs, let alone reports");
|
|
2095
|
+
assert.match(r.stderr, /answers for the repo above/);
|
|
2096
|
+
}
|
|
2097
|
+
|
|
2098
|
+
test("a worktree whose .git was deleted is unanswerable (2), never clean (0)", (t) => {
|
|
2099
|
+
const c = nestedWorktree(t);
|
|
2100
|
+
rmSync(join(c.w, ".git"));
|
|
2101
|
+
|
|
2102
|
+
refusedAsUnknownBeforeAnySay(c);
|
|
2103
|
+
});
|
|
2104
|
+
|
|
2105
|
+
// One byte over: an EMPTY `.git` DIRECTORY is something a `-e "$wt/.git"` guard
|
|
2106
|
+
// calls present, and git walks up past it exactly as it does past an absent one.
|
|
2107
|
+
test("a worktree whose .git is an empty directory is unanswerable (2), never clean (0)", (t) => {
|
|
2108
|
+
const c = nestedWorktree(t);
|
|
2109
|
+
rmSync(join(c.w, ".git"));
|
|
2110
|
+
mkdirSync(join(c.w, ".git"));
|
|
2111
|
+
assert.ok(existsSync(join(c.w, ".git")), "fixture: a `-e` guard must call this .git present, or it pins the case above again");
|
|
2112
|
+
|
|
2113
|
+
refusedAsUnknownBeforeAnySay(c);
|
|
2114
|
+
});
|
|
2115
|
+
|
|
2116
|
+
// And one byte over again, which is why the guard asks git instead of stat-ing
|
|
2117
|
+
// `.git`: a DIRECTORY holding a lone HEAD. Every "does the linkage exist" guard
|
|
2118
|
+
// spelled against the filesystem calls this present — `-e "$wt/.git/HEAD"` most
|
|
2119
|
+
// obviously — and git still walks up, because it wants HEAD *and* `objects/`
|
|
2120
|
+
// *and* `refs/` before it will call a directory a git dir. Each subset below
|
|
2121
|
+
// manufactured `clean:true` at exit 0 against such a guard (measured, git
|
|
2122
|
+
// 2.50.1); the last is a REAL `.git` whose HEAD an interrupted write truncated,
|
|
2123
|
+
// so this is not only a hand-built shape.
|
|
2124
|
+
for (const [why, build] of [
|
|
2125
|
+
["holding a lone HEAD", (g) => writeFileSync(join(g, "HEAD"), "ref: refs/heads/fix/9-nested\n")],
|
|
2126
|
+
["whose HEAD is empty", (g) => writeFileSync(join(g, "HEAD"), "")],
|
|
2127
|
+
["missing objects/", (g) => {
|
|
2128
|
+
writeFileSync(join(g, "HEAD"), "ref: refs/heads/fix/9-nested\n");
|
|
2129
|
+
mkdirSync(join(g, "refs"));
|
|
2130
|
+
}],
|
|
2131
|
+
]) {
|
|
2132
|
+
test(`a worktree whose .git is a directory ${why} is unanswerable (2), never clean (0)`, (t) => {
|
|
2133
|
+
const c = nestedWorktree(t);
|
|
2134
|
+
rmSync(join(c.w, ".git"));
|
|
2135
|
+
mkdirSync(join(c.w, ".git"));
|
|
2136
|
+
build(join(c.w, ".git"));
|
|
2137
|
+
assert.ok(existsSync(join(c.w, ".git", "HEAD")), "fixture: HEAD must be present, or this pins the empty-directory case again");
|
|
2138
|
+
|
|
2139
|
+
refusedAsUnknownBeforeAnySay(c);
|
|
2140
|
+
});
|
|
2141
|
+
}
|
|
2142
|
+
|
|
2143
|
+
// The other direction, and it is not theory: a guard spelled `-e
|
|
2144
|
+
// "$wt/.git/HEAD"` alone passes every refusal test above (measured) while
|
|
2145
|
+
// refusing every LINKED worktree on disk, whose `.git` is a file and which
|
|
2146
|
+
// therefore has no `.git/HEAD` to stat. That is the fleet's own shape —
|
|
2147
|
+
// `claim-ticket.sh` makes worktrees with `git worktree add` — so the false
|
|
2148
|
+
// refusal would land on every real caller while the suite stayed green. This
|
|
2149
|
+
// pins the shape the refusal tests do not reach.
|
|
2150
|
+
test("an intact linked worktree, whose .git is a file, still passes", (t) => {
|
|
2151
|
+
const c = nestedWorktree(t);
|
|
2152
|
+
rmSync(join(c.w, "precious.txt"));
|
|
2153
|
+
assert.ok(statSync(join(c.w, ".git")).isFile(), "fixture must leave the linkage intact, and leave it a FILE — the shape this test exists to pin");
|
|
2154
|
+
assert.equal(git(c.w, "status", "--porcelain"), "", "fixture must leave the worktree clean");
|
|
2155
|
+
|
|
2156
|
+
const r = audit(c);
|
|
2157
|
+
assert.equal(r.status, 0, `a linked worktree is the fleet's own shape; got ${r.status} ${r.stderr}`);
|
|
2158
|
+
assert.equal(r.json.clean, true);
|
|
2159
|
+
});
|
|
2160
|
+
|
|
2161
|
+
// The same worktree, reached through a symlinked path. `--show-toplevel`
|
|
2162
|
+
// resolves the symlink away, so the linkage guard's working-tree compare
|
|
2163
|
+
// (#2040) holds it against the CANONICAL `$wt`, never against the path as
|
|
2164
|
+
// typed — this case is what reds if that canonicalisation is ever dropped.
|
|
2165
|
+
test("an intact linked worktree, reached through a symlinked path, still passes", (t) => {
|
|
2166
|
+
const c = nestedWorktree(t);
|
|
2167
|
+
rmSync(join(c.w, "precious.txt"));
|
|
2168
|
+
const link = `${c.w}-symlink`;
|
|
2169
|
+
symlinkSync(c.w, link);
|
|
2170
|
+
t.after(() => rmSync(link, { force: true }));
|
|
2171
|
+
|
|
2172
|
+
const r = audit({ w: link, branch: c.branch });
|
|
2173
|
+
assert.equal(r.status, 0, `a symlinked path to a real worktree must still pass; got ${r.status} ${r.stderr}`);
|
|
2174
|
+
assert.equal(r.json.clean, true);
|
|
2175
|
+
});
|
|
2176
|
+
|
|
2177
|
+
// The same worktree, named `cafe` + U+0301 COMBINING ACUTE ACCENT and passed in
|
|
2178
|
+
// the spelling `worktree list --porcelain` gives it — reap.sh's `nfd` fixture
|
|
2179
|
+
// (#2072), here for the working-tree compare. Where the filesystem also
|
|
2180
|
+
// resolves the precomposed spelling to that directory (APFS), the
|
|
2181
|
+
// `core.precomposeunicode` `git clone` writes there lists it precomposed while
|
|
2182
|
+
// `--show-toplevel` answers the on-disk NFD bytes, so a byte compare refused
|
|
2183
|
+
// this healthy worktree at exit 2 (#2095). Where it does not (ext4), both
|
|
2184
|
+
// answers are the NFD bytes and this is the plain case.
|
|
2185
|
+
test("an intact linked worktree with an NFD name, passed as git lists it, still passes (#2095)", (t) => {
|
|
2186
|
+
const { parent, w: onDisk, branch } = nestedWorktree(t, "cafe\u0301");
|
|
2187
|
+
rmSync(join(onDisk, "precious.txt"));
|
|
2188
|
+
|
|
2189
|
+
const listing = git(parent, "worktree", "list", "--porcelain").split("\n")
|
|
2190
|
+
.filter((l) => l.startsWith("worktree ")).map((l) => l.slice(9));
|
|
2191
|
+
const wt = listing.find((p) => p.normalize("NFC").endsWith("/.worktrees/caf\u00e9"));
|
|
2192
|
+
assert.ok(wt, `fixture: git must list the worktree: ${listing}`);
|
|
2193
|
+
const top = git(onDisk, "rev-parse", "--show-toplevel");
|
|
2194
|
+
assert.ok(top.endsWith("/.worktrees/cafe\u0301"), "fixture: git must answer the on-disk NFD bytes");
|
|
2195
|
+
if (existsSync(onDisk.normalize("NFC"))) {
|
|
2196
|
+
assert.notEqual(wt, top, "fixture: a filesystem that aliases the two spellings must list one git's toplevel answer does not, byte for byte");
|
|
2197
|
+
}
|
|
2198
|
+
|
|
2199
|
+
const r = audit({ w: wt, branch });
|
|
2200
|
+
assert.equal(r.status, 0, `an NFD-named worktree passed as git lists it must still pass; got ${r.status} ${r.stderr}`);
|
|
2201
|
+
assert.equal(r.json.clean, true);
|
|
2202
|
+
});
|
|
2203
|
+
|
|
2204
|
+
// The same fixture, `$wt` passed exactly as it sits on disk (NFD) rather than
|
|
2205
|
+
// as git lists it. `--show-toplevel` already answers this spelling, so this
|
|
2206
|
+
// leaves the test above's working-tree compare a no-op and instead exercises
|
|
2207
|
+
// the OWNER compares above it (#2095): `owner` is read back from git's own
|
|
2208
|
+
// `gitdir` back-pointer file, spelled however `core.precomposeunicode` wrote
|
|
2209
|
+
// it when the linkage was created (measured: precomposed, even though the
|
|
2210
|
+
// directory itself stayed decomposed on disk), which can differ — byte for
|
|
2211
|
+
// byte, same directory — from `$wt` as passed here.
|
|
2212
|
+
test("an intact linked worktree with an NFD name, passed as it sits on disk, still passes (#2095)", (t) => {
|
|
2213
|
+
const c = nestedWorktree(t, "cafe\u0301");
|
|
2214
|
+
rmSync(join(c.w, "precious.txt"));
|
|
2215
|
+
|
|
2216
|
+
const r = audit(c);
|
|
2217
|
+
assert.equal(r.status, 0, `an NFD on-disk worktree path passed as-is must still pass; got ${r.status} ${r.stderr}`);
|
|
2218
|
+
assert.equal(r.json.clean, true);
|
|
2219
|
+
});
|
|
2220
|
+
|
|
2221
|
+
// ---------------------------------------------------------------------------
|
|
2222
|
+
// #189: whose ADMIN DIR answered, not just whose ROOT git resolved. A `.git`
|
|
2223
|
+
// FILE takes its root from the file's own location, so a `.git` rewritten to
|
|
2224
|
+
// name a SIBLING worktree's admin dir still passes --show-prefix — the root
|
|
2225
|
+
// is genuinely $wt — while status is computed against the sibling's HEAD and
|
|
2226
|
+
// index. Every case below refuses via `die`, exit 2, no payload: the guard
|
|
2227
|
+
// establishes identity BEFORE the audit runs, same as the --show-prefix block
|
|
2228
|
+
// above it, never a warning printed alongside a "clean" answer.
|
|
2229
|
+
// ---------------------------------------------------------------------------
|
|
2230
|
+
|
|
2231
|
+
test("a .git file naming a sibling worktree's admin dir is refused, never clean — the ticket's repro", (t) => {
|
|
2232
|
+
const c = nestedWorktreePair(t);
|
|
2233
|
+
assert.ok(existsSync(join(c.w, "precious.txt")), "fixture: uncommitted work must still be on disk");
|
|
2234
|
+
// The ticket's leak needs more than an empty prefix: `status` is computed
|
|
2235
|
+
// against the sibling's INDEX, but the WORKING TREE stays $wt's own files
|
|
2236
|
+
// on disk (a `.git` file redirects the git-dir, not the work-tree). Content
|
|
2237
|
+
// collision is what makes that read clean — the sibling commits the exact
|
|
2238
|
+
// bytes $wt already has sitting there uncommitted.
|
|
2239
|
+
writeFileSync(join(c.sibling, "precious.txt"), readFileSync(join(c.w, "precious.txt")));
|
|
2240
|
+
git(c.sibling, "add", "precious.txt");
|
|
2241
|
+
git(c.sibling, "commit", "-q", "-m", "precious.txt, committed here and only here");
|
|
2242
|
+
writeFileSync(join(c.w, ".git"), `gitdir: ${c.siblingAdmin}\n`);
|
|
2243
|
+
// The spoof still resolves a root of $wt and a foreign, CLEAN HEAD/index —
|
|
2244
|
+
// the exact leak the ticket measured, reproduced before asserting the fix.
|
|
2245
|
+
assert.equal(git(c.w, "rev-parse", "--show-prefix"), "", "fixture: the root claim must still admit, or this pins nothing new");
|
|
2246
|
+
assert.equal(git(c.w, "status", "--porcelain"), "", "fixture: the sibling's HEAD must read clean, or this is the old leak by a different name");
|
|
2247
|
+
|
|
2248
|
+
const r = audit(c);
|
|
2249
|
+
assert.equal(r.status, 2, `must be unanswerable, not clean; got ${r.status} ${r.stdout}`);
|
|
2250
|
+
assert.equal(r.stdout, "", "an unanswerable audit must not emit a payload");
|
|
2251
|
+
assert.match(r.stderr, /names another worktree's admin dir/);
|
|
2252
|
+
});
|
|
2253
|
+
|
|
2254
|
+
test("a .git file naming a sibling's admin dir by a RELATIVE gitdir: path is refused the same way", (t) => {
|
|
2255
|
+
const c = nestedWorktreePair(t);
|
|
2256
|
+
// Relative to $wt/.git's own directory, i.e. $wt itself — same shape git
|
|
2257
|
+
// itself resolves relative gitdir: lines against. Spelled as the literal the
|
|
2258
|
+
// fixture's own layout already fixes, rather than computed: the assertion
|
|
2259
|
+
// below is what keeps it honest, since a wrong spelling resolves elsewhere
|
|
2260
|
+
// and fails there loudly.
|
|
2261
|
+
writeFileSync(join(c.w, ".git"), "gitdir: ../../.git/worktrees/8-y\n");
|
|
2262
|
+
assert.equal(git(c.w, "rev-parse", "--git-dir"), c.siblingAdmin, "fixture: git must resolve the relative spoof to the sibling admin dir, or this pins nothing new");
|
|
2263
|
+
|
|
2264
|
+
const r = audit(c);
|
|
2265
|
+
assert.equal(r.status, 2, `must be unanswerable, not clean; got ${r.status} ${r.stdout}`);
|
|
2266
|
+
assert.equal(r.stdout, "");
|
|
2267
|
+
assert.match(r.stderr, /names another worktree's admin dir/);
|
|
2268
|
+
});
|
|
2269
|
+
|
|
2270
|
+
// The same spoof, spelled as a SYMLINK instead of a `gitdir:` file. It is not a
|
|
2271
|
+
// variant of the test above but a separate code path: `rev-parse --git-dir`
|
|
2272
|
+
// answers the bare `.git` for this shape — the same string a main checkout
|
|
2273
|
+
// answers — so a guard that reads that string as "the main worktree, nothing to
|
|
2274
|
+
// verify" skips the linkage check entirely and admits the leak at exit 0.
|
|
2275
|
+
// Measured on git 2.50.1, and measured admitted by the first spelling of this
|
|
2276
|
+
// PR's own guard.
|
|
2277
|
+
test("a .git SYMLINKED to a sibling worktree's admin dir is refused, never clean", (t) => {
|
|
2278
|
+
const c = nestedWorktreePair(t);
|
|
2279
|
+
assert.ok(existsSync(join(c.w, "precious.txt")), "fixture: uncommitted work must still be on disk");
|
|
2280
|
+
writeFileSync(join(c.sibling, "precious.txt"), readFileSync(join(c.w, "precious.txt")));
|
|
2281
|
+
git(c.sibling, "add", "precious.txt");
|
|
2282
|
+
git(c.sibling, "commit", "-q", "-m", "precious.txt, committed here and only here");
|
|
2283
|
+
rmSync(join(c.w, ".git"));
|
|
2284
|
+
symlinkSync(c.siblingAdmin, join(c.w, ".git"));
|
|
2285
|
+
assert.equal(git(c.w, "rev-parse", "--git-dir"), ".git", "fixture: this spelling must still answer the bare `.git`, or it is no longer the shape that bypassed the guard");
|
|
2286
|
+
assert.equal(git(c.w, "rev-parse", "--show-prefix"), "", "fixture: the root claim must still admit, or this pins nothing new");
|
|
2287
|
+
assert.equal(git(c.w, "status", "--porcelain"), "", "fixture: the sibling's HEAD must read clean, or this is the old leak by a different name");
|
|
2288
|
+
|
|
2289
|
+
const r = audit(c);
|
|
2290
|
+
assert.equal(r.status, 2, `must be unanswerable, not clean; got ${r.status} ${r.stdout}`);
|
|
2291
|
+
assert.equal(r.stdout, "", "an unanswerable audit must not emit a payload");
|
|
2292
|
+
assert.match(r.stderr, /names another worktree's admin dir/);
|
|
2293
|
+
});
|
|
2294
|
+
|
|
2295
|
+
test("an admin dir whose own gitdir back-pointer file is missing is unanswerable, never clean", (t) => {
|
|
2296
|
+
const c = nestedWorktree(t);
|
|
2297
|
+
const admin = git(c.w, "rev-parse", "--path-format=absolute", "--git-dir");
|
|
2298
|
+
rmSync(join(admin, "gitdir"));
|
|
2299
|
+
|
|
2300
|
+
const r = audit(c);
|
|
2301
|
+
assert.equal(r.status, 2, `must be unanswerable, not clean; got ${r.status} ${r.stdout}`);
|
|
2302
|
+
assert.equal(r.stdout, "");
|
|
2303
|
+
assert.match(r.stderr, /gitdir is missing or unreadable/);
|
|
2304
|
+
});
|
|
2305
|
+
|
|
2306
|
+
test("an admin dir whose own gitdir back-pointer file is unreadable is unanswerable, never clean", (t) => {
|
|
2307
|
+
const c = nestedWorktree(t);
|
|
2308
|
+
const admin = git(c.w, "rev-parse", "--path-format=absolute", "--git-dir");
|
|
2309
|
+
// No restore: the outer temp-dir cleanup (registered by `repo()`, and so
|
|
2310
|
+
// ahead of this test's own `t.after`) force-removes the whole tree first,
|
|
2311
|
+
// and deleting a file needs write access to its DIRECTORY, never to the
|
|
2312
|
+
// file itself — same reasoning the existing stash-reflog fixtures above
|
|
2313
|
+
// already rely on without restoring their own chmods.
|
|
2314
|
+
chmodSync(join(admin, "gitdir"), 0o000);
|
|
2315
|
+
|
|
2316
|
+
const r = audit(c);
|
|
2317
|
+
assert.equal(r.status, 2, `must be unanswerable, not clean; got ${r.status} ${r.stdout}`);
|
|
2318
|
+
assert.equal(r.stdout, "");
|
|
2319
|
+
assert.match(r.stderr, /gitdir is missing or unreadable/);
|
|
2320
|
+
});
|
|
2321
|
+
|
|
2322
|
+
// The false-refusal side of the same trim: a legitimate back-pointer file with
|
|
2323
|
+
// trailing whitespace tacked on must still compare equal, or the guard refuses
|
|
2324
|
+
// worktrees it has no reason to. Real git never writes trailing whitespace
|
|
2325
|
+
// here, but a hand-edited or copy-touched one could, and the comparison being
|
|
2326
|
+
// exact-string means a wrong turn on this trim is a silent over-refusal, not
|
|
2327
|
+
// a loud one.
|
|
2328
|
+
test("a back-pointer file with trailing whitespace still passes, not falsely refused", (t) => {
|
|
2329
|
+
const c = nestedWorktree(t);
|
|
2330
|
+
rmSync(join(c.w, "precious.txt"));
|
|
2331
|
+
const admin = git(c.w, "rev-parse", "--path-format=absolute", "--git-dir");
|
|
2332
|
+
const gitdirFile = join(admin, "gitdir");
|
|
2333
|
+
writeFileSync(gitdirFile, `${readFileSync(gitdirFile, "utf8").trimEnd()} \t\n`);
|
|
2334
|
+
|
|
2335
|
+
const r = audit(c);
|
|
2336
|
+
assert.equal(r.status, 0, `trailing whitespace on the back-pointer must not refuse; got ${r.status} ${r.stderr}`);
|
|
2337
|
+
assert.equal(r.json.clean, true);
|
|
2338
|
+
});
|
|
2339
|
+
|
|
2340
|
+
// ---------------------------------------------------------------------------
|
|
2341
|
+
// The ACCEPT side of the same guard. A gate on an irreversible action is as
|
|
2342
|
+
// wrong when it refuses a healthy checkout as when it admits a spoofed one, and
|
|
2343
|
+
// a suite that only feeds a refusal guard spoofs pins nothing about what it must
|
|
2344
|
+
// still answer for. Every shape below is one `git` itself produces, and each
|
|
2345
|
+
// was measured REFUSED (exit 2, no payload) by the first spelling of this
|
|
2346
|
+
// guard. The intact-linked-worktree and symlinked-path cases above, and every
|
|
2347
|
+
// `repo(t)` test in this file (a plain main checkout), are the rest of the set.
|
|
2348
|
+
// ---------------------------------------------------------------------------
|
|
2349
|
+
|
|
2350
|
+
// `--git-dir` answers the bare `.git` here too, exactly as it does for the
|
|
2351
|
+
// sibling-admin-dir SYMLINK spoof above — so this is the pair that shows the
|
|
2352
|
+
// guard discriminates on whose admin dir answered rather than on that string:
|
|
2353
|
+
// same `--git-dir` answer, opposite verdict.
|
|
2354
|
+
test("a .git symlinked to the worktree's OWN admin dir still passes", (t) => {
|
|
2355
|
+
const c = nestedWorktree(t);
|
|
2356
|
+
rmSync(join(c.w, "precious.txt"));
|
|
2357
|
+
const admin = git(c.w, "rev-parse", "--path-format=absolute", "--git-dir");
|
|
2358
|
+
rmSync(join(c.w, ".git"));
|
|
2359
|
+
symlinkSync(admin, join(c.w, ".git"));
|
|
2360
|
+
assert.equal(git(c.w, "rev-parse", "--git-dir"), ".git", "fixture: this must be the same `--git-dir` answer the spoof gives, or the pair proves nothing");
|
|
2361
|
+
|
|
2362
|
+
const r = audit(c);
|
|
2363
|
+
assert.equal(r.status, 0, `a .git symlinked to its own admin dir must pass; got ${r.status} ${r.stderr}`);
|
|
2364
|
+
assert.equal(r.json.clean, true);
|
|
2365
|
+
});
|
|
2366
|
+
|
|
2367
|
+
// git writes the back-pointer RELATIVE, not absolute, whenever
|
|
2368
|
+
// `worktree.useRelativePaths` is set — per-repo config, or `git worktree add
|
|
2369
|
+
// --relative-paths` per invocation. Written here by hand rather than by that
|
|
2370
|
+
// flag, in the exact spelling git produces (relative to the admin dir, which is
|
|
2371
|
+
// what git resolves it against): the flag and the config both arrived in git
|
|
2372
|
+
// 2.48, and a fixture that needs them stops covering anything, silently and
|
|
2373
|
+
// green, on any older git the suite is run under.
|
|
2374
|
+
test("a RELATIVE back-pointer still passes, not read as another worktree's admin dir", (t) => {
|
|
2375
|
+
const c = nestedWorktree(t);
|
|
2376
|
+
rmSync(join(c.w, "precious.txt"));
|
|
2377
|
+
const admin = git(c.w, "rev-parse", "--path-format=absolute", "--git-dir");
|
|
2378
|
+
writeFileSync(join(admin, "gitdir"), "../../../.worktrees/9-x/.git\n");
|
|
2379
|
+
|
|
2380
|
+
const r = audit(c);
|
|
2381
|
+
assert.equal(r.status, 0, `a relative back-pointer must not refuse; got ${r.status} ${r.stderr}`);
|
|
2382
|
+
assert.equal(r.json.clean, true);
|
|
2383
|
+
});
|
|
2384
|
+
|
|
2385
|
+
// Neither shape below is a linked worktree, so neither admin dir carries a
|
|
2386
|
+
// `gitdir` back-pointer to check — and "has no back-pointer" is not "belongs to
|
|
2387
|
+
// another worktree". Both are `.git` FILES redirecting to a git dir elsewhere,
|
|
2388
|
+
// which is what makes them land in the same guard as #189's spoof; both are
|
|
2389
|
+
// DIRTY, so a pass here is the audit actually answering (exit 1, `clean:false`)
|
|
2390
|
+
// rather than a refusal wearing a fail-safe exit code.
|
|
2391
|
+
//
|
|
2392
|
+
// Each audits its own checkout's feature branch rather than `main`: BASE_REF
|
|
2393
|
+
// defaults to `origin/main`, and the audited-branch guard refuses a BASE_REF
|
|
2394
|
+
// whose last component is the branch under audit, so `main` here would be
|
|
2395
|
+
// refused at exit 2 before either shape reached the linkage question these two
|
|
2396
|
+
// exist to ask.
|
|
2397
|
+
test("a submodule checkout is audited, not refused for carrying no back-pointer", (t) => {
|
|
2398
|
+
const c = repo(t);
|
|
2399
|
+
const sub = repo(t, "fix/2-sub", "no-undo-audit-sub-");
|
|
2400
|
+
git(c.w, "-c", "protocol.file.allow=always", "submodule", "add", "-q", git(sub.w, "remote", "get-url", "origin"), "sub");
|
|
2401
|
+
const w = join(c.w, "sub");
|
|
2402
|
+
writeFileSync(join(w, "dirt.txt"), "uncommitted, inside a submodule\n");
|
|
2403
|
+
|
|
2404
|
+
const r = audit({ w, branch: sub.branch });
|
|
2405
|
+
assert.equal(r.status, 1, `a dirty submodule must report dirty, not refuse; got ${r.status} ${r.stderr}`);
|
|
2406
|
+
assert.equal(r.json.clean, false);
|
|
2407
|
+
});
|
|
2408
|
+
|
|
2409
|
+
test("a --separate-git-dir clone is audited, not refused for carrying no back-pointer", (t) => {
|
|
2410
|
+
const c = repo(t);
|
|
2411
|
+
const w = `${c.w}-sgd`;
|
|
2412
|
+
execFileSync("git", ["clone", "-q", "--separate-git-dir", `${w}.git`, git(c.w, "remote", "get-url", "origin"), w], { env: ENV });
|
|
2413
|
+
writeFileSync(join(w, "dirt.txt"), "uncommitted, in a --separate-git-dir clone\n");
|
|
2414
|
+
|
|
2415
|
+
const r = audit({ w, branch: c.branch });
|
|
2416
|
+
assert.equal(r.status, 1, `a dirty --separate-git-dir clone must report dirty, not refuse; got ${r.status} ${r.stderr}`);
|
|
2417
|
+
assert.equal(r.json.clean, false);
|
|
2418
|
+
});
|
|
2419
|
+
|
|
2420
|
+
// Named in #189 alongside the spoof: not this ticket's mechanism (git refuses
|
|
2421
|
+
// before the linkage guard even runs, same as a deleted `.git`), but the same
|
|
2422
|
+
// enumerate pass that found the spoof named it too, so it is pinned here
|
|
2423
|
+
// rather than assumed.
|
|
2424
|
+
test("a dangling .git symlink is unanswerable (2), never clean (0)", (t) => {
|
|
2425
|
+
const c = nestedWorktree(t);
|
|
2426
|
+
rmSync(join(c.w, ".git"));
|
|
2427
|
+
symlinkSync("/nonexistent-target-189", join(c.w, ".git"));
|
|
2428
|
+
|
|
2429
|
+
refusedAsUnknownBeforeAnySay(c);
|
|
2430
|
+
});
|
|
2431
|
+
|
|
2432
|
+
// ---------------------------------------------------------------------------
|
|
2433
|
+
// #2040: `core.worktree` moves the WORKING TREE, not the git dir. Every shape
|
|
2434
|
+
// below passes the `--show-prefix` root claim (empty at rc 0) and the git-dir
|
|
2435
|
+
// owner check, and `status` then compares $wt's own HEAD and index against the
|
|
2436
|
+
// redirect target's files. A target holding the tracked content — any other
|
|
2437
|
+
// checkout of the branch — reads clean at rc 0 over `precious.txt`: the one
|
|
2438
|
+
// WRONG VERDICT this script can give, where every other broken shape refuses.
|
|
2439
|
+
// Refused by the working-tree compare (`--show-toplevel` against the canonical
|
|
2440
|
+
// $wt), exit 2, before anything is audited.
|
|
2441
|
+
// ---------------------------------------------------------------------------
|
|
2442
|
+
|
|
2443
|
+
/**
|
|
2444
|
+
* The three fixture claims that make a redirect case pin something: the work
|
|
2445
|
+
* is on disk, the root claim still admits the shape (or this is the #74 walk-up
|
|
2446
|
+
* again under another name), and git itself answers "clean" — the false clean
|
|
2447
|
+
* the refusal exists to replace. Then the refusal: exit 2, no payload, before
|
|
2448
|
+
* the audit runs, naming the working tree git actually answered for.
|
|
2449
|
+
*/
|
|
2450
|
+
function refusedAsRedirected(c, target) {
|
|
2451
|
+
assert.ok(existsSync(join(c.w, "precious.txt")), "fixture: uncommitted work must still be on disk");
|
|
2452
|
+
assert.equal(git(c.w, "rev-parse", "--show-prefix"), "", "fixture: the --show-prefix gate must still admit this shape, or this pins nothing new");
|
|
2453
|
+
assert.equal(git(c.w, "rev-parse", "--show-toplevel"), target, "fixture: git must answer for the redirect target");
|
|
2454
|
+
assert.equal(git(c.w, "status", "--porcelain", "-uall"), "", "fixture: git must read clean over the work — the false clean this pins");
|
|
2455
|
+
|
|
2456
|
+
const r = audit(c);
|
|
2457
|
+
assert.equal(r.status, 2, `a core.worktree redirect must be unanswerable, not clean: got ${r.status} ${r.stdout}`);
|
|
2458
|
+
assert.equal(r.stdout, "", "an unanswerable audit must not emit a payload");
|
|
2459
|
+
assert.doesNotMatch(r.stderr, /status --porcelain/, "the refusal must land before the audit runs");
|
|
2460
|
+
assert.ok(r.stderr.includes(`git answers for the working tree at ${target}, not ${c.w}`), r.stderr);
|
|
2461
|
+
}
|
|
2462
|
+
|
|
2463
|
+
/** extensions.worktreeConfig on in `repoDir`, then `core.worktree` set for `w` alone. */
|
|
2464
|
+
function redirectWorktree(repoDir, w, target) {
|
|
2465
|
+
git(repoDir, "config", "extensions.worktreeConfig", "true");
|
|
2466
|
+
git(w, "config", "--worktree", "core.worktree", target);
|
|
2467
|
+
}
|
|
2468
|
+
|
|
2469
|
+
test("b2: a linked worktree whose config.worktree sets core.worktree elsewhere is refused, never clean (#2040)", (t) => {
|
|
2470
|
+
// The ticket's b2: `.git` untouched, `$gd` is $wt's own admin dir with its
|
|
2471
|
+
// back-pointer intact, one `git config --worktree` away from a healthy tree.
|
|
2472
|
+
const c = nestedWorktreePair(t);
|
|
2473
|
+
redirectWorktree(c.parent, c.w, c.sibling);
|
|
2474
|
+
refusedAsRedirected(c, git(c.sibling, "rev-parse", "--show-toplevel"));
|
|
2475
|
+
});
|
|
2476
|
+
|
|
2477
|
+
test("c2: a .git naming a foreign git dir NOT called .git, whose core.worktree is elsewhere, is refused (#2040)", (t) => {
|
|
2478
|
+
// The ticket's c2. `$gd == common` and the `${gd%/.git}` strip is a no-op,
|
|
2479
|
+
// so the owner check admits it as the ceiling's `--separate-git-dir` shape.
|
|
2480
|
+
const c = nestedWorktree(t);
|
|
2481
|
+
const root = dirname(c.parent);
|
|
2482
|
+
const foreignGd = join(root, "foreign-gd");
|
|
2483
|
+
const foreign = join(root, "foreign");
|
|
2484
|
+
execFileSync("git", ["clone", "-q", "--separate-git-dir", foreignGd, git(c.parent, "remote", "get-url", "origin"), foreign], { env: ENV });
|
|
2485
|
+
git(foreign, "checkout", "-q", c.branch);
|
|
2486
|
+
git(foreign, "config", "core.worktree", foreign);
|
|
2487
|
+
writeFileSync(join(c.w, ".git"), `gitdir: ${foreignGd}\n`);
|
|
2488
|
+
assert.equal(git(c.w, "rev-parse", "--path-format=absolute", "--git-dir"), git(c.w, "rev-parse", "--path-format=absolute", "--git-common-dir"),
|
|
2489
|
+
"fixture: the foreign git dir must be the common-dir shape the owner check admits");
|
|
2490
|
+
refusedAsRedirected(c, git(foreign, "rev-parse", "--show-toplevel"));
|
|
2491
|
+
});
|
|
2492
|
+
|
|
2493
|
+
test("a MAIN checkout whose core.worktree names another checkout is refused, never clean (#2040)", (t) => {
|
|
2494
|
+
// Beyond the ticket's table, and the cheapest of all: one `git config
|
|
2495
|
+
// core.worktree` on an ordinary clone. `$gd == common`, named `.git`, owner
|
|
2496
|
+
// `$wt` — the owner check has nothing to object to.
|
|
2497
|
+
const c = repo(t);
|
|
2498
|
+
const elsewhere = `${c.w}-elsewhere`;
|
|
2499
|
+
git(c.w, "worktree", "add", "-q", "--detach", elsewhere);
|
|
2500
|
+
git(c.w, "config", "core.worktree", elsewhere);
|
|
2501
|
+
writeFileSync(join(c.w, "precious.txt"), "work that exists nowhere else\n");
|
|
2502
|
+
refusedAsRedirected(c, git(elsewhere, "rev-parse", "--show-toplevel"));
|
|
2503
|
+
});
|
|
2504
|
+
|
|
2505
|
+
test("a core.worktree INSIDE $wt is refused: every file outside it goes unread (#2040)", (t) => {
|
|
2506
|
+
const c = nestedWorktree(t);
|
|
2507
|
+
const sub = join(c.w, "sub");
|
|
2508
|
+
mkdirSync(sub);
|
|
2509
|
+
for (const f of [".gitignore", "f.txt", "g.txt"]) copyFileSync(join(c.w, f), join(sub, f));
|
|
2510
|
+
redirectWorktree(c.parent, c.w, sub);
|
|
2511
|
+
refusedAsRedirected(c, git(sub, "rev-parse", "--show-toplevel"));
|
|
2512
|
+
});
|
|
2513
|
+
|
|
2514
|
+
test("a core.worktree naming a MISSING directory is unanswerable (2), never the dirty verdict (1) (#2040)", (t) => {
|
|
2515
|
+
// git still answers `--show-toplevel` at rc 0 here (measured), so a guard
|
|
2516
|
+
// that canonicalised that answer through `cd` would fail under `set -e` and
|
|
2517
|
+
// exit 1 — the dirty verdict — over a tree nothing looked at.
|
|
2518
|
+
const c = nestedWorktree(t);
|
|
2519
|
+
const missing = join(dirname(c.parent), "never-created");
|
|
2520
|
+
redirectWorktree(c.parent, c.w, missing);
|
|
2521
|
+
const r = audit(c);
|
|
2522
|
+
assert.equal(r.status, 2, `got ${r.status} ${r.stdout} ${r.stderr}`);
|
|
2523
|
+
assert.equal(r.stdout, "");
|
|
2524
|
+
assert.match(r.stderr, /answers for the working tree at .*never-created, not /);
|
|
2525
|
+
});
|
|
2526
|
+
|
|
2527
|
+
// Why the working-tree compare was ADDED to the owner check rather than
|
|
2528
|
+
// replacing it: a `.git` naming a foreign git dir literally called `.git`
|
|
2529
|
+
// whose `core.worktree` names $wt BACK passes the new compare — git answers
|
|
2530
|
+
// for $wt — while every read goes against the foreign repo's HEAD and index.
|
|
2531
|
+
// A foreign index that already carries `precious.txt`, byte for byte, reads
|
|
2532
|
+
// clean over work that exists nowhere in THIS repo. Only the `${gd%/.git}`
|
|
2533
|
+
// owner compare refuses it; drop that and this reds (measured, #2040).
|
|
2534
|
+
test("a foreign */.git whose core.worktree names $wt back is still refused by the owner compare (#2040)", (t) => {
|
|
2535
|
+
const c = nestedWorktree(t);
|
|
2536
|
+
const foreign = join(dirname(c.parent), "foreign");
|
|
2537
|
+
execFileSync("git", ["clone", "-q", git(c.parent, "remote", "get-url", "origin"), foreign], { env: ENV });
|
|
2538
|
+
git(foreign, "checkout", "-q", c.branch);
|
|
2539
|
+
copyFileSync(join(c.w, "precious.txt"), join(foreign, "precious.txt"));
|
|
2540
|
+
git(foreign, "add", "precious.txt");
|
|
2541
|
+
git(foreign, "commit", "-q", "-m", "precious, committed in a repo that is not $wt's");
|
|
2542
|
+
git(foreign, "config", "core.worktree", c.w);
|
|
2543
|
+
writeFileSync(join(c.w, ".git"), `gitdir: ${join(foreign, ".git")}\n`);
|
|
2544
|
+
assert.equal(git(c.w, "rev-parse", "--show-toplevel"), `${git(c.parent, "rev-parse", "--show-toplevel")}/.worktrees/9-x`,
|
|
2545
|
+
"fixture: git must answer for $wt itself, or the working-tree compare refuses this and it pins nothing about the owner check");
|
|
2546
|
+
assert.equal(git(c.w, "status", "--porcelain", "-uall"), "", "fixture: the foreign index must read clean over the work");
|
|
2547
|
+
|
|
2548
|
+
const r = audit(c);
|
|
2549
|
+
assert.equal(r.status, 2, `got ${r.status} ${r.stdout}`);
|
|
2550
|
+
assert.equal(r.stdout, "");
|
|
2551
|
+
assert.match(r.stderr, /whose worktree is .*foreign, not /);
|
|
2552
|
+
});
|
|
2553
|
+
|
|
2554
|
+
// The `$(...)` that captures `wt_real` and `top` for the working-tree compare
|
|
2555
|
+
// strips ALL trailing newlines, not one: a `core.worktree` target whose REAL
|
|
2556
|
+
// directory name itself ends in a newline byte would lose it on both sides of
|
|
2557
|
+
// the compare, so a redirect to a genuinely different directory could string-
|
|
2558
|
+
// match into $wt. Constructed rather than found: `git worktree add`/`git
|
|
2559
|
+
// config core.worktree` both accept the byte as an ordinary path character.
|
|
2560
|
+
//
|
|
2561
|
+
// `refusedAsRedirected` is not reused here: it asserts `--show-toplevel`
|
|
2562
|
+
// against a JS-trimmed `target`, and `.trim()` removes the one real byte
|
|
2563
|
+
// this case is about, which would make the fixture assertion itself fail
|
|
2564
|
+
// against a correctly-fixed script. The behaviour asserted below is the
|
|
2565
|
+
// same as every other redirect case: refused, no payload, before the audit.
|
|
2566
|
+
test("a core.worktree target whose real name ends in a newline is still refused, never string-matched into $wt (#2040)", (t) => {
|
|
2567
|
+
const c = nestedWorktree(t);
|
|
2568
|
+
const twin = join(c.parent, ".worktrees", "9-x\n");
|
|
2569
|
+
execFileSync("git", ["clone", "-q", git(c.parent, "remote", "get-url", "origin"), twin], { env: ENV });
|
|
2570
|
+
git(twin, "checkout", "-q", c.branch);
|
|
2571
|
+
redirectWorktree(c.parent, c.w, twin);
|
|
2572
|
+
|
|
2573
|
+
const rawTop = execFileSync("git", ["-C", twin, "rev-parse", "--show-toplevel"], { env: ENV, encoding: "utf8" });
|
|
2574
|
+
assert.ok(rawTop.endsWith("9-x\n\n"), "fixture: git must resolve the twin to its real, newline-suffixed path, or this pins nothing new");
|
|
2575
|
+
assert.equal(
|
|
2576
|
+
execFileSync("git", ["-C", c.w, "status", "--porcelain", "-uall"], { env: ENV, encoding: "utf8" }),
|
|
2577
|
+
"",
|
|
2578
|
+
"fixture: git must read clean through the newline-suffixed target — the false clean this pins",
|
|
2579
|
+
);
|
|
2580
|
+
|
|
2581
|
+
const r = audit(c);
|
|
2582
|
+
assert.equal(r.status, 2, `a newline-suffixed redirect target must not string-compare equal to $wt: got ${r.status} ${r.stdout}`);
|
|
2583
|
+
assert.equal(r.stdout, "", "an unanswerable audit must not emit a payload");
|
|
2584
|
+
assert.match(r.stderr, /git answers for the working tree at/);
|
|
2585
|
+
assert.match(r.stderr, /cannot tell a clean worktree from a dirty one/);
|
|
2586
|
+
});
|
|
2587
|
+
|
|
2588
|
+
// The ACCEPT side, which a refusal-only suite cannot see: the compare keys on
|
|
2589
|
+
// the working tree git answers for, so a `core.worktree` that leaves it at $wt
|
|
2590
|
+
// must still be audited. Both measured admitted with the fix in place.
|
|
2591
|
+
test("a core.worktree naming $wt itself, through a symlinked spelling, is audited, not refused (#2040)", (t) => {
|
|
2592
|
+
const c = nestedWorktree(t);
|
|
2593
|
+
const link = join(dirname(c.parent), "parent-link");
|
|
2594
|
+
symlinkSync(c.parent, link);
|
|
2595
|
+
redirectWorktree(c.parent, c.w, join(link, ".worktrees", "9-x"));
|
|
2596
|
+
const r = audit(c);
|
|
2597
|
+
assert.equal(r.status, 1, `a core.worktree naming the worktree itself must not refuse: got ${r.status} ${r.stderr}`);
|
|
2598
|
+
assert.equal(r.json.clean, false);
|
|
2599
|
+
});
|
|
2600
|
+
|
|
2601
|
+
test("a core.worktree git IGNORES — the common config, seen from a linked worktree — is audited, not refused (#2040)", (t) => {
|
|
2602
|
+
// git does not apply the shared config's core.worktree to a linked worktree
|
|
2603
|
+
// (measured: `--show-toplevel` and `status` both still answer for $wt), so
|
|
2604
|
+
// refusing it would be refusing a setting that changes nothing.
|
|
2605
|
+
const c = nestedWorktree(t);
|
|
2606
|
+
git(c.parent, "config", "core.worktree", join(dirname(c.parent), "elsewhere"));
|
|
2607
|
+
const r = audit(c);
|
|
2608
|
+
assert.equal(r.status, 1, `got ${r.status} ${r.stderr}`);
|
|
2609
|
+
assert.equal(r.json.clean, false);
|
|
2610
|
+
});
|
|
2611
|
+
|
|
2612
|
+
// ---------------------------------------------------------------------------
|
|
2613
|
+
// Dirty means dirty. Every fixture above leaves an untracked file, which is the
|
|
2614
|
+
// one form `status --porcelain` reports with no index involved at all.
|
|
2615
|
+
// ---------------------------------------------------------------------------
|
|
2616
|
+
|
|
2617
|
+
// A payload that could not be written is not an answer, but the script's exit
|
|
2618
|
+
// code is spent before the write: without a guard the failing `printf` exits 1
|
|
2619
|
+
// under `set -e`, and 1 is "REFUSED, worktree dirty" — a clean worktree
|
|
2620
|
+
// reported as dirty, with no payload to contradict it. Same shape as the guard
|
|
2621
|
+
// inflight.sh carries on its own final printf.
|
|
2622
|
+
test("a payload that cannot be written is unanswerable (2), never a refusal (1)", (t) => {
|
|
2623
|
+
const c = repo(t);
|
|
2624
|
+
|
|
2625
|
+
// node cannot hand a child a closed fd 1, so sh closes it after the fork.
|
|
2626
|
+
const r = spawnSync("sh", ["-c", '"$0" "$@" >&-', SCRIPT, c.w, c.branch], {
|
|
2627
|
+
cwd: c.w,
|
|
2628
|
+
env: ENV,
|
|
2629
|
+
encoding: "utf8",
|
|
2630
|
+
});
|
|
2631
|
+
assert.equal(r.status, 2, `got ${r.status}; 1 would claim the worktree is dirty`);
|
|
2632
|
+
// WHICH 2 it is, and the only signal a caller whose stdout is gone can still
|
|
2633
|
+
// read. The design spec's row names this cause in its closed list (#1472),
|
|
2634
|
+
// and a refusal that reached here silently would be indistinguishable from
|
|
2635
|
+
// every other 2 in that list.
|
|
2636
|
+
assert.match(r.stderr, /^no-undo-audit: could not write the audit for /m,
|
|
2637
|
+
`the write failure must name itself on stderr; got ${JSON.stringify(r.stderr)}`);
|
|
2638
|
+
});
|
|
2639
|
+
|
|
2640
|
+
// --- #1514: the same question asked of the OTHER stream, and the direction
|
|
2641
|
+
// that is strictly worse. A caller whose stdout is gone gets no payload and
|
|
2642
|
+
// an honest 2, above. A caller whose stderr was gone got a complete payload
|
|
2643
|
+
// saying `"clean":true` AND an exit 1 stacked on top of it — REFUSED, from
|
|
2644
|
+
// the one tool whose job is to say whether a rebase would eat a commit, about
|
|
2645
|
+
// a tree that had already been measured safe on the same run.
|
|
2646
|
+
//
|
|
2647
|
+
// The cause was the whole class, not one site: `set -e` reads a failed
|
|
2648
|
+
// `echo`'s status, 1 out of this script is the dirty-worktree refusal, and
|
|
2649
|
+
// every bare `>&2` diagnostic outside `render()` was an unguarded command.
|
|
2650
|
+
// The run died on the FIRST of them — the status-command header, before git
|
|
2651
|
+
// had been asked anything — so the verdict was fabricated by a stream that
|
|
2652
|
+
// answers no part of the question. Measured on this exact fixture before the
|
|
2653
|
+
// fix: exit 1.
|
|
2654
|
+
test("a diagnostic stream that cannot be written is never a refusal (#1514)", (t) => {
|
|
2655
|
+
const c = repo(t);
|
|
2656
|
+
|
|
2657
|
+
// node cannot hand a child a closed fd 2 either, so sh closes it after the
|
|
2658
|
+
// fork — the same shape the closed-stdout test above uses on fd 1.
|
|
2659
|
+
const r = spawnSync("sh", ["-c", '"$0" "$@" 2>&-', SCRIPT, c.w, c.branch], {
|
|
2660
|
+
cwd: c.w,
|
|
2661
|
+
env: ENV,
|
|
2662
|
+
encoding: "utf8",
|
|
2663
|
+
});
|
|
2664
|
+
|
|
2665
|
+
assert.equal(r.status, 0, `got ${r.status}; 1 would claim a clean worktree is dirty on the strength of a stream that measured nothing`);
|
|
2666
|
+
// Losing the diagnostics is not losing the answer: the payload is the
|
|
2667
|
+
// audit's actual output and it is unaffected by fd 2. Without this the
|
|
2668
|
+
// status assertion above would also pass on a script that exited 0 early.
|
|
2669
|
+
assert.equal(JSON.parse(r.stdout).clean, true,
|
|
2670
|
+
`the verdict must still be delivered in full; got ${JSON.stringify(r.stdout)}`);
|
|
2671
|
+
});
|
|
2672
|
+
|
|
2673
|
+
// The other half of the pair, and the one that keeps the fix from being a
|
|
2674
|
+
// blanket `exit 0`: `|| :` must swallow the WRITE, never the verdict. This
|
|
2675
|
+
// tree really is dirty, the answer really is 1, and it has to survive having
|
|
2676
|
+
// nowhere to print the refusal — the two-line REFUSED message is itself two
|
|
2677
|
+
// of the newly guarded sites, and they sit after `rc=1` is already set.
|
|
2678
|
+
test("a genuinely refused worktree still refuses (1) with stderr closed (#1514)", (t) => {
|
|
2679
|
+
const c = repo(t);
|
|
2680
|
+
writeFileSync(join(c.w, "uncommitted.txt"), "work that exists nowhere else\n");
|
|
2681
|
+
|
|
2682
|
+
const r = spawnSync("sh", ["-c", '"$0" "$@" 2>&-', SCRIPT, c.w, c.branch], {
|
|
2683
|
+
cwd: c.w,
|
|
2684
|
+
env: ENV,
|
|
2685
|
+
encoding: "utf8",
|
|
2686
|
+
});
|
|
2687
|
+
|
|
2688
|
+
assert.equal(r.status, 1, `uncommitted work must still refuse with no stderr to say so on; got ${r.status}`);
|
|
2689
|
+
assert.equal(JSON.parse(r.stdout).clean, false,
|
|
2690
|
+
`and the refusal must still carry the payload that justifies it; got ${JSON.stringify(r.stdout)}`);
|
|
2691
|
+
});
|
|
2692
|
+
|
|
2693
|
+
// `die` is the third outcome and was the worst of the three: its whole body
|
|
2694
|
+
// is a write to fd 2 followed by `exit 2`, and `set -e` took the write's
|
|
2695
|
+
// status before the `exit` was ever reached. So every unanswerable question —
|
|
2696
|
+
// 30-odd `|| die` sites — came back 1 with stderr closed, telling an operator
|
|
2697
|
+
// to commit a worktree the run had never managed to look at. Exit 2's own
|
|
2698
|
+
// rule still holds here: no payload, because a payload is an answer.
|
|
2699
|
+
test("an unanswerable question with stderr closed exits 2, never 1 (#1514)", (t) => {
|
|
2700
|
+
const c = repo(t);
|
|
2701
|
+
const notARepo = `${c.w}-notarepo`;
|
|
2702
|
+
mkdirSync(notARepo);
|
|
2703
|
+
|
|
2704
|
+
const r = spawnSync("sh", ["-c", '"$0" "$@" 2>&-', SCRIPT, notARepo, c.branch], {
|
|
2705
|
+
cwd: c.w,
|
|
2706
|
+
env: ENV,
|
|
2707
|
+
encoding: "utf8",
|
|
2708
|
+
});
|
|
2709
|
+
|
|
2710
|
+
assert.equal(r.status, 2, `a non-worktree is unanswerable however unwritable stderr is; got ${r.status}`);
|
|
2711
|
+
assert.equal(r.stdout.trim(), "", "exit 2 emits no payload — a payload is an answer");
|
|
2712
|
+
});
|
|
2713
|
+
|
|
2714
|
+
// --- #1571: the same three outcomes, but the descriptor is not closed before
|
|
2715
|
+
// launch — a reader takes the first line and then genuinely leaves mid run,
|
|
2716
|
+
// a real SIGPIPE rather than a write against a dead fd. Every write above
|
|
2717
|
+
// this ticket guarded (`die`, `render`, `emit`) is a bare shell builtin, so
|
|
2718
|
+
// it runs in the script's own process rather than a forked child: unguarded,
|
|
2719
|
+
// the signal's default disposition killed that process on the spot, before
|
|
2720
|
+
// `|| :` was ever consulted, and the script exited 141 — outside its own
|
|
2721
|
+
// 0/1/2 contract and unreadable to a caller branching on which of the three
|
|
2722
|
+
// it got. Measured before the fix, the case asserting a clean tree exits 0,
|
|
2723
|
+
// never 141, itself returned 141.
|
|
2724
|
+
//
|
|
2725
|
+
// All three outcomes below are the closed-fd trio's own fixtures rerun
|
|
2726
|
+
// through `earlyClosingStderrReaderStatus` instead of `2>&-` — the write
|
|
2727
|
+
// that fails is now a signal, but which outcome the run reaches and what its
|
|
2728
|
+
// payload says must be unchanged either way.
|
|
2729
|
+
test("with a genuine SIGPIPE on stderr, a clean tree still exits 0, never 141 (#1571)", (t) => {
|
|
2730
|
+
const c = repo(t);
|
|
2731
|
+
|
|
2732
|
+
assert.equal(earlyClosingStderrReaderStatus(c), 0,
|
|
2733
|
+
"a reader leaving mid run must not turn a safe tree into a signal death");
|
|
2734
|
+
});
|
|
2735
|
+
|
|
2736
|
+
test("with a genuine SIGPIPE on stderr, a genuinely refused tree still exits 1, never 141 (#1571)", (t) => {
|
|
2737
|
+
const c = repo(t);
|
|
2738
|
+
writeFileSync(join(c.w, "uncommitted.txt"), "work that exists nowhere else\n");
|
|
2739
|
+
|
|
2740
|
+
assert.equal(earlyClosingStderrReaderStatus(c), 1,
|
|
2741
|
+
"uncommitted work must still refuse — not 0 (the signal never reached), not 141 (it reached and killed the run)");
|
|
2742
|
+
});
|
|
2743
|
+
|
|
2744
|
+
test("with a genuine SIGPIPE on stderr, an unanswerable question still exits 2, never 141 (#1571)", (t) => {
|
|
2745
|
+
const c = repo(t);
|
|
2746
|
+
const notARepo = `${c.w}-notarepo`;
|
|
2747
|
+
mkdirSync(notARepo);
|
|
2748
|
+
|
|
2749
|
+
assert.equal(earlyClosingStderrReaderStatus({ w: notARepo, branch: c.branch }, c.w), 2,
|
|
2750
|
+
"die's own write is a bare printf too — the same signal that killed the process before this fix must not reach it either");
|
|
2751
|
+
});
|
|
2752
|
+
|
|
2753
|
+
test("a modified tracked file refuses, like an untracked one", (t) => {
|
|
2754
|
+
const c = repo(t);
|
|
2755
|
+
writeFileSync(join(c.w, "f.txt"), "edited in place, committed nowhere\n");
|
|
2756
|
+
// `git` trims, and porcelain spends its first column on the index — so the
|
|
2757
|
+
// status this fixture needs is ` M`, read here with that column already gone.
|
|
2758
|
+
assert.equal(git(c.w, "status", "--porcelain"), "M f.txt", "fixture must modify a tracked file, unstaged");
|
|
2759
|
+
|
|
2760
|
+
const r = audit(c);
|
|
2761
|
+
assert.equal(r.status, 1, "an edit to a tracked file exists nowhere else either");
|
|
2762
|
+
assert.equal(r.json.clean, false);
|
|
2763
|
+
});
|
|
2764
|
+
|
|
2765
|
+
test("a staged change refuses", (t) => {
|
|
2766
|
+
const c = repo(t);
|
|
2767
|
+
writeFileSync(join(c.w, "staged.txt"), "staged, never committed\n");
|
|
2768
|
+
git(c.w, "add", "staged.txt");
|
|
2769
|
+
assert.equal(git(c.w, "status", "--porcelain"), "A staged.txt", "fixture must stage without committing");
|
|
2770
|
+
|
|
2771
|
+
const r = audit(c);
|
|
2772
|
+
assert.equal(r.status, 1, "the index is not a commit — a rebase does not carry it");
|
|
2773
|
+
assert.equal(r.json.clean, false);
|
|
2774
|
+
});
|
|
2775
|
+
|
|
2776
|
+
/** The design spec's script-surface row for this script, as one line. */
|
|
2777
|
+
function specRow() {
|
|
2778
|
+
const spec = readFileSync(
|
|
2779
|
+
fileURLToPath(new URL("../../docs/specs/2026-07-23-fleet-plugin-design.md", import.meta.url)),
|
|
2780
|
+
"utf8",
|
|
2781
|
+
);
|
|
2782
|
+
const row = spec.split("\n").find((l) => l.startsWith("| `no-undo-audit.sh` |"));
|
|
2783
|
+
assert.ok(row, "the script-surface table must still carry a no-undo-audit.sh row");
|
|
2784
|
+
return row;
|
|
2785
|
+
}
|
|
2786
|
+
|
|
2787
|
+
// The design spec's script-surface table names this script's payload field by
|
|
2788
|
+
// field, and #146 added four fields to it. The table went stale in the same
|
|
2789
|
+
// commit that added them — the fix for that is one edited row, and this is the
|
|
2790
|
+
// part that keeps the next one from going stale silently. Derived from a real
|
|
2791
|
+
// run, never from a hand-written key list: a list typed here drifts from the
|
|
2792
|
+
// script exactly the way the table did.
|
|
2793
|
+
test("the design spec's script-surface row names every field the payload actually emits", (t) => {
|
|
2794
|
+
const c = repo(t);
|
|
2795
|
+
const r = audit(c);
|
|
2796
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
2797
|
+
|
|
2798
|
+
const row = specRow();
|
|
2799
|
+
|
|
2800
|
+
// The Out cell alone, since the rest of the row legitimately names things that
|
|
2801
|
+
// are not keys: scanning the whole row let the In cell's `<worktree>` satisfy
|
|
2802
|
+
// `worktree` and the `Non-zero when` prose's "the stash count is reported"
|
|
2803
|
+
// satisfy `stash`, so either could be dropped from the type signature with
|
|
2804
|
+
// this test green (measured, both directions).
|
|
2805
|
+
const out = row.split("|")[3];
|
|
2806
|
+
|
|
2807
|
+
// Word-boundary match, so `worktree` cannot be satisfied by `worktreeRewritten`
|
|
2808
|
+
// sitting elsewhere in the cell — the exact substring trap that would let the
|
|
2809
|
+
// four new flags be dropped again while this test stayed green.
|
|
2810
|
+
const missing = Object.keys(r.json).filter((k) => !new RegExp(`\\b${k}\\b`).test(out));
|
|
2811
|
+
assert.deepEqual(missing, [], `the spec row omits fields the script emits: ${missing.join(", ")}`);
|
|
2812
|
+
});
|
|
2813
|
+
|
|
2814
|
+
// Both docs describe this script's exit-2 causes, and its linkage clause covers
|
|
2815
|
+
// THREE failures git reports differently. A parenthetical naming only the
|
|
2816
|
+
// walk-up one stood, byte-identical, in both files, and was false for the
|
|
2817
|
+
// second (#420); the third arrived with #2040. Measured, git 2.50.1: delete the
|
|
2818
|
+
// worktree's `.git` and `rev-parse --show-prefix` returns `.worktrees/<wt>/`
|
|
2819
|
+
// while `--show-toplevel` is the enclosing repo; rewrite that file to
|
|
2820
|
+
// `gitdir: …/worktrees/<sibling>` and `--show-prefix` is empty at rc 0 with
|
|
2821
|
+
// `--show-toplevel` the worktree itself, while HEAD, the branch and `status`
|
|
2822
|
+
// all answer from the sibling; set `core.worktree` elsewhere and
|
|
2823
|
+
// `--show-prefix` is still empty at rc 0 while `--show-toplevel` and `status`
|
|
2824
|
+
// answer for the other directory, against $wt's own HEAD and index.
|
|
2825
|
+
//
|
|
2826
|
+
// ONE exact-span assertion rather than a match per mechanism, because the claim
|
|
2827
|
+
// lives in the JOIN: separate matches for `enclosing repo` and for `another
|
|
2828
|
+
// worktree's HEAD and index` are both satisfied by a rewrite that re-merges the
|
|
2829
|
+
// two into a single wrong account ("git walks up and reports the enclosing
|
|
2830
|
+
// repo, or reads another worktree's HEAD and index"), which is the defect this
|
|
2831
|
+
// pin exists to keep out. Safe to pin as an exact span because both carriers
|
|
2832
|
+
// are one unwrapped line — a table row and a prose paragraph — so there is no
|
|
2833
|
+
// reflow to survive.
|
|
2834
|
+
//
|
|
2835
|
+
// SCOPE of the rc-0 clause: measured for a worktree nested inside its repo —
|
|
2836
|
+
// the only shape this fleet builds, since `claim-ticket.sh` derives the
|
|
2837
|
+
// worktree path under `.worktrees/` relative to the repo root. Outside any
|
|
2838
|
+
// repo the clause's own subject does not exist: with `.git` deleted git has
|
|
2839
|
+
// nothing to walk up to and `rev-parse` exits 128, though the script still
|
|
2840
|
+
// refuses at exit 2, via its not-a-git-worktree die rather than the one for
|
|
2841
|
+
// git answering above the worktree (measured, git 2.50.1). Re-open if the
|
|
2842
|
+
// fleet ever places a worktree outside the repo — the clause then needs
|
|
2843
|
+
// scoping, and the string is byte-identical across the docs this test reads
|
|
2844
|
+
// and this constant, so every carrier moves together.
|
|
2845
|
+
const LINKAGE_PARENTHETICAL =
|
|
2846
|
+
"its linkage is broken, and git still answers at rc 0 — for the enclosing repo when the `.git` is gone, from another worktree's HEAD and index when it names that worktree's admin dir, against another directory's files when `core.worktree` moves the working tree there";
|
|
2847
|
+
|
|
2848
|
+
test("both docs' exit-2 prose keeps the three linkage failures distinct", () => {
|
|
2849
|
+
for (const [rel, anchor] of [
|
|
2850
|
+
["../commands/run-merge-bot.md", (l) => l.includes("Exit **2**")],
|
|
2851
|
+
["../../docs/specs/2026-07-23-fleet-plugin-design.md", (l) => l.startsWith("| `no-undo-audit.sh` |")],
|
|
2852
|
+
]) {
|
|
2853
|
+
const line = readFileSync(fileURLToPath(new URL(rel, import.meta.url)), "utf8")
|
|
2854
|
+
.split("\n").find(anchor);
|
|
2855
|
+
assert.ok(line, `${rel} must still describe this script's exit-2 causes`);
|
|
2856
|
+
assert.ok(line.includes(LINKAGE_PARENTHETICAL),
|
|
2857
|
+
`${rel} no longer names all three linkage failures as distinct: a deleted \`.git\` is the one git walks up from, a \`.git\` naming another worktree's admin dir is the one git answers for this worktree while reading the other one's HEAD and index, and a \`core.worktree\` redirect is the one git answers with this worktree's HEAD and index against another directory's files. Merging them, or generalising until it names none, all land here.`);
|
|
2858
|
+
}
|
|
2859
|
+
});
|
|
2860
|
+
|
|
2861
|
+
// --- #1472: the exit-2 cause census.
|
|
2862
|
+
//
|
|
2863
|
+
// The two pins above read this row for its FIELDS and for one clause of its
|
|
2864
|
+
// exit-2 prose. Neither asks the closed-list question — does the row name
|
|
2865
|
+
// every cause this script can refuse for, and none it cannot reach? #1108
|
|
2866
|
+
// answered that for worktree-audit.sh and reap.sh after two PRs in one fleet run
|
|
2867
|
+
// each left a row asserting an enumeration its script had outgrown with the
|
|
2868
|
+
// suite green (#1104/#525, #1105/#482). This block answers it here, both
|
|
2869
|
+
// directions:
|
|
2870
|
+
//
|
|
2871
|
+
// TOO NARROW — the script grows a refusal the row does not carry. `CAUSES`
|
|
2872
|
+
// binds every `die` site to the phrase that represents it, and the census
|
|
2873
|
+
// test asserts that binding is EXACTLY the set of sites the script has. Add
|
|
2874
|
+
// a `die` and it reds on an unbound site; delete one and it reds on a
|
|
2875
|
+
// binding whose cause the script can no longer produce. The set is derived
|
|
2876
|
+
// from the script, so the binding cannot rot silently the way the row did —
|
|
2877
|
+
// a reworded message reds too, which is the point: the row is what then has
|
|
2878
|
+
// to be revisited.
|
|
2879
|
+
//
|
|
2880
|
+
// TOO BROAD — the row grows a cause no `die` produces. The census cannot see
|
|
2881
|
+
// that; a phrase added to the cell binds to nothing and no assert notices.
|
|
2882
|
+
// `EXIT2_ENUMERATION` is the pin that does: the whole closed list, byte for
|
|
2883
|
+
// byte, in the `UNKNOWN_LINE`/`ORPHAN_LINE` verbatim-constant discipline
|
|
2884
|
+
// this file already uses on the stash lines above.
|
|
2885
|
+
//
|
|
2886
|
+
// The span pin's cost is deliberate, and it was #1108's ruling: it reds on
|
|
2887
|
+
// EVERY edit to the enumeration, a legitimate rewording included. A structural
|
|
2888
|
+
// assertion loose enough to survive rewording cannot red on a rewrite that
|
|
2889
|
+
// quietly drops a real cause, which is the defect that was measured twice.
|
|
2890
|
+
//
|
|
2891
|
+
// WHAT THIS SCRIPT'S SHAPE CHANGES, and it is not cosmetic. Its two siblings
|
|
2892
|
+
// answer with a 2 or not at all, so their cells OPEN on `exit 2 only` and a
|
|
2893
|
+
// prefix slice is its own anchor. Exit 2 is one of THREE outcomes here:
|
|
2894
|
+
// the cell opens on the exit-1 refusal — which carries a closed list of its
|
|
2895
|
+
// own, the `stash: null` shapes — and closes on the `worktree`/`branch`
|
|
2896
|
+
// `null`-field rule, which fires at the verdict's own exit code — 0 or 1 —
|
|
2897
|
+
// never exclusively at exit 0.
|
|
2898
|
+
// Two consequences, and a copied exit-2-only census gets both wrong.
|
|
2899
|
+
//
|
|
2900
|
+
// The anchor is asserted, never assumed: `exit 2` occurs exactly once in the
|
|
2901
|
+
// cell, and the list is read from there for exactly its own length. A prefix
|
|
2902
|
+
// slice would red here on the exit-1 clause it never meant to read, and an
|
|
2903
|
+
// `includes` would let a cause be smuggled in ahead of the list.
|
|
2904
|
+
//
|
|
2905
|
+
// The premise is pinned, not inherited: `exitStatements` asserts that `die`'s
|
|
2906
|
+
// `exit 2` and the verdict's `exit "$rc"` are the only exits the script
|
|
2907
|
+
// spells, and that `$rc` is only ever 0 or 1. A sibling suite gets "every
|
|
2908
|
+
// nonzero exit is a `die`" for free; on this script an `exit 2` spelled
|
|
2909
|
+
// outside `die` would be an exit-2 cause with no message to bind, invisible
|
|
2910
|
+
// to the census, and a third verdict code would be an outcome no cell in the
|
|
2911
|
+
// row describes.
|
|
2912
|
+
//
|
|
2913
|
+
// Scoped to this script's own suite beside its siblings rather than lifted
|
|
2914
|
+
// into one shared table over every row: `.out-of-scope/cli-guard-test-
|
|
2915
|
+
// consolidation.md` refuses that consolidation for the CLI-guard pins, and its
|
|
2916
|
+
// reason holds unchanged here — a file no single script's suite runs recreates
|
|
2917
|
+
// the blind spot these pins exist to close.
|
|
2918
|
+
|
|
2919
|
+
/**
|
|
2920
|
+
* The `Non-zero when` cell of this script's row, and no more of the row.
|
|
2921
|
+
*
|
|
2922
|
+
* The row is split on `|` and the fifth cell taken; a stray `|` inside any
|
|
2923
|
+
* earlier cell's prose would silently narrow what gets scanned instead of
|
|
2924
|
+
* erroring, so the split's own arity is asserted first — measured at 6
|
|
2925
|
+
* (leading empty, script name, args, JSON shape, `Non-zero when`, trailing
|
|
2926
|
+
* empty).
|
|
2927
|
+
*/
|
|
2928
|
+
const nonZeroCell = () => {
|
|
2929
|
+
const cells = specRow().split("|");
|
|
2930
|
+
assert.equal(
|
|
2931
|
+
cells.length,
|
|
2932
|
+
6,
|
|
2933
|
+
"the no-undo-audit.sh row's cell count changed — an earlier cell may have gained a literal `|`",
|
|
2934
|
+
);
|
|
2935
|
+
return cells[4];
|
|
2936
|
+
};
|
|
2937
|
+
|
|
2938
|
+
/**
|
|
2939
|
+
* The row's exit-2 enumeration, verbatim: from `exit 2` to the end of the
|
|
2940
|
+
* sentence that closes the list. The exit-1 refusal ahead of it and the
|
|
2941
|
+
* `worktree`/`branch` `null`-field rule behind it — which fires at the
|
|
2942
|
+
* verdict's own exit code, 0 or 1, never exclusively at exit 0 — are their
|
|
2943
|
+
* own claims with their own pins, so the span stops where the closed list
|
|
2944
|
+
* does — the slice is the size of the claim.
|
|
2945
|
+
*/
|
|
2946
|
+
const EXIT2_ENUMERATION =
|
|
2947
|
+
"exit 2 the question is unanswerable — bad argument, no such worktree, a worktree git does not answer for (its linkage is broken, and git still answers at rc 0 — for the enclosing repo when the `.git` is gone, from another worktree's HEAD and index when it names that worktree's admin dir, against another directory's files when `core.worktree` moves the working tree there), `BASE_REF` is not spelled `origin/<branch>` or `refs/remotes/<path>` (#1565), `BASE_REF` names the audited branch (#1565), a ref that does not resolve, a probe that could not run — with the stash reflog's path resolution the exception (#570): that one failing reports `unknown` on the payload at the verdict's own exit code rather than withholding the audit, the same rule `worktree`/`branch` follow below — a conflicting path no pathspec can name, `json.sh` missing, unreadable or failed to load, a conflicting-path or at-risk array that could not be escaped (#119) — and no payload is emitted, or the audit itself could not be written (#1472) — that one can fail after the payload has already begun printing, so stdout carries it truncated and unparseable, which that exit code and the named stderr line are what distinguish from a complete answer.";
|
|
2948
|
+
|
|
2949
|
+
/**
|
|
2950
|
+
* Everything in the cell AFTER `EXIT2_ENUMERATION`, verbatim: the
|
|
2951
|
+
* `worktree`/`branch` `null`-field rule the enumeration's own doc comment
|
|
2952
|
+
* says is its own claim with its own pin. Without this, the byte-span
|
|
2953
|
+
* equality below only ever inspects a fixed-length window starting at
|
|
2954
|
+
* `exit 2 ` — a phantom cause appended after the enumeration's closing
|
|
2955
|
+
* sentence, worded without repeating the literal string `exit 2`, would
|
|
2956
|
+
* sit past that window and pass unseen. Pinning the remainder too closes
|
|
2957
|
+
* that gap: together the two constants account for every byte of the cell
|
|
2958
|
+
* from the anchor to the end of the row's cell.
|
|
2959
|
+
*/
|
|
2960
|
+
const EXIT2_TAIL =
|
|
2961
|
+
" `worktree` and `branch` are the exception (#431): they echo argv rather than reporting a finding, so an escaper that cannot render one reports that field and its `*Rewritten` flag as JSON `null` on the payload the audit already earned, at the verdict's own exit code. A `null` there is \"this run could not render the path or branch you passed in\", never \"there is no worktree\" and never a path ";
|
|
2962
|
+
|
|
2963
|
+
/**
|
|
2964
|
+
* The clauses the ROW collapses several refusals into, named because the
|
|
2965
|
+
* bindings below share them and a phrase typed ten times drifts nine ways.
|
|
2966
|
+
*
|
|
2967
|
+
* Sharing is the row's editorial call and not a looseness here: a reader who
|
|
2968
|
+
* meets any of the ten refusals about a worktree git will not answer for
|
|
2969
|
+
* does the same thing about it, and so does one who meets either library
|
|
2970
|
+
* refusal. The phrases are the short load-bearing labels; their exact wording
|
|
2971
|
+
* is `EXIT2_ENUMERATION`'s job.
|
|
2972
|
+
*
|
|
2973
|
+
* The two BASE_REF clauses are named for a different reason than sharing:
|
|
2974
|
+
* each represents exactly one refusal, and `BASE_REF_CLAUSES` below reads
|
|
2975
|
+
* them again to pin that both operator-facing enumerations name them. Spelled
|
|
2976
|
+
* inline they would sit in two places with no constant holding them together,
|
|
2977
|
+
* which is the drift the rest of this block exists to stop.
|
|
2978
|
+
*/
|
|
2979
|
+
const LIBRARY_CLAUSE = "`json.sh` missing, unreadable or failed to load";
|
|
2980
|
+
const LINKAGE_CLAUSE = "a worktree git does not answer for";
|
|
2981
|
+
const REF_CLAUSE = "a ref that does not resolve";
|
|
2982
|
+
const PROBE_CLAUSE = "a probe that could not run";
|
|
2983
|
+
const ESCAPE_CLAUSE = "a conflicting-path or at-risk array that could not be escaped (#119)";
|
|
2984
|
+
const BASE_REF_SHAPE_CLAUSE = "`BASE_REF` is not spelled `origin/<branch>` or `refs/remotes/<path>` (#1565)";
|
|
2985
|
+
const AUDITED_BRANCH_CLAUSE = "`BASE_REF` names the audited branch (#1565)";
|
|
2986
|
+
|
|
2987
|
+
/**
|
|
2988
|
+
* Every clause representing a BASE_REF refusal, as a closed list.
|
|
2989
|
+
*
|
|
2990
|
+
* `plugin/commands/run-merge-bot.md` is the operator-facing doc for this
|
|
2991
|
+
* script's only documented invocation, and its exit-2 enumeration is derived
|
|
2992
|
+
* from nothing: the census below catches a cause the design-spec ROW goes
|
|
2993
|
+
* silent about, and nothing caught one the COMMAND DOC goes silent about —
|
|
2994
|
+
* #1565's own accept-list reached the row and not that doc, with the suite
|
|
2995
|
+
* green. Asserted closed against the script's own messages, so a third
|
|
2996
|
+
* BASE_REF refusal reds here until it is listed and named in both docs.
|
|
2997
|
+
*/
|
|
2998
|
+
const BASE_REF_CLAUSES = [BASE_REF_SHAPE_CLAUSE, AUDITED_BRANCH_CLAUSE];
|
|
2999
|
+
|
|
3000
|
+
/**
|
|
3001
|
+
* Every `die` site in no-undo-audit.sh, bound to the phrase in the row that
|
|
3002
|
+
* represents it.
|
|
3003
|
+
*
|
|
3004
|
+
* The KEY is the message as the script spells it, interpolations and all.
|
|
3005
|
+
* `die` is this script's only exit-2 path — `exitStatements` asserts that of
|
|
3006
|
+
* the script rather than assuming it — so the message set IS the exit-2 cause
|
|
3007
|
+
* set, and keying on it is what makes this derived rather than a third
|
|
3008
|
+
* hand-written copy of the contract sitting beside the script and the row.
|
|
3009
|
+
*/
|
|
3010
|
+
const CAUSES = new Map([
|
|
3011
|
+
["cannot read $json_lib — refusing to act without the JSON escaping helpers", LIBRARY_CLAUSE],
|
|
3012
|
+
["$json_lib failed to load", LIBRARY_CLAUSE],
|
|
3013
|
+
["usage: no-undo-audit.sh <worktree> <branch>", "bad argument"],
|
|
3014
|
+
["worktree $wt does not exist", "no such worktree"],
|
|
3015
|
+
["$wt is not a git worktree", LINKAGE_CLAUSE],
|
|
3016
|
+
["git answers for the repo above $wt, not $wt — cannot tell a clean worktree from a dirty one", LINKAGE_CLAUSE],
|
|
3017
|
+
["git will not name the git dir answering for $wt — cannot verify its linkage", LINKAGE_CLAUSE],
|
|
3018
|
+
["git will not name $wt's common git dir — cannot verify its linkage", LINKAGE_CLAUSE],
|
|
3019
|
+
["$gd/gitdir is missing or unreadable — cannot verify $wt's linkage", LINKAGE_CLAUSE],
|
|
3020
|
+
["$gd/gitdir names a directory that does not resolve — cannot verify $wt's linkage", LINKAGE_CLAUSE],
|
|
3021
|
+
["$wt's .git names another worktree's admin dir — cannot tell a clean worktree from a dirty one", LINKAGE_CLAUSE],
|
|
3022
|
+
["$wt's .git names $gd, whose worktree is $owner, not $wt — cannot tell a clean worktree from a dirty one", LINKAGE_CLAUSE],
|
|
3023
|
+
["git will not name the working tree answering for $wt — cannot verify its linkage", LINKAGE_CLAUSE],
|
|
3024
|
+
["git answers for the working tree at $top, not $wt — cannot tell a clean worktree from a dirty one", LINKAGE_CLAUSE],
|
|
3025
|
+
["BASE_REF must be spelled origin/<branch> or refs/remotes/<path>, got '$base'", BASE_REF_SHAPE_CLAUSE],
|
|
3026
|
+
["BASE_REF must not name the audited branch, got '$base'", AUDITED_BRANCH_CLAUSE],
|
|
3027
|
+
["$base does not resolve as $base_rev", REF_CLAUSE],
|
|
3028
|
+
["$branch_rev does not resolve — run 'git fetch origin' and retry", REF_CLAUSE],
|
|
3029
|
+
["git status failed in $wt — cannot tell a clean worktree from a dirty one", PROBE_CLAUSE],
|
|
3030
|
+
["awk failed counting the stash entries — cannot report the stash count", PROBE_CLAUSE],
|
|
3031
|
+
["cannot create a temporary file", PROBE_CLAUSE],
|
|
3032
|
+
["git merge-tree could not answer (exit $mt_rc) against origin/$branch — cannot determine conflicts", PROBE_CLAUSE],
|
|
3033
|
+
["could not read git merge-tree's output (python3) — cannot determine conflicts", PROBE_CLAUSE],
|
|
3034
|
+
["a conflicting path contains a newline — cannot build a pathspec for it", "a conflicting path no pathspec can name"],
|
|
3035
|
+
["could not escape the conflicting paths for $branch", ESCAPE_CLAUSE],
|
|
3036
|
+
["git merge-base failed for $base ($base_rev) and $branch_rev — cannot tell what a resolution would eat", PROBE_CLAUSE],
|
|
3037
|
+
["listing commits for the conflicting paths failed (git log or xargs) — cannot tell what a resolution would eat", PROBE_CLAUSE],
|
|
3038
|
+
["awk failed deduplicating the at-risk commits — cannot tell what a resolution would eat", PROBE_CLAUSE],
|
|
3039
|
+
["could not escape the at-risk commits for $branch", ESCAPE_CLAUSE],
|
|
3040
|
+
["could not write the audit for $branch", "the audit itself could not be written (#1472)"],
|
|
3041
|
+
]);
|
|
3042
|
+
|
|
3043
|
+
/**
|
|
3044
|
+
* How many sites a message may have. One, except the `mktemp` refusal, which
|
|
3045
|
+
* the script arms before each of the two temporaries it creates and spells
|
|
3046
|
+
* identically at both: it is one cause, and an operator does nothing
|
|
3047
|
+
* different about the second.
|
|
3048
|
+
*
|
|
3049
|
+
* The siblings' census asserts instead that every message is unique. That is
|
|
3050
|
+
* true of their scripts and is a FALSE REFUSAL here — copied over, it reds on
|
|
3051
|
+
* this script for something that is not a defect. Counting keeps what
|
|
3052
|
+
* uniqueness was buying: a new `die` reusing an existing message reds on the
|
|
3053
|
+
* count rather than binding to a cause that is not its own, and a shared
|
|
3054
|
+
* message that stops being shared reds as the missing site it is.
|
|
3055
|
+
*/
|
|
3056
|
+
const SITES_PER_MESSAGE = new Map([["cannot create a temporary file", 2]]);
|
|
3057
|
+
|
|
3058
|
+
/**
|
|
3059
|
+
* The message of every `die` call in the script, read off the script, one
|
|
3060
|
+
* entry per cause.
|
|
3061
|
+
*
|
|
3062
|
+
* Comment lines are dropped: prose quoting a `die "…"` is not a call site, and
|
|
3063
|
+
* minting a cause out of one would red this suite over a comment. Greedy to
|
|
3064
|
+
* the last quote on the line, so a message carrying a nested `"$…"`
|
|
3065
|
+
* substitution arrives whole rather than truncated at its first inner quote.
|
|
3066
|
+
*/
|
|
3067
|
+
function dieSites() {
|
|
3068
|
+
const src = readFileSync(SCRIPT, "utf8");
|
|
3069
|
+
// Re-derived for #1684, which replaced the hand-copy of `emit`'s body
|
|
3070
|
+
// that stood here with a call to `emit` itself: the `|| :` of #1514 and
|
|
3071
|
+
// the `( trap '' PIPE; … )` subshell of #1571 now live in one function
|
|
3072
|
+
// body rather than two. The premise this anchor protects — that `die`
|
|
3073
|
+
// formats one message and reaches `exit 2` — is unchanged by that fix, as
|
|
3074
|
+
// it was by the two before it.
|
|
3075
|
+
assert.match(
|
|
3076
|
+
src,
|
|
3077
|
+
/^die\(\) \{ emit "\$NAME: \$1"; exit 2; \}$/m,
|
|
3078
|
+
"the census derives its cause set from one `die` that exits 2 — that definition has changed, so re-derive before trusting this file",
|
|
3079
|
+
);
|
|
3080
|
+
const lines = src.split("\n");
|
|
3081
|
+
const sites = lines
|
|
3082
|
+
.filter((l) => !/^\s*#/.test(l))
|
|
3083
|
+
.flatMap((l) => [...l.matchAll(/(?:^|[;&|(\s])die "(.*)"/g)].map((m) => m[1]));
|
|
3084
|
+
assert.ok(sites.length > 1, `the scan found ${sites.length} die sites, so its spelling has drifted off the script`);
|
|
3085
|
+
|
|
3086
|
+
// `die` is defined ABOVE `emit` and, since #1684, CALLS it — and a shell
|
|
3087
|
+
// resolves a function name at call time, not at definition time. So every
|
|
3088
|
+
// `die` call site has to sit below `emit()`'s definition: one hoisted
|
|
3089
|
+
// above it runs `emit` as an unknown command, `set -e` takes that 127,
|
|
3090
|
+
// and the script answers an unanswerable question with a status outside
|
|
3091
|
+
// its own 0/1/2 contract — on whichever branch reached it and nowhere
|
|
3092
|
+
// else, which is why no fixture would find it. The ordering is the whole
|
|
3093
|
+
// of what makes the forward reference safe, so it is asserted here rather
|
|
3094
|
+
// than left to the paragraph beside the definition.
|
|
3095
|
+
const emitDef = lines.findIndex((l) => /^emit\(\) \{/.test(l));
|
|
3096
|
+
assert.ok(emitDef >= 0,
|
|
3097
|
+
"`emit()` is no longer defined at the start of a line — `die` calls it, so this ordering check no longer knows what it is measuring");
|
|
3098
|
+
const firstCall = lines.findIndex((l) => !/^\s*#/.test(l) && /(?:^|[;&|(\s])die "/.test(l));
|
|
3099
|
+
assert.ok(firstCall > emitDef,
|
|
3100
|
+
`the first \`die\` call sits at line ${firstCall + 1}, above \`emit\`'s definition at line ${emitDef + 1} — it would exit 127, not the 2 this script's contract promises`);
|
|
3101
|
+
for (const message of new Set(sites)) {
|
|
3102
|
+
assert.equal(
|
|
3103
|
+
sites.filter((m) => m === message).length,
|
|
3104
|
+
SITES_PER_MESSAGE.get(message) ?? 1,
|
|
3105
|
+
`\`die "${message}"\` is spelled at a number of sites this census does not expect: two causes sharing a message cannot be bound apart, and a message that stops being shared is a site that went missing`,
|
|
3106
|
+
);
|
|
3107
|
+
}
|
|
3108
|
+
return new Set(sites);
|
|
3109
|
+
}
|
|
3110
|
+
|
|
3111
|
+
/**
|
|
3112
|
+
* Every `exit` statement the script spells, as it spells it.
|
|
3113
|
+
*
|
|
3114
|
+
* Read from anywhere on the line, not just statement position: `&& exit 2` is
|
|
3115
|
+
* an exit path as much as a bare one, and a scan anchored on line start or
|
|
3116
|
+
* `; ` misses it — measured, and measured green against a stray `exit 2` this
|
|
3117
|
+
* census exists to red on.
|
|
3118
|
+
*
|
|
3119
|
+
* Quoted spans are cut out first, which is what keeps the word inside a
|
|
3120
|
+
* MESSAGE from being minted into an exit path: `die "git merge-tree could not
|
|
3121
|
+
* answer (exit $mt_rc) …"` carries one, and so does the render fold's "the
|
|
3122
|
+
* payload and the exit status stand". Comment lines are dropped for the reason
|
|
3123
|
+
* `dieSites` drops them.
|
|
3124
|
+
*/
|
|
3125
|
+
function exitStatements(src) {
|
|
3126
|
+
return src
|
|
3127
|
+
.split("\n")
|
|
3128
|
+
.filter((l) => !/^\s*#/.test(l))
|
|
3129
|
+
.flatMap((l) => {
|
|
3130
|
+
const quoted = [...l.matchAll(/'[^']*'|"[^"]*"/g)].map((m) => [m.index, m.index + m[0].length]);
|
|
3131
|
+
return [...l.matchAll(/\bexit\b *([^;)&|]*)/g)]
|
|
3132
|
+
.filter((m) => !quoted.some(([from, to]) => m.index >= from && m.index < to))
|
|
3133
|
+
.map((m) => `exit ${m[1].trim()}`.trim());
|
|
3134
|
+
});
|
|
3135
|
+
}
|
|
3136
|
+
|
|
3137
|
+
/**
|
|
3138
|
+
* Every `rc=<value>` assignment to the verdict variable, scanned the same
|
|
3139
|
+
* way `exitStatements` scans `exit`: from anywhere on the line rather than
|
|
3140
|
+
* anchored to its start, with quoted spans cut out first and comment lines
|
|
3141
|
+
* dropped. A line-start/line-end anchor would miss a value assigned inline
|
|
3142
|
+
* (`… && rc=2`) or trailed by a comment (`rc=2 # oops`) — exactly the two
|
|
3143
|
+
* shapes `exitStatements` was already built to survive. `\brc=` — not a bare
|
|
3144
|
+
* `rc=` — is what keeps `sl_rc=`, `sr_rc=`, `mt_rc=` and `stash_reflog_rc=`
|
|
3145
|
+
* out: an underscore is a word character, so there is no boundary between it
|
|
3146
|
+
* and the `r` those names share with the verdict variable.
|
|
3147
|
+
*/
|
|
3148
|
+
function rcAssignments(src) {
|
|
3149
|
+
return src
|
|
3150
|
+
.split("\n")
|
|
3151
|
+
.filter((l) => !/^\s*#/.test(l))
|
|
3152
|
+
.flatMap((l) => {
|
|
3153
|
+
const quoted = [...l.matchAll(/'[^']*'|"[^"]*"/g)].map((m) => [m.index, m.index + m[0].length]);
|
|
3154
|
+
return [...l.matchAll(/\brc=(\S+)/g)]
|
|
3155
|
+
.filter((m) => !quoted.some(([from, to]) => m.index >= from && m.index < to))
|
|
3156
|
+
.map((m) => m[1]);
|
|
3157
|
+
});
|
|
3158
|
+
}
|
|
3159
|
+
|
|
3160
|
+
test("`die` is the only way this script reaches exit 2, and its other exits are the two verdicts (#1472)", () => {
|
|
3161
|
+
const src = readFileSync(SCRIPT, "utf8");
|
|
3162
|
+
|
|
3163
|
+
assert.deepEqual(
|
|
3164
|
+
exitStatements(src).sort(),
|
|
3165
|
+
['exit "$rc"', "exit 2"],
|
|
3166
|
+
"the census reads the exit-2 causes off `die`'s messages, so every exit-2 path must BE a `die`: an `exit 2` spelled anywhere else is a cause with no message to bind, and the row could stay silent about it with this suite green",
|
|
3167
|
+
);
|
|
3168
|
+
|
|
3169
|
+
assert.deepEqual(
|
|
3170
|
+
[...new Set(rcAssignments(src))].sort(),
|
|
3171
|
+
["0", "1"],
|
|
3172
|
+
"`$rc` is the verdict, and the row states exactly two of them — 1 the worktree is dirty, 0 it is clean. A third value assigned here is an outcome no cell in that row describes",
|
|
3173
|
+
);
|
|
3174
|
+
|
|
3175
|
+
// `set -eu` is on, so an unguarded command failing exits with ITS status
|
|
3176
|
+
// rather than with any of the three — a 1 out of a failed `.` or `printf`
|
|
3177
|
+
// reads as the dirty-worktree refusal, which is what the guards above are
|
|
3178
|
+
// for and what their own fixtures pin. This test is about the exits the
|
|
3179
|
+
// script spells itself.
|
|
3180
|
+
});
|
|
3181
|
+
|
|
3182
|
+
test("the design spec's row represents every exit-2 cause this script can reach, and none it cannot (#1472)", () => {
|
|
3183
|
+
// Both directions in one equality: an unbound site is a cause the row may be
|
|
3184
|
+
// silent about, and a binding with no site is a cause the row claims while
|
|
3185
|
+
// the script can no longer produce it.
|
|
3186
|
+
assert.deepEqual([...dieSites()].sort(), [...CAUSES.keys()].sort());
|
|
3187
|
+
|
|
3188
|
+
const cell = nonZeroCell();
|
|
3189
|
+
for (const phrase of new Set(CAUSES.values())) {
|
|
3190
|
+
assert.equal(
|
|
3191
|
+
cell.split(phrase).length - 1,
|
|
3192
|
+
1,
|
|
3193
|
+
`the \`Non-zero when\` cell must carry "${phrase}" exactly once.\ncell: ${cell}`,
|
|
3194
|
+
);
|
|
3195
|
+
}
|
|
3196
|
+
});
|
|
3197
|
+
|
|
3198
|
+
test("the design spec's row states this script's exit-2 causes as a closed list, byte for byte (#1472)", () => {
|
|
3199
|
+
const cell = nonZeroCell();
|
|
3200
|
+
|
|
3201
|
+
// The anchor, asserted rather than assumed: this cell does not open on the
|
|
3202
|
+
// exit-2 list the way its siblings' cells do, so the span below is read from
|
|
3203
|
+
// the one place the cell names exit 2 — and that there is exactly one such
|
|
3204
|
+
// place is half the claim. A second one is a second enumeration, which is
|
|
3205
|
+
// the row carrying its own contradiction.
|
|
3206
|
+
assert.equal(
|
|
3207
|
+
cell.split("exit 2 ").length - 1,
|
|
3208
|
+
1,
|
|
3209
|
+
`the cell must name exit 2 exactly once — it is the anchor the closed list is read from.\ncell: ${cell}`,
|
|
3210
|
+
);
|
|
3211
|
+
|
|
3212
|
+
// A slice, not an `includes`: a cause smuggled in anywhere inside the list
|
|
3213
|
+
// sits outside an exact span of the list's own length. The check runs to
|
|
3214
|
+
// the END of the cell, not just the enumeration's own length: a phantom
|
|
3215
|
+
// cause appended after the enumeration's closing sentence would otherwise
|
|
3216
|
+
// sit past a fixed-length window and pass unseen, provided it avoided the
|
|
3217
|
+
// literal string `exit 2` (which the anchor assertion above would still
|
|
3218
|
+
// catch) — `EXIT2_TAIL` is what closes that gap for a phantom cause that
|
|
3219
|
+
// doesn't repeat it.
|
|
3220
|
+
const from = cell.indexOf("exit 2 ");
|
|
3221
|
+
assert.equal(cell.slice(from, from + EXIT2_ENUMERATION.length), EXIT2_ENUMERATION);
|
|
3222
|
+
assert.equal(cell.slice(from + EXIT2_ENUMERATION.length), EXIT2_TAIL);
|
|
3223
|
+
});
|
|
3224
|
+
|
|
3225
|
+
test("both operator-facing docs name every BASE_REF refusal this script can reach (#1565)", () => {
|
|
3226
|
+
// The closed list, asserted against the script rather than trusted. Both
|
|
3227
|
+
// directions: a BASE_REF `die` bound outside the list is a refusal the docs
|
|
3228
|
+
// may be silent about, and a clause with no `die` behind it is one the docs
|
|
3229
|
+
// claim while the script cannot reach it.
|
|
3230
|
+
for (const [message, clause] of CAUSES) {
|
|
3231
|
+
if (!message.includes("BASE_REF")) continue;
|
|
3232
|
+
assert.ok(
|
|
3233
|
+
BASE_REF_CLAUSES.includes(clause),
|
|
3234
|
+
`\`die "${message}"\` is a BASE_REF refusal bound to "${clause}", which BASE_REF_CLAUSES does not carry — add it there and name it in both docs, or the command doc can stay silent about it with this suite green`,
|
|
3235
|
+
);
|
|
3236
|
+
}
|
|
3237
|
+
assert.equal(
|
|
3238
|
+
[...CAUSES.keys()].filter((m) => m.includes("BASE_REF")).length,
|
|
3239
|
+
BASE_REF_CLAUSES.length,
|
|
3240
|
+
"every clause in BASE_REF_CLAUSES must have a `die` behind it",
|
|
3241
|
+
);
|
|
3242
|
+
|
|
3243
|
+
for (const rel of ["../commands/run-merge-bot.md", "../../docs/specs/2026-07-23-fleet-plugin-design.md"]) {
|
|
3244
|
+
const anchor = rel.endsWith("run-merge-bot.md")
|
|
3245
|
+
? (l) => l.includes("Exit **2**")
|
|
3246
|
+
: (l) => l.startsWith("| `no-undo-audit.sh` |");
|
|
3247
|
+
const line = readFileSync(fileURLToPath(new URL(rel, import.meta.url)), "utf8")
|
|
3248
|
+
.split("\n").find(anchor);
|
|
3249
|
+
assert.ok(line, `${rel} must still describe this script's exit-2 causes`);
|
|
3250
|
+
for (const clause of BASE_REF_CLAUSES) {
|
|
3251
|
+
assert.equal(
|
|
3252
|
+
line.split(clause).length - 1,
|
|
3253
|
+
1,
|
|
3254
|
+
`${rel} must name "${clause}" exactly once.\nline: ${line}`,
|
|
3255
|
+
);
|
|
3256
|
+
}
|
|
3257
|
+
}
|
|
3258
|
+
});
|
|
3259
|
+
|
|
3260
|
+
// --- #119: the escaping library this script now sources rather than carries.
|
|
3261
|
+
//
|
|
3262
|
+
// `.` is a POSIX special builtin, so failing to open its operand aborts a
|
|
3263
|
+
// non-interactive shell before any `||` on the line can run — measured, /bin/sh
|
|
3264
|
+
// (macOS bash 3.2), bash 3.2 and `bash --posix` all exit 1 with the guard
|
|
3265
|
+
// unfired. Exit 1 is a VERDICT here — `REFUSED — commit the worktree before
|
|
3266
|
+
// rebasing`, and "refused" means a dirty worktree this script actually looked
|
|
3267
|
+
// at — so a bare 1 out of a missing file would report that refusal without
|
|
3268
|
+
// having measured anything. The `[ -r ]` ahead of the `.` is what makes it a 2.
|
|
3269
|
+
test("a missing json.sh is exit 2, not a verdict about the worktree", (t) => {
|
|
3270
|
+
const c = repo(t);
|
|
3271
|
+
const lone = mkdtempSync(join(tmpdir(), "no-undo-audit-nolib-"));
|
|
3272
|
+
t.after(() => rmSync(lone, { recursive: true, force: true }));
|
|
3273
|
+
copyFileSync(SCRIPT, join(lone, "no-undo-audit.sh"));
|
|
3274
|
+
|
|
3275
|
+
const r = spawnSync("sh", [join(lone, "no-undo-audit.sh"), c.w, c.branch], {
|
|
3276
|
+
cwd: c.w, env: ENV, encoding: "utf8",
|
|
3277
|
+
});
|
|
3278
|
+
|
|
3279
|
+
assert.equal(r.status, 2,
|
|
3280
|
+
"a missing library is `the question could not be answered`. Exit 0 would call an unexamined worktree safe to rebase, which is the false safe this whole script exists to prevent.");
|
|
3281
|
+
assert.match(r.stderr, /json\.sh/,
|
|
3282
|
+
"and it names the file — this script has many exit-2 paths and the operator should not have to guess which fired");
|
|
3283
|
+
assert.equal(r.stdout, "", "no payload: nothing was measured");
|
|
3284
|
+
});
|
|
3285
|
+
|
|
3286
|
+
// --- #119, second half: what happens when the library is THERE and its tools
|
|
3287
|
+
// are not. `jarr`/`jarr_rewritten` return non-zero on a failed stage now, which
|
|
3288
|
+
// is the whole point of the extraction — and under `set -eu` a bare
|
|
3289
|
+
// `var=$(… | jarr)` would then abort with the failing tool's own status. On
|
|
3290
|
+
// this script that status is 1, byte-identical to the dirty-worktree refusal,
|
|
3291
|
+
// on a worktree the run had already logged as clean, with no payload and no
|
|
3292
|
+
// diagnostic. These three pin the `|| die` that converts it to a 2.
|
|
3293
|
+
//
|
|
3294
|
+
// One shim shape, selected on CONTENT rather than on argv, because the two jarr
|
|
3295
|
+
// call sites are invoked with identical arguments and only the values passing
|
|
3296
|
+
// through them differ. `sed` fails jarr; `tr -d` fails jrewritten and therefore
|
|
3297
|
+
// jarr_rewritten, which is the other operand of each `&&` chain.
|
|
3298
|
+
//
|
|
3299
|
+
// `selector` overrides that default arg match, which is what #431's cases need:
|
|
3300
|
+
// `jstr` ends in a `tr` carrying no `-d`, so the two defaults above cannot
|
|
3301
|
+
// address it. Passing json.sh's shared scrub set matches BOTH the replacing
|
|
3302
|
+
// `tr` that closes `jstr` and the deleting one inside `jrewritten` — which is
|
|
3303
|
+
// not ambiguity to route around, because the caller consults `jstr` first and
|
|
3304
|
+
// therefore always reports `jstr` as the escaper that failed.
|
|
3305
|
+
function withBrokenEscaper(t, { tool, marker, selector }) {
|
|
3306
|
+
const bin = mkdtempSync(join(tmpdir(), "no-undo-audit-esc-"));
|
|
3307
|
+
t.after(() => rmSync(bin, { recursive: true, force: true }));
|
|
3308
|
+
const real = execFileSync("sh", ["-c", `command -v ${tool}`], { encoding: "utf8" }).trim();
|
|
3309
|
+
// The selector arg is what keeps this off the script's OWN sed/tr calls:
|
|
3310
|
+
// `s/^/"/` appears only in jarr's rule list and `-d` only in jrewritten's.
|
|
3311
|
+
selector = selector ?? (tool === "sed" ? `'s/^/"/'` : "-d");
|
|
3312
|
+
writeFileSync(join(bin, tool), `#!/bin/sh
|
|
3313
|
+
case " $* " in
|
|
3314
|
+
*${selector}*)
|
|
3315
|
+
in=$(cat)
|
|
3316
|
+
case "$in" in *'${marker}'*) exit 1 ;; esac
|
|
3317
|
+
printf '%s\\n' "$in" | exec ${real} "$@" ;;
|
|
3318
|
+
esac
|
|
3319
|
+
exec ${real} "$@"
|
|
3320
|
+
`);
|
|
3321
|
+
chmodSync(join(bin, tool), 0o755);
|
|
3322
|
+
return `${bin}:${process.env.PATH}`;
|
|
3323
|
+
}
|
|
3324
|
+
|
|
3325
|
+
// --- #896: the two `|| die`s the three cases below exist for, and the
|
|
3326
|
+
// fatality all three read off the exit CODE — which is the shell's choice and
|
|
3327
|
+
// not this script's.
|
|
3328
|
+
//
|
|
3329
|
+
// Downgrade either guard to a message-preserving warning and the two `sed`
|
|
3330
|
+
// cases emit no payload at all to catch it by. Each pair — `conflicts_json`
|
|
3331
|
+
// with `conflicts_rewritten_json`, `at_risk_json` with
|
|
3332
|
+
// `at_risk_rewritten_json` — is assigned by one `&&` chain, so the failing
|
|
3333
|
+
// `jarr` short-circuits the second and leaves that name unset; with the guard
|
|
3334
|
+
// advisory the payload `printf` reads it and `set -u` aborts instead. Measured
|
|
3335
|
+
// on this box: bash exits 0, not 1 — `no-undo-audit.sh:538`'s `trap 'rm -f
|
|
3336
|
+
// "$mt_out" "$ps_out"' EXIT`, armed before either guard, resets the abort
|
|
3337
|
+
// status on its way out, so `<name>: unbound variable` still reaches stderr
|
|
3338
|
+
// but the caller gets back the same 0 a genuinely healthy run reports — a
|
|
3339
|
+
// false SAFE, and worse than the exit 1 this comment used to claim. dash is
|
|
3340
|
+
// unaffected by the trap and still exits 2 saying `<name>: parameter not
|
|
3341
|
+
// set` — the very status a firing guard returns, under the same message the
|
|
3342
|
+
// downgrade still prints. This file spawns a bare `sh` and
|
|
3343
|
+
// `.github/workflows/ci.yml`'s `check` job
|
|
3344
|
+
// runs on `ubuntu-latest`, where that name is dash. Measured with the at-risk
|
|
3345
|
+
// guard downgraded: every case in this file stayed green under dash and the
|
|
3346
|
+
// guard was pinned on a Mac alone.
|
|
3347
|
+
//
|
|
3348
|
+
// The unset NAME is common to both shells and is printed by neither a healthy
|
|
3349
|
+
// run nor a firing guard, so that is what discriminates. The `tr` case needs
|
|
3350
|
+
// no such assertion and is given none: `jarr` succeeds there and
|
|
3351
|
+
// `jarr_rewritten`'s capture is still ASSIGNED, empty, so nothing is unset.
|
|
3352
|
+
// Downgraded, that run exits 0 with a payload that PARSES — one conflicting
|
|
3353
|
+
// path beside `"conflictsRewritten":[]`, a flag list silently emptied
|
|
3354
|
+
// (measured, both shells) — which its status and stdout assertions already
|
|
3355
|
+
// refuse.
|
|
3356
|
+
|
|
3357
|
+
test("a jarr that cannot escape the conflicts is exit 2 with a cause, never the refusal that means dirty", (t) => {
|
|
3358
|
+
const c = bareConflictRepo(t, "boom-conflict.txt");
|
|
3359
|
+
const r = audit(c, { ...ENV, PATH: withBrokenEscaper(t, { tool: "sed", marker: "boom-conflict" }) });
|
|
3360
|
+
|
|
3361
|
+
assert.equal(r.status, 2,
|
|
3362
|
+
"the worktree is clean and was measured clean — exit 1 here would report it dirty on the strength of a broken sed");
|
|
3363
|
+
assert.match(r.stderr, /could not escape the conflicting paths/,
|
|
3364
|
+
"and it names which stage failed rather than exiting silently");
|
|
3365
|
+
assert.equal(r.stdout, "", "no payload: an unescaped conflicts list is not an answer");
|
|
3366
|
+
assert.doesNotMatch(r.stderr, /conflicts_rewritten_json/,
|
|
3367
|
+
"the guard must stop the script, not warn and leave the payload `printf` reading a name the `&&` chain never assigned. The NAME, because bash's wording and status and dash's differ and only the name is common to both.");
|
|
3368
|
+
});
|
|
3369
|
+
|
|
3370
|
+
test("a jarr that cannot escape the at-risk commits is exit 2 with its own cause", (t) => {
|
|
3371
|
+
const c = bareConflictRepo(t, "plain.txt");
|
|
3372
|
+
git(c.w, "checkout", "-q", "main");
|
|
3373
|
+
writeFileSync(join(c.w, "plain.txt"), "later main side\n");
|
|
3374
|
+
git(c.w, "add", "--", ":(literal)plain.txt");
|
|
3375
|
+
git(c.w, "commit", "-q", "-m", "BOOMATRISK subject");
|
|
3376
|
+
git(c.w, "push", "-q", "origin", "main");
|
|
3377
|
+
git(c.w, "checkout", "-q", c.branch);
|
|
3378
|
+
|
|
3379
|
+
const r = audit(c, { ...ENV, PATH: withBrokenEscaper(t, { tool: "sed", marker: "BOOMATRISK" }) });
|
|
3380
|
+
|
|
3381
|
+
// A distinct message, not the conflicts one: the two guards are separate
|
|
3382
|
+
// statements and a single shared message could not tell the operator which
|
|
3383
|
+
// array the run lost.
|
|
3384
|
+
assert.equal(r.status, 2, "an at-risk list that could not be rendered is unanswerable, not a dirty worktree");
|
|
3385
|
+
assert.match(r.stderr, /could not escape the at-risk commits/,
|
|
3386
|
+
"and the cause names the at-risk list, not the conflicts one");
|
|
3387
|
+
assert.equal(r.stdout, "", "no payload: a run that cannot say what a resolution would eat has not answered");
|
|
3388
|
+
assert.doesNotMatch(r.stderr, /at_risk_rewritten_json/,
|
|
3389
|
+
"and the guard is fatal, not advisory — the NAME again, for the reason the conflicts case states: the status a downgrade produces here is 2 under the shell CI runs.");
|
|
3390
|
+
});
|
|
3391
|
+
|
|
3392
|
+
test("a jarr_rewritten that cannot answer is exit 2 too — the `&&` chain covers both operands", (t) => {
|
|
3393
|
+
const c = bareConflictRepo(t, "boom-conflict.txt");
|
|
3394
|
+
const r = audit(c, { ...ENV, PATH: withBrokenEscaper(t, { tool: "tr", marker: "boom-conflict" }) });
|
|
3395
|
+
|
|
3396
|
+
assert.equal(r.status, 2,
|
|
3397
|
+
"`conflictsRewritten` is what tells the runbook a path is not safe to hand to `git diff` — a run that cannot compute it has not answered");
|
|
3398
|
+
assert.match(r.stderr, /could not escape the conflicting paths/);
|
|
3399
|
+
assert.equal(r.stdout, "", "no payload: half the pair is not a receipt");
|
|
3400
|
+
});
|
|
3401
|
+
|
|
3402
|
+
test("a shimmed escaper that works answers the audit in full — the guards refuse only a real outage", (t) => {
|
|
3403
|
+
// The false-positive half, and the control the three cases above need:
|
|
3404
|
+
// shadowing `sed` on PATH is not by itself fatal to this script. The same
|
|
3405
|
+
// fixture and the same shadowed name as the first of them, a marker nothing
|
|
3406
|
+
// in this run carries — so those exit 2s are the escaper failing, not the
|
|
3407
|
+
// shim's mere presence. Without this they could be measuring a PATH they
|
|
3408
|
+
// broke wholesale and still read green. The `tr` shim has its counterpart in
|
|
3409
|
+
// the #431 cases below, which install that shape and get a payload back.
|
|
3410
|
+
const c = bareConflictRepo(t, "boom-conflict.txt");
|
|
3411
|
+
const r = audit(c, { ...ENV, PATH: withBrokenEscaper(t, { tool: "sed", marker: "no value in this run carries this" }) });
|
|
3412
|
+
|
|
3413
|
+
assert.equal(r.status, 0, "the worktree is clean and every stage rendered — a shim on PATH is not itself a refusal");
|
|
3414
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
3415
|
+
assert.deepEqual(r.json.conflicts, ["boom-conflict.txt"], "the array the first case could not render, rendered");
|
|
3416
|
+
assert.deepEqual(r.json.conflictsRewritten, [false]);
|
|
3417
|
+
assert.deepEqual(subjects(r), ["MAIN COMMIT AT RISK"], "and the at-risk array the second one could not");
|
|
3418
|
+
});
|
|
3419
|
+
|
|
3420
|
+
// --- #1160: the OTHER direction from the three cases above, on the same shim.
|
|
3421
|
+
// Those escape `conflicts[]` and `atRisk[]` into the payload and must reach
|
|
3422
|
+
// exit 2 when they cannot. These two only RENDER already-computed values to
|
|
3423
|
+
// stderr for a human, so they decide nothing and must reach no exit at all.
|
|
3424
|
+
// Written bare — `printf … | sed 's/^/ conflict: /' >&2` — the pipeline's
|
|
3425
|
+
// status was sed's and `set -eu` took it, so a render exited the audit at 1,
|
|
3426
|
+
// the dirty-worktree refusal, on a worktree the same run had already printed
|
|
3427
|
+
// `clean` for, with no payload and no `no-undo-audit:` line naming a cause.
|
|
3428
|
+
//
|
|
3429
|
+
// A shim rather than the locale, and that is what makes these the half CI can
|
|
3430
|
+
// run. The invalid-UTF-8 case above reaches the same renders through the pin,
|
|
3431
|
+
// but only on BSD sed: under a GNU toolchain the byte passes straight through
|
|
3432
|
+
// and no render ever fails, and `ci.yml`'s `check` job runs ubuntu-latest.
|
|
3433
|
+
//
|
|
3434
|
+
// `selector` is the renders' shared `s/^/` prefix, so one shim reaches the
|
|
3435
|
+
// `conflict: ` and `at risk: ` renders and the porcelain one, and reaches
|
|
3436
|
+
// json.sh's escapers not at all — their only `s/^/` rule is `s/^/"/` (#119's
|
|
3437
|
+
// cases above select on exactly that). `marker: ""` switches the helper's
|
|
3438
|
+
// content gate off: it exists because the two jarr call sites share one argv,
|
|
3439
|
+
// and these three do not, so argv alone addresses them.
|
|
3440
|
+
const withFailingRenders = (t) =>
|
|
3441
|
+
withBrokenEscaper(t, { tool: "sed", marker: "", selector: `'s/^/ '` });
|
|
3442
|
+
|
|
3443
|
+
test("a render that cannot reach stderr leaves the verdict it decides nothing about intact (#1160)", (t) => {
|
|
3444
|
+
const c = bareConflictRepo(t, "boom-conflict.txt");
|
|
3445
|
+
const r = audit(c, { ...ENV, PATH: withFailingRenders(t) });
|
|
3446
|
+
|
|
3447
|
+
// The fault injection first: without it every assertion below measures an
|
|
3448
|
+
// unmutated run and passes on a script with the guards deleted.
|
|
3449
|
+
assert.doesNotMatch(r.stderr, /^ {4}conflict: /m,
|
|
3450
|
+
"the fault injection never fired — the conflict list still rendered, so nothing here is measuring a failed render");
|
|
3451
|
+
assert.doesNotMatch(r.stderr, /^ {4}at risk: /m,
|
|
3452
|
+
"nor did it reach the at-risk render, which is a separate call site and was a separate bare pipeline");
|
|
3453
|
+
|
|
3454
|
+
assert.equal(r.status, 0,
|
|
3455
|
+
`the worktree is clean and was measured clean — exit 1 here is the render deciding the verdict; got ${r.status} ${r.stderr}`);
|
|
3456
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
3457
|
+
assert.deepEqual(r.json.conflicts, ["boom-conflict.txt"],
|
|
3458
|
+
"the payload is computed before either render and must survive both of them failing");
|
|
3459
|
+
assert.deepEqual(subjects(r), ["MAIN COMMIT AT RISK"],
|
|
3460
|
+
"and the run continues past the first failed render rather than stopping at it — the at-risk step is downstream of it");
|
|
3461
|
+
|
|
3462
|
+
// Not `|| :`: a dropped conflict list reads exactly like an empty one, which
|
|
3463
|
+
// is the same silent-failure class as the abort. Each render names itself,
|
|
3464
|
+
// because a single shared message could not tell the operator which went
|
|
3465
|
+
// missing.
|
|
3466
|
+
assert.match(r.stderr, /^no-undo-audit: could not render the conflicting-path list to stderr/m,
|
|
3467
|
+
"a lost diagnostic must say so, and say which one");
|
|
3468
|
+
assert.match(r.stderr, /^no-undo-audit: could not render the at-risk commit list to stderr/m,
|
|
3469
|
+
"and the at-risk render must name itself rather than share the line above");
|
|
3470
|
+
});
|
|
3471
|
+
|
|
3472
|
+
test("a porcelain render that cannot reach stderr still refuses WITH its payload (#1160)", (t) => {
|
|
3473
|
+
// The dirty half, and the one the case above cannot cover: here exit 1 is
|
|
3474
|
+
// the correct answer, so the status cannot discriminate and the PAYLOAD is
|
|
3475
|
+
// what does. This render runs before the payload is written, so unguarded
|
|
3476
|
+
// its abort took the whole answer with it — a refusal with nothing on stdout
|
|
3477
|
+
// to say what was measured, indistinguishable from the exit 1 a failing
|
|
3478
|
+
// render fabricates on a clean tree.
|
|
3479
|
+
const c = repo(t);
|
|
3480
|
+
writeFileSync(join(c.w, "uncommitted.txt"), "work that exists nowhere else\n");
|
|
3481
|
+
|
|
3482
|
+
const r = audit(c, { ...ENV, PATH: withFailingRenders(t) });
|
|
3483
|
+
|
|
3484
|
+
assert.doesNotMatch(r.stderr, /^ {4}\?\? uncommitted\.txt$/m,
|
|
3485
|
+
"the fault injection never fired — the porcelain dump still rendered");
|
|
3486
|
+
assert.equal(r.status, 1,
|
|
3487
|
+
`a dirty worktree refuses on its own account, at the status it always did; got ${r.status} ${r.stderr}`);
|
|
3488
|
+
assert.equal(r.jsonError, null, `the refusal must still carry its payload; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
3489
|
+
assert.equal(r.json.clean, false, "and the payload must say what was measured, not merely that something failed");
|
|
3490
|
+
assert.match(r.stderr, /REFUSED — commit the worktree before rebasing/,
|
|
3491
|
+
"the refusal the caller acts on is the audit's own sentence, which sits past the failed render");
|
|
3492
|
+
assert.match(r.stderr, /^no-undo-audit: could not render the uncommitted-work list to stderr/m,
|
|
3493
|
+
"and the dump the operator would have read is reported missing rather than dropped");
|
|
3494
|
+
});
|
|
3495
|
+
|
|
3496
|
+
// render()'s trailing `|| :` is what stands between an aborted audit and a
|
|
3497
|
+
// caller whose stderr is completely gone — both the primary write AND its own
|
|
3498
|
+
// one-line fallback disclaimer fail, so the guard's third piece is the only
|
|
3499
|
+
// one left to catch it. The two cases above reach that state through a `sed`
|
|
3500
|
+
// shim on PATH; this one reaches it the way a real caller does, by closing
|
|
3501
|
+
// fd 2 on the whole process.
|
|
3502
|
+
//
|
|
3503
|
+
// That end-to-end shape was unreachable until #1514, and this test used to
|
|
3504
|
+
// extract `render()` by name and run it standalone for exactly that reason:
|
|
3505
|
+
// every OTHER bare `>&2` write in the script was unguarded too, so a closed
|
|
3506
|
+
// fd 2 aborted the run on the first of them — the status-command header,
|
|
3507
|
+
// before git had been asked anything — and #1160's guard was dead code to
|
|
3508
|
+
// any test that closed fd 2 for real. The standalone extraction went with
|
|
3509
|
+
// the gap it existed to work around: this run drives the same three-piece
|
|
3510
|
+
// guard through the same function, twice, inside the script that owns it.
|
|
3511
|
+
//
|
|
3512
|
+
// The PAYLOAD is the proof it got there, and it has to be, because a run
|
|
3513
|
+
// with no stderr has no other way to report. `conflicts[]` and `atRisk[]`
|
|
3514
|
+
// are rendered by two separate `render()` calls and then carried to stdout,
|
|
3515
|
+
// so a non-empty pair says both renders failed, neither decided anything,
|
|
3516
|
+
// and the run continued past each — an exit 0 alone would also be produced
|
|
3517
|
+
// by a script that bailed out early with an empty payload.
|
|
3518
|
+
//
|
|
3519
|
+
// Neither of the stronger signals finding 2 raised fits this script without
|
|
3520
|
+
// breaking an invariant already established and tested elsewhere: `exec`-ing
|
|
3521
|
+
// a marker to fd 1 would inject a second value into the one-JSON-object
|
|
3522
|
+
// stdout contract every payload test parses strictly (`jq -e "."` above), and
|
|
3523
|
+
// a distinguishable exit code would hand a render exactly the
|
|
3524
|
+
// verdict-deciding power #1160 exists to take away — this file's own header
|
|
3525
|
+
// says a render "answers nothing and so must reach no exit at all". Pinning
|
|
3526
|
+
// the accepted tradeoff (the JSON payload already carries what every render
|
|
3527
|
+
// only duplicates onto stderr) rather than changing it.
|
|
3528
|
+
test("a closed fd 2 reaches both renders and leaves the verdict intact (#1160, #1514)", (t) => {
|
|
3529
|
+
const c = bareConflictRepo(t, "boom-conflict.txt");
|
|
3530
|
+
|
|
3531
|
+
const r = spawnSync("sh", ["-c", '"$0" "$@" 2>&-', SCRIPT, c.w, c.branch], {
|
|
3532
|
+
cwd: c.w,
|
|
3533
|
+
env: ENV,
|
|
3534
|
+
encoding: "utf8",
|
|
3535
|
+
});
|
|
3536
|
+
|
|
3537
|
+
assert.equal(r.status, 0,
|
|
3538
|
+
`the worktree is clean and was measured clean — 1 is a render deciding the verdict and 2 is a question this run could answer; got ${r.status}`);
|
|
3539
|
+
const json = JSON.parse(r.stdout);
|
|
3540
|
+
assert.deepEqual(json.conflicts, ["boom-conflict.txt"],
|
|
3541
|
+
"the conflicting-path render sits between the status header and this field — an empty list means the run never got past it");
|
|
3542
|
+
assert.deepEqual(json.atRisk.map((l) => l.replace(/^\S+ /, "")), ["MAIN COMMIT AT RISK"],
|
|
3543
|
+
"and the at-risk render is downstream of the first, so this is what says the run continued past a second failed one");
|
|
3544
|
+
});
|
|
3545
|
+
|
|
3546
|
+
// The sweep, and the only thing that keeps it swept. Every test above walks
|
|
3547
|
+
// ONE path: a clean tree, a dirty tree, a conflicted tree, a non-worktree.
|
|
3548
|
+
// The stash chain alone has four branches, `die` has thirty-odd call sites,
|
|
3549
|
+
// and no realistic fixture set visits all of them — so a future write that
|
|
3550
|
+
// bypasses `die`/`render`/`emit` and reaches fd 2 bare on a branch nothing
|
|
3551
|
+
// exercises reintroduces the whole bug silently, and no fixture would catch
|
|
3552
|
+
// it either.
|
|
3553
|
+
//
|
|
3554
|
+
// Only `render()`'s write carried failure protection before this PR (#1514).
|
|
3555
|
+
// #1160 guarded the FOLD that assembles the stash-diagnostic line against a
|
|
3556
|
+
// corrupting `tr`/`sed` exit status — a different hazard — and left every
|
|
3557
|
+
// stderr WRITE, `die`'s included, free to abort the run on a closed fd 2.
|
|
3558
|
+
// #1514 (this PR) is what guards those, and centralizes them behind `die`,
|
|
3559
|
+
// `render`, and the new `emit` so there is one function body to check per
|
|
3560
|
+
// guard, not a dozen call sites to keep in sync. #1684 went one further and
|
|
3561
|
+
// folded `die`'s own copy of that body into a call to `emit`, so the census
|
|
3562
|
+
// below counts two direct writers where it once counted three — `die` still
|
|
3563
|
+
// reaches fd 2, but no longer by a statement of its own. #1685 folded
|
|
3564
|
+
// `render`'s own copy the same way; the count stayed at two because
|
|
3565
|
+
// `render`'s pipeline write and its fallback already shared one segment
|
|
3566
|
+
// (see the comment on the count assertion below), so the fold removed a
|
|
3567
|
+
// duplicated guard, not a writer the census was counting separately.
|
|
3568
|
+
//
|
|
3569
|
+
// The rule is structural, so it is asserted structurally rather than
|
|
3570
|
+
// sampled: a diagnostic decides nothing, so every statement that writes to
|
|
3571
|
+
// fd 2 must end its `||` chain in the no-op, either directly (`|| :`) or by
|
|
3572
|
+
// calling a function whose own chain does (`|| emit …`, verified on
|
|
3573
|
+
// `emit`'s own segment in the same pass). `render()`'s body spans two lines
|
|
3574
|
+
// and ends in a call to `emit` on the second, so continuations are joined
|
|
3575
|
+
// before the check; `emit()`'s carries more (`; }`) after ITS OWN guard, on
|
|
3576
|
+
// the SAME joined line, separated by `;` rather than `||` — so a line is
|
|
3577
|
+
// split on top-level `;` first and each resulting statement's own tail is
|
|
3578
|
+
// checked, not just the line's last `>&2`. Quote-aware: a `;`
|
|
3579
|
+
// inside a quoted argument (render's fallback message, now `emit`'s own
|
|
3580
|
+
// argument, carries one) is not a statement separator. Unaware of that, a
|
|
3581
|
+
// line carrying two independent `>&2`-writing statements —
|
|
3582
|
+
// `echo "a" >&2; echo "b" >&2 || :` — would report the whole line compliant
|
|
3583
|
+
// off the LAST write's guard alone, leaving the first invisible.
|
|
3584
|
+
//
|
|
3585
|
+
// Paren-aware too, since #1571: `emit()`'s one write still runs inside
|
|
3586
|
+
// `( trap '' PIPE; write )`, so the `;` between the trap and the write sits
|
|
3587
|
+
// INSIDE that subshell rather than between top-level statements. Splitting
|
|
3588
|
+
// on every unquoted `;` regardless of nesting would cut that one segment
|
|
3589
|
+
// into two, over-counting below and — worse — leaving one half of the pair
|
|
3590
|
+
// to be graded for a guard that was never its own to carry. Depth only
|
|
3591
|
+
// needs `(`/`)`, never `{`/`}`: no write in this script sits inside a brace
|
|
3592
|
+
// group that isn't also the enclosing function body, which
|
|
3593
|
+
// `splitTopLevelStatements` is never handed. The guard check's own tail
|
|
3594
|
+
// regex still tolerates one optional `)` immediately after the write's
|
|
3595
|
+
// `>&2` for the same reason — `emit`'s subshell close sits between its
|
|
3596
|
+
// write and the `|| :` that guards it. `render`'s fallback carries no
|
|
3597
|
+
// subshell of its own to tolerate anymore, only a call to the function
|
|
3598
|
+
// that does; the tolerance stays because `emit`'s segment still needs it,
|
|
3599
|
+
// not because `render`'s does.
|
|
3600
|
+
function splitTopLevelStatements(line) {
|
|
3601
|
+
const segments = [];
|
|
3602
|
+
let cur = "";
|
|
3603
|
+
let quote = null;
|
|
3604
|
+
let depth = 0;
|
|
3605
|
+
for (let i = 0; i < line.length; i += 1) {
|
|
3606
|
+
const ch = line[i];
|
|
3607
|
+
if (quote) {
|
|
3608
|
+
cur += ch;
|
|
3609
|
+
if (ch === "\\" && quote === '"' && i + 1 < line.length) {
|
|
3610
|
+
i += 1;
|
|
3611
|
+
cur += line[i];
|
|
3612
|
+
} else if (ch === quote) {
|
|
3613
|
+
quote = null;
|
|
3614
|
+
}
|
|
3615
|
+
continue;
|
|
3616
|
+
}
|
|
3617
|
+
if (ch === "'" || ch === '"') {
|
|
3618
|
+
quote = ch;
|
|
3619
|
+
cur += ch;
|
|
3620
|
+
} else if (ch === "(") {
|
|
3621
|
+
depth += 1;
|
|
3622
|
+
cur += ch;
|
|
3623
|
+
} else if (ch === ")") {
|
|
3624
|
+
depth = Math.max(0, depth - 1);
|
|
3625
|
+
cur += ch;
|
|
3626
|
+
} else if (ch === ";" && depth === 0) {
|
|
3627
|
+
segments.push(cur);
|
|
3628
|
+
cur = "";
|
|
3629
|
+
} else {
|
|
3630
|
+
cur += ch;
|
|
3631
|
+
}
|
|
3632
|
+
}
|
|
3633
|
+
segments.push(cur);
|
|
3634
|
+
return segments;
|
|
3635
|
+
}
|
|
3636
|
+
|
|
3637
|
+
// A `case pat)` arm's close paren has no opening `(` to match, so an
|
|
3638
|
+
// unclamped counter would carry depth negative for the rest of the line —
|
|
3639
|
+
// every `;` after it stops looking top-level, and the census below would
|
|
3640
|
+
// stop seeing statements it must see. Clamping at zero is what keeps a
|
|
3641
|
+
// `case` arm from swallowing its neighbors.
|
|
3642
|
+
test("an unmatched `)` from a case arm does not swallow the statements after it", () => {
|
|
3643
|
+
assert.deepEqual(
|
|
3644
|
+
splitTopLevelStatements("case x in x) a; b; esac; c"),
|
|
3645
|
+
["case x in x) a", " b", " esac", " c"],
|
|
3646
|
+
);
|
|
3647
|
+
});
|
|
3648
|
+
// Shared by the fd-2 write census below and its self-referential-emit
|
|
3649
|
+
// regression test: extracting this keeps both walking the exact same
|
|
3650
|
+
// filters instead of two copies drifting apart.
|
|
3651
|
+
function censusStderrWrites(lines) {
|
|
3652
|
+
const writes = lines
|
|
3653
|
+
.filter((l) => !/^\s*#/.test(l))
|
|
3654
|
+
.flatMap(splitTopLevelStatements)
|
|
3655
|
+
.filter((s) => s.includes(">&2"));
|
|
3656
|
+
|
|
3657
|
+
// A `|| emit` tail only reads as guarded because `emit`'s OWN segment is
|
|
3658
|
+
// checked separately, in this same pass — delegating to it is how a
|
|
3659
|
+
// write that can't speak for itself borrows a guard that already lives
|
|
3660
|
+
// elsewhere. `emit()`'s own definition can't borrow from itself that
|
|
3661
|
+
// way: a `|| emit "lost"` fallback on emit's own write recurses into the
|
|
3662
|
+
// same failing write instead of reaching a segment this pass has
|
|
3663
|
+
// already cleared, and hangs rather than terminating (measured: exit
|
|
3664
|
+
// 124, no termination). `delegates` is the exemption both filters below
|
|
3665
|
+
// share, with that one carve-out.
|
|
3666
|
+
const delegates = (s) =>
|
|
3667
|
+
/^\s*\)?\s*\|\|\s*emit\b/.test(s.slice(s.lastIndexOf(">&2") + 3)) &&
|
|
3668
|
+
!/^\s*emit\(\)/.test(s.trim());
|
|
3669
|
+
|
|
3670
|
+
const unguarded = writes.filter(
|
|
3671
|
+
(s) => !/^\s*\)?\s*\|\|\s*:(\s|;|$)/.test(s.slice(s.lastIndexOf(">&2") + 3)) && !delegates(s),
|
|
3672
|
+
);
|
|
3673
|
+
const untrapped = writes.filter(
|
|
3674
|
+
(s) => !/\(\s*trap\s+''\s+PIPE\s*;/.test(s) && !delegates(s),
|
|
3675
|
+
);
|
|
3676
|
+
return { writes, unguarded, untrapped };
|
|
3677
|
+
}
|
|
3678
|
+
|
|
3679
|
+
|
|
3680
|
+
test("no stderr write in the script can abort the run under errexit (#1514)", () => {
|
|
3681
|
+
const joined = readFileSync(SCRIPT, "utf8").replace(/\\\n\s*/g, " ").split("\n");
|
|
3682
|
+
const { writes, unguarded, untrapped } = censusStderrWrites(joined);
|
|
3683
|
+
|
|
3684
|
+
// Without this the filter could silently match nothing — a regex typo, a
|
|
3685
|
+
// rename — and the assertion below would pass on an empty list, which is
|
|
3686
|
+
// the shape a false green takes here. The count is exact, not a floor:
|
|
3687
|
+
// `render` and `emit` are the only two places in the script allowed to
|
|
3688
|
+
// touch fd 2 directly, so it can only ever be 2 — any other number means
|
|
3689
|
+
// a bare write appeared outside both of them, or one vanished. It was 3
|
|
3690
|
+
// until #1684 folded `die`'s own copy of `emit`'s body into a call: the
|
|
3691
|
+
// segment that held `die`'s `>&2` is gone, and `render`'s and `emit`'s
|
|
3692
|
+
// are what the filter still returns. #1685 folded `render`'s own copy the
|
|
3693
|
+
// same way and left the count where #1684 put it, for a different reason
|
|
3694
|
+
// than `die`'s drop: `render`'s two writes (the `sed` pipeline and its
|
|
3695
|
+
// fallback) always shared ONE segment, so routing the fallback through a
|
|
3696
|
+
// call to `emit` removed a duplicated guard, not a segment — the filter
|
|
3697
|
+
// still sees one `>&2` per site, same as before either fix. Measured
|
|
3698
|
+
// against the tree, not decremented on paper: arithmetic on the old count
|
|
3699
|
+
// is not a safe way to arrive at the new one.
|
|
3700
|
+
assert.equal(writes.length, 2,
|
|
3701
|
+
`render and emit are the only statements that may write to fd 2 directly; found ${writes.length}`);
|
|
3702
|
+
|
|
3703
|
+
assert.deepEqual(unguarded.map((s) => s.trim()), [],
|
|
3704
|
+
"each of these ends the script on its own write status under `set -e`, and 1 out of this script is REFUSED — append `|| :` or route the fallback through `emit`, which already does");
|
|
3705
|
+
|
|
3706
|
+
// #1700 made `emit()`'s `( trap '' PIPE; … )` wrapper the ONLY SIGPIPE
|
|
3707
|
+
// guard for all 29 `die` call sites — `die` now calls `emit` instead of
|
|
3708
|
+
// carrying its own copy. #1685 moved `render`'s fallback write behind the
|
|
3709
|
+
// same call, so its trap now lives in `emit`'s OWN segment rather than
|
|
3710
|
+
// render's — the `unguarded` filter above already treats a `|| emit` tail
|
|
3711
|
+
// as guarded for that reason (unless the segment IS emit's own
|
|
3712
|
+
// definition — see `censusStderrWrites`'s `delegates`), and this filter
|
|
3713
|
+
// has to agree: a segment whose write reaches fd 2 only by delegating to
|
|
3714
|
+
// `emit` carries no trap text of its own to find, and is not untrapped
|
|
3715
|
+
// for it, because `emit`'s segment is checked separately, right here, in
|
|
3716
|
+
// the same pass. `|| :` only swallows the *status* a killed write would
|
|
3717
|
+
// leave behind, not the SIGPIPE that killed it, which is why a bare
|
|
3718
|
+
// `|| :` tail (with no `emit` call) still has to show the trap substring
|
|
3719
|
+
// in its own segment to pass.
|
|
3720
|
+
assert.deepEqual(untrapped.map((s) => s.trim()), [],
|
|
3721
|
+
"each of these can deliver SIGPIPE and kill the script instead of turning a dropped write into EPIPE — wrap it in `( trap '' PIPE; … )` or route it through `emit`, which already does");
|
|
3722
|
+
});
|
|
3723
|
+
|
|
3724
|
+
// #1703 found that treating any `|| emit` tail as guarded let `emit` guard
|
|
3725
|
+
// ITSELF: a self-referential `|| emit "lost"` fallback on emit's own write
|
|
3726
|
+
// recurses into the same failing write instead of reaching a segment this
|
|
3727
|
+
// census clears elsewhere, and hangs rather than terminating (measured:
|
|
3728
|
+
// exit 124, no termination). `censusStderrWrites`'s `delegates` carve-out
|
|
3729
|
+
// is what keeps that shape out of the real script; this proves the
|
|
3730
|
+
// carve-out actually fires, not just that the real script currently lacks
|
|
3731
|
+
// the shape.
|
|
3732
|
+
test("a self-referential emit() fallback does not pass the fd-2 write census (#1703)", () => {
|
|
3733
|
+
const selfReferential = ["emit() { ( trap '' PIPE; printf '%s\\n' \"$1\" >&2 ) || emit \"lost\"; }"];
|
|
3734
|
+
const { unguarded } = censusStderrWrites(selfReferential);
|
|
3735
|
+
assert.deepEqual(unguarded.map((s) => s.trim()), [selfReferential[0].replace(/;\s*}$/, "").trim()],
|
|
3736
|
+
"emit() falling back to calling itself must still be flagged unguarded — it recurses into the same failing write and hangs instead of terminating");
|
|
3737
|
+
});
|
|
3738
|
+
|
|
3739
|
+
/**
|
|
3740
|
+
* Fails the stash-diagnostic fold and nothing else, addressed by ARGV.
|
|
3741
|
+
*
|
|
3742
|
+
* That fold is the script's only two-operand `tr` whose first operand is the
|
|
3743
|
+
* literal `\n` — json.sh's four all carry either `-d` or a `\001-\007` range
|
|
3744
|
+
* — so the argv test is exact. Content selection is unusable here:
|
|
3745
|
+
* `withBrokenEscaper` reads the tool's stdin through `$(cat)` and re-feeds it
|
|
3746
|
+
* with `printf '%s\n'`, and that round trip appends a newline to every value
|
|
3747
|
+
* json.sh escapes through its own `tr` calls.
|
|
3748
|
+
*/
|
|
3749
|
+
function withFailingDiagnosticFold(t) {
|
|
3750
|
+
const s = shimDir(t, "no-undo-audit-fold-");
|
|
3751
|
+
const real = s.real("tr");
|
|
3752
|
+
const fired = join(s.bin, "fired");
|
|
3753
|
+
s.write("tr", `if [ $# -eq 2 ] && [ "$1" = '\\n' ] && [ "$2" = ' ' ]; then
|
|
3754
|
+
: >"${fired}"
|
|
3755
|
+
echo "tr: RIP" >&2
|
|
3756
|
+
exit 1
|
|
3757
|
+
fi
|
|
3758
|
+
exec ${real} "$@"
|
|
3759
|
+
`);
|
|
3760
|
+
return { path: s.path(), fired };
|
|
3761
|
+
}
|
|
3762
|
+
|
|
3763
|
+
test("a stash diagnostic that cannot be folded still answers, and says which half went missing (#1160)", (t) => {
|
|
3764
|
+
// The FOURTH site of the class, and the one #1160's body does not enumerate
|
|
3765
|
+
// — it is named only in this script's own locale-pin comment, alongside the
|
|
3766
|
+
// three renders. It folds git's multi-line `stash list` diagnostic onto the
|
|
3767
|
+
// audit's own line, decides nothing (`$stash` is already `null` and this
|
|
3768
|
+
// branch is already reporting a fault), and unguarded an assignment's
|
|
3769
|
+
// `$( … | tr … )` still hands `set -eu` tr's own status. `tr` is the surer
|
|
3770
|
+
// carrier than the sed renders too: it exits on an invalid byte wherever in
|
|
3771
|
+
// the line it sits, where sed tolerates some positions.
|
|
3772
|
+
//
|
|
3773
|
+
// Same fixture as the fold's own behaviour test above — a corrupt stash TIP
|
|
3774
|
+
// object, the state where `stash list` itself fails and git says why.
|
|
3775
|
+
const c = repo(t);
|
|
3776
|
+
stashSomething(c.w);
|
|
3777
|
+
const sha = git(c.w, "rev-parse", "refs/stash");
|
|
3778
|
+
const obj = join(c.w, ".git", "objects", sha.slice(0, 2), sha.slice(2));
|
|
3779
|
+
chmodSync(obj, 0o644);
|
|
3780
|
+
writeFileSync(obj, "junk\n");
|
|
3781
|
+
|
|
3782
|
+
const fold = withFailingDiagnosticFold(t);
|
|
3783
|
+
const r = audit(c, { ...ENV, PATH: fold.path });
|
|
3784
|
+
|
|
3785
|
+
assert.ok(existsSync(fold.fired),
|
|
3786
|
+
"the fault injection never fired — git's diagnostic no longer flows through a `tr`, so nothing below measures a failed fold");
|
|
3787
|
+
assert.equal(r.status, 0,
|
|
3788
|
+
`the worktree is clean and was measured clean — exit 1 here is the fold deciding the verdict; got ${r.status} ${r.stderr}`);
|
|
3789
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
3790
|
+
assert.equal(r.json.stash, null,
|
|
3791
|
+
"the count is still unknown — that verdict is reached before the fold and must not depend on it");
|
|
3792
|
+
assert.equal(stashLines(r).length, 1, "and the line is still printed, exactly once");
|
|
3793
|
+
assert.match(stashLine(r), /git said more, and folding it onto this line failed/,
|
|
3794
|
+
"git's own text is the half that went missing, and the line says so rather than ending where the fold did or carrying it truncated at the byte");
|
|
3795
|
+
});
|
|
3796
|
+
|
|
3797
|
+
// --- #431: the same broken escaper, one block further down, and the OPPOSITE
|
|
3798
|
+
// answer. The three cases above escape `conflicts[]` and `atRisk[]` — findings
|
|
3799
|
+
// the operator can obtain nowhere else, and which step 5 of the no-undo runbook
|
|
3800
|
+
// hands to `git diff -- <path>`. A run that cannot render those has not
|
|
3801
|
+
// answered, so they keep their `die`.
|
|
3802
|
+
//
|
|
3803
|
+
// `worktree` and `branch` are the other kind. They are echoes of argv: the
|
|
3804
|
+
// caller supplied both and still holds them, and by the time this block runs
|
|
3805
|
+
// `clean` has been measured, `stash` counted, and both arrays already rendered.
|
|
3806
|
+
// A `tr` that has gone missing there used to convert that finished audit into
|
|
3807
|
+
// exit 2 with no payload — discarding every real finding over the formatting of
|
|
3808
|
+
// two values the caller typed. Each renders independently now and reports JSON
|
|
3809
|
+
// `null` when it cannot, which is what #120 shipped for the same class in
|
|
3810
|
+
// inflight.sh.
|
|
3811
|
+
//
|
|
3812
|
+
// `null` is the honest report and not a quieter `""`: a field that could not be
|
|
3813
|
+
// escaped has no usable path to hand to `git diff` in any case, and `""` is
|
|
3814
|
+
// indistinguishable from a path, which is the false-reassurance direction.
|
|
3815
|
+
test("an escaper that cannot render the branch reports it null and still delivers every finding", (t) => {
|
|
3816
|
+
const c = conflictRepo(t, "plain.txt", "fix/1-BOOMBRANCH");
|
|
3817
|
+
const r = audit(c, { ...ENV, PATH: withBrokenEscaper(t, {
|
|
3818
|
+
tool: "tr", marker: "BOOMBRANCH", selector: `'\\001-\\007\\013\\016-\\037'`,
|
|
3819
|
+
}) });
|
|
3820
|
+
|
|
3821
|
+
assert.equal(r.status, 0,
|
|
3822
|
+
"the worktree was measured clean and every finding survived — exit 2 here would retract a finished audit over a formatter");
|
|
3823
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
3824
|
+
assert.equal(r.json.branch, null, "the unrenderable field is null, not a truncated or empty string");
|
|
3825
|
+
assert.equal(r.json.branchRewritten, null,
|
|
3826
|
+
"and its paired flag too — a `false` there would claim nothing was replaced in a value nothing could examine");
|
|
3827
|
+
|
|
3828
|
+
// The point of the whole change: everything the audit established is still on
|
|
3829
|
+
// the payload, and the OTHER escaped field is untouched by its sibling's
|
|
3830
|
+
// failure.
|
|
3831
|
+
assert.equal(r.json.worktree, c.w, "the worktree renders independently and keeps its real value");
|
|
3832
|
+
assert.equal(r.json.worktreeRewritten, false);
|
|
3833
|
+
assert.equal(r.json.clean, true, "measured before the escape ran, and still reported");
|
|
3834
|
+
assert.equal(r.json.stash, 0);
|
|
3835
|
+
assert.deepEqual(r.json.conflicts, ["plain.txt"], "the conflicting path was found and is still named");
|
|
3836
|
+
assert.deepEqual(r.json.conflictsRewritten, [false]);
|
|
3837
|
+
assert.deepEqual(subjects(r), ["MAIN COMMIT AT RISK"],
|
|
3838
|
+
"the commit a careless resolution would eat is still named — that finding is the reason this script exists");
|
|
3839
|
+
|
|
3840
|
+
assert.match(r.stderr, /could not render the branch .*\(jstr\)/,
|
|
3841
|
+
"and the operator is told which field and which escaper, not left to diff the payload against a healthy one");
|
|
3842
|
+
});
|
|
3843
|
+
|
|
3844
|
+
test("an escaper that cannot say whether the worktree path was rewritten reports it null, leaving the branch intact", (t) => {
|
|
3845
|
+
const c = conflictRepo(t, "plain.txt", undefined, "no-undo-audit-BOOMWT-");
|
|
3846
|
+
const r = audit(c, { ...ENV, PATH: withBrokenEscaper(t, { tool: "tr", marker: "BOOMWT" }) });
|
|
3847
|
+
|
|
3848
|
+
assert.equal(r.status, 0);
|
|
3849
|
+
assert.equal(r.jsonError, null, `payload must parse; got ${r.jsonError?.message}\n${r.stdout}`);
|
|
3850
|
+
assert.equal(r.json.worktree, null,
|
|
3851
|
+
"`jstr` rendered this path perfectly and `jrewritten` still could not say whether a byte was replaced — an undisclosed rewrite is not a path a reader may trust");
|
|
3852
|
+
assert.equal(r.json.worktreeRewritten, null);
|
|
3853
|
+
assert.equal(r.json.branch, c.branch, "the branch renders independently and is unaffected");
|
|
3854
|
+
assert.equal(r.json.branchRewritten, false);
|
|
3855
|
+
assert.deepEqual(r.json.conflicts, ["plain.txt"]);
|
|
3856
|
+
assert.deepEqual(subjects(r), ["MAIN COMMIT AT RISK"]);
|
|
3857
|
+
|
|
3858
|
+
// Named separately from `jstr` because the two fail independently: `jstr` can
|
|
3859
|
+
// render a string while `jrewritten` cannot judge it, which is exactly this
|
|
3860
|
+
// case. One shared word would send a debugger to whichever it guessed.
|
|
3861
|
+
assert.match(r.stderr, /could not render the worktree .*\(jrewritten\)/);
|
|
3862
|
+
});
|
|
3863
|
+
|
|
3864
|
+
// #431's acceptance criterion that lives in prose rather than in the payload: a
|
|
3865
|
+
// reader of the runbook has to meet what a `null` field means BEFORE they act
|
|
3866
|
+
// on one. Both carriers state it, on the same two lines the linkage pin above
|
|
3867
|
+
// anchors on — the runbook's exit-2 paragraph and the design spec's own row.
|
|
3868
|
+
//
|
|
3869
|
+
// Two phrases, not one, because either alone is satisfied by a rewrite that
|
|
3870
|
+
// loses the point. `there is no worktree` alone passes prose that names the
|
|
3871
|
+
// misreading without ruling it out; `could not render the path or branch you passed in`
|
|
3872
|
+
// alone passes prose that says what the field IS while leaving the dangerous
|
|
3873
|
+
// reading unaddressed. The claim is the pair: this is what `null` means, and
|
|
3874
|
+
// that is what it does not.
|
|
3875
|
+
//
|
|
3876
|
+
// Deliberately NOT pinned as one span: the two docs word the surrounding
|
|
3877
|
+
// sentence differently on purpose — the runbook bolds its `never` for an
|
|
3878
|
+
// operator mid-pass, the spec row does not — and a span pin would force one
|
|
3879
|
+
// voice on both or drift into pinning nothing.
|
|
3880
|
+
test("both docs rule out reading a null worktree as an absent one", () => {
|
|
3881
|
+
for (const rel of ["../commands/run-merge-bot.md", "../../docs/specs/2026-07-23-fleet-plugin-design.md"]) {
|
|
3882
|
+
const doc = readFileSync(fileURLToPath(new URL(rel, import.meta.url)), "utf8");
|
|
3883
|
+
assert.ok(doc.includes("could not render the path or branch you passed in"),
|
|
3884
|
+
`${rel} must say what a null worktree/branch IS — the run could not render the argument the caller supplied — since the field is an echo of argv rather than a finding`);
|
|
3885
|
+
assert.ok(doc.includes('"there is no worktree"'),
|
|
3886
|
+
`${rel} must rule out the false-reassurance reading by name: a null path is not an absent worktree, and an operator who reads it as one skips the proof step believing there was nothing to prove`);
|
|
3887
|
+
}
|
|
3888
|
+
});
|
|
3889
|
+
|
|
3890
|
+
// --- #1020: the ambient git variables, one fixture each.
|
|
3891
|
+
//
|
|
3892
|
+
// The `--show-prefix` gate is this script's answer to "can git operate here is
|
|
3893
|
+
// not is this the tree it answers about", and both variables walk past it —
|
|
3894
|
+
// but through different doors and to different outcomes, so one fixture
|
|
3895
|
+
// cannot speak for both. PR #1015 measured the cost of trying: a case
|
|
3896
|
+
// overriding only one of the pair leaves the other half of
|
|
3897
|
+
// `unset GIT_DIR GIT_WORK_TREE` unpinned and green.
|
|
3898
|
+
|
|
3899
|
+
test("an ambient GIT_WORK_TREE does not report a dirty worktree as clean (#1020)", (t) => {
|
|
3900
|
+
// The silent-failure half, and the one that clears an irreversible rebase.
|
|
3901
|
+
//
|
|
3902
|
+
// GIT_WORK_TREE outranks `-C`, so `git -C "$wt" rev-parse --show-prefix`
|
|
3903
|
+
// answers about the ambient tree. The cwd is not under that tree, so git
|
|
3904
|
+
// returns an EMPTY prefix — the exact value the gate reads as "$wt IS the
|
|
3905
|
+
// root" — and the status below then compares $wt's index against the
|
|
3906
|
+
// ambient tree's files.
|
|
3907
|
+
//
|
|
3908
|
+
// The ambient tree holds a COPY of the branch's tracked content, which is
|
|
3909
|
+
// what makes the leaked answer an empty one. A bare empty directory would
|
|
3910
|
+
// report every tracked file deleted, the audit would refuse, and the case
|
|
3911
|
+
// would pin the false-refusal cousin instead of the false clean — the
|
|
3912
|
+
// strictly less dangerous direction, and the one this script exists to rule
|
|
3913
|
+
// out by name ("clean for a tree nothing looked at").
|
|
3914
|
+
const c = repo(t);
|
|
3915
|
+
const twin = join(c.w, "..", "twin");
|
|
3916
|
+
mkdirSync(twin);
|
|
3917
|
+
for (const f of ["f.txt", "g.txt"]) copyFileSync(join(c.w, f), join(twin, f));
|
|
3918
|
+
writeFileSync(join(c.w, "precious.txt"), "work that exists nowhere else\n");
|
|
3919
|
+
|
|
3920
|
+
// The fixture's own positive control, both directions: without the first the
|
|
3921
|
+
// worktree might never have been dirty, without the second the leak may have
|
|
3922
|
+
// stopped existing and this case would pin nothing either way.
|
|
3923
|
+
assert.equal(git(c.w, "status", "--porcelain"), "?? precious.txt",
|
|
3924
|
+
"fixture: the worktree must really be dirty, or this case measures nothing");
|
|
3925
|
+
assert.equal(
|
|
3926
|
+
execFileSync("git", ["-C", c.w, "status", "--porcelain"], {
|
|
3927
|
+
cwd: c.w, env: { ...ENV, GIT_WORK_TREE: twin }, encoding: "utf8",
|
|
3928
|
+
}),
|
|
3929
|
+
"",
|
|
3930
|
+
"fixture: the ambient GIT_WORK_TREE must really silence that answer, or the leak this pins no longer exists",
|
|
3931
|
+
);
|
|
3932
|
+
|
|
3933
|
+
const r = audit(c, { ...ENV, GIT_WORK_TREE: twin });
|
|
3934
|
+
|
|
3935
|
+
// Exit code first, with stderr as the message, for the reason
|
|
3936
|
+
// release-ticket.test.mjs's own #427 pair records: reaching for a payload
|
|
3937
|
+
// field first turns a refusing run into a null-deref that names nothing,
|
|
3938
|
+
// where this order carries the script's own words.
|
|
3939
|
+
assert.equal(r.status, 1, r.stderr);
|
|
3940
|
+
assert.equal(r.json.clean, false,
|
|
3941
|
+
"an ambient GIT_WORK_TREE must not clear a rebase over uncommitted work — `clean: true` here is the exact failure the --show-prefix gate exists to prevent, reached through a door that gate cannot close");
|
|
3942
|
+
assert.match(r.stderr, /REFUSED — commit the worktree before rebasing/);
|
|
3943
|
+
assert.equal(readFileSync(join(c.w, "precious.txt"), "utf8"), "work that exists nowhere else\n");
|
|
3944
|
+
});
|
|
3945
|
+
|
|
3946
|
+
test("an ambient GIT_DIR does not blame a healthy .git for another repository (#1020)", (t) => {
|
|
3947
|
+
// The false-refusal half, and it cannot use the dirty check as its detector:
|
|
3948
|
+
// GIT_DIR is caught EARLIER, by the linkage check, which compares $wt's
|
|
3949
|
+
// `.git` against the repository git resolved and finds the ambient one
|
|
3950
|
+
// instead. Measured pre-fix: exit 2 naming $wt's own `.git` as the fault, on
|
|
3951
|
+
// a worktree whose linkage is perfect — a safe rebase blocked, and the
|
|
3952
|
+
// operator sent to repair a file that was never broken.
|
|
3953
|
+
const c = repo(t);
|
|
3954
|
+
const other = join(c.w, "..", "other");
|
|
3955
|
+
execFileSync("git", ["clone", "-q", join(c.w, "..", "origin.git"), other], { env: ENV });
|
|
3956
|
+
|
|
3957
|
+
const r = audit(c, { ...ENV, GIT_DIR: join(other, ".git") });
|
|
3958
|
+
|
|
3959
|
+
// Exit code first, stderr as the message: pre-fix the script dies before it
|
|
3960
|
+
// prints any payload, so `r.json.clean` first is a TypeError naming nothing.
|
|
3961
|
+
assert.equal(r.status, 0, r.stderr);
|
|
3962
|
+
assert.equal(r.json.clean, true, "a clean worktree must still audit clean with an unrelated GIT_DIR ambient");
|
|
3963
|
+
assert.equal(r.json.worktree, c.w,
|
|
3964
|
+
"and the payload must describe the worktree the caller named, not the one the environment did");
|
|
3965
|
+
});
|