Artifactspublic-v0

ply

main
ply4 git calls286 ms48 KiB2 cachedprotocol v2
757 lines46 KiBRaw
1// commitChanges (#103 item 2): one commit from an ordered change list, including the first commit of an empty
2// repository. The product is start × place × op, judged by git over http-backend: the same list applied with
3// git add / rm / mv and committed with fixed dates, then `git cat-file commit` byte for byte and fsck --strict.
4// One named disagreement: `create` of a path that exists. git add overwrites; ply refuses. A new ref whose tree
5// equals its parent is not an empty commit and the ref stays absent.
6import { test, beforeAll as before, afterAll as after } from 'vitest';
7import assert from 'node:assert/strict';
8import { join } from 'node:path';
9import { cpSync, mkdirSync, writeFileSync, readFileSync, symlinkSync, chmodSync } from 'node:fs';
10import { git, gitBuf, tmpRoot, makeBareRepo, startGitServer, fsckProblems, noMaintenance } from './server.js';
11import { commitChanges } from '../src/changes.js';
12import { editFile } from '../src/edit.js';
13import {
14 emptyDir, lazyNode, fileLeaf, putAt, removeAt, renameAt, replaceOnce, encodeMem, insertChild, requireDir,
15} from '../src/memtree.js';
16import { withLimits, LIMIT_OBJECT_OVERHEAD as OV } from '../src/limits.js';
17import { generateSigner } from '../src/sign.js';
18import { Tracer, arrayExporter, flattenLine } from '../src/otel.js';
19import { spawnSync } from 'node:child_process';
20import { unwrapAsync } from './unwrap.js';
22const ZERO = '0'.repeat(40);
23const commit = (url, spec, opts = {}) => unwrapAsync(commitChanges(url, spec, { tracer: new Tracer(), ...opts }));
24const edit = (url, spec, opts = {}) => unwrapAsync(editFile(url, spec, { tracer: new Tracer(), ...opts }));
25const AUTHOR = { name: 'Ada', email: 'ada@x', tz: '+0000', time: 1700000000 };
26const STAMP = {
27 GIT_AUTHOR_NAME: 'Ada', GIT_AUTHOR_EMAIL: 'ada@x', GIT_AUTHOR_DATE: '1700000000 +0000',
28 GIT_COMMITTER_NAME: 'Ada', GIT_COMMITTER_EMAIL: 'ada@x', GIT_COMMITTER_DATE: '1700000000 +0000',
29};
30const STARTS = ['empty', 'linear'];
31const PLACES = ['main', 'orphan', 'from-ref', 'from-oid', 'from-old', 'other', 'zero'];
32const OP_NAMES = ['create', 'create-dup', 'write', 'edit', 'edit-zero', 'edit-two', 'delete', 'delete-miss', 'rename', 'rename-onto', 'rename-self', 'sequence', 'exec', 'symlink', 'touch'];
34let root, cleanup, srv, n = 0, linearBare, emptyBare, tips;
36before(async () => {
37 ({ root, cleanup } = tmpRoot());
38 srv = await startGitServer(root);
39 emptyBare = makeBareRepo(root, 'empty');
40 linearBare = makeBareRepo(root, 'linear');
41 tips = seedLinear(linearBare);
42 assert.deepEqual(fsckProblems(linearBare), []);
43 assert.deepEqual(fsckProblems(emptyBare), []);
44});
45after(async () => { await srv.close(); cleanup(); });
47const gitEnv = (cwd, args, env = {}) => git(cwd, ['-c', 'commit.gpgsign=false', '-c', 'core.autocrlf=false', ...args], { env: { ...process.env, ...env } });
48function seedLinear(bare) {
49 const work = join(root, 'seed-linear');
50 git(root, ['-c', 'core.autocrlf=false', 'init', '-q', '-b', 'main', work]); noMaintenance(work);
51 git(work, ['config', 'commit.gpgsign', 'false']);
52 git(work, ['config', 'core.autocrlf', 'false']);
53 writeFileSync(join(work, 'a.txt'), 'old\n');
54 git(work, ['add', '-A']);
55 gitEnv(work, ['-c', 'user.name=seed', '-c', 'user.email=seed@x', 'commit', '-q', '-m', 'first']);
56 const old = git(work, ['rev-parse', 'HEAD']).trim();
57 mkdirSync(join(work, 'dir'));
58 writeFileSync(join(work, 'a.txt'), 'v1\n');
59 writeFileSync(join(work, 'dir/b.txt'), 'b\n');
60 writeFileSync(join(work, 'run.sh'), '#!/bin/sh\n');
61 chmodSync(join(work, 'run.sh'), 0o755);
62 symlinkSync('a.txt', join(work, 'link'));
63 writeFileSync(join(work, 'twin.txt'), 'xx\n');
64 git(work, ['add', '-A']);
65 gitEnv(work, ['-c', 'user.name=seed', '-c', 'user.email=seed@x', 'commit', '-q', '-m', 'second']);
66 const tip = git(work, ['rev-parse', 'HEAD']).trim();
67 git(work, ['push', '-q', bare, 'HEAD:refs/heads/main', 'HEAD:refs/heads/side']);
68 return { old, tip };
70const copy = template => {
71 const name = `c${n++}`;
72 const bare = join(root, `${name}.git`);
73 cpSync(template, bare, { recursive: true });
74 return { bare, url: `${srv.url}/${name}.git` };
75};
76const refOid = (bare, ref) => { try { return git(bare, ['rev-parse', '--verify', ref]).trim(); } catch { return null; } };
77const refOf = place => (place === 'orphan' || place === 'from-ref' || place === 'from-oid' ? 'refs/heads/topic' : 'refs/heads/main');
78function baseOf(start, place) {
79 if (place === 'main' || place === 'orphan') return {};
80 if (place === 'from-ref') return { base: 'refs/heads/side' };
81 if (place === 'from-oid') return { base: start === 'linear' ? tips.tip : 'a'.repeat(40) };
82 if (place === 'from-old') return { base: start === 'linear' ? tips.old : 'a'.repeat(40) };
83 if (place === 'other') return { base: 'refs/heads/side' };
84 return { base: ZERO };
86const changesOf = op => ({
87 create: [{ kind: 'create', path: 'n.txt', content: 'n\n' }],
88 'create-dup': [{ kind: 'create', path: 'a.txt', content: 'z\n' }],
89 write: [{ kind: 'write', path: 'a.txt', content: 'w\n' }],
90 edit: [{ kind: 'edit', path: 'a.txt', find: 'v1', replace: 'v2' }],
91 'edit-zero': [{ kind: 'edit', path: 'a.txt', find: 'NO', replace: 'x' }],
92 'edit-two': [{ kind: 'edit', path: 'twin.txt', find: 'x', replace: 'y' }],
93 delete: [{ kind: 'delete', path: 'a.txt' }],
94 'delete-miss': [{ kind: 'delete', path: 'nope.txt' }],
95 rename: [{ kind: 'rename', from: 'a.txt', to: 'z.txt' }],
96 'rename-onto': [{ kind: 'rename', from: 'a.txt', to: 'twin.txt' }],
97 'rename-self': [{ kind: 'rename', from: 'a.txt', to: 'a.txt' }],
98 sequence: [{ kind: 'create', path: 'n.txt', content: 'n\n' }, { kind: 'write', path: 'w.txt', content: 'w\n' }, { kind: 'delete', path: 'twin.txt' }],
99 exec: [{ kind: 'create', path: 'bin/run', content: '#!/bin/sh\n', mode: '100755' }],
100 symlink: [{ kind: 'create', path: 'slink', content: 'a.txt', mode: '120000' }],
101 touch: [{ kind: 'write', path: 'a.txt', content: 'v1\n' }],
102})[op];
103function classOf(start, place, op) {
104 if (place === 'zero') return { kind: 'pre', code: 'bad_request', fetches: 0 };
105 if (start === 'empty' && (place === 'from-ref' || place === 'from-oid' || place === 'from-old' || place === 'other')) return { kind: 'pre', code: 'not_found' };
106 if (start === 'linear' && place === 'from-old') return { kind: 'pre', code: 'bad_request', rebase: true };
107 if (start === 'linear' && place === 'other') return { kind: 'pre', code: 'cas_conflict' };
108 const tip = start === 'linear' && (place === 'main' || place === 'from-ref' || place === 'from-oid');
109 if (!tip) {
110 if (op === 'edit' || op === 'edit-zero' || op === 'edit-two' || op === 'delete' || op === 'delete-miss' || op === 'rename' || op === 'rename-onto' || op === 'sequence') return { kind: 'refuse', code: 'not_found' };
111 if (op === 'rename-self') return { kind: 'refuse', code: 'bad_request' };
112 return { kind: 'commit', root: true };
113 }
114 if (op === 'create-dup') return { kind: 'disagree' };
115 if (op === 'edit-zero' || op === 'edit-two') return { kind: 'refuse', code: 'match_count' };
116 if (op === 'delete-miss') return { kind: 'refuse', code: 'not_found' };
117 if (op === 'rename-onto' || op === 'rename-self') return { kind: 'refuse', code: 'bad_request' };
118 if (op === 'touch') return place === 'main' ? { kind: 'noop' } : { kind: 'nothing' };
119 return { kind: 'commit', root: false };
121function workFrom(bare, kind) {
122 const work = join(root, `w${n++}`);
123 if (kind === 'tip') git(root, ['-c', 'core.autocrlf=false', 'clone', '-q', bare, work]);
124 else git(root, ['-c', 'core.autocrlf=false', 'init', '-q', '-b', 'main', work]);
125 noMaintenance(work);
126 git(work, ['config', 'commit.gpgsign', 'false']);
127 git(work, ['config', 'core.autocrlf', 'false']);
128 return work;
130function writeRepo(work, path, data, mode) {
131 const full = join(work, path);
132 mkdirSync(join(full, '..'), { recursive: true });
133 writeFileSync(full, data);
134 if (mode !== undefined) chmodSync(full, mode);
135 git(work, ['add', '--', path]);
137function applyGit(work, op) {
138 if (op === 'create') writeRepo(work, 'n.txt', 'n\n');
139 else if (op === 'create-dup') writeRepo(work, 'a.txt', 'z\n');
140 else if (op === 'write' || op === 'touch') writeRepo(work, 'a.txt', op === 'write' ? 'w\n' : 'v1\n');
141 else if (op === 'edit') {
142 const full = join(work, 'a.txt');
143 writeFileSync(full, readFileSync(full, 'utf8').replace('v1', 'v2'));
144 git(work, ['add', '--', 'a.txt']);
145 } else if (op === 'delete') git(work, ['rm', '-q', '--', 'a.txt']);
146 else if (op === 'rename') git(work, ['mv', 'a.txt', 'z.txt']);
147 else if (op === 'sequence') {
148 writeRepo(work, 'n.txt', 'n\n'); writeRepo(work, 'w.txt', 'w\n'); git(work, ['rm', '-q', '--', 'twin.txt']);
149 } else if (op === 'exec') writeRepo(work, 'bin/run', '#!/bin/sh\n', 0o755);
150 else if (op === 'symlink') { symlinkSync('a.txt', join(work, 'slink')); git(work, ['add', '--', 'slink']); }
151 else throw new Error(`no git twin for ${op}`);
153function tryCommit(work, env = STAMP) {
154 try {
155 gitEnv(work, ['-c', 'user.name=Ada', '-c', 'user.email=ada@x', 'commit', '-q', '-m', 'c'], env);
156 return { oid: git(work, ['rev-parse', 'HEAD']).trim(), err: '' };
157 } catch (e) { return { oid: null, err: String(e.stderr ?? e.message).slice(0, 500) }; }
159function counts() {
160 const c = { upload: 0, receive: 0 };
161 const fetchImpl = async (url, init) => {
162 const kind = String(url).split('?')[0].split('/').pop();
163 if (kind === 'git-upload-pack') c.upload++;
164 else if (kind === 'git-receive-pack') c.receive++;
165 return fetch(url, init);
166 };
167 return { c, fetchImpl };
169const caught = p => p.then(() => null, e => e);
170function sameCommit(bare, plyOid, work, gitOid) {
171 const plyBytes = gitBuf(bare, ['cat-file', 'commit', plyOid]);
172 const gitBytes = gitBuf(work, ['cat-file', 'commit', gitOid]);
173 if (!plyBytes.equals(gitBytes)) throw new Error(`commit bytes differ\n--- ply\n${plyBytes.toString()}\n--- git\n${gitBytes.toString()}`);
176async function productCell(start, place, op) {
177 const repo = copy(start === 'linear' ? linearBare : emptyBare);
178 const ref = refOf(place);
179 const before = refOid(repo.bare, ref);
180 const { c, fetchImpl } = counts();
181 const spec = { ref, ...baseOf(start, place), changes: changesOf(op), author: AUTHOR, message: 'c' };
182 const cls = classOf(start, place, op);
183 const run = () => commit(repo.url, spec, { fetchImpl });
184 if (cls.kind === 'pre' || cls.kind === 'refuse') {
185 const e = await caught(run());
186 assert.ok(e, `${start}/${place}/${op} expected ${cls.code}`);
187 assert.equal(e.code, cls.code, e.message);
188 if (cls.fetches === 0) assert.equal(c.upload + c.receive, 0);
189 if (cls.rebase) { assert.equal(e.what, 'rebase'); assert.match(e.message, /not the tip/); }
190 if (cls.code === 'match_count') assert.equal(e.matches, op === 'edit-two' ? 2 : 0);
191 if (cls.code === 'cas_conflict') { assert.equal(e.expected, ZERO); assert.equal(e.actual, tips.tip); }
192 assert.equal(c.receive, 0);
193 assert.equal(refOid(repo.bare, ref), before);
194 return;
195 }
196 const work = workFrom(repo.bare, cls.root ? 'empty' : 'tip');
197 applyGit(work, op);
198 const twin = tryCommit(work);
199 if (cls.kind === 'disagree' || cls.kind === 'nothing') {
200 if (cls.kind === 'disagree') assert.ok(twin.oid, twin.err);
201 else assert.equal(twin.oid, null, twin.err);
202 const e = await caught(run());
203 assert.ok(e);
204 assert.equal(e.code, 'bad_request', e.message);
205 assert.match(e.message, cls.kind === 'nothing' ? /nothing to commit/ : /already exists/);
206 assert.equal(c.receive, 0);
207 assert.equal(refOid(repo.bare, ref), before);
208 return;
209 }
210 if (cls.kind === 'noop') {
211 assert.equal(twin.oid, null, twin.err);
212 const res = await run();
213 assert.equal(res.outcome, 'noop');
214 assert.equal(res.pushBytes, 0);
215 assert.equal(res.created, false);
216 assert.equal(res.oid, tips.tip);
217 assert.equal(c.receive, 0);
218 assert.equal(refOid(repo.bare, ref), tips.tip);
219 return;
220 }
221 assert.ok(twin.oid, twin.err);
222 const res = await run();
223 assert.equal(res.outcome, 'commit');
224 assert.equal(res.ref, ref);
225 assert.equal(res.created, !(start === 'linear' && place === 'main'));
226 assert.equal(res.oldOid, res.created ? null : tips.tip);
227 assert.equal(res.parent, cls.root ? null : tips.tip);
228 assert.ok(res.pushBytes > 0);
229 assert.equal(c.receive, 1);
230 sameCommit(repo.bare, res.oid, work, twin.oid);
231 assert.equal(refOid(repo.bare, ref), res.oid);
232 assert.deepEqual(fsckProblems(repo.bare), []);
235test('commitChanges: start × place × op against git cat-file and fsck --strict', async () => {
236 const failures = [];
237 let seen = 0;
238 walk: for (const start of STARTS) for (const place of PLACES) for (const op of OP_NAMES) {
239 seen++;
240 try { await productCell(start, place, op); }
241 catch (e) {
242 failures.push(`${start}/${place}/${op}: ${e instanceof Error ? e.message.split('\n')[0] : e}`);
243 if (failures.length >= 12) break walk;
244 }
245 }
246 console.log(`commit-changes product: ${seen} cells, ${failures.length} failures`);
247 assert.deepEqual(failures, []);
248 assert.equal(seen, STARTS.length * PLACES.length * OP_NAMES.length);
249});
251const enc = s => new TextEncoder().encode(s);
252const OID = 'a'.repeat(40);
253const OID_B = 'b'.repeat(40);
255test('memtree: every refusal, and the size boundaries git would still hash', () => {
256 // every memtree edit returns a Result: the refusals are values, not throws
257 const refused = (r, check) => { assert.equal(r.ok, false); assert.ok(check(r.error), r.error?.message); };
258 const value = r => { assert.equal(r.ok, true, r.ok ? '' : r.error.message); return r.value; };
259 const lim1 = withLimits({ maxTreeEntries: 1, maxObjectSize: 4 });
260 const wide = withLimits({ maxTreeEntries: 20, maxObjectSize: 64 });
261 refused(requireDir(lazyNode('100644', OID), 'a.txt'), e => e.code === 'bad_request' && /not a directory/.test(e.message));
262 assert.equal(value(requireDir(emptyDir(), '')).size, 0);
263 const leaf = value(fileLeaf('100644', enc('abcd'), lim1, 'f'));
264 assert.equal(leaf.data.length, 4);
265 refused(fileLeaf('100644', enc('abcde'), lim1, 'f'), e => e.code === 'limit_exceeded' && e.what === 'object size' && e.got === 5 && e.max === 4);
266 refused(fileLeaf('120000', enc(''), lim1, 's'), e => e.code === 'bad_request' && e.what === 'symlink');
267 refused(fileLeaf('120000', Uint8Array.of(0x61, 0), lim1, 's'), e => e.code === 'bad_request' && e.what === 'symlink');
268 assert.equal(value(fileLeaf('120000', enc('ab'), lim1, 's')).mode, '120000');
269 const entries = new Map();
270 value(insertChild(entries, 'a', lazyNode('100644', OID), lim1));
271 refused(insertChild(entries, 'b', lazyNode('100644', OID_B), lim1), e => e.code === 'limit_exceeded' && e.what === 'tree entries' && e.got === 2 && e.max === 1);
272 value(insertChild(entries, 'a', lazyNode('100644', OID_B), lim1));
273 const one = emptyDir(); one.entries.set('a', lazyNode('100644', OID));
274 assert.equal(typeof value(encodeMem(one, lim1)).oid, 'string');
275 const two = emptyDir(); two.entries.set('a', lazyNode('100644', OID)); two.entries.set('b', lazyNode('100644', OID_B));
276 refused(encodeMem(two, lim1), e => e.code === 'limit_exceeded' && e.what === 'tree entries' && e.got === 2);
277 const rootDir = emptyDir();
278 refused(putAt(rootDir, ['missing', 'f'], lazyNode('100644', OID), false, wide), e => e.code === 'bad_request' && /not a directory/.test(e.message));
279 rootDir.entries.set('file', lazyNode('100644', OID));
280 refused(putAt(rootDir, ['file', 'f'], lazyNode('100644', OID_B), true, wide), e => /not a directory/.test(e.message));
281 const sub = emptyDir(); rootDir.entries.set('dir', sub);
282 refused(putAt(rootDir, ['dir'], lazyNode('100644', OID_B), true, wide), e => /is a directory/.test(e.message));
283 rootDir.entries.set('tree', lazyNode('40000', OID));
284 refused(putAt(rootDir, ['tree'], lazyNode('100644', OID_B), true, wide), e => /is a directory/.test(e.message));
285 refused(putAt(rootDir, ['file'], lazyNode('100644', OID_B), false, wide), e => /already exists/.test(e.message));
286 value(putAt(rootDir, ['file'], lazyNode('100644', OID_B), true, wide));
287 assert.equal(rootDir.entries.get('file').oid, OID_B);
288 value(putAt(rootDir, ['fresh'], lazyNode('100644', OID), false, wide));
289 refused(removeAt(rootDir, ['nope']), e => e.code === 'not_found');
290 refused(removeAt(rootDir, ['gone', 'f']), e => e.code === 'not_found' && /gone not found/.test(e.message));
291 refused(removeAt(rootDir, ['file', 'f']), e => e.code === 'not_found' && /not a directory/.test(e.message));
292 sub.entries.set('b', lazyNode('100644', OID));
293 value(removeAt(rootDir, ['dir', 'b']));
294 assert.equal(rootDir.entries.has('dir'), false);
295 refused(renameAt(emptyDir(), ['a'], ['a']), e => /already named/.test(e.message));
296 refused(renameAt(emptyDir(), ['nope'], ['nope', 'x']), e => /into itself/.test(e.message));
297 refused(renameAt(emptyDir(), ['missing'], ['z']), e => e.code === 'not_found');
298 refused(renameAt(emptyDir(), ['missing', 'child'], ['z']), e => e.code === 'not_found' && /missing not found/.test(e.message));
299 const ren = emptyDir(); ren.entries.set('file', lazyNode('100644', OID));
300 refused(renameAt(ren, ['file', 'a'], ['z']), e => /not a directory/.test(e.message));
301 refused(renameAt(ren, ['file'], ['no', 'z']), e => e.code === 'not_found' && /no not found/.test(e.message));
302 ren.entries.set('file2', lazyNode('100644', OID_B));
303 refused(renameAt(ren, ['file2'], ['file']), e => e.code === 'bad_request' && /already exists/.test(e.message));
304 const nested = emptyDir(); nested.entries.set('f', lazyNode('100644', OID)); ren.entries.set('dir', nested);
305 refused(renameAt(ren, ['dir', 'f'], ['file', 'z']), e => e.code === 'not_found' && /not a directory/.test(e.message));
306 value(renameAt(ren, ['dir', 'f'], ['kept']));
307 assert.equal(ren.entries.has('dir'), false);
308 assert.equal(ren.entries.get('kept').oid, OID);
309 // replaceOnce returns a Result: the refusals are values, not throws
310 refused(replaceOnce(enc('ab'), enc(''), enc('x')), e => e.code === 'bad_request');
311 refused(replaceOnce(enc('ab'), enc('z'), enc('x')), e => e.code === 'match_count' && e.matches === 0);
312 refused(replaceOnce(enc('xx'), enc('x'), enc('y')), e => e.code === 'match_count' && e.matches === 2);
313 const replaced = replaceOnce(enc('v1\n'), enc('v1'), enc('v2'));
314 assert.equal(replaced.ok, true);
315 assert.equal(Buffer.from(replaced.value).toString(), 'v2\n');
316});
318test('commitChanges rejects a bad list before any fetch', async () => {
319 const repo = copy(emptyBare);
320 const author = AUTHOR;
321 const message = 'c';
322 const one = { kind: 'create', path: 'n.txt', content: 'n\n' };
323 const bad = [
324 [{ ref: 'refs/heads/main', changes: [], author, message }, /non-empty/],
325 [{ ref: 'refs/heads/main', changes: null, author, message }, /non-empty/],
326 [{ ref: 'refs/heads/main', changes: [null], author, message }, /object/],
327 [{ ref: 'refs/heads/main', changes: [{ kind: 'nope' }], author, message }, /unknown change kind/],
328 [{ ref: 'refs/heads/main', changes: [{ kind: 'edit', path: 'a.txt', find: '', replace: 'x' }], author, message }, /find must be non-empty/],
329 [{ ref: 'refs/heads/main', changes: [{ kind: 'create', path: 'n.txt', content: 1 }], author, message }, /content/],
330 [{ ref: 'refs/heads/main', changes: [{ kind: 'create', path: 'n.txt', content: 'n', mode: '100666' }], author, message }, /mode/],
331 [{ ref: 'refs/heads/main', changes: [one], author, message, base: ZERO }, /zero oid is not a commit/],
332 [{ ref: 'refs/heads/main', changes: [one], author, message, base: 'A'.repeat(40) }, /lowercase hex/],
333 [{ ref: 'refs/heads/main', changes: [{ kind: 'create' }, { kind: 'create', path: 'b', content: 'b' }], author, message }, /limit/, { maxPackObjects: 1 }],
334 ];
335 for (const [spec, re, limits] of bad) {
336 const { c, fetchImpl } = counts();
337 const e = await caught(commit(repo.url, spec, { fetchImpl, ...(limits ? { limits: withLimits(limits) } : {}) }));
338 assert.ok(e, re);
339 assert.equal(e.code, limits ? 'limit_exceeded' : 'bad_request', `${e.code} ${e.message}`);
340 assert.match(e.message, re, e.message);
341 assert.equal(c.upload + c.receive, 0, e.message);
342 }
343 const { c, fetchImpl } = counts();
344 const e = await caught(commit(repo.url, { ref: 'refs/heads/main', changes: [one], author, message }, { fetchImpl, head: 'not-an-oid' }));
345 assert.equal(e.code, 'bad_request');
346 assert.match(e.message, /lowercase hex|40/);
347 assert.equal(c.upload + c.receive, 0);
348});
350// #142: each change charges its new bytes (content, replacement; none for a delete or rename) plus LIMIT_OBJECT_OVERHEAD
351// against maxPushBytes, before any fetch. Exactly the cap commits; one byte under it is limit_exceeded with no request.
352test('commitChanges charges bytes plus the per-object overhead against maxPushBytes', async () => {
353 const changes = [{ kind: 'create', path: 'a.txt', content: 'abcd' }, { kind: 'create', path: 'b.txt', content: 'xy' }];
354 const cap = 6 + 2 * OV;
355 const at = copy(emptyBare);
356 const res = await commit(at.url, { ref: 'refs/heads/main', changes, author: AUTHOR, message: 'c' }, { limits: withLimits({ maxPushBytes: cap }) });
357 assert.equal(git(at.bare, ['cat-file', '-p', `${res.oid}:b.txt`]), 'xy');
358 const { c, fetchImpl } = counts();
359 const e = await caught(commit(copy(emptyBare).url, { ref: 'refs/heads/main', changes, author: AUTHOR, message: 'c' }, { fetchImpl, limits: withLimits({ maxPushBytes: cap - 1 }) }));
360 assert.equal(e.code, 'limit_exceeded');
361 assert.equal(e.what, 'push bytes');
362 assert.equal(e.got, cap);
363 assert.equal(e.max, cap - 1);
364 assert.equal(c.upload + c.receive, 0);
365 // an edit charges its replacement, a delete and a rename the overhead alone
366 const mixed = [{ kind: 'edit', path: 'a.txt', find: 'a', replace: 'xyz' }, { kind: 'delete', path: 'b.txt' }, { kind: 'rename', from: 'c', to: 'd' }];
367 const m = await caught(commit(copy(emptyBare).url, { ref: 'refs/heads/main', changes: mixed, author: AUTHOR, message: 'c' }, { fetchImpl, limits: withLimits({ maxPushBytes: 3 + 3 * OV - 1 }) }));
368 assert.equal(m.what, 'push bytes');
369 assert.equal(m.got, 3 + 3 * OV);
370 assert.equal(c.upload + c.receive, 0);
371});
373test('one change at the pack-object cap commits; a blob at the object cap commits and one past it does not', async () => {
374 const ok = copy(emptyBare);
375 const res = await commit(ok.url, { ref: 'refs/heads/main', changes: [{ kind: 'create', path: 'n.txt', content: 'abcd' }], author: AUTHOR, message: 'c' }, { limits: withLimits({ maxPackObjects: 1, maxObjectSize: 4 }) });
376 assert.equal(res.created, true);
377 assert.equal(git(ok.bare, ['cat-file', '-p', `${res.oid}:n.txt`]), 'abcd');
378 const over = copy(emptyBare);
379 const before = refOid(over.bare, 'refs/heads/main');
380 const e = await caught(commit(over.url, { ref: 'refs/heads/main', changes: [{ kind: 'create', path: 'n.txt', content: 'abcde' }], author: AUTHOR, message: 'c' }, { limits: withLimits({ maxObjectSize: 4 }) }));
381 assert.equal(e.code, 'limit_exceeded');
382 assert.equal(e.what, 'object size');
383 assert.equal(refOid(over.bare, 'refs/heads/main'), before);
384 const wide = copy(emptyBare);
385 const tree = await caught(commit(wide.url, { ref: 'refs/heads/main', changes: [{ kind: 'create', path: 'a.txt', content: 'a' }, { kind: 'create', path: 'b.txt', content: 'b' }], author: AUTHOR, message: 'c' }, { limits: withLimits({ maxTreeEntries: 1 }) }));
386 assert.equal(tree.code, 'limit_exceeded');
387 assert.equal(tree.what, 'tree entries');
388 assert.equal(refOid(wide.bare, 'refs/heads/main'), null);
389});
391test('heads, bases, and a 40-hex editFile', async () => {
392 const match = copy(linearBare);
393 const res = await commit(match.url, { ref: 'refs/heads/main', changes: changesOf('create'), author: AUTHOR, message: 'c' }, { head: tips.tip });
394 assert.equal(res.parent, tips.tip);
395 assert.equal(git(match.bare, ['rev-parse', 'main^']).trim(), tips.tip);
396 const stale = copy(linearBare);
397 const { c, fetchImpl } = counts();
398 const missed = await caught(commit(stale.url, { ref: 'refs/heads/main', changes: changesOf('create'), author: AUTHOR, message: 'c' }, { head: tips.old, fetchImpl }));
399 assert.equal(missed.code, 'cas_conflict');
400 assert.equal(missed.expected, tips.old);
401 assert.equal(missed.actual, tips.tip);
402 assert.equal(missed.cause, undefined);
403 assert.equal(c.receive, 0);
404 assert.equal(refOid(stale.bare, 'refs/heads/main'), tips.tip);
405 const held = copy(linearBare);
406 const { c: zc, fetchImpl: zf } = counts();
407 const zeroHead = await caught(commit(held.url, { ref: 'refs/heads/main', changes: changesOf('create'), author: AUTHOR, message: 'c' }, { head: ZERO, fetchImpl: zf }));
408 assert.equal(zeroHead.code, 'cas_conflict');
409 assert.equal(zeroHead.expected, ZERO);
410 assert.equal(zeroHead.actual, tips.tip);
411 assert.equal(zc.receive, 0);
412 const born = copy(emptyBare);
413 const created = await commit(born.url, { ref: 'refs/heads/topic', changes: changesOf('create'), author: AUTHOR, message: 'c' }, { head: ZERO });
414 assert.equal(created.created, true);
415 assert.equal(created.oldOid, null);
416 assert.equal(refOid(born.bare, 'refs/heads/topic'), created.oid);
417 const blob = git(linearBare, ['rev-parse', `${tips.tip}:a.txt`]).trim();
418 const notCommit = copy(linearBare);
419 const miss = await caught(commit(notCommit.url, { ref: 'refs/heads/topic', base: blob, changes: changesOf('create'), author: AUTHOR, message: 'c' }));
420 assert.equal(miss.code, 'not_found');
421 assert.equal(miss.miss.kind, 'commit');
422 assert.equal(miss.miss.miss.kind, 'not_commit');
423 assert.equal(miss.miss.miss.type, 'blob');
424 assert.equal(refOid(notCommit.bare, 'refs/heads/topic'), null);
425 const absent = copy(emptyBare);
426 const gone = await caught(commit(absent.url, { ref: 'refs/heads/topic', base: 'b'.repeat(40), changes: changesOf('create'), author: AUTHOR, message: 'c' }));
427 assert.equal(gone.code, 'not_found');
428 assert.equal(gone.miss.kind, 'commit');
429 assert.equal(gone.miss.miss.kind, 'absent');
430 const bytes = new Uint8Array([0x6e, 0x0a]);
431 const raw = copy(emptyBare);
432 const rawRes = await commit(raw.url, { ref: 'refs/heads/main', changes: [{ kind: 'create', path: 'n.txt', content: bytes }], author: AUTHOR, message: 'c' });
433 assert.equal(git(raw.bare, ['cat-file', '-p', `${rawRes.oid}:n.txt`]), 'n\n');
434 const edited = copy(linearBare);
435 const direct = await edit(edited.url, { ref: 'refs/heads/main', base: tips.tip, path: 'a.txt', find: 'v1', replace: 'v2', author: AUTHOR, message: 'c' });
436 const work = workFrom(linearBare, 'tip');
437 applyGit(work, 'edit');
438 const twin = tryCommit(work);
439 sameCommit(edited.bare, direct.newCommit, work, twin.oid);
440 const off = copy(linearBare);
441 const { c: ec, fetchImpl: ef } = counts();
442 const rebase = await caught(edit(off.url, { ref: 'refs/heads/main', base: tips.old, path: 'a.txt', find: 'old', replace: 'x', author: AUTHOR, message: 'c' }, { fetchImpl: ef }));
443 assert.equal(rebase.code, 'bad_request');
444 assert.equal(rebase.what, 'rebase');
445 assert.match(rebase.message, /not the tip/);
446 assert.equal(ec.receive, 0);
447 assert.equal(refOid(off.bare, 'refs/heads/main'), tips.tip);
448 const { c: zc2, fetchImpl: zf2 } = counts();
449 const zeroBase = await caught(edit(off.url, { base: ZERO, path: 'a.txt', find: 'v1', replace: 'v2', author: AUTHOR, message: 'c' }, { fetchImpl: zf2 }));
450 assert.equal(zeroBase.code, 'bad_request');
451 assert.match(zeroBase.message, /zero oid/);
452 assert.equal(zc2.upload + zc2.receive, 0);
453 const branch = copy(linearBare);
454 const made = await edit(branch.url, { ref: 'refs/heads/topic', base: tips.tip, path: 'a.txt', find: 'v1', replace: 'v2', author: AUTHOR, message: 'c' });
455 assert.equal(git(branch.bare, ['rev-parse', 'topic']).trim(), made.newCommit);
456 assert.equal(git(branch.bare, ['rev-parse', 'topic^']).trim(), tips.tip);
457});
459test('author and committer stamps match git on both the stream and the in-memory path', async () => {
460 const env = {
461 GIT_AUTHOR_NAME: 'Ada', GIT_AUTHOR_EMAIL: 'ada@x', GIT_AUTHOR_DATE: '1700000000 +0000',
462 GIT_COMMITTER_NAME: 'Bea', GIT_COMMITTER_EMAIL: 'bea@x', GIT_COMMITTER_DATE: '1700001000 -0500',
463 };
464 const author = { name: 'Ada', email: 'ada@x', tz: '+0000', time: 1700000000 };
465 const committer = { name: 'Bea', email: 'bea@x', tz: '-0500', time: 1700001000 };
466 for (const op of ['create', 'edit']) {
467 const repo = copy(linearBare);
468 const work = workFrom(repo.bare, 'tip');
469 applyGit(work, op);
470 const twin = tryCommit(work, env);
471 const res = await commit(repo.url, { ref: 'refs/heads/main', changes: changesOf(op), author, committer, message: 'c' });
472 sameCommit(repo.bare, res.oid, work, twin.oid);
473 assert.deepEqual(fsckProblems(repo.bare), []);
474 }
475});
477function retip(bare, ref, parent) {
478 const tree = git(bare, ['rev-parse', `${parent}^{tree}`]).trim();
479 const oid = gitEnv(bare, ['commit-tree', tree, '-p', parent, '-m', 'race'], {
480 GIT_AUTHOR_NAME: 'race', GIT_AUTHOR_EMAIL: 'race@x', GIT_AUTHOR_DATE: '1710000000 +0000',
481 GIT_COMMITTER_NAME: 'race', GIT_COMMITTER_EMAIL: 'race@x', GIT_COMMITTER_DATE: '1710000000 +0000',
482 }).trim();
483 git(bare, ['update-ref', ref, oid]);
484 return oid;
486function counting(hook) {
487 const c = { 'git-upload-pack': 0, 'git-receive-pack': 0 };
488 const fetchImpl = async (url, init) => {
489 const kind = String(url).split('?')[0].split('/').pop();
490 if (Object.hasOwn(c, kind)) { c[kind] += 1; await hook(kind, c); }
491 return fetch(url, init);
492 };
493 return { c, fetchImpl };
496test('a lost compare-and-swap reports the oid that won, and an update is retried once', async () => {
497 const retry = copy(linearBare);
498 let moved = null;
499 const race = counting(kind => { if (kind === 'git-receive-pack' && moved === null) moved = retip(retry.bare, 'refs/heads/main', tips.tip); });
500 const res = await commit(retry.url, { ref: 'refs/heads/main', changes: changesOf('create'), author: AUTHOR, message: 'c' }, { fetchImpl: race.fetchImpl });
501 assert.equal(res.parent, moved);
502 assert.equal(res.oldOid, moved);
503 assert.equal(git(retry.bare, ['rev-parse', 'main']).trim(), res.oid);
504 assert.equal(git(retry.bare, ['rev-parse', 'main^']).trim(), moved);
505 assert.equal(git(retry.bare, ['cat-file', '-p', `${res.oid}:n.txt`]), 'n\n');
506 assert.equal(race.c['git-receive-pack'], 2);
507 assert.deepEqual(fsckProblems(retry.bare), []);
509 const pinned = copy(linearBare);
510 let winner = null;
511 const once = counting(kind => { if (kind === 'git-receive-pack' && winner === null) winner = retip(pinned.bare, 'refs/heads/main', tips.tip); });
512 const lost = await caught(commit(pinned.url, { ref: 'refs/heads/main', changes: changesOf('create'), author: AUTHOR, message: 'c' }, { head: tips.tip, fetchImpl: once.fetchImpl }));
513 assert.equal(lost.code, 'cas_conflict');
514 assert.equal(lost.expected, tips.tip);
515 assert.equal(lost.actual, winner);
516 assert.ok(lost.cause);
517 assert.equal(once.c['git-receive-pack'], 1);
518 assert.equal(refOid(pinned.bare, 'refs/heads/main'), winner);
520 const created = copy(linearBare);
521 let taken = null;
522 const createRace = counting(kind => { if (kind === 'git-receive-pack' && taken === null) { git(created.bare, ['update-ref', 'refs/heads/topic', tips.old]); taken = tips.old; } });
523 const clash = await caught(commit(created.url, { ref: 'refs/heads/topic', changes: changesOf('create'), author: AUTHOR, message: 'c' }, { fetchImpl: createRace.fetchImpl }));
524 assert.equal(clash.code, 'cas_conflict');
525 assert.equal(clash.expected, ZERO);
526 assert.equal(clash.actual, taken);
527 assert.equal(createRace.c['git-receive-pack'], 1);
528 assert.equal(refOid(created.bare, 'refs/heads/topic'), tips.old);
530 const fast = copy(linearBare);
531 let shifted = null;
532 let uploads = 0;
533 const convert = counting(kind => {
534 if (kind !== 'git-upload-pack') return;
535 uploads += 1;
536 if (uploads === 2) { git(fast.bare, ['update-ref', 'refs/heads/topic', tips.old]); shifted = tips.old; }
537 });
538 const converted = await caught(commit(fast.url, { ref: 'refs/heads/topic', base: tips.tip, changes: changesOf('edit'), author: AUTHOR, message: 'c' }, { fetchImpl: convert.fetchImpl }));
539 assert.equal(converted.code, 'cas_conflict', converted.message);
540 assert.equal(converted.expected, ZERO);
541 assert.equal(converted.actual, shifted);
542 assert.equal(converted.cause.what, 'rebase');
543 assert.equal(convert.c['git-receive-pack'], 0);
544 assert.equal(refOid(fast.bare, 'refs/heads/topic'), tips.old);
546 const again = copy(linearBare);
547 let ahead = null;
548 const streamed = counting(kind => { if (kind === 'git-receive-pack' && ahead === null) ahead = retip(again.bare, 'refs/heads/main', tips.tip); });
549 const edited = await commit(again.url, { ref: 'refs/heads/main', changes: changesOf('edit'), author: AUTHOR, message: 'c' }, { fetchImpl: streamed.fetchImpl });
550 assert.equal(edited.parent, ahead);
551 assert.equal(git(again.bare, ['rev-parse', 'main^']).trim(), ahead);
552 assert.equal(git(again.bare, ['cat-file', '-p', `${edited.oid}:a.txt`]), 'v2\n');
553 assert.equal(streamed.c['git-receive-pack'], 2);
554 assert.deepEqual(fsckProblems(again.bare), []);
556 const strict = copy(linearBare);
557 let jumped = null;
558 let seenUpload = 0;
559 const early = counting(kind => {
560 if (kind !== 'git-upload-pack') return;
561 seenUpload += 1;
562 if (seenUpload === 2) jumped = retip(strict.bare, 'refs/heads/main', tips.tip);
563 });
564 const rethrown = await caught(commit(strict.url, { ref: 'refs/heads/main', changes: changesOf('edit'), author: AUTHOR, message: 'c' }, { head: tips.tip, fetchImpl: early.fetchImpl }));
565 assert.equal(rethrown.code, 'cas_conflict');
566 assert.equal(rethrown.actual, jumped);
567 assert.equal(rethrown.expected, tips.tip);
568 assert.equal(early.c['git-receive-pack'], 0);
569 assert.equal(refOid(strict.bare, 'refs/heads/main'), jumped);
570});
572test('an omitted ref follows HEAD: unborn, born, detached, absent', async () => {
573 const empty = copy(emptyBare);
574 const work = workFrom(empty.bare, 'empty');
575 applyGit(work, 'create');
576 const twin = tryCommit(work);
577 const res = await commit(empty.url, { changes: changesOf('create'), author: AUTHOR, message: 'c' });
578 assert.equal(res.ref, 'refs/heads/main');
579 assert.equal(res.created, true);
580 assert.equal(res.parent, null);
581 sameCommit(empty.bare, res.oid, work, twin.oid);
582 assert.deepEqual(fsckProblems(empty.bare), []);
583 const born = copy(linearBare);
584 const bornWork = workFrom(born.bare, 'tip');
585 applyGit(bornWork, 'create');
586 const bornTwin = tryCommit(bornWork);
587 const onMain = await commit(born.url, { changes: changesOf('create'), author: AUTHOR, message: 'c' });
588 assert.equal(onMain.ref, 'refs/heads/main');
589 assert.equal(onMain.parent, tips.tip);
590 sameCommit(born.bare, onMain.oid, bornWork, bornTwin.oid);
591 const detached = copy(linearBare);
592 git(detached.bare, ['update-ref', '--no-deref', 'HEAD', tips.tip]);
593 const det = await caught(commit(detached.url, { changes: changesOf('create'), author: AUTHOR, message: 'c' }));
594 assert.equal(det.code, 'bad_request');
595 assert.match(det.message, /detached/);
596 assert.equal(refOid(detached.bare, 'refs/heads/main'), tips.tip);
597 // A symref to a branch that has no commit is unborn, including when other branches exist. The new commit is a root.
598 const missingHead = copy(linearBare);
599 git(missingHead.bare, ['symbolic-ref', 'HEAD', 'refs/heads/topic']);
600 const missingWork = workFrom(missingHead.bare, 'empty');
601 applyGit(missingWork, 'create');
602 const missingTwin = tryCommit(missingWork);
603 const onTopic = await commit(missingHead.url, { changes: changesOf('create'), author: AUTHOR, message: 'c' });
604 assert.equal(onTopic.ref, 'refs/heads/topic');
605 assert.equal(onTopic.parent, null);
606 assert.equal(onTopic.created, true);
607 sameCommit(missingHead.bare, onTopic.oid, missingWork, missingTwin.oid);
608 assert.equal(refOid(missingHead.bare, 'refs/heads/main'), tips.tip);
609 // Deleting HEAD makes http-backend 404 the repository. The absent answer is a server that will not name an unborn
610 // HEAD (`lsrefs.unborn ignore`): there is no oid and no target, so the caller must pass ref.
611 const absent = copy(emptyBare);
612 git(absent.bare, ['config', 'lsrefs.unborn', 'ignore']);
613 const miss = await caught(commit(absent.url, { changes: changesOf('create'), author: AUTHOR, message: 'c' }));
614 assert.equal(miss.code, 'not_found');
615 assert.match(miss.message, /absent/);
616 assert.equal(refOid(absent.bare, 'refs/heads/main'), null);
617});
619test('paths git refuses or that ply refuses by name, with the tree git would write', async () => {
620 const nested = copy(linearBare);
621 const nestedWork = workFrom(nested.bare, 'tip');
622 const full = join(nestedWork, 'dir/b.txt');
623 writeFileSync(full, readFileSync(full, 'utf8').replace('b\n', 'B\n'));
624 git(nestedWork, ['add', '--', 'dir/b.txt']);
625 writeRepo(nestedWork, 'n.txt', 'n\n');
626 const nestedTwin = tryCommit(nestedWork);
627 const nestedRes = await commit(nested.url, { ref: 'refs/heads/main', changes: [{ kind: 'edit', path: 'dir/b.txt', find: 'b\n', replace: 'B\n' }, { kind: 'create', path: 'n.txt', content: 'n\n' }], author: AUTHOR, message: 'c' });
628 sameCommit(nested.bare, nestedRes.oid, nestedWork, nestedTwin.oid);
629 const pruned = copy(linearBare);
630 const pruneWork = workFrom(pruned.bare, 'tip');
631 git(pruneWork, ['rm', '-q', '--', 'dir/b.txt']);
632 const pruneTwin = tryCommit(pruneWork);
633 const pruneRes = await commit(pruned.url, { ref: 'refs/heads/main', changes: [{ kind: 'delete', path: 'dir/b.txt' }], author: AUTHOR, message: 'c' });
634 sameCommit(pruned.bare, pruneRes.oid, pruneWork, pruneTwin.oid);
635 assert.equal(git(pruned.bare, ['ls-tree', '-r', '--name-only', pruneRes.oid]).includes('dir/'), false);
636 const moved = copy(linearBare);
637 const moveWork = workFrom(moved.bare, 'tip');
638 git(moveWork, ['mv', 'dir', 'folder']);
639 const moveTwin = tryCommit(moveWork);
640 const moveRes = await commit(moved.url, { ref: 'refs/heads/main', changes: [{ kind: 'rename', from: 'dir', to: 'folder' }], author: AUTHOR, message: 'c' });
641 sameCommit(moved.bare, moveRes.oid, moveWork, moveTwin.oid);
642 const into = copy(linearBare);
643 const intoErr = await caught(commit(into.url, { ref: 'refs/heads/main', changes: [{ kind: 'rename', from: 'dir', to: 'dir/x' }], author: AUTHOR, message: 'c' }));
644 assert.equal(intoErr.code, 'bad_request');
645 assert.match(intoErr.message, /into itself/);
646 assert.equal(refOid(into.bare, 'refs/heads/main'), tips.tip);
647 const missing = copy(emptyBare);
648 const missingErr = await caught(commit(missing.url, { ref: 'refs/heads/main', changes: [{ kind: 'rename', from: 'nope', to: 'nope/x' }], author: AUTHOR, message: 'c' }));
649 assert.equal(missingErr.code, 'bad_request');
650 assert.match(missingErr.message, /into itself/);
651 assert.equal(refOid(missing.bare, 'refs/heads/main'), null);
652 const link = copy(linearBare);
653 const linkErr = await caught(commit(link.url, { ref: 'refs/heads/main', changes: [{ kind: 'edit', path: 'link', find: 'a', replace: 'b' }, { kind: 'create', path: 'n.txt', content: 'n\n' }], author: AUTHOR, message: 'c' }));
654 assert.equal(linkErr.code, 'not_found');
655 assert.match(linkErr.message, /not a regular file/);
656 assert.equal(refOid(link.bare, 'refs/heads/main'), tips.tip);
657 const under = copy(linearBare);
658 const underErr = await caught(commit(under.url, { ref: 'refs/heads/main', changes: [{ kind: 'create', path: 'a.txt/child', content: 'x\n' }], author: AUTHOR, message: 'c' }));
659 assert.equal(underErr.code, 'bad_request');
660 assert.match(underErr.message, /not a directory/);
661 const deep = copy(linearBare);
662 const deepErr = await caught(commit(deep.url, { ref: 'refs/heads/main', changes: [{ kind: 'delete', path: 'missing/x' }], author: AUTHOR, message: 'c' }));
663 assert.equal(deepErr.code, 'not_found');
664 assert.match(deepErr.message, /missing not found/);
665 const emptyLink = copy(emptyBare);
666 const emptySym = await caught(commit(emptyLink.url, { ref: 'refs/heads/main', changes: [{ kind: 'create', path: 's', content: '', mode: '120000' }], author: AUTHOR, message: 'c' }));
667 assert.equal(emptySym.code, 'bad_request');
668 assert.equal(emptySym.what, 'symlink');
669 const nul = copy(emptyBare);
670 const nulErr = await caught(commit(nul.url, { ref: 'refs/heads/main', changes: [{ kind: 'create', path: 's', content: Uint8Array.of(0x61, 0), mode: '120000' }], author: AUTHOR, message: 'c' }));
671 assert.equal(nulErr.code, 'bad_request');
672 assert.equal(nulErr.what, 'symlink');
673 const cancel = copy(emptyBare);
674 const cancelled = await caught(commit(cancel.url, { ref: 'refs/heads/main', changes: [{ kind: 'create', path: 'n.txt', content: 'n\n' }, { kind: 'delete', path: 'n.txt' }], author: AUTHOR, message: 'c' }));
675 assert.equal(cancelled.code, 'bad_request');
676 assert.match(cancelled.message, /nothing to commit/);
677 assert.equal(refOid(cancel.bare, 'refs/heads/main'), null);
678 const same = copy(linearBare);
679 const { c, fetchImpl } = counts();
680 const noop = await commit(same.url, { ref: 'refs/heads/main', changes: [{ kind: 'edit', path: 'a.txt', find: 'v1', replace: 'v1' }], author: AUTHOR, message: 'c' }, { fetchImpl });
681 assert.equal(noop.outcome, 'noop');
682 assert.equal(c.receive, 0);
683 assert.equal(refOid(same.bare, 'refs/heads/main'), tips.tip);
684 const fresh = copy(linearBare);
685 const none = await caught(commit(fresh.url, { ref: 'refs/heads/topic', base: tips.tip, changes: [{ kind: 'edit', path: 'a.txt', find: 'v1', replace: 'v1' }], author: AUTHOR, message: 'c' }));
686 assert.equal(none.code, 'bad_request');
687 assert.match(none.message, /nothing to commit/);
688 assert.equal(refOid(fresh.bare, 'refs/heads/topic'), null);
689 const pointer = 'version https://git-lfs.github.com/spec/v1\noid sha256:' + 'ab'.repeat(32) + '\nsize 1\n';
690 const lfs = copy(linearBare);
691 const lfsWork = workFrom(lfs.bare, 'tip');
692 writeRepo(lfsWork, 'pointer.txt', pointer);
693 gitEnv(lfsWork, ['-c', 'user.name=seed', '-c', 'user.email=seed@x', 'commit', '-q', '-m', 'pointer']);
694 git(lfsWork, ['push', '-q', lfs.bare, 'HEAD:refs/heads/main']);
695 const lfsTip = git(lfs.bare, ['rev-parse', 'main']).trim();
696 const lfsChanges = [{ kind: 'edit', path: 'pointer.txt', find: 'size 1', replace: 'size 2' }, { kind: 'create', path: 'n.txt', content: 'n\n' }];
697 const refused = await caught(commit(lfs.url, { ref: 'refs/heads/main', changes: lfsChanges, author: AUTHOR, message: 'c' }));
698 assert.equal(refused.code, 'lfs_pointer');
699 assert.equal(refOid(lfs.bare, 'refs/heads/main'), lfsTip);
700 const lfsEdit = workFrom(lfs.bare, 'tip');
701 const body = readFileSync(join(lfsEdit, 'pointer.txt'), 'utf8').replace('size 1', 'size 2');
702 writeFileSync(join(lfsEdit, 'pointer.txt'), body);
703 git(lfsEdit, ['add', '--', 'pointer.txt']);
704 writeRepo(lfsEdit, 'n.txt', 'n\n');
705 const lfsTwin = tryCommit(lfsEdit);
706 const allowed = await commit(lfs.url, { ref: 'refs/heads/main', changes: lfsChanges, author: AUTHOR, message: 'c' }, { allowLfsPointer: true });
707 sameCommit(lfs.bare, allowed.oid, lfsEdit, lfsTwin.oid);
708});
710test('verify re-reads the ref, and a signer produces a commit git verify-commit accepts', async () => {
711 const repo = copy(emptyBare);
712 const ex = arrayExporter();
713 const res = await commit(repo.url, { ref: 'refs/heads/main', changes: changesOf('create'), author: AUTHOR, message: 'c' }, { tracer: new Tracer({ exporter: ex }), verify: true });
714 const span = ex.lines.flatMap(flattenLine).find(s => s.name === 'git.commit');
715 assert.equal(span.attrs['git.commit.verify.ref'], res.oid);
716 assert.equal(span.attrs['git.commit.outcome'], 'commit');
717 assert.equal(ex.lines.flatMap(flattenLine).some(s => s.name === 'git.edit.blob'), false);
718 assert.equal(ex.lines.flatMap(flattenLine).some(s => s.name === 'git.commit.apply'), true);
719 const stream = copy(linearBare);
720 const ex2 = arrayExporter();
721 await commit(stream.url, { ref: 'refs/heads/main', changes: changesOf('edit'), author: AUTHOR, message: 'c' }, { tracer: new Tracer({ exporter: ex2 }), verify: true });
722 const names = ex2.lines.flatMap(flattenLine).map(s => s.name);
723 assert.ok(names.includes('git.edit.blob'));
724 assert.equal(names.includes('git.commit.apply'), false);
725 const { signer } = await generateSigner();
726 const signers = join(root, 'allowed_signers');
727 writeFileSync(signers, `${AUTHOR.email} namespaces="git" ${signer.publicKeyLine}\n`);
728 const signed = copy(emptyBare);
729 const made = await commit(signed.url, { ref: 'refs/heads/main', changes: changesOf('create'), author: AUTHOR, message: 'c' }, { signer });
730 const verified = spawnSync('git', ['-C', signed.bare, '-c', 'gpg.format=ssh', '-c', 'gpg.ssh.program=ssh-keygen', '-c', `gpg.ssh.allowedSignersFile=${signers}`, 'verify-commit', made.oid], { encoding: 'utf8' });
731 assert.equal(verified.status, 0, verified.stdout + verified.stderr);
732 const signedEdit = copy(linearBare);
733 const edited = await commit(signedEdit.url, { ref: 'refs/heads/main', changes: changesOf('edit'), author: AUTHOR, message: 'c' }, { signer });
734 const verifiedEdit = spawnSync('git', ['-C', signedEdit.bare, '-c', 'gpg.format=ssh', '-c', 'gpg.ssh.program=ssh-keygen', '-c', `gpg.ssh.allowedSignersFile=${signers}`, 'verify-commit', edited.oid], { encoding: 'utf8' });
735 assert.equal(verifiedEdit.status, 0, verifiedEdit.stdout + verifiedEdit.stderr);
736});
738// git branches from an annotated tag by peeling it (`git checkout -b from-tag v1`). One edit takes the streamed path
739// and two changes the in-memory one: both must start from the commit the tag names, not from the tag object.
740test('a single edit from an annotated-tag base is the commit git makes from that tag', async () => {
741 const { bare, url } = copy(linearBare);
742 gitEnv(bare, ['tag', '-a', '-m', 'v1', 'v1', 'refs/heads/main'], STAMP);
743 const peeled = git(bare, ['rev-parse', 'refs/tags/v1^{commit}']).trim();
744 assert.notEqual(git(bare, ['rev-parse', 'refs/tags/v1']).trim(), peeled);
745 const work = join(root, `tagwork${n++}`);
746 git(root, ['clone', '-q', bare, work]);
747 git(work, ['checkout', '-q', '-b', 'from-tag', 'v1']);
748 writeFileSync(join(work, 'a.txt'), 'v2\n');
749 git(work, ['add', 'a.txt']);
750 gitEnv(work, ['commit', '-q', '-m', 'edit from tag'], STAMP);
751 const want = git(work, ['cat-file', 'commit', 'HEAD']);
752 const r = await commit(url, { ref: 'refs/heads/from-tag', base: 'refs/tags/v1', author: AUTHOR, message: 'edit from tag', changes: [{ kind: 'edit', path: 'a.txt', find: 'v1', replace: 'v2' }] });
753 assert.equal(git(bare, ['cat-file', 'commit', 'refs/heads/from-tag']), want);
754 assert.equal(r.parent, peeled);
755 assert.equal(r.created, true);
756 assert.deepEqual(fsckProblems(bare), []);
757});
Diagnostics4 git calls · 286 ms
CallStatusSizeTime0286 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200145 B · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=145
git.response.cache=HIT
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=145
git.pkt.payload.max=76
git.ls_refs.count=2
git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020038 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=280
git.response.bytes=38434
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
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=6
git.pkt.data=4
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=38434
git.pkt.payload.max=38339
git.sideband.band1.bytes=38338
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=63
git.pack.bytes=38338
git.pack.ofs_delta=0
git.pack.ref_delta=8
git.pack.chain.max=8
git.pack.trailer=0bc36ab7adf82d3109177f11f1ea93f1ce071f77
git.pack.full=55
Open pack map
fetch20010.0 KiB280 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=10220
git.response.cache=MISS
git.response.duration_ms=280
git.fetch.wants=3794adc04ee51579c69bc342cfb11b2fd9de4083
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=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=10220
git.pkt.payload.max=10199
git.sideband.band1.bytes=10198
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://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply.git
Ref
refs/heads/main
Commit
19912563f540228593df0c489082e5e2508e21e4
cf-ray
a5e20efdbe6e7f3b
Request
539c217a…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format