main
1// empty: 'keep' against stock git (#117). git is the only judge of the commit bytes.
2//
3// cherry-pick, the change already applied git cherry-pick --keep-redundant-commits
4// cherry-pick, the commit was empty git cherry-pick --allow-empty (and --keep-redundant-commits; same bytes)
5// cherry-pick of a root the parent side is the empty tree; already-contained → --keep-redundant-commits
6// revert, including of a root git revert --no-edit
7// revert that is already applied git revert --no-edit stops; git commit --allow-empty --no-edit writes the
8// message revert prepared (git 2.43 revert has no --allow-empty)
9//
10// Author and committer are different people, times and zones. A cherry-pick keeps the picked commit's author line;
11// a revert's author is the person reverting. Both use the caller's committer.
12import { test, beforeAll as before, afterAll as after } from 'vitest';
13import assert from 'node:assert/strict';
14import { join } from 'node:path';
15import { mkdirSync, writeFileSync, rmSync, mkdtempSync } from 'node:fs';
16import { spawnSync } from 'node:child_process';
17import { tmpdir } from 'node:os';
18import { git, tmpRoot, makeBareRepo, startGitServer, initRepo } from './server.js';
19import { oracleRediff } from './gitversion.js';
20import { cherryPick, revertCommit, pickCommit } from '../src/rebase.js';
21import { Tracer, arrayExporter, flattenLine } from '../src/otel.js';
22import { unwrapAsync } from './unwrap.js';
24const rediff = oracleRediff();
25const BASE_ENV = { ...process.env, GIT_CONFIG_COUNT: '1', GIT_CONFIG_KEY_0: 'commit.gpgsign', GIT_CONFIG_VALUE_0: 'false' };
26const ORIG = { name: 'orig', email: 'orig@x', time: 1400000000, tz: '+0200' };
27const 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: 1500000000 }, { name: 'Cid', email: 'cid@x', time: 1500000001, tz: '+0545' }],
30];
31const envOf = (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'}` });
32const sh = (cwd, args, env = BASE_ENV) => spawnSync('git', ['-C', cwd, ...args], { encoding: 'utf8', env });
33const ok = (cwd, args, env = BASE_ENV) => { const r = sh(cwd, args, env); assert.equal(r.status, 0, `git ${args.join(' ')}: ${r.stderr}${r.stdout}`); return r.stdout.trim(); };
35let root, cleanup, srv, n = 0;
36before(async () => { ({ root, cleanup } = tmpRoot()); srv = await startGitServer(root); });
37after(async () => { await srv?.close(); cleanup?.(); });
39const workOf = () => { const name = `pe${n++}`; const bare = makeBareRepo(root, name); const w = join(root, `w${name}`); mkdirSync(w); const env = envOf(ORIG, ORIG); initRepo(w); return { name, bare, w, env, url: `${srv.url}/${name}.git` }; };
40const commit = (b, msg) => { ok(b.w, ['add', '-A'], b.env); ok(b.w, ['commit', '-q', '--allow-empty', '-m', msg], b.env); return ok(b.w, ['rev-parse', 'HEAD'], b.env); };
41const put = (b, file, text) => writeFileSync(join(b.w, file), text);
42const clone = (bare, branch) => { const d = mkdtempSync(join(tmpdir(), 'pe-')); ok(d, ['clone', '-q', '-b', branch, bare, '.']); return d; };
43const raw = (repo, rev) => ok(repo, ['cat-file', '-p', rev]);
44const tip = (bare, branch) => ok(bare, ['rev-parse', branch]);
45const opts = tracer => ({ tracer, rediff });
46const who = (a, c) => ({ author: a, committer: c });
48// same-content history: root (f.txt), main (m.txt), feat (b.txt, not on main), an empty commit on feat.
49function contentRepo() {
50 const b = workOf();
51 put(b, 'f.txt', 'f\n'); const rootOid = commit(b, 'base');
52 put(b, 'm.txt', 'm\n'); const main = commit(b, 'add m');
53 ok(b.w, ['checkout', '-q', '-b', 'feat', rootOid], b.env);
54 put(b, 'b.txt', 'b\n'); const feat = commit(b, 'add b');
55 const empty = commit(b, 'intentionally empty');
56 ok(b.w, ['push', '-q', b.bare, 'refs/heads/main', 'refs/heads/feat'], b.env);
57 rmSync(b.w, { recursive: true, force: true });
58 return { ...b, rootOid, main, feat, empty };
59}
61// rename already applied on dst, the same rename sitting on src.
62function renameRepo() {
63 const b = workOf();
64 put(b, 'old.txt', 'old\n'); const base = commit(b, 'base');
65 ok(b.w, ['checkout', '-q', '-b', 'src', base], b.env);
66 ok(b.w, ['mv', 'old.txt', 'new.txt'], b.env); const renamed = commit(b, 'rename');
67 ok(b.w, ['checkout', '-q', '-b', 'dst', base], b.env);
68 ok(b.w, ['mv', 'old.txt', 'new.txt'], b.env); commit(b, 'also rename');
69 ok(b.w, ['push', '-q', b.bare, 'refs/heads/src', 'refs/heads/dst'], b.env);
70 rmSync(b.w, { recursive: true, force: true });
71 return { ...b, renamed };
72}
74// root, its child main, and an unrelated orphan `other` (x.txt only).
75function rootRepo() {
76 const b = workOf();
77 put(b, 'f.txt', 'f\n'); const rootOid = commit(b, 'base');
78 put(b, 'm.txt', 'm\n'); const main = commit(b, 'add m');
79 ok(b.w, ['checkout', '-q', '--orphan', 'other'], b.env);
80 ok(b.w, ['rm', '-qrf', '.'], b.env);
81 put(b, 'x.txt', 'x\n'); const other = commit(b, 'other');
82 ok(b.w, ['push', '-q', b.bare, 'refs/heads/main', 'refs/heads/other'], b.env);
83 rmSync(b.w, { recursive: true, force: true });
84 return { ...b, rootOid, main, other };
85}
87function gitPick(dir, oid, env, flag) {
88 const args = ['cherry-pick', ...(flag ? [flag] : []), oid];
89 return sh(dir, args, env);
90}
91// A revert git completed, or — when it stopped because the tree did not change — the commit
92// `git commit --allow-empty --no-edit` writes from the message revert left behind.
93function gitRevert(dir, oid, env, keep) {
94 const before = ok(dir, ['rev-parse', 'HEAD'], env);
95 const r = sh(dir, ['revert', '--no-edit', oid], env);
96 if (r.status === 0) return { wrote: true, before };
97 assert.match(`${r.stdout}${r.stderr}`, /nothing to commit|working tree clean/u, `git revert ${oid}: ${r.stderr}`);
98 assert.equal(ok(dir, ['rev-parse', 'HEAD'], env), before, 'an empty revert must not move HEAD');
99 if (!keep) return { wrote: false, before };
100 const c = sh(dir, ['commit', '--allow-empty', '--no-edit'], env);
101 assert.equal(c.status, 0, `git commit --allow-empty: ${c.stderr}${c.stdout}`);
102 return { wrote: true, before };
103}
105test('cherry-pick --keep-redundant-commits and --allow-empty: commit bytes match git, stop writes nothing', async () => {
106 for (const [a, c] of PAIRS) {
107 const env = envOf(a, c);
108 const r = contentRepo();
109 const d = clone(r.bare, 'main');
110 try {
111 // `add m` is already the tip: its patch is contained, so the pick is empty
112 const stopped = await unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/main', commit: r.main, committer: c, empty: 'stop' }, opts(new Tracer())));
113 assert.equal(stopped.outcome, 'empty');
114 assert.equal(stopped.oid, r.main);
115 assert.equal(tip(r.bare, 'main'), r.main);
116 const plain = gitPick(d, r.main, env);
117 assert.notEqual(plain.status, 0);
118 assert.equal(ok(d, ['rev-parse', 'HEAD'], env), r.main);
119 ok(d, ['cherry-pick', '--abort'], env);
121 const kept = await unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/main', commit: r.main, committer: c, empty: 'keep' }, opts(new Tracer())));
122 assert.equal(kept.outcome, 'kept_empty');
123 assert.equal(kept.onto, r.main);
124 assert.equal(kept.tree, ok(r.bare, ['rev-parse', 'main^{tree}']));
125 const g = gitPick(d, r.main, env, '--keep-redundant-commits');
126 assert.equal(g.status, 0, g.stderr);
127 assert.equal(raw(r.bare, kept.oid), raw(d, 'HEAD'));
129 // an initially empty commit: --allow-empty and --keep-redundant-commits write the same object
130 const d2 = clone(r.bare, 'main');
131 try {
132 const emptyStop = await unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/main', commit: r.empty, committer: c }, opts(new Tracer())));
133 assert.equal(emptyStop.outcome, 'empty');
134 assert.equal(tip(r.bare, 'main'), kept.oid, 'stop does not move the ref the keep just wrote');
135 const allow = gitPick(d2, r.empty, env, '--allow-empty');
136 assert.equal(allow.status, 0, allow.stderr);
137 const d3 = clone(r.bare, 'main');
138 try {
139 // both clones start at the kept redundant commit; the empty stop above did not move main.
140 const again = gitPick(d3, r.empty, env, '--keep-redundant-commits');
141 assert.equal(again.status, 0, again.stderr);
142 assert.equal(raw(d2, 'HEAD'), raw(d3, 'HEAD'));
143 const emptyKept = await unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/main', commit: r.empty, committer: c, empty: 'keep' }, opts(new Tracer())));
144 assert.equal(emptyKept.outcome, 'kept_empty');
145 assert.equal(raw(r.bare, emptyKept.oid), raw(d2, 'HEAD'));
146 } finally { rmSync(d3, { recursive: true, force: true }); }
147 } finally { rmSync(d2, { recursive: true, force: true }); }
148 } finally { rmSync(d, { recursive: true, force: true }); }
149 }
150});
152test('revert of a change already absent: stop writes nothing; keep matches git commit --allow-empty --no-edit', async () => {
153 for (const [a, c] of PAIRS) {
154 const env = envOf(a, c);
155 const r = contentRepo();
156 // first revert removes m.txt; the second is empty
157 const first = await unwrapAsync(revertCommit(r.url, { ref: 'refs/heads/main', commit: r.main, ...who(a, c) }, opts(new Tracer())));
158 assert.equal(first.outcome, 'ok');
159 const d = clone(r.bare, 'main');
160 try {
161 gitRevert(d, r.main, env, false);
162 assert.equal(raw(r.bare, first.oid), raw(d, 'HEAD'));
163 const before = tip(r.bare, 'main');
164 const stopped = await unwrapAsync(revertCommit(r.url, { ref: 'refs/heads/main', commit: r.main, ...who(a, c), empty: 'stop' }, opts(new Tracer())));
165 assert.equal(stopped.outcome, 'empty');
166 assert.equal(stopped.oid, before);
167 assert.equal(tip(r.bare, 'main'), before);
168 gitRevert(d, r.main, env, false);
169 assert.equal(ok(d, ['rev-parse', 'HEAD'], env), first.oid);
170 // git 2.43's empty revert exits without a sequencer (REVERT_HEAD is absent); MERGE_MSG is what --allow-empty reads.
171 const kept = await unwrapAsync(revertCommit(r.url, { ref: 'refs/heads/main', commit: r.main, ...who(a, c), empty: 'keep' }, opts(new Tracer())));
172 assert.equal(kept.outcome, 'kept_empty');
173 assert.equal(kept.tree, ok(r.bare, ['rev-parse', `${before}^{tree}`]));
174 gitRevert(d, r.main, env, true);
175 assert.equal(raw(r.bare, kept.oid), raw(d, 'HEAD'));
176 } finally { rmSync(d, { recursive: true, force: true }); }
177 }
178});
180test('already-applied rename: keep matches git cherry-pick --keep-redundant-commits', async () => {
181 const [a, c] = PAIRS[0];
182 const env = envOf(a, c);
183 const r = renameRepo();
184 const d = clone(r.bare, 'dst');
185 try {
186 const stopped = await unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/dst', commit: r.renamed, committer: c }, opts(new Tracer())));
187 assert.equal(stopped.outcome, 'empty');
188 const plain = gitPick(d, r.renamed, env);
189 assert.notEqual(plain.status, 0, 'git without the flag must not write the empty rename pick');
190 ok(d, ['cherry-pick', '--abort'], env);
191 const kept = await unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/dst', commit: r.renamed, committer: c, empty: 'keep' }, opts(new Tracer())));
192 assert.equal(kept.outcome, 'kept_empty');
193 const g = gitPick(d, r.renamed, env, '--keep-redundant-commits');
194 assert.equal(g.status, 0, g.stderr);
195 assert.equal(raw(r.bare, kept.oid), raw(d, 'HEAD'));
196 } finally { rmSync(d, { recursive: true, force: true }); }
197});
199test('root commit: cherry-pick and revert match git, including an empty keep onto a descendant', async () => {
200 for (const [a, c] of PAIRS) {
201 const env = envOf(a, c);
202 const r = rootRepo();
203 const onto = clone(r.bare, 'other');
204 const down = clone(r.bare, 'main');
205 try {
206 const picked = await unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/other', commit: r.rootOid, committer: c }, opts(new Tracer())));
207 assert.equal(picked.outcome, 'ok');
208 const g = gitPick(onto, r.rootOid, env);
209 assert.equal(g.status, 0, g.stderr);
210 assert.equal(raw(r.bare, picked.oid), raw(onto, 'HEAD'));
211 const reverted = await unwrapAsync(revertCommit(r.url, { ref: 'refs/heads/other', commit: r.rootOid, ...who(a, c) }, opts(new Tracer())));
212 assert.equal(reverted.outcome, 'ok');
213 gitRevert(onto, r.rootOid, env, false);
214 assert.equal(raw(r.bare, reverted.oid), raw(onto, 'HEAD'));
216 // the root's patch is already in main: stop writes nothing, keep matches --keep-redundant-commits
217 const stopped = await unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/main', commit: r.rootOid, committer: c, empty: 'stop' }, opts(new Tracer())));
218 assert.equal(stopped.outcome, 'empty');
219 assert.equal(tip(r.bare, 'main'), r.main);
220 const plain = gitPick(down, r.rootOid, env);
221 assert.notEqual(plain.status, 0);
222 ok(down, ['cherry-pick', '--abort'], env);
223 const kept = await unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/main', commit: r.rootOid, committer: c, empty: 'keep' }, opts(new Tracer())));
224 assert.equal(kept.outcome, 'kept_empty');
225 const gk = gitPick(down, r.rootOid, env, '--keep-redundant-commits');
226 assert.equal(gk.status, 0, gk.stderr);
227 assert.equal(raw(r.bare, kept.oid), raw(down, 'HEAD'));
228 } finally { rmSync(onto, { recursive: true, force: true }); rmSync(down, { recursive: true, force: true }); }
229 }
230});
232test('a caller message on an empty keep replaces git\'s message; the author line does not change', async () => {
233 const [, c] = PAIRS[0];
234 const r = contentRepo();
235 const d = clone(r.bare, 'main');
236 try {
237 const env = envOf(ORIG, c);
238 const kept = await unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/main', commit: r.main, committer: c, empty: 'keep', message: 'custom\n' }, opts(new Tracer())));
239 assert.equal(kept.outcome, 'kept_empty');
240 const g = gitPick(d, r.main, env, '--keep-redundant-commits');
241 assert.equal(g.status, 0, g.stderr);
242 const tree = ok(d, ['rev-parse', 'HEAD^{tree}'], env);
243 const parent = ok(d, ['rev-parse', 'HEAD^'], env);
244 const rewritten = ok(d, ['commit-tree', tree, '-p', parent, '-m', 'custom'], env);
245 assert.equal(raw(r.bare, kept.oid), raw(d, rewritten));
246 } finally { rmSync(d, { recursive: true, force: true }); }
247});
249test('empty other than stop or keep is bad_request; a merge commit is not_linear', async () => {
250 const r = contentRepo();
251 const c = PAIRS[0][1];
252 await assert.rejects(unwrapAsync(pickCommit(r.url, { ref: 'refs/heads/main', commit: r.feat, committer: c, empty: 'drop' }, opts(new Tracer()))), e => e.code === 'bad_request' && e.what === 'empty');
253 assert.equal(tip(r.bare, 'main'), r.main);
254 // a merge: two parents, refused before any transplant. git cherry-pick without -m refuses it too.
255 const w = join(root, 'merge-src'); mkdirSync(w);
256 const env = envOf(ORIG, ORIG);
257 ok(w, ['clone', '-q', r.bare, '.'], env);
258 ok(w, ['checkout', '-q', '-b', 'side'], env);
259 writeFileSync(join(w, 's.txt'), 's\n');
260 ok(w, ['add', '-A'], env); ok(w, ['commit', '-q', '-m', 'side'], env);
261 ok(w, ['checkout', '-q', 'main'], env);
262 ok(w, ['merge', '--no-ff', '-q', '-m', 'merge', 'side'], env);
263 const merge = ok(w, ['rev-parse', 'HEAD'], env);
264 ok(w, ['push', '-q', r.bare, 'HEAD:refs/heads/main'], env);
265 const d = clone(r.bare, 'main');
266 try {
267 await assert.rejects(unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/main', commit: merge, committer: c }, opts(new Tracer()))), e => e.code === 'not_linear');
268 const g = gitPick(d, merge, envOf(PAIRS[0][0], c));
269 assert.notEqual(g.status, 0);
270 assert.match(`${g.stderr}`, /is a merge but no -m option was given/u);
271 } finally { rmSync(d, { recursive: true, force: true }); rmSync(w, { recursive: true, force: true }); }
272});
274test('spans record empty stop or keep, and a kept commit carries the same result attributes as a written one', async () => {
275 const r = contentRepo();
276 const c = PAIRS[0][1];
277 const stopEx = arrayExporter();
278 const stopped = await unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/main', commit: r.main, committer: c, empty: 'stop' }, opts(new Tracer({ exporter: stopEx }))));
279 assert.equal(stopped.outcome, 'empty');
280 const stopRoot = stopEx.lines.flatMap(flattenLine).find(s => s.name === 'git.cherry_pick');
281 assert.equal(stopRoot.attrs['git.cherry_pick.empty'], 'stop');
282 assert.equal(stopRoot.attrs['git.cherry_pick.outcome'], 'empty');
283 assert.equal(stopRoot.attrs['git.cherry_pick.new'], undefined);
284 const keepEx = arrayExporter();
285 const kept = await unwrapAsync(cherryPick(r.url, { ref: 'refs/heads/main', commit: r.main, committer: c, empty: 'keep' }, opts(new Tracer({ exporter: keepEx }))));
286 assert.equal(kept.outcome, 'kept_empty');
287 const keepRoot = keepEx.lines.flatMap(flattenLine).find(s => s.name === 'git.cherry_pick');
288 assert.equal(keepRoot.attrs['git.cherry_pick.empty'], 'keep');
289 assert.equal(keepRoot.attrs['git.cherry_pick.outcome'], 'kept_empty');
290 assert.equal(keepRoot.attrs['git.cherry_pick.new'], kept.oid);
291 assert.equal(keepRoot.attrs['git.cherry_pick.tree'], kept.tree);
292 assert.equal(typeof keepRoot.attrs['git.cherry_pick.push.bytes'], 'number');
293 assert.equal(typeof keepRoot.attrs['git.cherry_pick.trees.fetched'], 'number');
294});
Diagnostics4 git calls · 568 ms
CallStatusSizeTime0568 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=2 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 B2 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=145 git.response.cache=UPDATING git.response.duration_ms=2 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 · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=562 git.response.bytes=38434 git.response.cache=HIT git.response.duration_ms=2 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=55Open pack map
fetch2004.2 KiB563 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=4293 git.response.cache=MISS git.response.duration_ms=563 git.fetch.wants=8bb3f9f845b1ec1dff7234b61a2b724fb438e11a 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=4293 git.pkt.payload.max=4272 git.sideband.band1.bytes=4271 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
- a5e18fea9cc85b65
- Request
- 1ad25a07…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format