GitHubivarvong

ply

main
ply6 git calls89 ms35 KiB4 cachedprotocol v2
518 lines48 KiBRaw
1// Rebase / squash / cherry-pick / revert, decided by git: per-commit trees equal `git rebase` in a clone, authors and
2// messages preserved, committer ours; cherry-pick and revert trees equal `git cherry-pick` / `git revert`; conflicts where
3// git conflicts; the branch update is a CAS on the old tip.
4import { test, beforeAll as before, afterAll as after } from 'vitest';
5import assert from 'node:assert/strict';
6import { join } from 'node:path';
7import { mkdirSync, writeFileSync, rmSync, mkdtempSync } from 'node:fs';
8import { spawnSync } from 'node:child_process';
9import { tmpdir } from 'node:os';
10import { git, tmpRoot, makeBareRepo, startGitServer, commitFiles, fsckProblems, initRepo } from './server.js';
11import { rebaseBranch, cherryPick, revertCommit, linearChain, carriedExtra, squashMessage } from '../src/rebase.js';
12import { mergeBranch } from '../src/merge.js';
13import { editFile } from '../src/edit.js';
14import { generateSigner } from '../src/sign.js';
15import { Tracer, arrayExporter, flattenLine } from '../src/otel.js';
16import { oracleRediff } from './gitversion.js';
17import { unwrap, unwrapAsync } from './unwrap.js';
18const rediff = oracleRediff();
20let root, cleanup, srv, n = 0;
21const committer = { name: 'rebaser', email: 'rebaser@x' };
22before(async () => { ({ root, cleanup } = tmpRoot()); srv = await startGitServer(root); });
23after(async () => { await srv.close(); cleanup(); });
25const ENV = { ...process.env, GIT_AUTHOR_NAME: 'w', GIT_AUTHOR_EMAIL: 'w@x', GIT_COMMITTER_NAME: 'w', GIT_COMMITTER_EMAIL: 'w@x', GIT_AUTHOR_DATE: '1700000000 +0000', GIT_COMMITTER_DATE: '1700000000 +0000', GIT_CONFIG_COUNT: '1', GIT_CONFIG_KEY_0: 'commit.gpgsign', GIT_CONFIG_VALUE_0: 'false' };
26const sh = (cwd, args, input) => spawnSync('git', ['-C', cwd, ...args], { encoding: 'utf8', env: ENV, input });
27const ok = r => { assert.equal(r.status, 0, r.stderr); return r.stdout.trim(); };
28const repo = () => { const name = `r${n++}`; const bare = makeBareRepo(root, name); const work = join(root, `w${name}`); mkdirSync(work); initRepo(work); return { name, bare, work, url: `${srv.url}/${name}.git` }; };
29const commitOn = (r, branch, from, files, msg) => {
30 let start = from; if (!/^[0-9a-f]{40}/.test(from)) { ok(sh(r.work, ['fetch', '-q', r.bare, `refs/heads/${from}`])); start = 'FETCH_HEAD'; }
31 ok(sh(r.work, ['checkout', '-q', '-B', branch, start]));
32 for (const [p, v] of Object.entries(files)) { if (v === null) { rmSync(join(r.work, p), { force: true }); continue; } mkdirSync(join(r.work, p, '..'), { recursive: true }); writeFileSync(join(r.work, p), v); }
33 ok(sh(r.work, ['add', '-A'])); ok(sh(r.work, ['commit', '-q', '-m', msg])); ok(sh(r.work, ['push', '-q', '-f', r.bare, `HEAD:refs/heads/${branch}`]));
34 return ok(sh(r.work, ['rev-parse', 'HEAD']));
35};
36const clone = bare => { const d = mkdtempSync(join(tmpdir(), 'rb-')); ok(sh(d, ['clone', '-q', bare, '.'])); return d; };
37const treeOf = (cwd, rev) => ok(sh(cwd, ['rev-parse', `${rev}^{tree}`]));
38const log = (cwd, range) => ok(sh(cwd, ['log', '--format=%T|%an|%ae|%s', '--reverse', range])).split('\n').filter(Boolean);
39const T = () => ({ tracer: new Tracer(), rediff });
41test('pure: linearChain and carriedExtra', () => {
42 const c = new Map([['3', { parents: ['2'], tree: 't3' }], ['2', { parents: ['1'], tree: 't2' }], ['1', { parents: ['0'], tree: 't1' }], ['m', { parents: ['3', 'x'], tree: 'tm' }]]);
43 assert.deepEqual(unwrap(linearChain(c, '3', '0')).map(x => x.oid), ['1', '2', '3']);
44 assert.deepEqual(unwrap(linearChain(c, '3', '3')), []);
45 assert.throws(() => unwrap(linearChain(c, 'm', '0')), e => e.code === 'not_linear');
46 assert.deepEqual(carriedExtra(['encoding utf8', 'gpgsig -----BEGIN', ' abc', ' def', 'x y']), ['encoding utf8', 'x y']);
47});
49test('rebase: three commits onto a moved main — trees, authors, messages equal git rebase; committer is ours; CAS on the old tip', async () => {
50 const r = repo();
51 const base = commitFiles(r.work, r.bare, { 'a.txt': 'a\n', 'd/x.txt': 'x\n' }, 'base');
52 commitOn(r, 'feat', 'main', { 'b.txt': 'b\n' }, 'feat 1');
53 commitOn(r, 'feat', 'feat', { 'd/x.txt': 'x-feat\n' }, 'feat 2');
54 const tip = commitOn(r, 'feat', 'feat', { 'c.txt': 'c\n', 'b.txt': null }, 'feat 3: drop b');
55 const mainNow = commitOn(r, 'main', 'main', { 'a.txt': 'a-main\n', 'e.txt': 'e\n' }, 'main moved');
56 // git's answer
57 const d = clone(r.bare); ok(sh(d, ['checkout', '-q', 'feat'])); ok(sh(d, ['rebase', '-q', 'origin/main'])); const gitLog = log(d, 'origin/main..HEAD'); rmSync(d, { recursive: true, force: true });
58 const ex = arrayExporter();
59 const res = await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/feat', onto: 'refs/heads/main', committer }, { tracer: new Tracer({ exporter: ex }) }));
60 assert.equal(res.outcome, 'rebase'); assert.equal(res.commits, 3); assert.equal(res.oldTip, tip); assert.equal(res.onto, mainNow); assert.equal(res.base, base);
61 assert.equal(ok(sh(r.bare, ['rev-parse', 'feat'])), res.oid);
62 const ours = log(r.bare, `main..feat`);
63 assert.deepEqual(ours, gitLog, 'tree|author|email|subject per commit == git rebase');
64 assert.equal(ok(sh(r.bare, ['log', '-1', '--format=%cn <%ce>', 'feat'])), 'rebaser <rebaser@x>');
65 assert.equal(ok(sh(r.bare, ['rev-parse', 'feat~3'])), mainNow, 'replayed on top of main');
66 assert.deepEqual(fsckProblems(r.bare), []);
67 const rootSpan = ex.lines.flatMap(flattenLine).find(s => s.name === 'git.rebase'); assert.equal(rootSpan.attrs['git.rebase.commits'], 3);
68 // up to date now; and a stale view of the tip is refused by the CAS
69 assert.equal((await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/feat', onto: 'refs/heads/main', committer }, T()))).outcome, 'up_to_date');
70 // land it: fast-forward
71 const m = await unwrapAsync(mergeBranch(r.url, { ref: 'refs/heads/main', from: 'refs/heads/feat', author: committer }, T()));
72 assert.equal(m.outcome, 'fast_forward'); assert.equal(ok(sh(r.bare, ['rev-parse', 'main'])), res.oid);
73 assert.equal(ok(sh(r.bare, ['cat-file', '-p', 'main:d/x.txt'])), 'x-feat'); assert.equal(sh(r.bare, ['cat-file', '-e', 'main:b.txt']).status !== 0, true);
74});
76test('rebase: conflict where git conflicts; branch untouched; squash produces one commit with the merged tree; branch behind → fast-forward', async () => {
77 const r = repo();
78 commitFiles(r.work, r.bare, { 'a.txt': 'a\n' }, 'base');
79 commitOn(r, 'feat', 'main', { 'a.txt': 'a-feat\n', 'b.txt': 'b\n' }, 'feat');
80 const tip = commitOn(r, 'feat', 'feat', { 'c.txt': 'c\n' }, 'feat 2');
81 commitOn(r, 'main', 'main', { 'a.txt': 'a-main\n' }, 'main');
82 const d = clone(r.bare); ok(sh(d, ['checkout', '-q', 'feat'])); assert.notEqual(sh(d, ['rebase', '-q', 'origin/main']).status, 0, 'git conflicts too'); rmSync(d, { recursive: true, force: true });
83 await assert.rejects(unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/feat', onto: 'refs/heads/main', committer }, T())), e => e.code === 'merge_conflict' && e.conflicts[0] === 'a.txt' && e.replayed.length === 0);
84 assert.equal(ok(sh(r.bare, ['rev-parse', 'feat'])), tip, 'untouched');
85 // resolve on main's side by taking feat's a.txt, then squash
86 commitOn(r, 'main', 'main', { 'a.txt': 'a-feat\n' }, 'main takes feat a');
87 const d2 = clone(r.bare); ok(sh(d2, ['checkout', '-q', 'feat'])); ok(sh(d2, ['rebase', '-q', 'origin/main'])); const gitTree = treeOf(d2, 'HEAD'); rmSync(d2, { recursive: true, force: true });
88 const s = await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/feat', onto: 'refs/heads/main', committer, squash: true, message: 'feat squashed' }, T()));
89 assert.equal(s.outcome, 'squash'); assert.equal(treeOf(r.bare, 'feat'), gitTree, 'squash tree == git rebase tip tree');
90 assert.equal(ok(sh(r.bare, ['rev-list', '--count', 'main..feat'])), '1'); assert.equal(ok(sh(r.bare, ['log', '-1', '--format=%s', 'feat'])), 'feat squashed');
91 // a branch strictly behind main: rebase makes it equal main
92 ok(sh(r.work, ['push', '-q', r.bare, `${ok(sh(r.bare, ['rev-parse', 'main~1']))}:refs/heads/old`]));
93 const b = await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/old', onto: 'refs/heads/main', committer }, T()));
94 assert.equal(b.outcome, 'fast_forward'); assert.equal(ok(sh(r.bare, ['rev-parse', 'old'])), ok(sh(r.bare, ['rev-parse', 'main'])));
95 assert.deepEqual(fsckProblems(r.bare), []);
96});
98test('rebase and squash when the merge base sits exactly at the first window\'s cut (8 commits above both tips): equal to git, not a TypeError', async () => {
99 // mergeBase names the root through its children on both sides without ever fetching it (depth 8 holds the 8 commits
100 // above it); rebase read commits.get(base).tree → "Cannot read properties of undefined" on an honest server (#41).
101 // 7 a side: the base is inside the window (the control); 8: it is exactly at the cut.
102 for (const n of [7, 8]) for (const squash of [false, true]) {
103 const r = repo();
104 const base = commitFiles(r.work, r.bare, { 'f.txt': 'root\n' }, 'root');
105 let tip = base, main = base;
106 for (let i = 1; i <= n; i++) { tip = commitOn(r, 'feat', tip, { [`t${i}.txt`]: `${i}\n` }, `feat ${i}`); main = commitOn(r, 'main', main, { [`m${i}.txt`]: `${i}\n` }, `main ${i}`); }
107 const d = clone(r.bare); ok(sh(d, ['checkout', '-q', 'feat'])); ok(sh(d, ['rebase', '-q', 'origin/main'])); const gitTree = treeOf(d, 'HEAD'); const gitLog = log(d, 'origin/main..HEAD'); rmSync(d, { recursive: true, force: true });
108 const res = await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/feat', onto: 'refs/heads/main', committer, squash }, T()));
109 assert.equal(res.outcome, squash ? 'squash' : 'rebase', `n=${n}`); assert.equal(res.base, base); assert.equal(res.commits, n);
110 assert.equal(ok(sh(r.bare, ['rev-parse', 'feat'])), res.oid);
111 assert.equal(treeOf(r.bare, 'feat'), gitTree, `${squash ? 'squash' : 'rebase'}: tip tree == git rebase`);
112 if (!squash) assert.deepEqual(log(r.bare, 'main..feat').map(l => l.split('|')[0]), gitLog.map(l => l.split('|')[0]), 'every replayed tree == git');
113 assert.deepEqual(fsckProblems(r.bare), []);
114 }
115});
117test('cherry-pick and revert: trees equal git; signature not carried; signed result verifies', async () => {
118 const r = repo();
119 commitFiles(r.work, r.bare, { 'a.txt': 'a\n', 'b.txt': 'b\n' }, 'base');
120 const fix = commitOn(r, 'feat', 'main', { 'b.txt': 'b-fixed\n', 'new.txt': 'n\n' }, 'the fix');
121 commitOn(r, 'feat', 'feat', { 'a.txt': 'unrelated\n' }, 'unrelated');
122 commitOn(r, 'main', 'main', { 'c.txt': 'c\n' }, 'main moved');
123 const d = clone(r.bare); ok(sh(d, ['cherry-pick', fix])); const gitPick = treeOf(d, 'HEAD'); ok(sh(d, ['revert', '--no-edit', 'HEAD'])); const gitRevert = treeOf(d, 'HEAD'); rmSync(d, { recursive: true, force: true });
124 const { signer } = await generateSigner();
125 const p = await unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/main', commit: fix, committer }, { tracer: new Tracer(), signer }));
126 assert.equal(p.outcome, 'ok'); assert.equal(p.tree, gitPick); assert.equal(ok(sh(r.bare, ['rev-parse', 'main'])), p.oid);
127 assert.equal(ok(sh(r.bare, ['log', '-1', '--format=%an|%s', 'main'])), 'w|the fix', 'author and message kept');
128 assert.match(ok(sh(r.bare, ['cat-file', 'commit', p.oid])), /gpgsig -----BEGIN SSH SIGNATURE/);
129 const rv = await unwrapAsync(revertCommit(r.url, { ref: 'refs/heads/main', commit: p.oid, committer }, T()));
130 assert.equal(rv.outcome, 'ok'); assert.equal(rv.tree, gitRevert);
131 assert.match(ok(sh(r.bare, ['log', '-1', '--format=%s', 'main'])), /^Revert "the fix"/);
132 assert.equal((await unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/main', commit: p.oid, committer }, T()))).outcome, 'ok', 'picking the fix again after its revert applies again');
133 assert.equal(treeOf(r.bare, 'main'), gitPick);
134 // picking a commit whose change is already present is empty
135 assert.equal((await unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/main', commit: fix, committer }, T()))).outcome, 'empty');
136 // conflict: pick a commit that rewrote a.txt onto a main that also rewrote it
137 const clash = commitOn(r, 'feat', 'feat', { 'a.txt': 'feat-a\n' }, 'clash');
138 commitOn(r, 'main', 'main', { 'a.txt': 'main-a\n' }, 'main a');
139 await assert.rejects(unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/main', commit: clash, committer }, T())), e => e.code === 'merge_conflict' && e.conflicts.includes('a.txt'));
140 assert.deepEqual(fsckProblems(r.bare), []);
141});
143test('property: line-level edits to the same files on both sides — rebase and cherry-pick equal git, or conflict where git does', async () => {
144 const { Rng } = await import('./fuzz/prng.js');
145 const rng = new Rng(+(process.env.REBASE_SEED ?? 7)); const N = +(process.env.REBASE_CASES ?? 12);
146 const stats = { rebased: 0, conflict: 0, conservative: 0, picked: 0 };
147 for (let i = 0; i < N; i++) {
148 const r = repo();
149 const lines = p => Array.from({ length: 10 }, (_, j) => (j % 4 === 3 ? '\n' : `${p}:${j}\n`));
150 const files = { 'a.txt': lines('a').join(''), 'src/b.txt': lines('b').join('') };
151 commitFiles(r.work, r.bare, files, 'base');
152 const cur = { main: { ...files }, feat: { ...files } };
153 const step = (br, from, msg) => { const ch = {}; const p = rng.pick(Object.keys(files)); const ls = cur[br][p].split(/(?<=\n)/); const at = rng.int(0, ls.length - 1);
154 const k = rng.int(0, 2); if (k === 0) ls[at] = `${br}${i}.${rng.int(0, 99)}\n`; else if (k === 1) ls.splice(at, 1); else ls.splice(at, 0, rng.int(0, 1) ? '\n' : `${br} ins ${rng.int(0, 99)}\n`);
155 ch[p] = cur[br][p] = ls.join(''); return commitOn(r, br, from, ch, msg); };
156 const f1 = step('feat', 'main', 'feat 1'); ok(sh(r.work, ['push', '-q', r.bare, `${f1}:refs/heads/keep`])); step('feat', 'feat', 'feat 2'); step('feat', 'feat', 'feat 3');
157 step('main', 'main', 'main 1'); step('main', 'main', 'main 2');
158 // rebase
159 const d = clone(r.bare); ok(sh(d, ['checkout', '-q', 'feat'])); const gr = sh(d, ['rebase', '-q', 'origin/main']);
160 const gitLog = gr.status === 0 ? log(d, 'origin/main..HEAD') : null; rmSync(d, { recursive: true, force: true });
161 let res = null, err = null; try { res = await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/feat', onto: 'refs/heads/main', committer }, T())); } catch (e) { if (e.code !== 'merge_conflict') { if (process.env.REBASE_DEBUG) console.log(i, e.hookOutput, e.report); throw e; } err = e; }
162 if (res) { assert.ok(gitLog, `case ${i}: we rebased cleanly but git conflicts`); assert.deepEqual(log(r.bare, 'main..feat'), gitLog, `case ${i}: per-commit trees == git rebase`); stats.rebased++; }
163 else if (gitLog) stats.conservative++; else stats.conflict++;
164 // cherry-pick the first feat commit onto main
165 const d2 = clone(r.bare); const gp = sh(d2, ['cherry-pick', f1]); const gitPick = gp.status === 0 ? treeOf(d2, 'HEAD') : null; const gitEmpty = gp.status !== 0 && /empty/.test(gp.stderr + gp.stdout); rmSync(d2, { recursive: true, force: true });
166 let p = null; try { p = await unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/main', commit: f1, committer }, T())); } catch (e) { if (e.code !== 'merge_conflict') throw e; }
167 if (p?.outcome === 'ok') { assert.equal(p.tree, gitPick, `case ${i}: cherry-pick tree == git`); stats.picked++; }
168 else if (p?.outcome === 'empty') assert.ok(gitEmpty, `case ${i}: we say empty, git does not`);
169 else if (gitPick) stats.conservative++;
170 assert.deepEqual(fsckProblems(r.bare), []);
171 }
172 console.log(` ${N} cases: ${JSON.stringify(stats)}`);
173 assert.ok(stats.rebased > 0 && stats.picked > 0, 'line merges exercised');
174 assert.equal(stats.conservative, 0, 'no conservative conflicts on plain line edits');
175});
177test('attributes in cherry-pick and rebase: \'ours\' is the tree being built on, as a worktree cherry-pick/rebase reads it', async () => {
178 const r = repo();
179 const L = n => Array.from({ length: n }, (_, i) => `entry ${i}\n`).join('');
180 commitFiles(r.work, r.bare, { 'CHANGELOG': L(3) }, 'base');
181 const pick = commitOn(r, 'feat', 'main', { 'CHANGELOG': L(3) + 'feat entry\n' }, 'feat entry');
182 commitOn(r, 'main', 'main', { 'CHANGELOG': L(3) + 'main entry\n', '.gitattributes': 'CHANGELOG merge=union\n' }, 'main entry + union');
183 // git, in a worktree on main (attributes from the checkout = ours)
184 const d = clone(r.bare); const gp = sh(d, ['cherry-pick', pick]); assert.equal(gp.status, 0, gp.stderr); const gitTree = treeOf(d, 'HEAD'); rmSync(d, { recursive: true, force: true });
185 await assert.rejects(unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/main', commit: pick, committer }, T())), e => e.code === 'merge_conflict', 'without attributes the appends clash');
186 const p = await unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/main', commit: pick, committer }, { ...T(), attributes: 'ours' }));
187 assert.equal(p.tree, gitTree, 'union cherry-pick tree == git cherry-pick');
188 // rebase: feat (one commit appending) onto main, attributes from the tree being built on
189 const r2 = repo();
190 commitFiles(r2.work, r2.bare, { 'CHANGELOG': L(3), '.gitattributes': 'CHANGELOG merge=union\n' }, 'base');
191 commitOn(r2, 'feat', 'main', { 'CHANGELOG': L(3) + 'feat 1\n' }, 'f1');
192 commitOn(r2, 'feat', 'feat', { 'CHANGELOG': L(3) + 'feat 1\nfeat 2\n' }, 'f2');
193 commitOn(r2, 'main', 'main', { 'CHANGELOG': L(3) + 'main 1\n' }, 'm1');
194 const d2 = clone(r2.bare); ok(sh(d2, ['checkout', '-q', 'feat'])); const gr = sh(d2, ['rebase', '-q', 'origin/main']); assert.equal(gr.status, 0, gr.stderr); const gitLog = log(d2, 'origin/main..HEAD'); rmSync(d2, { recursive: true, force: true });
195 const rb = await unwrapAsync(rebaseBranch(r2.url, { ref: 'refs/heads/feat', onto: 'refs/heads/main', committer }, { ...T(), attributes: 'ours' }));
196 assert.equal(rb.outcome, 'rebase'); assert.deepEqual(log(r2.bare, 'main..feat'), gitLog, 'per-commit trees == git rebase with union');
197});
199test('end to end: edit on a branch, main moves, rebase, fast-forward land, delete — all git operations', async () => {
200 const r = repo();
201 commitFiles(r.work, r.bare, { 'README.md': 'hello MARK\n', 'other.txt': 'o\n' }, 'base');
202 const e = await unwrapAsync(editFile(r.url, { path: 'README.md', find: 'MARK', replace: 'WORLD', ref: 'refs/heads/ply/x', base: 'refs/heads/main', author: committer, message: 'edit' }, T()));
203 commitOn(r, 'main', 'main', { 'other.txt': 'o2\n' }, 'main moved');
204 const rb = await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/ply/x', onto: 'refs/heads/main', committer }, T()));
205 assert.equal(rb.outcome, 'rebase'); assert.equal(rb.commits, 1);
206 const m = await unwrapAsync(mergeBranch(r.url, { ref: 'refs/heads/main', from: 'refs/heads/ply/x', author: committer }, T()));
207 assert.equal(m.outcome, 'fast_forward');
208 assert.equal(ok(sh(r.bare, ['cat-file', '-p', 'main:README.md'])), 'hello WORLD'); assert.equal(ok(sh(r.bare, ['cat-file', '-p', 'main:other.txt'])), 'o2');
209 assert.equal(ok(sh(r.bare, ['log', '--format=%s', '-3', 'main'])).split('\n').join('|'), 'edit|main moved|base', 'linear history');
210 assert.deepEqual(fsckProblems(r.bare), []);
211});
213// The default squash message is built from commit messages, which whoever can push to the branch writes.
214test('squash message: exact values worked by hand; interior blank lines are the commit\'s own and stay', () => {
215 // each message loses only its trailing newlines; null and all-newline messages contribute nothing; one blank line
216 // between the rest; one final newline
217 assert.equal(squashMessage(['a\n', 'b\n\n\n', null, '\n\n', '', 'c']), 'a\n\nb\n\nc\n');
218 assert.equal(squashMessage(['subject\n\nbody 1\n\n\n\nbody 2\n']), 'subject\n\nbody 1\n\n\n\nbody 2\n');
219 assert.equal(squashMessage(['\n\nlead\n', ' \n']), '\n\nlead\n\n \n');
220 assert.equal(squashMessage([]), '\n'); assert.equal(squashMessage([null, '\n']), '\n');
221 assert.equal(squashMessage(['x\r\n']), 'x\r\n', 'only LF is stripped: CR LF keeps its CR, then one LF is added');
222});
224test('squash message: time is linear in the message (64x the newlines costs under 300x the time, best of 5)', () => {
225 // 'feat' + N newlines + 'tail' + N newlines: the interior run is where `\n+$` went quadratic (the engine retries the
226 // run from every newline in it), the trailing run is the work a linear strip does. A quadratic step costs 4096x.
227 const msg = n => `feat${'\n'.repeat(n)}tail${'\n'.repeat(n)}`;
228 // by hand: each of the two copies keeps 'feat' + n newlines + 'tail' (n + 8 chars), '\n\n' between, '\n' after
229 const best = (n) => { const m = msg(n); let b = Infinity; for (let i = 0; i < 5; i++) { const t0 = performance.now(); const out = squashMessage([m, m]); b = Math.min(b, performance.now() - t0); assert.equal(out.length, 2 * (n + 8) + 3); } return b; };
230 for (let i = 0; i < 50; i++) squashMessage([msg(200), msg(200)]); // warm the JIT on a size neither measurement uses
231 const ts = best(500); const tb = best(32_000);
232 assert.ok(tb < 300 * ts, `64x the input cost ${(tb / ts).toFixed(0)}x the time (${ts.toFixed(4)} ms -> ${tb.toFixed(4)} ms)`);
233});
235test('limits: maxRebaseCommits governs a rebase above maxMergeDepth too: a chain of exactly the limit rebases as git does, one more is refused as the rebase chain', async () => {
236 const { DEFAULT_LIMITS } = await import('../src/limits.js');
237 const r = repo();
238 commitFiles(r.work, r.bare, { 'a.txt': 'a\n' }, 'base');
239 for (let i = 1; i <= 13; i++) commitOn(r, 'feat', i === 1 ? 'main' : 'feat', { [`f${i}.txt`]: `${i}\n` }, `feat ${i}`);
240 ok(sh(r.work, ['push', '-q', r.bare, 'HEAD~1:refs/heads/feat12'])); // the same branch without its last commit: 12 above the base
241 const tip13 = ok(sh(r.bare, ['rev-parse', 'feat']));
242 commitOn(r, 'main', 'main', { 'e.txt': 'e\n' }, 'main moved');
243 // git's answer: it rebases both; 12 and 13 are the counts of commits made above
244 const d = clone(r.bare);
245 ok(sh(d, ['checkout', '-q', 'feat'])); ok(sh(d, ['rebase', '-q', 'origin/main'])); assert.equal(ok(sh(d, ['rev-list', '--count', 'origin/main..HEAD'])), '13');
246 ok(sh(d, ['checkout', '-q', 'feat12'])); ok(sh(d, ['rebase', '-q', 'origin/main'])); const gitLog = log(d, 'origin/main..HEAD'); rmSync(d, { recursive: true, force: true });
247 assert.equal(gitLog.length, 12);
248 const refused = (ref, limits, what, got, max) => assert.rejects(unwrapAsync(rebaseBranch(r.url, { ref, onto: 'refs/heads/main', committer }, { ...T(), limits: { ...DEFAULT_LIMITS, ...limits } })), e => { assert.deepEqual({ code: e.code, what: e.what, got: e.got, max: e.max }, { code: 'limit_exceeded', what, got, max }); return true; });
249 // the rebase limit below, at and above the merge depth: each time it is the bound that fires, one commit over
250 await refused('refs/heads/feat', { maxMergeDepth: 64, maxRebaseCommits: 12 }, 'rebase chain', 13, 12);
251 await refused('refs/heads/feat', { maxMergeDepth: 12, maxRebaseCommits: 12 }, 'rebase chain', 13, 12);
252 await refused('refs/heads/feat', { maxMergeDepth: 4, maxRebaseCommits: 12 }, 'rebase chain', 13, 12);
253 assert.equal(ok(sh(r.bare, ['rev-parse', 'feat'])), tip13, 'a refused rebase leaves the branch');
254 const res = await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/feat12', onto: 'refs/heads/main', committer }, { ...T(), limits: { ...DEFAULT_LIMITS, maxMergeDepth: 4, maxRebaseCommits: 12 } }));
255 assert.equal(res.outcome, 'rebase'); assert.equal(res.commits, 12);
256 assert.deepEqual(log(r.bare, 'main..feat12'), gitLog, 'tree, author and subject of every replayed commit == git rebase');
257 assert.deepEqual(fsckProblems(r.bare), []);
258});
260// A stale short branch in a long linear history: 40 commits, a 3-commit branch on the 40th, main 20 commits further.
261// The base is 21 commits from onto, past maxMergeDepth 8, and the branch's own history (43 commits) is longer than
262// the branch walk (maxRebaseCommits + 2). Both walks end cut and share no commit, so nothing shows the chain to be
263// over its limit: git counts 3. The refusal is the merge-base search's, with the fields that search has always
264// reported for this repository (max: the caller's maxMergeDepth; got: the doubling it would have needed next, 2 x 8).
265test('limits: a short branch whose onto is further than maxMergeDepth is refused by maxMergeDepth, never as a rebase chain it does not have; raising the limit named rebases it as git does', async () => {
266 const { DEFAULT_LIMITS } = await import('../src/limits.js');
267 const r = repo();
268 const L = []; let at = 1_700_000_000; let mark = 0;
269 const commit = (ref, from, path, body) => { mark++; L.push(`commit ${ref}`, `mark :${mark}`, `committer s <s@x> ${at++} +0000`, 'data 1', 'c', ...(from ? [`from :${from}`] : []), `M 100644 inline ${path}`, `data ${body.length}`, body); return mark; };
270 let base = 0; for (let i = 1; i <= 40; i++) base = commit('refs/heads/main', base, 'a.txt', `a${i}`);
271 let prev = base; for (let i = 1; i <= 3; i++) prev = commit('refs/heads/short', prev, 's.txt', `${i}`);
272 prev = base; for (let i = 1; i <= 20; i++) prev = commit('refs/heads/main', prev, 'm.txt', `${i}`);
273 prev = base; for (let i = 1; i <= 10; i++) prev = commit('refs/heads/long', prev, 'l.txt', `${i}`);
274 commit('refs/heads/main1', base, 'm.txt', '1');
275 ok(sh(r.bare, ['fast-import', '--quiet'], `${L.join('\n')}\n`));
276 const tip = ok(sh(r.bare, ['rev-parse', 'short']));
277 // git: the chain is 3 commits, onto is 20 past the base, the branch's history is 43 commits
278 assert.equal(ok(sh(r.bare, ['rev-list', '--count', 'main..short'])), '3');
279 assert.equal(ok(sh(r.bare, ['rev-list', '--count', 'short..main'])), '20');
280 assert.equal(ok(sh(r.bare, ['rev-list', '--count', 'short'])), '43');
281 const run = async (limits) => { const e = await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/short', onto: 'refs/heads/main', committer }, { ...T(), limits: { ...DEFAULT_LIMITS, ...limits } })).then(() => null, x => x); return { code: e?.code, what: e?.what, got: e?.got, max: e?.max }; };
282 // the chain limit above the chain (12), exactly the chain (3), and far above the history (5000): never the limit named
283 for (const maxRebaseCommits of [12, 3, 5000]) assert.deepEqual(await run({ maxMergeDepth: 8, maxRebaseCommits }), { code: 'limit_exceeded', what: 'merge-base search depth', got: 16, max: 8 }, `maxRebaseCommits ${maxRebaseCommits}`);
284 assert.equal(ok(sh(r.bare, ['rev-parse', 'short'])), tip, 'a refused rebase leaves the branch');
285 // following the error (a merge depth that reaches the base, 21 from onto) rebases the 3 commits; the tree is git's
286 const d = clone(r.bare); ok(sh(d, ['checkout', '-q', 'short'])); ok(sh(d, ['rebase', '-q', 'origin/main'])); const gitTree = treeOf(d, 'HEAD'); rmSync(d, { recursive: true, force: true });
287 const res = await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/short', onto: 'refs/heads/main', committer }, { ...T(), limits: { ...DEFAULT_LIMITS, maxMergeDepth: 32, maxRebaseCommits: 12 } }));
288 assert.equal(res.outcome, 'rebase'); assert.equal(res.commits, 3); assert.equal(treeOf(r.bare, 'short'), gitTree);
289 // The other side of the same cut: a chain that is over its limit (git counts 10, the limit is 4) on an onto whose
290 // history is cut at maxMergeDepth 16. The base is 11 commits from the tip: inside a merge-base search of 16, past a
291 // branch walk of only maxRebaseCommits + 2 = 6. The branch is walked as deep as that search, so the base is found
292 // and the limit named is the chain's (got = max + 1, the documented one-past), as before the walks were separated.
293 assert.equal(ok(sh(r.bare, ['rev-list', '--count', 'main1..long'])), '10');
294 const over = await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/long', onto: 'refs/heads/main1', committer }, { ...T(), limits: { ...DEFAULT_LIMITS, maxMergeDepth: 16, maxRebaseCommits: 4 } })).then(() => null, x => x);
295 assert.deepEqual({ code: over?.code, what: over?.what, got: over?.got, max: over?.max }, { code: 'limit_exceeded', what: 'rebase chain', got: 5, max: 4 });
296});
298// What a refusal costs in requests where onto's history is deeper than maxMergeDepth, so the onto walk is not whole
299// after one request (as it is in the tests below, whose onto history is two commits): 10,100 linear commits, a
300// 10,001-commit branch on the last of them, onto one commit past it. Bounds by hand, from the walks (both double
301// from 8 to their cap):
302// the merge-base search alone, what a refusal cost before the chain limit governed it: ls-refs + two walks at each
303// of the 8 depths 8, 16, ... 1024 = 17
304// WORKER_LIMITS: the branch walk is max(256 + 2, 1024) = 1024 deep, the same 8 depths: 1 + 8 + 8 = 17, no more
305// DEFAULT_LIMITS: the branch walk goes on at 2048, 4096, 8192 and 10,002 (12 depths), the onto walk stops at 1024
306// (8 depths): 1 + 12 + 8 = 21, four more (accepted; README, Limits)
307test('limits: refusing an over-long chain on a deep onto history costs no more requests than the merge-base search under WORKER_LIMITS (17), and 26 under DEFAULT_LIMITS (17 + 9 windows)', async () => {
308 const { DEFAULT_LIMITS, WORKER_LIMITS } = await import('../src/limits.js');
309 const r = repo();
310 const L = []; let at = 1_700_000_000; let mark = 0;
311 const commit = (ref, from, path, body) => { mark++; L.push(`commit ${ref}`, `mark :${mark}`, `committer s <s@x> ${at++} +0000`, 'data 1', 'c', ...(from ? [`from :${from}`] : []), `M 100644 inline ${path}`, `data ${body.length}`, body); return mark; };
312 let base = 0; for (let i = 1; i <= 10_100; i++) base = commit('refs/heads/main', base, 'a.txt', `a${i}`);
313 let prev = base; for (let i = 1; i <= 10_001; i++) prev = commit('refs/heads/long', prev, 'l.txt', `${i}`);
314 commit('refs/heads/main', base, 'm.txt', '1');
315 ok(sh(r.bare, ['fast-import', '--quiet'], `${L.join('\n')}\n`));
316 // git: the chain is 10,001 commits (over both presets' limits), and onto's history is deeper than maxMergeDepth
317 assert.equal(ok(sh(r.bare, ['rev-list', '--count', 'main..long'])), '10001');
318 assert.equal(ok(sh(r.bare, ['rev-list', '--count', 'main'])), '10101');
319 assert.ok(DEFAULT_LIMITS.maxMergeDepth < 10_101 && WORKER_LIMITS.maxMergeDepth < 10_101);
320 const run = async (limits) => { const c = counting(); const e = await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/long', onto: 'refs/heads/main', committer }, { ...T(), fetchImpl: c.fetchImpl, limits })).then(() => null, x => x); return { code: e?.code, what: e?.what, got: e?.got, max: e?.max, requests: c.n }; };
321 // WORKER_LIMITS: the base is 10,002 commits from the tip, past a walk of 1024, and the onto walk is cut, so the
322 // chain is not known to be over 256: the refusal is the merge-base search's, with its fields
323 const w = await run(WORKER_LIMITS);
324 assert.deepEqual({ code: w.code, what: w.what, got: w.got, max: w.max }, { code: 'limit_exceeded', what: 'merge-base search depth', got: 2048, max: 1024 });
325 assert.ok(w.requests <= 17, `WORKER_LIMITS refusal took ${w.requests} requests`);
326 // DEFAULT_LIMITS: the branch walk reaches the base at 10,002 commits; the chain is one over 10,000. Requests, by hand:
327 // the merge-base search to maxMergeDepth 1024 as under WORKER_LIMITS (17), then the branch walk past it in windows
328 // of 1024 from the cut frontier at position 1025 to the base at 10,002: ceil((10002 - 1024) / 1024) = 9; 17 + 9 = 26
329 const d = await run(DEFAULT_LIMITS);
330 assert.deepEqual({ code: d.code, what: d.what, got: d.got, max: d.max }, { code: 'limit_exceeded', what: 'rebase chain', got: 10_001, max: 10_000 });
331 assert.ok(d.requests <= 26, `DEFAULT_LIMITS refusal took ${d.requests} requests`);
332 console.log(`refusing a 10,001-commit chain on a 10,101-commit onto history: WORKER_LIMITS ${w.requests} requests, DEFAULT_LIMITS ${d.requests}`);
333}, 120_000);
335// git rebase leaves out a commit whose patch id equals an upstream commit's before it replays anything (its default,
336// --no-reapply-cherry-picks). ply has no patch ids: it replays every commit and drops the ones that come out empty.
337// The two agree while the upstream copy is in place and differ once upstream has reverted it; this pins both, each
338// against the git command it equals, so the day patch ids are ported the second half changes knowingly.
339test('rebase of a commit whose patch is already upstream: dropped as git rebase drops it while the copy stands; after an upstream revert ply is git rebase --reapply-cherry-picks', async () => {
340 const r = repo();
341 commitFiles(r.work, r.bare, { f: 'base\n' }, 'base');
342 const t1 = commitOn(r, 'topic', 'main', { f: 'base\nfeature\n' }, 'T1');
343 ok(sh(r.work, ['push', '-q', r.bare, `${t1}:refs/heads/topic2`]));
344 commitOn(r, 'main', 'main', { g: 'g\n' }, 'unrelated');
345 ok(sh(r.work, ['cherry-pick', t1])); ok(sh(r.work, ['push', '-q', r.bare, 'HEAD:refs/heads/main']));
346 assert.notEqual(ok(sh(r.work, ['rev-parse', 'HEAD'])), t1, 'the upstream copy is another commit, so the branch is not simply behind');
347 const gitRebase = (branch, flags) => { const d = clone(r.bare); ok(sh(d, ['checkout', '-q', branch])); ok(sh(d, ['rebase', '-q', ...flags, 'origin/main'])); const out = { tree: treeOf(d, 'HEAD'), commits: log(d, 'origin/main..HEAD') }; rmSync(d, { recursive: true, force: true }); return out; };
348 // the copy stands: git rewrites nothing and the branch is main
349 const g1 = gitRebase('topic', []); assert.deepEqual(g1.commits, []);
350 const p1 = await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/topic', onto: 'refs/heads/main', committer }, T()));
351 assert.equal(p1.outcome, 'rebase'); assert.deepEqual(p1.dropped, [t1]); assert.deepEqual(p1.replayed, []);
352 assert.equal(treeOf(r.bare, 'topic'), g1.tree); assert.deepEqual(log(r.bare, 'main..topic'), g1.commits); assert.equal(p1.oid, ok(sh(r.bare, ['rev-parse', 'main'])));
353 // upstream reverts the copy and moves on
354 ok(sh(r.work, ['revert', '--no-edit', 'HEAD'])); ok(sh(r.work, ['push', '-q', r.bare, 'HEAD:refs/heads/main'])); commitOn(r, 'main', 'main', { h: 'h\n' }, 'later');
355 const gDefault = gitRebase('topic2', []); const gReapply = gitRebase('topic2', ['--reapply-cherry-picks']);
356 assert.deepEqual(gDefault.commits, [], 'git by default: T1 is left out (its patch id is upstream)'); assert.equal(gReapply.commits.length, 1);
357 const p2 = await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/topic2', onto: 'refs/heads/main', committer }, T()));
358 assert.deepEqual(p2.dropped, []); assert.deepEqual(p2.replayed.map(x => x.from), [t1]);
359 assert.equal(treeOf(r.bare, 'topic2'), gReapply.tree); assert.deepEqual(log(r.bare, 'main..topic2'), gReapply.commits);
360 assert.notEqual(gReapply.tree, gDefault.tree, 'the known difference from git\'s default (README: rebase)');
361 assert.deepEqual(fsckProblems(r.bare), []);
362});
364test('squash message equals the one git rebase -i (pick, squash, squash) writes, byte for byte', async () => {
365 const r = repo();
366 commitFiles(r.work, r.bare, { 'a.txt': 'a\n' }, 'base');
367 commitOn(r, 'feat', 'main', { 'b.txt': 'b\n' }, 'feat 1: subject only');
368 commitOn(r, 'feat', 'feat', { 'c.txt': 'c\n' }, 'feat 2: with a body\n\nfirst paragraph\nsecond line\n\nsecond paragraph');
369 commitOn(r, 'feat', 'feat', { 'd.txt': 'd\n' }, 'feat 3');
370 commitOn(r, 'main', 'main', { 'e.txt': 'e\n' }, 'main moved');
371 // git's answer: squash every commit after the first into it; the editor accepts the prepared message as it is
372 const d = clone(r.bare); ok(sh(d, ['checkout', '-q', 'feat']));
373 const rb = spawnSync('git', ['-C', d, 'rebase', '-q', '-i', 'origin/main'], { encoding: 'utf8', env: { ...ENV, GIT_SEQUENCE_EDITOR: 'sed -i.bak -e "2,\\$s/^pick/squash/"', GIT_EDITOR: 'true' } });
374 assert.equal(rb.status, 0, rb.stderr); assert.equal(ok(sh(d, ['rev-list', '--count', 'origin/main..HEAD'])), '1');
375 const body = (cwd, rev) => { const raw = sh(cwd, ['cat-file', 'commit', rev]); assert.equal(raw.status, 0, raw.stderr); return raw.stdout.slice(raw.stdout.indexOf('\n\n') + 2); };
376 const gitMessage = body(d, 'HEAD'); const gitTree = treeOf(d, 'HEAD'); rmSync(d, { recursive: true, force: true });
377 assert.equal(gitMessage, 'feat 1: subject only\n\nfeat 2: with a body\n\nfirst paragraph\nsecond line\n\nsecond paragraph\n\nfeat 3\n', 'what git writes (pinned so a git that words it otherwise is noticed)');
378 const s = await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/feat', onto: 'refs/heads/main', committer, squash: true }, T()));
379 assert.equal(s.outcome, 'squash'); assert.equal(body(r.bare, 'feat'), gitMessage); assert.equal(treeOf(r.bare, 'feat'), gitTree);
380});
382// A linear branch of `n` commits above a one-commit base, and main moved one commit, by fast-import (commitOn costs
383// four git processes a commit). Each branch commit rewrites one file in a directory, so a replay reads the commit's
384// root tree and that directory's tree.
385const longBranch = (r, n) => {
386 const L = []; let at = 1_700_000_000;
387 const commit = (ref, mark, msg, from, path, body) => [`commit ${ref}`, `mark :${mark}`, `committer s <s@x> ${at++} +0000`, `data ${msg.length}`, msg, ...(from ? [`from :${from}`] : []), `M 100644 inline ${path}`, `data ${body.length}`, body];
388 L.push(...commit('refs/heads/main', 1, 'base', 0, 'a.txt', 'a'));
389 for (let i = 2; i <= n + 1; i++) L.push(...commit('refs/heads/feat', i, `f${i}`, i - 1, 'd/f.txt', `${i}`));
390 L.push(...commit('refs/heads/main', n + 2, 'moved', 1, 'e.txt', 'e'));
391 ok(sh(r.bare, ['fast-import', '--quiet'], `${L.join('\n')}\n`));
392};
393// the transport's requests, counted where they leave: every HTTP request of the operation
394const counting = () => { const c = { n: 0, fetchImpl: (u, i) => { c.n++; return fetch(u, i); } }; return c; };
396test('limits: a chain over maxRebaseCommits is refused as the rebase chain (max the limit, got one past it) however far over, at no more requests than the merge-base search cost before; maxMergeDepth still bounds the onto walk', async () => {
397 const { DEFAULT_LIMITS } = await import('../src/limits.js');
398 const r = repo(); longBranch(r, 30);
399 const tip = ok(sh(r.bare, ['rev-parse', 'feat']));
400 assert.equal(ok(sh(r.bare, ['rev-list', '--count', 'main..feat'])), '30'); // git: the chain is 30 commits
401 const run = async (limits) => { const c = counting(); const e = await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/feat', onto: 'refs/heads/main', committer }, { ...T(), fetchImpl: c.fetchImpl, limits: { ...DEFAULT_LIMITS, ...limits } })).then(() => null, x => x); return { code: e?.code, what: e?.what, got: e?.got, max: e?.max, requests: c.n }; };
402 // ten over the limit, the merge depth below, at and above it: the chain limit is the one named, and max is the
403 // caller's own number. Requests, by hand: ls-refs, the branch walk at 8, 16 and 22 (the limit + 2), and the onto
404 // walk once (main is two commits: whole at the first depth). Before: ls-refs + two walks at each of 8, 16, 22 = 7.
405 for (const maxMergeDepth of [16, 20, 1024]) assert.deepEqual(await run({ maxMergeDepth, maxRebaseCommits: 20 }), { code: 'limit_exceeded', what: 'rebase chain', got: 21, max: 20, requests: 5 }, `maxMergeDepth ${maxMergeDepth}`);
406 // maxMergeDepth 0 allows no merge-base search: refused after ls-refs alone, as mergeBranch is
407 assert.deepEqual(await run({ maxMergeDepth: 0, maxRebaseCommits: 40 }), { code: 'limit_exceeded', what: 'merge-base search depth', got: 1, max: 0, requests: 1 });
408 assert.equal(ok(sh(r.bare, ['rev-parse', 'feat'])), tip, 'a refused rebase leaves the branch');
409});
411test('limits: WORKER_LIMITS.maxRebaseCommits, the cheapest shape of chain (every commit changes one file onto did not touch): at the limit the rebase equals git and takes at most 800 requests; one commit over is refused as the rebase chain', async () => {
412 const { WORKER_LIMITS } = await import('../src/limits.js');
413 const N = WORKER_LIMITS.maxRebaseCommits;
414 assert.ok(N <= 800, `WORKER_LIMITS.maxRebaseCommits is ${N}: a replay costs at least one request a commit`);
415 const r = repo(); longBranch(r, N + 1);
416 ok(sh(r.bare, ['update-ref', 'refs/heads/atlimit', 'feat~1']));
417 assert.equal(ok(sh(r.bare, ['rev-list', '--count', 'main..atlimit'])), String(N));
418 const over = counting();
419 const e = await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/feat', onto: 'refs/heads/main', committer }, { ...T(), fetchImpl: over.fetchImpl, limits: WORKER_LIMITS })).then(() => null, x => x);
420 assert.deepEqual({ code: e?.code, what: e?.what, got: e?.got, max: e?.max }, { code: 'limit_exceeded', what: 'rebase chain', got: N + 1, max: N });
421 // 17: ls-refs + two walks at each of 8 depths (8 … 1024), what the search cost before the chain limit governed it
422 assert.ok(over.n <= 17, `refusing took ${over.n} requests`);
423 // git's answer for the chain at the limit
424 const d = clone(r.bare); ok(sh(d, ['checkout', '-q', 'atlimit'])); ok(sh(d, ['rebase', '-q', 'origin/main'])); const gitTree = treeOf(d, 'HEAD'); const gitCount = ok(sh(d, ['rev-list', '--count', 'origin/main..HEAD'])); rmSync(d, { recursive: true, force: true });
425 const at = counting();
426 const res = await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/atlimit', onto: 'refs/heads/main', committer }, { ...T(), fetchImpl: at.fetchImpl, limits: WORKER_LIMITS }));
427 assert.equal(res.outcome, 'rebase'); assert.equal(String(res.commits), gitCount); assert.equal(treeOf(r.bare, 'atlimit'), gitTree);
428 // a Worker request may make 1,000 subrequests (standard plan); 800 leaves the caller room for its own
429 assert.ok(at.n <= 800, `a ${N}-commit rebase took ${at.n} requests`);
430 console.log(`WORKER_LIMITS rebase: ${N} commits, ${at.n} requests; refusing ${N + 1}: ${over.n} requests`);
431}, 300_000);
433// The windowed branch walk (rebaseBaseOf, capA > 2 * maxMergeDepth) at a size worked by hand: maxMergeDepth 4 and
434// maxRebaseCommits 20 put the branch cap at 22 and the windows at 4 commits. A fast-import graph: `main` is `base`
435// plus `mainLen` commits; `feat` is `n` single-parent commits on `base` (or on a new root when unrelated), with a merge
436// of a side commit at `mergeAt` commits above its foot when given.
437const windowedGraph = (r, { n, mainLen = 1, mergeAt = 0, unrelated = false }) => {
438 const L = []; let at = 1_700_000_000; let mark = 0;
439 const commit = (ref, from, path, body, merge = 0) => { mark++; L.push(`commit ${ref}`, `mark :${mark}`, `committer s <s@x> ${at++} +0000`, 'data 1', 'c', ...(from ? [`from :${from}`] : []), ...(merge ? [`merge :${merge}`] : []), `M 100644 inline ${path}`, `data ${body.length}`, body); return mark; };
440 const base = commit('refs/heads/main', 0, 'a.txt', 'a');
441 let m = base; for (let i = 1; i <= mainLen; i++) m = commit('refs/heads/main', m, 'm.txt', `${i}`);
442 const side = mergeAt ? commit('refs/heads/side', base, 's.txt', 's') : 0;
443 if (unrelated) L.push('reset refs/heads/feat');
444 let f = unrelated ? 0 : base; for (let i = 1; i <= n; i++) f = commit('refs/heads/feat', f, 'f.txt', `${i}`, i === mergeAt ? side : 0);
445 ok(sh(r.bare, ['fast-import', '--quiet'], `${L.join('\n')}\n`));
446};
447const W = { maxMergeDepth: 4, maxRebaseCommits: 20 };
448const windowedRun = async (r, limits, fetchImpl) => {
449 const { DEFAULT_LIMITS } = await import('../src/limits.js');
450 const c = counting(); const f = fetchImpl ? fetchImpl(c) : c.fetchImpl;
451 const e = await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/feat', onto: 'refs/heads/main', committer }, { ...T(), fetchImpl: f, limits: { ...DEFAULT_LIMITS, ...limits } })).then(() => null, x => x);
452 return { code: e?.code ?? null, what: e?.what, got: e?.got, max: e?.max, requests: c.n };
453};
455test('limits: windowed branch walk, cap reached: onto whole is the rebase chain, onto cut is the merge-base search depth; requests by hand', async () => {
456 // ls-refs 1; the search at depth 8 walks both sides to maxMergeDepth 4 (2); windows of 4 from positions 5, 9, 13,
457 // 17 and 21 (the last cut to capA - 21 + 1 = 2): 5; position 23 is past capA 22. 1 + 2 + 5 = 8.
458 const whole = repo(); windowedGraph(whole, { n: 30 });
459 assert.equal(ok(sh(whole.bare, ['rev-list', '--count', 'main..feat'])), '30');
460 assert.deepEqual(await windowedRun(whole, W), { code: 'limit_exceeded', what: 'rebase chain', got: 21, max: 20, requests: 8 });
461 // onto is base + 10: cut at 4, so the chain is not known to be over; the same 8 requests
462 const cut = repo(); windowedGraph(cut, { n: 30, mainLen: 10 });
463 assert.deepEqual(await windowedRun(cut, W), { code: 'limit_exceeded', what: 'merge-base search depth', got: 8, max: 4, requests: 8 });
464 // a chain of 21 reaches base (in onto) at position 22 = capA: hit, 21 > 20. Windows at 5, 9, 13, 17, 21: 8 requests
465 const over = repo(); windowedGraph(over, { n: 21 });
466 assert.deepEqual(await windowedRun(over, W), { code: 'limit_exceeded', what: 'rebase chain', got: 21, max: 20, requests: 8 });
467}, 60_000);
469test('limits: windowed branch walk, a transport error inside a window is the operation\'s error, after the windows before it', async () => {
470 const r = repo(); windowedGraph(r, { n: 30 });
471 // request 1 ls-refs, 2-3 the search, 4 the first window (position 5): it fails
472 const failing = (c) => (u, i) => { c.n++; if (c.n === 4) return Promise.reject(new TypeError('network down')); return fetch(u, i); };
473 const e = await windowedRun(r, W, failing);
474 assert.notEqual(e.code, 'limit_exceeded'); assert.notEqual(e.code, null);
475 assert.equal(e.requests, 4);
476 console.log(`window transport error: ${e.code}`);
477}, 60_000);
479test('limits: windowed branch walk, a chain at the limit with its base past maxMergeDepth rebases as git does, at 4 window requests over the unwindowed walk', async () => {
480 const r = repo(); windowedGraph(r, { n: 20 });
481 const tip = ok(sh(r.bare, ['rev-parse', 'feat']));
482 const d = clone(r.bare); ok(sh(d, ['checkout', '-q', 'feat'])); ok(sh(d, ['rebase', '-q', 'origin/main'])); const gitTree = treeOf(d, 'HEAD'); rmSync(d, { recursive: true, force: true });
483 // unwindowed (maxMergeDepth 16, capA 22 <= 32): ls-refs, the search at 8 (2), 16 (branch only: main is whole), 22.
484 // windowed: ls-refs, the search at 8 (2), windows at 5, 9, 13, 17 (base hit at 21, held), the search again at 22
485 // (both sides: the onto walk was dropped for the windows): 2. 5 vs 9 before an identical replay: +4.
486 const plain = await windowedRun(r, { maxMergeDepth: 16, maxRebaseCommits: 20 });
487 assert.equal(plain.code, null);
488 ok(sh(r.bare, ['update-ref', 'refs/heads/feat', tip]));
489 const win = await windowedRun(r, W);
490 assert.equal(win.code, null); assert.equal(treeOf(r.bare, 'feat'), gitTree);
491 assert.equal(win.requests, plain.requests + 4);
492 // capA exactly twice maxMergeDepth is not windowed: maxRebaseCommits 6 at maxMergeDepth 4 (capA 8) costs what it
493 // costs at maxMergeDepth 8 (one search round at 8, both walks whole)
494 const six = repo(); windowedGraph(six, { n: 6 });
495 const sixTip = ok(sh(six.bare, ['rev-parse', 'feat']));
496 const at4 = await windowedRun(six, { maxMergeDepth: 4, maxRebaseCommits: 6 });
497 ok(sh(six.bare, ['update-ref', 'refs/heads/feat', sixTip]));
498 const at8 = await windowedRun(six, { maxMergeDepth: 8, maxRebaseCommits: 6 });
499 assert.deepEqual([at4.code, at8.code], [null, null]); assert.equal(at4.requests, at8.requests);
500 console.log(`chain 20 at the limit: unwindowed ${plain.requests} requests, windowed ${win.requests}; chain 6: ${at4.requests}`);
501}, 60_000);
503test('limits: windowed branch walk, a chain ending at a merge or a root within capA goes on to the full search', async () => {
504 // a merge 10 commits above base, 20 commits in all: from the tip it is at position 11. Windows at 5 and 9 (9-12
505 // holds it): break; the search resumes at 16 >= 12 (2 walks) and settles at 22 (the branch walk; main is whole),
506 // as the unwindowed search does (ls-refs, 8 (2), 16, 22: 5): 1 + 2 + 2 + 2 + 1 = 8, refused as a non-linear branch
507 const m = repo(); windowedGraph(m, { n: 20, mergeAt: 10 });
508 const mTip = ok(sh(m.bare, ['rev-parse', 'feat']));
509 assert.deepEqual(await windowedRun(m, { maxMergeDepth: 16, maxRebaseCommits: 20 }), { code: 'not_linear', what: undefined, got: undefined, max: undefined, requests: 5 });
510 assert.equal(ok(sh(m.bare, ['rev-parse', 'feat'])), mTip);
511 assert.deepEqual(await windowedRun(m, W), { code: 'not_linear', what: undefined, got: undefined, max: undefined, requests: 8 });
512 // an unrelated branch of 22 commits: its root at position 22 = capA is a break, not a cap. Windows at 5, 9, 13, 17,
513 // 21; the search resumes at 22 (2): 1 + 2 + 5 + 2 = 10, and the answer is the unrelated search's, not a limit
514 const u = repo(); windowedGraph(u, { n: 22, unrelated: true });
515 const got = await windowedRun(u, W);
516 assert.notEqual(got.code, 'limit_exceeded'); assert.equal(got.requests, 10);
517 console.log(`unrelated 22 at capA: ${got.code}`);
518}, 60_000);
Diagnostics6 git calls · 89 ms
CallStatusSizeTime089 ms
discover200191 B11 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=191
git.server.agent=git/github-9922ec520325-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=191
git.pkt.payload.max=36
ls-refs20012 KiB · hit11 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=11990
git.response.cache=HIT
git.response.duration_ms=11
git.protocol.version=2
git.pkt.frames=152
git.pkt.data=151
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=11990
git.pkt.payload.max=122
git.ls_refs.count=151
git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 102004.2 KiB · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=4347
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4347
git.pkt.payload.max=4247
git.sideband.band1.bytes=4247
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=10
git.pack.bytes=4247
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=a6caf8e129bea82c9bae4cbc7c130c9205add400
git.pack.full=10
Open pack map
fetchtree:0200919 B · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=919
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=800d30ba8923860bfc460ca884b7f9ba22083588
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=919
git.pkt.payload.max=892
git.sideband.band1.bytes=892
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=892
git.pack.trailer=5323b9266eea5ded637285d1c5320699f12be76e
Open pack map
fetchtree:02005.6 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=5724
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=58fabb6768b3f737ee9cd8bac647bdc27405fb4d
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=5724
git.pkt.payload.max=5697
git.sideband.band1.bytes=5697
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=5697
git.pack.trailer=be7e0453974a5fe832f9065a33bb7e3434c10e9b
Open pack map
fetch20012 KiB70 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=68
git.response.bytes=12586
git.response.cache=MISS
git.response.duration_ms=70
git.fetch.wants=261cd48a341e99e0fc3714f6e815b348c0526396
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=5
git.pkt.data=4
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=12586
git.pkt.payload.max=8192
git.sideband.band1.bytes=12554
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://github.com/ivarvong/ply
Ref
refs/heads/main
Commit
19912563f540228593df0c489082e5e2508e21e4
cf-ray
a5e0f83f2c6d332c
Request
f25c9943…
Protocol
v2 · sha1
Agent
git/github-9922ec520325-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format