diff --git a/.agents/skills/verify-lane-schedule/SKILL.md b/.agents/skills/verify-lane-schedule/SKILL.md index 274274da..03c5a74c 100644 --- a/.agents/skills/verify-lane-schedule/SKILL.md +++ b/.agents/skills/verify-lane-schedule/SKILL.md @@ -226,7 +226,7 @@ With every fact TRUE in the code, setting each fact FALSE in the model alone fai ## Coverage record -Sync commit: `33c2441001bb13345d5304a891ca6f16ccf0b24b` (the last commit at which someone confirmed that the model describes this code; only `check.sh --record` moves it). +Sync commit: `a8961d13f4d047b1130ca97dbd8ff07abe291f92` (the last commit at which someone confirmed that the model describes this code; only `check.sh --record` moves it). Every declaration in these files is covered: diff --git a/.agents/skills/verify-lane-schedule/model/fingerprints.txt b/.agents/skills/verify-lane-schedule/model/fingerprints.txt index 579cbdee..5236f5ef 100644 --- a/.agents/skills/verify-lane-schedule/model/fingerprints.txt +++ b/.agents/skills/verify-lane-schedule/model/fingerprints.txt @@ -173,6 +173,7 @@ internal/githttp/hook.go:const_EnvTestMode 0fb6278868ab64de8ed830c27d8012270084a internal/githttp/hook.go:const_EnvUpdateBarrier 6ff404753ce21cb0b7ae4bb9850b3c65c197d7aa44201a9dbbc89f076bd51e31 internal/githttp/hook.go:const_EnvUpdateBarrierEach 30d1db7c45c3f53685529b58faf0f2170bcb1fdd89f6cdecdbbafd8cdafe5387 internal/githttp/hook.go:const_importSyncedRefusal f9c4ddcfa5cfc3723957964e8931f1739395d817bb0d86e2791c9696c93dd41a +internal/githttp/hook.go:const_maxGitRefusedProbes 0b12df63e73be9ef62e9c210390ea316bf9fcc1b87211e7cf60fa3c514b523ce internal/githttp/hook.go:const_maxPushRefDepth 18915f2d5777fe14fc95965757f1cddd8bcaa074f25ddf971982ea6e7d1d9db4 internal/githttp/hook.go:const_maxPushRefNameBytes d0b62ce6ef4e41216d91b71f4a304234de19717a1ac7da561c62009d3a029854 internal/githttp/hook.go:const_maxPushRefSegmentBytes 84c00b544f5aed1b74877f054708eff1fcac2a8a24d221ffa9600854d60bfedb @@ -188,6 +189,7 @@ internal/githttp/hook.go:enforceRefPolicy 2cd2cf6aea243221e8fe27280a6333695d4455 internal/githttp/hook.go:enforceSyncClaimFence efb7b07e54bbb4008ac0cf53778c2ba690ad936051c103421aaf308b52e1cdf9 internal/githttp/hook.go:evalRefPolicy b232fd0a747ced157cec1bc4588e53bc13c54a39ff829f45108ae62c9601b5c8 internal/githttp/hook.go:gitRefSegmentValid be11ac7ba983b399f85ec59e9ac524d9d95329fe7c774f011934bf97b1b26cf7 +internal/githttp/hook.go:gitRefusalProbe.reaches 938ddcf9757e6b985bb3662f50b9fba53f51125d2d343ee8e0fff13ad530c065 internal/githttp/hook.go:hiddenOnRemote 91f396bdce763dd700bf642823435bfc3cec17f641d83f07af33f96ceacbe254 internal/githttp/hook.go:historyIntact b38332684e10b27880f57f3e4611858550bf615f33a5613fdb0af59c75b70770 internal/githttp/hook.go:import_"bufio" c8716a546ce61a4869563c666058adbec5f4783581ea4f74788f87ec1f0731f1 @@ -222,6 +224,7 @@ internal/githttp/hook.go:isBranchTip 4b841bc86abf0876e093b19da54ff7e1fda6011f3b3 internal/githttp/hook.go:loadRefPolicies e974fac4024c3534aaa1c55514851744f5b1999bf8a503573a7530130e140c63 internal/githttp/hook.go:looksLikeSSHPublicKey 0dfb41f2c68c29f7bd43dc0630624e292599c1da3b168d3ba9eade2349706e53 internal/githttp/hook.go:mustEnv f003f721f345a1dd8d73bf4b0c6f95e97157cab55714e6b922b10859e4c9c6b8 +internal/githttp/hook.go:newGitRefusalProbe 2c8f044c7fdb1d98ae3a941702951fab72596e708fff64397e85fd7a607ef21d internal/githttp/hook.go:newSigVerifier a555514bf11dbd3caaded84b9cca58ef7a8b7a83e39b24bfda507edcbb5a446a internal/githttp/hook.go:normalizeQuarantineKeep 08fe44518b58336b37472b2c027b85ee48b39b335a0f5d311bcf72e6e29675fb internal/githttp/hook.go:objectExists a9ba9ba7c3d15548a14c9ef9c56571831f9518ca386c0009ecd24b03373ab1b9 @@ -231,11 +234,12 @@ internal/githttp/hook.go:parseRefTxCommitted 8471c50ec96eec8e6db39370335fbb30116 internal/githttp/hook.go:pauseAtUpdateBarrier a57f4482fb6742f6146ca93452fc9e003a70fd9bab1147a87bb4b32b465859c0 internal/githttp/hook.go:postReceiveHook 1f060af8c07a0c11ab59f6a26f444d7648ddfa9fac7bb506346e808a72311261 internal/githttp/hook.go:preReceive 472bccca1a374479cedb08f228f17893a2f20c1b971761085d5bdf0c60f35ba0 -internal/githttp/hook.go:preReceivePolicyGate c95e8f2792be33cb9ebf77a71d13662a6296dfe42edc4368bca10d8e466cdd60 +internal/githttp/hook.go:preReceivePolicyGate ca36486a30be47ace72b37aa7c77fec418a88d92a1a36951caf6d2a88a1ec4fd internal/githttp/hook.go:pushCommandUpdatable 7ff2f47180eec838d8c98da58a9836bc69a8b9ee1ac9d0f9270ee816594c04ba internal/githttp/hook.go:pushRefDeletable 33c0ee3ab6b1f4a8886faa66aa3db0a25efcb8407667c33d5c4fa1b3a5de3a94 internal/githttp/hook.go:pushRefPathTooLong 93ab890b939c3afe6a0e7d177327109d6aa988cb24a51bc8dff8d26546cfcbee internal/githttp/hook.go:pushRefUpdatable 4f8b9c8242b42701e22e8cbec49dd3e9e458d0cd1a2ba38393c3a38517da2fe9 +internal/githttp/hook.go:reachesUpdateHook 5370101391a75e2e20512ffaf0e7573a93f20842b5eab67e936e576e47b74d7f internal/githttp/hook.go:referenceTransactionHook 50b0df140f1b52aed35700b5036f9a3d6513077107112e8275d801041fc63139 internal/githttp/hook.go:refuseSyncedImport 636fa8d40bbc1bfb508cdee46f3a80bffe7589f6b32494c1eddfede0850df0fe internal/githttp/hook.go:reservationVerdicts e149d8f3b071db14f7a96349dd982da8d48f07fd8384528b0a16cf4ec69573cf @@ -245,6 +249,7 @@ internal/githttp/hook.go:sigVerifier.verifyCommit e169a962f642d67522cc48b9cd4916 internal/githttp/hook.go:testMode 9056ef1dbdfa8f6875d87e4272e7d18f29c722a37755a607dd4e3dae1c4f790e internal/githttp/hook.go:type_casDropStore 06047f65ba2b2821b24756ed0226f105f16bc6329d5146740837508a34f017dc internal/githttp/hook.go:type_dfConflictProber 04652c08a0fb8fe305ed46a44368d76b7ee70a2b034912ddacfc6db8d8acde8a +internal/githttp/hook.go:type_gitRefusalProbe 0f1622f30b1981f51d3eecbb1bf64f22b40a7e7ec0fbe637250a9bcfcfcb49a1 internal/githttp/hook.go:type_refCommand e037686129d2cb179e4c3a64f0ea09d26c1b12957eb5b3f7d0fcdf695832a1ef internal/githttp/hook.go:type_refPolicyMemo 7f1604056f494e4a6eaa2d453b94f924641594c8edd53a6daeec1f591b9ba04e internal/githttp/hook.go:type_refValuesProber 0389595e3fb341c7e67784d27674d922ddfcca2e3ae993cdba85ea0c8613a665 diff --git a/docs/explanation/wire-compatibility.md b/docs/explanation/wire-compatibility.md index 9277264c..05b15002 100644 --- a/docs/explanation/wire-compatibility.md +++ b/docs/explanation/wire-compatibility.md @@ -125,12 +125,17 @@ Beyond the smart-HTTP wire format, the semantics clients depend on match git: fail independently, as in git, unless the server declines the whole push. It does so for its own limits, such as the pack quota, and in one case for refused refs. Refused refs are policy-denied refs, refs hidden on this remote, branches that the push sets to an - object that is not a commit, and names that a push cannot create + object that is not a commit, refs that git itself refuses before the update hook (the + push leaves their objects incomplete, or it is a shallow update that git does not + allow), and names that a push cannot create or update (for example, a name with only one level under `refs/`, such as `refs/funny` or `refs/heads`, another git-invalid name, a name with more than 32 `/`-separated parts, a part over 200 bytes, or a name over 900 bytes). When no other ref of the push needs the objects that the push sends (only refused refs need them, or the other refs move to commits the server already has), the server declines the whole push and stores no pack. + The server asks git about at most eight distinct refused targets in one push. A push with + more than eight of them, or on a server whose git is older than 2.39, stores its pack as + before, and git still refuses each of those refs. A push that only deletes refs sends no objects and, within the server's own limits, keeps per-ref results. - **Documented deviation:** refs advance through per-ref compare-and-swap on the ref diff --git a/internal/githttp/hook.go b/internal/githttp/hook.go index adc2ffba..20a1da04 100644 --- a/internal/githttp/hook.go +++ b/internal/githttp/hook.go @@ -1474,13 +1474,18 @@ func atomicPushDFCheck(commands []refCommand) error { } // preReceivePolicyGate refuses pack publication unless it is justified: -// either the push is wholly policy-allowed (vestigial packs from creates at -// existing SHAs stay accepted, matching the no-pack path), or at least one -// policy-ALLOWED command needs quarantined objects (its target is not in -// the main object store yet). A push containing any denied command whose -// allowed commands all target already-stored objects is refused whole — -// its pack could only serve the denied refs (round-7 decoy) — with a -// message telling the client to push the allowed updates separately. It +// either no command is refused (vestigial packs from creates at existing +// SHAs stay accepted, matching the no-pack path), or at least one command +// that policy allows and git lets reach the update hook needs quarantined +// objects (its target is not in the main object store yet). A push +// containing any denied command, or any command that git refuses before the +// update hook (reachesUpdateHook), whose other commands all target +// already-stored objects is refused whole — +// its pack could only serve the denied refs (round-7 decoy) — and a policy +// denial's message tells the client to push the allowed updates separately. +// The checks of reachesUpdateHook are asked once for each distinct target and +// at most maxGitRefusedProbes times for targets that fail (gitRefusalProbe), so +// a push with many incomplete commands costs a bounded number of git children. It // reuses the shared verdicts (enforceRefPolicy, plus the refusals of hidden // refs and of names the server cannot store) — the quarantine is visible to // pre-receive via git's alternates, so is-ancestor and verify-sig checks @@ -1521,6 +1526,10 @@ func preReceivePolicyGate(commands []refCommand, verdicts []error, atomicPush bo assertf(len(verdicts) == len(commands), "preReceivePolicyGate: %d verdicts for %d commands", len(verdicts), len(commands)) var firstErr error needsPack := false + // The first command that git refuses before the update hook runs (see + // reachesUpdateHook): it can never land, so its pack is no reason to publish. + gitRefused := "" + probe := newGitRefusalProbe(reachesUpdateHook) for i, c := range commands { // Verdicts are the caller's single shared policy evaluation // (Codex round-4 P2 on #173: re-evaluating here double-charged @@ -1537,12 +1546,21 @@ func preReceivePolicyGate(commands []refCommand, verdicts []error, atomicPush bo continue } if c.new != ref.ZeroSHA && !objectInMainStore(c.new) { - needsPack = true + switch { + case needsPack: + case probe.reaches(c.new): + needsPack = true + case gitRefused == "": + gitRefused = c.ref + } } } - if firstErr == nil || needsPack { + if needsPack || (firstErr == nil && gitRefused == "") { return nil } + if firstErr == nil { + return refusef("pre-receive: the quarantined pack is needed only by ref updates that git refuses (%s: its objects are incomplete, or the update is a shallow update), refusing pack publication", gitRefused) + } return fmt.Errorf("pre-receive: the quarantined pack is needed only by policy-denied ref updates, refusing pack publication (push the allowed updates separately): %w", firstErr) } @@ -2917,6 +2935,72 @@ func objectExists(sha string) bool { panic(fmt.Sprintf("githttp assertion failed: cat-file -e %s: %v", sha, err)) } +// maxGitRefusedProbes bounds the connectivity checks that one push may spend +// on commands that fail them. Each check walks the new objects of one command, +// and a writer can send a push with hundreds of commands that name incomplete +// histories. A push that has more distinct failing targets than this publishes +// its pack as it did before the check existed (see gitRefusalProbe). +const maxGitRefusedProbes = 8 + +// gitRefusalProbe asks reach once for each distinct target, and stops asking +// once maxGitRefusedProbes targets have failed. A target that it did not ask +// about reads as reaching the update hook: the gate then publishes, as it did +// before the check, because a quota-counted junk pack costs less than a wrong +// refusal of a push that git accepts. The gate stops asking at the first target +// that passes, so one push spends at most maxGitRefusedProbes checks. +type gitRefusalProbe struct { + reach func(sha string) bool + known map[string]bool + refused int +} + +func newGitRefusalProbe(reach func(sha string) bool) *gitRefusalProbe { + return &gitRefusalProbe{reach: reach, known: map[string]bool{}} +} + +func (p *gitRefusalProbe) reaches(sha string) bool { + if r, ok := p.known[sha]; ok { + return r + } + if p.refused >= maxGitRefusedProbes { + return true + } + r := p.reach(sha) + p.known[sha] = r + if !r { + p.refused++ + } + return r +} + +// reachesUpdateHook reports whether git's own connectivity check passes for sha. +// receive-pack runs one such check over all commands before it runs any hook +// (rev-list --objects --stdin --not --all, with the hidden refs excluded, over +// the quarantine and the main store), and when it fails, it checks the commands +// one by one and refuses the ones that fail ("missing necessary objects"). A +// shallow update that git does not allow is skipped, and the pack that carries +// it lacks the parents of its commits, so this check fails for it as well. git +// still runs pre-receive for such a command, but the update hook never runs for +// it, so no ref can ever point into a pack that only it needs. An exit of 128 +// is the answer "no": git cannot read the objects that the command needs. Any +// other status says nothing about the command (a git without --exclude-hidden, +// before 2.39, answers 129; a signal or an out-of-memory kill ends it too), so +// the answer is "yes" and the gate publishes as it did before: a wrong refusal +// of a push that git accepts is worse than one junk pack. A git that cannot +// start is a bug, which fails fast like objectExists. +func reachesUpdateHook(sha string) bool { + cmd := exec.Command("git", "rev-list", "--objects", "--stdin", "--not", "--exclude-hidden=receive", "--all", "--quiet", "--alternate-refs") + cmd.Stdin = strings.NewReader(sha + "\n") + err := cmd.Run() + if err == nil { + return true + } + if ee, ok := errors.AsType[*exec.ExitError](err); ok { + return ee.ExitCode() != 128 + } + panic(fmt.Sprintf("githttp assertion failed: connectivity rev-list %s: %v", sha, err)) +} + // objectInMainStore reports whether sha is resolvable WITHOUT the // quarantine. git runs pre-receive with GIT_OBJECT_DIRECTORY pointed at the // quarantine and the main store as an alternate (githooks(5)); stripping diff --git a/internal/githttp/noreplace_hook_test.go b/internal/githttp/noreplace_hook_test.go index 5c421d55..f769ad3e 100644 --- a/internal/githttp/noreplace_hook_test.go +++ b/internal/githttp/noreplace_hook_test.go @@ -24,7 +24,7 @@ func TestHookObjectChecksKeepNoReplaceObjects(t *testing.T) { kinds, err := objectKinds([]string{id}) return err == nil && kinds[id] == "commit" } - for name, check := range map[string]func(string) bool{"objectExists": objectExists, "objectInMainStore": objectInMainStore, "objectKinds": objectIsCommit} { + for name, check := range map[string]func(string) bool{"objectExists": objectExists, "objectInMainStore": objectInMainStore, "objectKinds": objectIsCommit, "reachesUpdateHook": reachesUpdateHook} { _ = os.Remove(log) if !check(strings.Repeat("a", 40)) { t.Fatalf("%s = false for a git that exits 0", name) diff --git a/internal/githttp/refused_push_pack_e2e_test.go b/internal/githttp/refused_push_pack_e2e_test.go new file mode 100644 index 00000000..9fa03225 --- /dev/null +++ b/internal/githttp/refused_push_pack_e2e_test.go @@ -0,0 +1,247 @@ +package githttp + +// Regression for junk pack rows. git refuses some commands BEFORE the +// update hook (missing objects, shallow update not allowed) yet still hands +// them to pre-receive. Such a command can never land, so the publication +// gate (preReceivePolicyGate) must not store a pack that only it needs: a +// registered pack that no ref reaches is never swept, counts against the +// quota, and every repack carries it forward. + +import ( + "bytes" + "os" + "os/exec" + "path/filepath" + "strconv" + "strings" + "testing" + + "github.com/coder/openstorage/internal/ref" +) + +// refusedGit runs git in dir with stdin and returns its raw stdout. +func refusedGit(t *testing.T, dir, stdin string, args ...string) []byte { + t.Helper() + cmd := exec.Command("git", append(append([]string{}, gitIdentity...), args...)...) + cmd.Dir = dir + cmd.Stdin = strings.NewReader(stdin) + var stderr bytes.Buffer + cmd.Stderr = &stderr + out, err := cmd.Output() + if err != nil { + t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, stderr.String()) + } + return out +} + +// assertOnlySetupPack fails unless repo holds exactly want packs, the ones +// its setup push stored, as rows and as blobs. +func assertOnlySetupPack(t *testing.T, repo string, want int) { + t.Helper() + if packs := pgPacks(t, repo); len(packs) != want { + t.Errorf("registered packs = %d, want %d: a push git refused registered its pack", len(packs), want) + } + if objs := s3Packs(t, repo); len(objs) != want { + t.Errorf("stored pack blobs = %d, want %d: a push git refused uploaded its pack", len(objs), want) + } +} + +func TestPackPublication_GitRefusedCommandStoresNothing(t *testing.T) { + t.Run("shallow push", func(t *testing.T) { + src := initWorkRepo(t) + commitFile(t, src, "b.txt", "two\n", "two", nil) + shallow := filepath.Join(t.TempDir(), "shallow") + mustGit(t, "", "clone", "-q", "--depth", "1", "file://"+src, shallow) + repo := registerRepo(t, "refused-shallow") + token := mintTestToken(t, repo, "git:read,git:write", "") + out, err := tryGit(shallow, "push", remoteURL(nodeA, repo, token), "HEAD:refs/heads/main") + if err == nil || !strings.Contains(out, "shallow update not allowed") { + t.Fatalf("want git's shallow refusal, got err=%v:\n%s", err, out) + } + assertOnlySetupPack(t, repo, 0) + }) + + t.Run("many refs at one incomplete commit", func(t *testing.T) { + // A writer can name one incomplete commit with hundreds of refs. The + // gate asks git once for the commit, and refuses the push. + repo, work, token, stored := refusedRawSetup(t, "refused-many") + bad := refusedBadCommits(t, work, stored, 1) + resp := refusedRawPush(t, repo, token, work, bad, 200) + if !strings.Contains(resp, "needed only by ref updates that git refuses") { + t.Fatalf("the pusher does not read the refusal:\n%.600q", resp) + } + assertOnlySetupPack(t, repo, 1) + }) + + t.Run("more distinct incomplete commits than the probe budget", func(t *testing.T) { + // The gate spends at most maxGitRefusedProbes failing checks. Past that + // it publishes as it did before the check, and git still refuses each ref. + repo, work, token, stored := refusedRawSetup(t, "refused-distinct") + bad := refusedBadCommits(t, work, stored, maxGitRefusedProbes+1) + resp := refusedRawPush(t, repo, token, work, bad, 1) + if !strings.Contains(resp, "ng refs/heads/x0") || strings.Contains(resp, "ok refs/heads/x") { + t.Fatalf("git must refuse every ref:\n%.600q", resp) + } + assertOnlySetupPack(t, repo, 2) + }) + + t.Run("within the probe budget", func(t *testing.T) { + repo, work, token, stored := refusedRawSetup(t, "refused-budget") + bad := refusedBadCommits(t, work, stored, maxGitRefusedProbes) + refusedRawPush(t, repo, token, work, bad, 1) + assertOnlySetupPack(t, repo, 1) + }) + + for _, tc := range []struct { + name string + decoy bool // also create refs/heads/y at the stored commit + }{ + {"commit with a missing tree", false}, + {"beside a create at a stored commit", true}, + } { + t.Run(tc.name, func(t *testing.T) { + repo := registerRepo(t, "refused-raw") + work := initWorkRepo(t) + token := mintTestToken(t, repo, "git:read,git:write", "") + mustGit(t, work, "push", remoteURL(nodeA, repo, token), "HEAD:refs/heads/main") + stored := revParse(t, work, "HEAD") + junk := strings.TrimSpace(string(refusedGit(t, work, strings.Repeat("junk ", 500), "hash-object", "-w", "--stdin"))) + bad := strings.TrimSpace(string(refusedGit(t, work, "tree "+strings.Repeat("1", 40)+"\nparent "+stored+ + "\nauthor a 1700000000 +0000\ncommitter a 1700000000 +0000\n\nmissing tree\n", + "hash-object", "-t", "commit", "-w", "--literally", "--stdin"))) + pack := refusedGit(t, work, bad+"\n"+junk+"\n", "pack-objects", "--stdout", "-q") + + var body bytes.Buffer + caps := "\x00report-status side-band-64k" + if tc.decoy { + body.WriteString(pktLine(ref.ZeroSHA + " " + stored + " refs/heads/y" + caps + "\n")) + caps = "" + } + body.WriteString(pktLine(ref.ZeroSHA + " " + bad + " refs/heads/x" + caps + "\n")) + body.WriteString("0000") + body.Write(pack) + _, resp := postReceivePack(t, nodeA, repo, token, bytes.NewReader(body.Bytes()), false) + if !strings.Contains(resp, "ng refs/heads/x") { + t.Fatalf("git accepted refs/heads/x:\n%q", resp) + } + // The pusher reads why nothing was stored when the whole push is refused. + if !strings.Contains(resp, "needed only by ref updates that git refuses") { + t.Fatalf("the pusher does not read the refusal:\n%q", resp) + } + assertOnlySetupPack(t, repo, 1) + }) + } +} + +// reachesUpdateHook answers "no" only when git exits 128, the status of a +// connectivity check that cannot read an object. A git that does not know an +// option (129), a signal and an out-of-memory kill say nothing about the +// command, so the gate must not refuse a push on them. +func TestReachesUpdateHookRefusesOnlyOnExit128(t *testing.T) { + for _, tc := range []struct { + status string + script string // the body of the git stand-in + want bool + }{ + {"0", "exit 0", true}, {"1", "exit 1", true}, {"128", "exit 128", false}, + {"129", "exit 129", true}, {"137", "exit 137", true}, + // A real signal reaches Go as exit code -1, not 137. + {"signal", "kill -9 $$", true}, + } { + t.Run("exit "+tc.status, func(t *testing.T) { + bin := t.TempDir() + script := "#!/bin/sh\n" + tc.script + "\n" + if err := os.WriteFile(filepath.Join(bin, "git"), []byte(script), 0o755); err != nil { + t.Fatal(err) + } + t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH")) + if got := reachesUpdateHook(strings.Repeat("a", 40)); got != tc.want { + t.Fatalf("reachesUpdateHook with a git that exits %s = %v, want %v", tc.status, got, tc.want) + } + }) + } +} + +// refusedRawSetup stores one commit through a real push and returns the +// repository, the work tree, the token and the stored commit. +func refusedRawSetup(t *testing.T, name string) (repo, work, token, stored string) { + t.Helper() + repo = registerRepo(t, name) + work = initWorkRepo(t) + token = mintTestToken(t, repo, "git:read,git:write", "") + mustGit(t, work, "push", remoteURL(nodeA, repo, token), "HEAD:refs/heads/main") + return repo, work, token, revParse(t, work, "HEAD") +} + +// refusedBadCommits writes n distinct commits whose tree no store holds. +func refusedBadCommits(t *testing.T, work, parent string, n int) []string { + t.Helper() + var ids []string + for i := range n { + ids = append(ids, strings.TrimSpace(string(refusedGit(t, work, "tree "+strings.Repeat("1", 40)+"\nparent "+parent+ + "\nauthor a 1700000000 +0000\ncommitter a 1700000000 +0000\n\nmissing tree "+strconv.Itoa(i)+"\n", + "hash-object", "-t", "commit", "-w", "--literally", "--stdin")))) + } + return ids +} + +// refusedRawPush sends, in one raw receive-pack request, perCommit creates of +// refs/heads/x- at each commit in bad, with a pack that holds only those +// commits. It returns git's response. +func refusedRawPush(t *testing.T, repo, token, work string, bad []string, perCommit int) string { + t.Helper() + var body bytes.Buffer + caps := "\x00report-status side-band-64k" + for i, id := range bad { + for j := range perCommit { + name := "refs/heads/x" + strconv.Itoa(i) + if perCommit > 1 { + name += "-" + strconv.Itoa(j) + } + body.WriteString(pktLine(ref.ZeroSHA + " " + id + " " + name + caps + "\n")) + caps = "" + } + } + body.WriteString("0000") + body.Write(refusedGit(t, work, strings.Join(bad, "\n")+"\n", "pack-objects", "--stdout", "-q")) + _, resp := postReceivePack(t, nodeA, repo, token, bytes.NewReader(body.Bytes()), false) + return resp +} + +// The probe asks once for each distinct target and stops asking after +// maxGitRefusedProbes failures. +func TestGitRefusalProbeBoundsTheChecks(t *testing.T) { + // The docs say eight, so the value is pinned here and in the docs together. + if maxGitRefusedProbes != 8 { + t.Fatalf("maxGitRefusedProbes = %d, want 8: docs/explanation/wire-compatibility.md says eight", maxGitRefusedProbes) + } + var asked []string + probe := newGitRefusalProbe(func(sha string) bool { asked = append(asked, sha); return false }) + for range 1000 { + if probe.reaches("a") { + t.Fatal("a target that failed reads as reaching the update hook") + } + } + if len(asked) != 1 { + t.Fatalf("1000 commands at one target asked git %d times, want 1", len(asked)) + } + asked = nil + probe = newGitRefusalProbe(func(sha string) bool { asked = append(asked, sha); return false }) + var reached int + for i := range 1000 { + if probe.reaches(strconv.Itoa(i)) { + reached++ + } + } + if len(asked) != maxGitRefusedProbes || reached != 1000-maxGitRefusedProbes { + t.Fatalf("1000 distinct failing targets asked git %d times (%d read as reaching), want %d and %d", + len(asked), reached, maxGitRefusedProbes, 1000-maxGitRefusedProbes) + } + // A target that passes is remembered and never costs a failure. + asked = nil + probe = newGitRefusalProbe(func(sha string) bool { asked = append(asked, sha); return sha == "ok" }) + first, again := probe.reaches("ok"), probe.reaches("ok") + if !first || !again || len(asked) != 1 { + t.Fatalf("a passing target asked git %d times (answers %v, %v), want 1 and true, true", len(asked), first, again) + } +}