GitHubivarvong
ply
main
1// Author and committer stamps, decided by git (#103 item 4): each stamp carries its own name, email, time and zone, and
2// merge `from` / rebase `onto` take a 40-hex commit id as well as a branch. For every stamp pair and every operation, ply
3// writes the commit(s) against a bare repository and git writes them in a clone of the same repository with
4// GIT_AUTHOR_* / GIT_COMMITTER_* set to the same stamps; the raw objects (`git cat-file -p`) must be byte-equal.
5import { test, beforeAll as before, afterAll as after } from 'vitest';
6import assert from 'node:assert/strict';
7import { join } from 'node:path';
8import { mkdirSync, writeFileSync, rmSync, mkdtempSync } from 'node:fs';
9import { spawnSync } from 'node:child_process';
10import { tmpdir } from 'node:os';
11import { git, tmpRoot, makeBareRepo, startGitServer, initRepo } from './server.js';
12import { oracleRediff } from './gitversion.js';
13import { mergeBranch } from '../src/merge.js';
14import { rebaseBranch, cherryPick, revertCommit } from '../src/rebase.js';
15import { Tracer } from '../src/otel.js';
16import { unwrapAsync } from './unwrap.js';
18const rediff = oracleRediff();
19const BASE_ENV = { ...process.env, GIT_CONFIG_COUNT: '1', GIT_CONFIG_KEY_0: 'commit.gpgsign', GIT_CONFIG_VALUE_0: 'false' };
20const as = (a, c) => ({ ...BASE_ENV, GIT_AUTHOR_NAME: a.name, GIT_AUTHOR_EMAIL: a.email, GIT_AUTHOR_DATE: `@${a.time} ${a.tz ?? '+0000'}`, GIT_COMMITTER_NAME: c.name, GIT_COMMITTER_EMAIL: c.email, GIT_COMMITTER_DATE: `@${c.time} ${c.tz ?? '+0000'}` });
21const run = (cwd, args, env) => { const r = spawnSync('git', ['-C', cwd, ...args], { encoding: 'utf8', env }); assert.equal(r.status, 0, `git ${args.join(' ')}: ${r.stderr}${r.stdout}`); return r.stdout.trim(); };
23// the history's own commits, by someone else at another time
24const ORIG = { name: 'orig', email: 'orig@x', time: 1400000000, tz: '+0200' };
25// stamp pairs: author, committer — zones either side of UTC, the civil extremes, a half and a quarter hour, and an
26// omitted zone (+0000)
27export const PAIRS = [
28 [{ name: 'Ann', email: 'ann@x', time: 1700000000, tz: '+0530' }, { name: 'Cid', email: 'cid@x', time: 1700003600, tz: '-0800' }],
29 [{ name: 'Ann', email: 'ann@x', time: 1600000000, tz: '-1200' }, { name: 'Cid', email: 'cid@x', time: 1600000123, tz: '+1400' }],
30 [{ name: 'Ann', email: 'ann@x', time: 1500000000 }, { name: 'Cid', email: 'cid@x', time: 1500000001, tz: '+0545' }],
31];
32export const OPS = ['merge from a branch', 'merge from a commit id', 'rebase onto a branch', 'rebase onto a commit id', 'squash', 'cherry-pick', 'revert'];
34let root, cleanup, srv, n = 0;
35before(async () => { ({ root, cleanup } = tmpRoot()); srv = await startGitServer(root); });
36after(async () => { await srv?.close(); cleanup?.(); });
38// base ← main (m.txt); base ← feat (a.txt, then b.txt). Returns the bare repo and the oids.
39function build() {
40 const name = `st${n++}`; const bare = makeBareRepo(root, name); const w = join(root, `w${name}`); mkdirSync(w);
41 const env = as(ORIG, ORIG);
42 initRepo(w);
43 const commit = (file, msg) => { writeFileSync(join(w, file), `${file}\n`); run(w, ['add', '-A'], env); run(w, ['commit', '-q', '-m', msg], env); return run(w, ['rev-parse', 'HEAD'], env); };
44 const base = commit('f.txt', 'base');
45 const main = commit('m.txt', 'm');
46 run(w, ['checkout', '-q', '-b', 'feat', base], env);
47 commit('a.txt', 'a'); const feat = commit('b.txt', 'b');
48 run(w, ['push', '-q', bare, 'refs/heads/main', 'refs/heads/feat'], env);
49 rmSync(w, { recursive: true, force: true });
50 return { name, bare, url: `${srv.url}/${name}.git`, main, feat };
51}
52const clone = (bare, branch) => { const d = mkdtempSync(join(tmpdir(), 'st-')); run(d, ['clone', '-q', '-b', branch, bare, '.'], BASE_ENV); return d; };
53const raw = (repo, oid) => run(repo, ['cat-file', '-p', oid], BASE_ENV);
54// the commits from..to, oldest first, as raw objects
55const range = (repo, from, to) => run(repo, ['rev-list', '--reverse', `${from}..${to}`], BASE_ENV).split('\n').filter(Boolean).map(o => raw(repo, o));
57// one cell: [ply's raw commits, git's raw commits]
58async function cell(op, a, c) {
59 const r = build(); const opts = { tracer: new Tracer(), rediff };
60 const d = clone(r.bare, op.startsWith('rebase') || op === 'squash' ? 'feat' : 'main');
61 try {
62 const env = as(a, c);
63 if (op.startsWith('merge')) {
64 const from = op === 'merge from a branch' ? 'refs/heads/feat' : r.feat;
65 const res = await unwrapAsync(mergeBranch(r.url, { ref: 'refs/heads/main', from, author: a, committer: c, message: 'merge msg', noFastForward: true }, opts));
66 run(d, ['merge', '--no-ff', '-q', '-m', 'merge msg', r.feat], env);
67 return [[raw(r.bare, res.oid)], [raw(d, 'HEAD')]];
68 }
69 if (op.startsWith('rebase')) {
70 const onto = op === 'rebase onto a branch' ? 'refs/heads/main' : r.main;
71 const res = await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/feat', onto, committer: c }, opts));
72 run(d, ['rebase', '-q', r.main], env);
73 return [range(r.bare, r.main, res.oid), range(d, r.main, 'HEAD')];
74 }
75 if (op === 'squash') {
76 const res = await unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/feat', onto: 'refs/heads/main', committer: c, author: a, squash: true, message: 'squash msg\n' }, opts));
77 run(d, ['checkout', '-q', '-B', 'sq', r.main], env); run(d, ['merge', '--squash', '-q', r.feat], env); run(d, ['commit', '-q', '-m', 'squash msg'], env);
78 return [[raw(r.bare, res.oid)], [raw(d, 'HEAD')]];
79 }
80 if (op === 'cherry-pick') {
81 const res = await unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/main', commit: r.feat, committer: c }, opts));
82 run(d, ['cherry-pick', r.feat], env);
83 return [[raw(r.bare, res.oid)], [raw(d, 'HEAD')]];
84 }
85 const res = await unwrapAsync(revertCommit(r.url, { ref: 'refs/heads/main', commit: r.main, committer: c, author: a }, opts));
86 run(d, ['revert', '--no-edit', r.main], env);
87 return [[raw(r.bare, res.oid)], [raw(d, 'HEAD')]];
88 } finally { rmSync(d, { recursive: true, force: true }); }
89}
91test('author and committer stamps with zones, from/onto by commit id: every commit byte-equal to git\'s', { timeout: 10 * 60_000 }, async () => {
92 const bad = []; let cells = 0, commits = 0;
93 for (const [i, [a, c]] of PAIRS.entries()) for (const op of OPS) {
94 const [ours, theirs] = await cell(op, a, c); cells++; commits += theirs.length;
95 assert.ok(theirs.length > 0, `${op}: git wrote nothing`);
96 if (JSON.stringify(ours) !== JSON.stringify(theirs)) bad.push(`pair ${i} ${op}:\n ply: ${JSON.stringify(ours)}\n git: ${JSON.stringify(theirs)}`);
97 }
98 console.log(`# stamps: ${cells} cells (${PAIRS.length} stamp pairs × ${OPS.length} operations), ${commits} commits compared, ${bad.length} differ`);
99 assert.deepEqual(bad, []);
100});
102test('from / onto: the zero oid is refused before any request; an oid that is not a commit is not_found', async () => {
103 const r = build(); const opts = { tracer: new Tracer(), rediff };
104 const zero = '0'.repeat(40);
105 await assert.rejects(unwrapAsync(mergeBranch(r.url, { from: zero, author: PAIRS[0][0] }, opts)), e => e.code === 'bad_request' && e.what === 'from');
106 await assert.rejects(unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/feat', onto: zero, committer: PAIRS[0][1] }, opts)), e => e.code === 'bad_request' && e.what === 'onto');
107 const tree = run(r.bare, ['rev-parse', 'main^{tree}'], BASE_ENV);
108 await assert.rejects(unwrapAsync(mergeBranch(r.url, { from: tree, author: PAIRS[0][0] }, opts)), e => e.code === 'not_found' && e.what === 'from' && e.miss?.kind === 'commit' && e.miss.miss.kind === 'not_commit' && e.miss.miss.type === 'tree' && e.miss.miss.oid === tree);
109 const onto = 'f'.repeat(40);
110 await assert.rejects(unwrapAsync(rebaseBranch(r.url, { ref: 'refs/heads/feat', onto, committer: PAIRS[0][1] }, opts)), e => e.code === 'not_found' && e.what === 'onto' && e.miss?.kind === 'commit' && e.miss.miss.kind === 'absent' && e.miss.miss.oid === onto);
111 assert.equal(run(r.bare, ['rev-parse', 'main'], BASE_ENV), r.main, 'nothing pushed');
112 assert.equal(run(r.bare, ['rev-parse', 'feat'], BASE_ENV), r.feat, 'nothing pushed');
113});
Diagnostics6 git calls · 34 ms
CallStatusSizeTime034 ms
discover200191 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch 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 · 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=11990 git.response.cache=HIT git.response.duration_ms=3 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 · 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=4347 git.response.cache=HIT git.response.duration_ms=2 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=10Open 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=3 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=5323b9266eea5ded637285d1c5320699f12be76eOpen pack map
fetchtree:02005.6 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=24 git.response.bytes=5724 git.response.cache=HIT git.response.duration_ms=3 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=be7e0453974a5fe832f9065a33bb7e3434c10e9bOpen pack map
fetch2002.9 KiB · hit24 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2942 git.response.cache=HIT git.response.duration_ms=24 git.fetch.wants=5bd1bdfcc55baf58a2869f0662251b0951d1a762 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=2942 git.pkt.payload.max=2915 git.sideband.band1.bytes=2915 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
- a5e2cc261ad27929
- Request
- 2de18103…
- 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