GitHubivarvong
ply
main
1// examples/push-tree.mjs, examples/checkout-tree.mjs and examples/push-repo.mjs, run as a user runs them: a child `node` whose PATH holds node
2// and nothing else (no git on the client), against `git http-backend` (test/server.js), with the credential in the
3// environment behind Basic auth. git is only the judge: push → checkout must give back the source directory byte for
4// byte with its modes and symlinks; `git clone` of the remote must equal the source; a repeat with nothing changed is a
5// noop and a one-file change sends one blob, the trees on its path and the commit.
6import { test, beforeAll as before, afterAll as after } from 'vitest';
7import assert from 'node:assert/strict';
8import { join, dirname } from 'node:path';
9import { mkdirSync, symlinkSync, writeFileSync, chmodSync, readdirSync, lstatSync, readlinkSync, readFileSync } from 'node:fs';
10import { execFile } from 'node:child_process';
11import { promisify } from 'node:util';
12import { fileURLToPath } from 'node:url';
13import { git, tmpRoot, makeBareRepo, startGitServer, startAuthGate, initRepo } from './server.js';
15const REPO = join(dirname(fileURLToPath(import.meta.url)), '..');
16const TOKEN = 'tok-3x4mple';
17let root, cleanup, srv, auth, bin, n = 0;
19before(async () => {
20 ({ root, cleanup } = tmpRoot()); srv = await startGitServer(root);
21 auth = await startAuthGate(srv.url, h => h === `Basic ${Buffer.from(`x-access-token:${TOKEN}`).toString('base64')}`);
22 bin = join(root, 'bin'); mkdirSync(bin); symlinkSync(process.execPath, join(bin, 'node'));
23});
24after(async () => { await auth.close(); await srv.close(); cleanup(); });
26// the example in a child node with PATH = just node; PLY_TOKEN in env, never argv. Async: the servers are in-process.
27const run = async (script, args, env = {}) => (await promisify(execFile)(join(bin, 'node'), [join(REPO, 'examples', script), ...args], {
28 cwd: REPO, encoding: 'utf8',
29 env: { PATH: bin, HOME: root, PLY_TOKEN: TOKEN, GIT_AUTHOR_NAME: 'Example', GIT_AUTHOR_EMAIL: 'ex@example.com', ...env },
30})).stdout;
32// every path under `base` (not .git): type, permission bits, bytes, symlink target
33function snapshot(base, rel = '', out = new Map()) {
34 for (const name of readdirSync(join(base, rel)).sort()) {
35 if (name === '.git') continue;
36 const p = rel ? `${rel}/${name}` : name, st = lstatSync(join(base, p));
37 if (st.isSymbolicLink()) out.set(p, `link ${readlinkSync(join(base, p))}`);
38 else if (st.isDirectory()) snapshot(base, p, out);
39 else out.set(p, `file ${(st.mode & 0o777).toString(8)} ${readFileSync(join(base, p)).toString('base64')}`);
40 }
41 return out;
42}
43const same = (got, want, what) => assert.deepEqual([...snapshot(got)], [...snapshot(want)], what);
45test('examples: push-tree → checkout-tree round-trips byte-identical with modes; git clone == source; repeats are thin', async () => {
46 makeBareRepo(root, 'ex'); const url = `${auth.url}/ex.git`;
47 const src = join(root, 'src');
48 const files = { 'README.md': '# example\n', 'empty': '', 'src/lib/deep/x.js': 'export const x = 1;\n', 'src/main.js': 'import "./lib/deep/x.js";\n', 'dir with space/ünï.txt': 'unicode\n', 'bin/build.sh': '#!/bin/sh\necho build\n', 'data.bin': Buffer.from(Array.from({ length: 70000 }, (_, i) => (i * 2654435761) >>> 24)) };
49 for (const [p, d] of Object.entries(files)) { mkdirSync(dirname(join(src, p)), { recursive: true }); writeFileSync(join(src, p), d); }
50 chmodSync(join(src, 'bin/build.sh'), 0o755); chmodSync(join(src, 'README.md'), 0o644);
51 symlinkSync('src/main.js', join(src, 'link')); symlinkSync('../../README.md', join(src, 'src/lib/up'));
53 // create
54 const out1 = await run('push-tree.mjs', [src, url, '--ref', 'main', '--message', 'first']);
55 const head1 = git(join(root, 'ex.git'), ['rev-parse', 'refs/heads/main']).trim();
56 assert.match(out1, new RegExp(`refs/heads/main: \\(new\\) -> ${head1}`));
57 const sent1 = +/sent (\d+) objects/.exec(out1)[1];
58 assert.ok(/(\d+) pack bytes/.exec(out1)[1] > 0);
59 git(join(root, 'ex.git'), ['fsck', '--strict']);
60 assert.equal(git(join(root, 'ex.git'), ['log', '-1', '--format=%an <%ae>%n%B', 'main']).trim(), 'Example <ex@example.com>\nfirst');
62 // git clone of the remote == source
63 const cloned = join(root, `clone${n++}`); git(root, ['clone', '-q', join(root, 'ex.git'), cloned]);
64 same(cloned, src, 'git clone == source');
65 assert.equal(git(cloned, ['ls-files', '-s', 'bin/build.sh']).split(' ')[0], '100755');
67 // checkout == source
68 const co = join(root, `co${n++}`);
69 const outCo = await run('checkout-tree.mjs', [url, co, '--ref', 'main']);
70 assert.match(outCo, new RegExp(`refs/heads/main @ ${head1} -> `));
71 assert.match(outCo, /wrote 7 files \(\d+ bytes\), 2 symlinks, 0 submodule dirs/);
72 same(co, src, 'checkout == source');
74 // repeat, unchanged: noop, the ref does not move
75 assert.match(await run('push-tree.mjs', [src, url]), /^noop: refs\/heads\/main already has this tree/);
76 assert.equal(git(join(root, 'ex.git'), ['rev-parse', 'refs/heads/main']).trim(), head1);
78 // repeat, one deep file changed: 1 blob + the 4 trees on its path (root, src, src/lib, src/lib/deep) + the commit
79 writeFileSync(join(src, 'src/lib/deep/x.js'), 'export const x = 2;\n');
80 const out3 = await run('push-tree.mjs', [src, url]);
81 const head3 = git(join(root, 'ex.git'), ['rev-parse', 'refs/heads/main']).trim();
82 assert.match(out3, new RegExp(`refs/heads/main: ${head1} -> ${head3}`));
83 assert.match(out3, /sent 6 objects \(1 blobs, 4 trees, 1 commit\)/);
84 assert.ok(6 < sent1, `thin: ${sent1} objects on create`);
85 git(join(root, 'ex.git'), ['fsck', '--strict']);
86 const co2 = join(root, `co${n++}`); await run('checkout-tree.mjs', [url, co2]);
87 same(co2, src, 'checkout after update == source');
88 const cloned2 = join(root, `clone${n++}`); git(root, ['clone', '-q', join(root, 'ex.git'), cloned2]);
89 same(cloned2, src, 'git clone after update == source');
91 // no credential in env: the server's 401 surfaces, nothing pushed
92 await assert.rejects(run('push-tree.mjs', [src, url], { PLY_TOKEN: '' }), e => /auth/.test(e.stderr) && !e.stderr.includes(TOKEN));
93 assert.ok(auth.seen.every(s => s.authorization === null || s.authorization.startsWith('Basic ')), 'only Basic, from env');
94 console.log(` create: ${out1.trim().split('\n')[1].trim()}\n update: ${out3.trim().split('\n')[1].trim()}\n checkout: ${outCo.trim().split('\n')[1].trim()}`);
95});
97test('examples: push-repo with only node on PATH: first push, incremental, up to date, non-fast-forward refused then forced, tags, dry run', async () => {
98 const bare = makeBareRepo(root, 'repo', { stock: true }); const url = `${auth.url}/repo.git`;
99 const local = join(root, 'local-repo');
100 initRepo(local);
101 const commit = (i) => {
102 writeFileSync(join(local, 'app.js'), Array.from({ length: 200 }, (_, k) => `export const v${k} = ${k === i ? 'changed' : k};\n`).join(''));
103 writeFileSync(join(local, `note${i}.md`), `note ${i}\n`);
104 git(local, ['add', '-A']); git(local, ['-c', 'user.name=L', '-c', 'user.email=l@x', 'commit', '-q', '-m', `c${i}`]);
105 };
106 for (let i = 0; i < 4; i++) commit(i);
107 git(local, ['branch', 'old', 'main~2']);
108 git(local, ['-c', 'user.name=L', '-c', 'user.email=l@x', 'tag', '-a', 'v1', '-m', 'v1', 'main~1']);
109 const rev = (d, r) => git(d, ['rev-parse', r]).trim();
110 const fsckBare = () => git(bare, ['fsck', '--strict']);
111 const objects = args => git(local, ['rev-list', '--objects', ...args]).trim().split('\n').length;
113 const out1 = await run('push-repo.mjs', [local, url, 'old:main']);
114 assert.match(out1, /\[new\] refs\/heads\/main/);
115 assert.match(out1, new RegExp(`sent ${objects(['old'])} objects \\(\\d+ deltas\\)`));
116 fsckBare(); assert.equal(rev(bare, 'main'), rev(local, 'old'));
118 const out2 = await run('push-repo.mjs', [local, url, 'main']);
119 assert.match(out2, new RegExp(`${rev(local, 'old').slice(0, 7)}\\.\\.${rev(local, 'main').slice(0, 7)} refs/heads/main`));
120 assert.match(out2, new RegExp(`sent ${objects(['main', '^old'])} objects`));
121 fsckBare(); assert.equal(rev(bare, 'main'), rev(local, 'main'));
123 assert.match(await run('push-repo.mjs', [local, url]), /^Everything up-to-date/);
125 await assert.rejects(run('push-repo.mjs', [local, url, 'old:main']), e => /^not_fast_forward: .*refs\/heads\/main \(non-fast-forward\)/.test(e.stderr));
126 assert.equal(rev(bare, 'main'), rev(local, 'main'));
127 assert.match(await run('push-repo.mjs', [local, url, '+old:refs/heads/rewound']), /\[new\] refs\/heads\/rewound/);
128 assert.match(await run('push-repo.mjs', [local, url, 'main:rewound', '--force']), /\.\.[0-9a-f]{7} refs\/heads\/rewound/);
130 const dry = await run('push-repo.mjs', [local, url, '--tags', '--dry-run']);
131 assert.match(dry, /\[new\] refs\/tags\/v1\ndry run, would send 1 objects/);
132 assert.ok(!git(bare, ['tag', '-l']).includes('v1'));
133 assert.match(await run('push-repo.mjs', [local, url, '--tags']), /sent 1 objects/);
134 assert.equal(git(bare, ['cat-file', '-t', 'v1']).trim(), 'tag');
135 fsckBare();
136 for (const ref of ['refs/heads/main', 'refs/heads/rewound', 'refs/tags/v1']) assert.equal(rev(bare, ref), rev(local, ref.replace('rewound', 'main')), ref);
137});
Diagnostics6 git calls · 197 ms
CallStatusSizeTime0197 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=1 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 · hit2 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=2 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 · hit1 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=1 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=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=5323b9266eea5ded637285d1c5320699f12be76eOpen 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=189 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=be7e0453974a5fe832f9065a33bb7e3434c10e9bOpen pack map
fetch2003.4 KiB189 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=3445 git.response.cache=MISS git.response.duration_ms=189 git.fetch.wants=8dc91cc8235f7783ee2b9779a4874d95790b3307 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=3445 git.pkt.payload.max=3418 git.sideband.band1.bytes=3418 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
- a5e0f838efe7332c
- Request
- c188e6a2…
- 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