import type { CheckoutChanges, CheckoutFile } from './api'; import type { FileChanges } from './review'; // The diff client shows one list of changed files whatever it reads: a turn's // changes from its history, or the checkout's from the daemon. /** Where the changes come from: one turn, or the checkout against HEAD or its upstream. */ export type Scope = 'turn' | 'uncommitted' | 'upstream'; export type Status = CheckoutFile['status']; export interface ChangedFile { path: string; oldPath: string | null; status: Status; added: number; removed: number; /** One diff per change the turn made to the file, or one for the checkout; `reason` when there is none to show. */ parts: { diff: string; reason: string | null }[]; } const omitted: Record, string> = { binary: 'a binary file', too_large: 'too large to show here; open the file', budget: 'past what one read shows; open the file', }; export function fromCheckout(changes: CheckoutChanges): ChangedFile[] { return changes.files.map((file) => ({ path: file.path, oldPath: file.old_path, status: file.status, added: file.added, removed: file.removed, parts: [{ diff: file.diff ?? '', reason: file.omitted ? omitted[file.omitted] : null }], })); } export function fromTurn(files: FileChanges[]): ChangedFile[] { return files.map((file) => { const first = file.changes[0]; const last = file.changes.at(-1)?.diff ?? ''; // A change that made the file records no text before it. const created = first?.old_text === null || /^(--- \/dev\/null|@@ -0,0 )/m.test(first?.diff ?? ''); const status: Status = created ? 'added' : /^\+\+\+ \/dev\/null$/m.test(last) ? 'deleted' : 'modified'; return { path: file.path, oldPath: null, status, added: file.added, removed: file.removed, parts: file.changes.map((change) => ({ diff: change.diff, reason: change.reason ?? null })), }; }); } /** * The `change` number a part's diff goes by in notes' anchors (anchors.ts * finds lines by window, path and change): a turn's parts count from 0, * the checkout's bases and a whole file sit apart, so a note made in one * scope never lands on lines of another. */ export function partChange(scope: Scope, index: number): number { return scope === 'turn' ? index : scope === 'uncommitted' ? -2 : -3; } /** A whole file's change number: notes on a file opened in a tab. */ export const FILE_CHANGE = -1;