A commit reached by a short oid is kept too
Misha Kaletsky, 2026-11-24 20:23 UTC · 9ddbacf46308b4e6b73a6c81ca02a90ece00667a · parent a0d6f5c
2 files changed
M app.ts
@@ -364,10 +364,14 @@ const { repo } = view; if (!/^[0-9a-f]{7,40}$/.test(wanted)) return page(view, 404, "commits", "Not found", note("That is no commit's oid."), null); - const keptText = wanted.length === 40 ? await itx.kv.get(`git-app:commit:${repo}:${wanted}`) : null; - let commit: KeptCommit; - if (keptText) commit = KeptCommit.parse(JSON.parse(keptText)); - else { + // A commit never changes, so what the app worked out of it is kept under its whole oid. A + // shorter oid is found in the log first. + const keptOf = async (oid: string) => { + const text = await itx.kv.get(`git-app:commit:${repo}:${oid}`); + return text ? KeptCommit.parse(JSON.parse(text)) : null; + }; + let commit = wanted.length === 40 ? await keptOf(wanted) : null; + if (!commit) { const found = (await handle.log({ limit: COMMITS_SEARCHED })).find(({ oid }) => oid.startsWith(wanted), ); @@ -377,15 +381,18 @@ ); return page(view, 404, "commits", "Not found", missing, null); } - commit = { - oid: found.oid, - parents: found.parents, - message: found.message, - author: found.author.name, - timestamp: found.timestamp, - changes: await changesOf(handle, found), - }; - await itx.kv.put(`git-app:commit:${repo}:${commit.oid}`, JSON.stringify(commit)); + commit = wanted.length === 40 ? null : await keptOf(found.oid); + if (!commit) { + commit = { + oid: found.oid, + parents: found.parents, + message: found.message, + author: found.author.name, + timestamp: found.timestamp, + changes: await changesOf(handle, found), + }; + await itx.kv.put(`git-app:commit:${repo}:${commit.oid}`, JSON.stringify(commit)); + } } const [subject, ...rest] = commit.message.trim().split("\n");
M tests/pages.test.mjs
@@ -184,6 +184,15 @@ assert.deepEqual(dataOf(html), { diffs: [null, { path: "small.ts", before: "1\n", after: "2\n" }], }); + + // the short oid's second reader asks the log which commit it is, and no files + await git.fetch(new Request(`https://git.example.com/repos/kernel/commit/${short}`), mind); + assert.deepEqual(mind.asked, [ + "log /repos/kernel", + "files /repos/kernel", + "files /repos/kernel", + "log /repos/kernel", + ]); }); test("a repo that is not public is for members: anyone else gets the mind's sign-in, and the repo is not read", async () => {