diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 9c38a82..f3abab4 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -285,6 +285,7 @@ range syntax (main..feature, main...feature) also work.`) diffArgs, description, effectiveRef, + pinnedRef: prBase?.oid, version: pkg.version, registryInfo: { repoRoot, repoHash, repoName }, }); diff --git a/packages/cli/src/server.ts b/packages/cli/src/server.ts index 018a9c4..e00476d 100644 --- a/packages/cli/src/server.ts +++ b/packages/cli/src/server.ts @@ -116,6 +116,11 @@ interface ServerOptions { diffArgs: string[]; description?: string; effectiveRef?: string; + /** + * When the instance was started for a pull request, the commit its diff must be taken + * from. A `ref` in the URL that disagrees is corrected rather than honoured. + */ + pinnedRef?: string; version?: string; registryInfo?: { repoRoot: string; @@ -166,6 +171,7 @@ export function startServer(options: ServerOptions): Promise { effectiveRef, version, registryInfo, + pinnedRef, } = options; const includeUntracked = diffArgs.length === 0; @@ -225,6 +231,16 @@ export function startServer(options: ServerOptions): Promise { return; } + // A pull request's diff has to match the one GitHub shows, so a stale ref in the URL — + // a tab left open from an earlier session, a bookmark — is corrected on load. Switching + // revision inside the UI is a client-side navigation and never reaches this. + if (pinnedRef && pathname === '/diff' && url.searchParams.get('ref') !== pinnedRef) { + url.searchParams.set('ref', pinnedRef); + res.writeHead(302, { Location: `${pathname}?${url.searchParams.toString()}` }); + res.end(); + return; + } + if (pathname === '/api/revert-file' && req.method === 'POST') { try { const body = JSON.parse(await readBody(req));