Skip to content

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

TypeScript

mods/diff: renames list as git prints them with no body, a file that turned up since the start is session work, and each file but the diff's last keeps git's closing line

poteat883b12cc195d
diff --git a/mods/diff/hooks/git/display-path-of/display-path-of.ts b/mods/diff/hooks/git/display-path-of/display-path-of.ts
new file mode 100644
index 00000000..2a8cd95b
--- /dev/null
+++ b/mods/diff/hooks/git/display-path-of/display-path-of.ts
@@ -0,0 +1,17 @@
+import { renameDisplayOf } from '../rename-display-of'
+import type Types from '../types'
+
+/**
+ * A row's name as the built-in lists it: the path, or for a rename the one
+ * line `git diff --stat` prints for it (renameDisplayOf).
+ *
+ * @param file the row
+ * @returns the name to draw and to sort by
+ */
+export function displayPathOf(
+ file: Pick<Types.FileStat, 'path' | 'renamedFrom'>,
+): string {
+ const { renamedFrom, path } = file
+
+ return renamedFrom === null ? path : renameDisplayOf(renamedFrom, path)
+}
diff --git a/mods/diff/hooks/git/display-path-of/index.ts b/mods/diff/hooks/git/display-path-of/index.ts
new file mode 100644
index 00000000..cc631ae8
--- /dev/null
+++ b/mods/diff/hooks/git/display-path-of/index.ts
@@ -0,0 +1,3 @@
+export * from './display-path-of.js'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/git/fetch-file-hunks/fetch-file-hunks.ts b/mods/diff/hooks/git/fetch-file-hunks/fetch-file-hunks.ts
index acfbc3dd..49cf11d3 100644
--- a/mods/diff/hooks/git/fetch-file-hunks/fetch-file-hunks.ts
+++ b/mods/diff/hooks/git/fetch-file-hunks/fetch-file-hunks.ts
@@ -2,14 +2,15 @@ import Argv from '../argv'
import { EMPTY_FILE_HUNKS } from '../empty-file-hunks'
import Parse from '../parse'
import type Types from '../types'
+import { isLastOfDiff } from './is-last-of-diff'
/**
* One file's hunks against the base its row was read against, so body and
* counts agree, by literal pathspec, as the built-in panel reads hunks.
*
- * A rename passes both its paths so git pairs them; an untracked or binary
- * row, or on an unborn HEAD a file edited after staging, has no honest
- * body and answers no hunks.
+ * An untracked, binary or renamed row (the built-in shows a rename's
+ * counts, never its body), or on an unborn HEAD a file edited after
+ * staging, answers no hunks; the diff's last file, no closing empty line.
*
* @param run runs git against the pinned repository
* @param data the fetch the row belongs to
@@ -22,7 +23,10 @@ export async function fetchFileHunks(
file: Types.FileStat,
): Promise<Types.FileHunks | null> {
const hasNoBody =
- file.isUntracked || file.isBinary || data.stalePaths.includes(file.path)
+ file.isUntracked ||
+ file.isBinary ||
+ file.renamedFrom !== null ||
+ data.stalePaths.includes(file.path)
if (hasNoBody) {
return EMPTY_FILE_HUNKS
@@ -33,9 +37,10 @@ export async function fetchFileHunks(
...Argv.DIFF_LEADING_ARGS,
data.baseRef,
'--',
- ...(file.renamedFrom === null ? [] : [file.renamedFrom]),
file.path,
])
- return exitCode === 0 ? Parse.parseFileDiff(stdout) : null
+ const printed = isLastOfDiff(data, file) ? stdout.replace(/\n$/, '') : stdout
+
+ return exitCode === 0 ? Parse.parseFileDiff(printed) : null
}
diff --git a/mods/diff/hooks/git/fetch-file-hunks/index.ts b/mods/diff/hooks/git/fetch-file-hunks/index.ts
index 1fb21a84..d842c72e 100644
--- a/mods/diff/hooks/git/fetch-file-hunks/index.ts
+++ b/mods/diff/hooks/git/fetch-file-hunks/index.ts
@@ -1,3 +1,4 @@
export * from './fetch-file-hunks.js'
+export * from './is-last-of-diff'
export * as default from '.'
diff --git a/mods/diff/hooks/git/fetch-file-hunks/is-last-of-diff/index.ts b/mods/diff/hooks/git/fetch-file-hunks/is-last-of-diff/index.ts
new file mode 100644
index 00000000..127f8704
--- /dev/null
+++ b/mods/diff/hooks/git/fetch-file-hunks/is-last-of-diff/index.ts
@@ -0,0 +1,3 @@
+export * from './is-last-of-diff.js'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/git/fetch-file-hunks/is-last-of-diff/is-last-of-diff.ts b/mods/diff/hooks/git/fetch-file-hunks/is-last-of-diff/is-last-of-diff.ts
new file mode 100644
index 00000000..930c178c
--- /dev/null
+++ b/mods/diff/hooks/git/fetch-file-hunks/is-last-of-diff/is-last-of-diff.ts
@@ -0,0 +1,18 @@
+import type Types from '../../types'
+
+/**
+ * Whether a row is the last file of the whole diff git printed for its
+ * fetch: the last tracked row, the untracked ones being listed apart.
+ *
+ * The built-in reads that diff in one piece and trims its end, so the last
+ * file alone loses git's closing empty line and the context row it becomes.
+ *
+ * @param data the fetch the row belongs to
+ * @param file the row
+ * @returns whether git printed this file last
+ */
+export const isLastOfDiff = (
+ data: Types.DiffData,
+ file: Types.FileStat,
+): boolean =>
+ data.files.filter(row => !row.isUntracked).at(-1)?.path === file.path
diff --git a/mods/diff/hooks/git/git-backend-of.ts b/mods/diff/hooks/git/git-backend-of.ts
index ab611aef..57da1213 100644
--- a/mods/diff/hooks/git/git-backend-of.ts
+++ b/mods/diff/hooks/git/git-backend-of.ts
@@ -14,7 +14,8 @@ import type Types from './types'
* one `rev-parse` in that directory, then every child pinned to it.
*
* Each child runs with `--git-dir`, `--work-tree` and cwd the top, under
- * the C locale and the fetch's timeout (the built-in's execPinnedGit).
+ * the C locale and the fetch's timeout (the built-in's execPinnedGit); the
+ * paths dirty at the start are read once, first (Probes.dirtyPathsOf).
*
* @param host the bound host's runner and probes
* @returns the backend, or null outside a git working tree
@@ -46,6 +47,7 @@ export async function gitBackendOf(
}
const run = runOf(repository)
+ const baseline = await Probes.dirtyPathsOf(run)
const depsOf = (): Types.GitDeps => ({
run,
@@ -53,6 +55,7 @@ export async function gitBackendOf(
mtimeOf: host.mtimeOf,
entryKindsOf: host.entryKindsOf,
sessionStartMs: host.sessionStartMsOf(),
+ baseline,
onBranchBase: host.onBranchBase,
})
diff --git a/mods/diff/hooks/git/index.ts b/mods/diff/hooks/git/index.ts
index 1222c76a..490d9356 100644
--- a/mods/diff/hooks/git/index.ts
+++ b/mods/diff/hooks/git/index.ts
@@ -1,5 +1,6 @@
export * from './argv'
export * from './cut-hunks'
+export * from './display-path-of'
export * from './empty-file-hunks'
export * from './failed-run'
export * from './fetch-diff'
@@ -11,6 +12,7 @@ export * from './hunk-of'
export * from './large-file-hunks'
export * from './parse'
export * from './probes'
+export * from './rename-display-of'
export * from './tiers'
export * from './types'
diff --git a/mods/diff/hooks/git/parse/body-line-of/body-line-of.ts b/mods/diff/hooks/git/parse/body-line-of/body-line-of.ts
new file mode 100644
index 00000000..ffe42e3e
--- /dev/null
+++ b/mods/diff/hooks/git/parse/body-line-of/body-line-of.ts
@@ -0,0 +1,8 @@
+/**
+ * A body line as a hunk keeps it: git's closing empty line becomes the
+ * empty context row the built-in panel draws for it; the rest stand.
+ *
+ * @param line a line isBodyLine passed
+ * @returns the line, a lone space for the empty one
+ */
+export const bodyLineOf = (line: string) => (line === '' ? ' ' : line)
diff --git a/mods/diff/hooks/git/parse/body-line-of/index.ts b/mods/diff/hooks/git/parse/body-line-of/index.ts
new file mode 100644
index 00000000..c263ed3e
--- /dev/null
+++ b/mods/diff/hooks/git/parse/body-line-of/index.ts
@@ -0,0 +1,3 @@
+export * from './body-line-of.js'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/git/parse/index.ts b/mods/diff/hooks/git/parse/index.ts
index b3f08909..cb229a3d 100644
--- a/mods/diff/hooks/git/parse/index.ts
+++ b/mods/diff/hooks/git/parse/index.ts
@@ -1,3 +1,4 @@
+export * from './body-line-of'
export * from './hunk-header'
export * from './is-body-line'
export * from './is-whole-answer'
diff --git a/mods/diff/hooks/git/parse/is-body-line/is-body-line.ts b/mods/diff/hooks/git/parse/is-body-line/is-body-line.ts
index a0bc81fd..09054a5a 100644
--- a/mods/diff/hooks/git/parse/is-body-line/is-body-line.ts
+++ b/mods/diff/hooks/git/parse/is-body-line/is-body-line.ts
@@ -2,8 +2,14 @@
* Whether a line after a hunk header is a body line (added, removed or
* context) rather than a note such as `\ No newline at end of file`.
*
+ * The empty line that ends git's output counts, as the built-in panel
+ * counts it: the last hunk of a file closes on one more context row.
+ *
* @param line one line of `git diff` output
- * @returns whether it starts with `+`, `-` or a space
+ * @returns whether it starts with `+`, `-` or a space, or is empty
*/
export const isBodyLine = (line: string) =>
- line.startsWith('+') || line.startsWith('-') || line.startsWith(' ')
+ line === '' ||
+ line.startsWith('+') ||
+ line.startsWith('-') ||
+ line.startsWith(' ')
diff --git a/mods/diff/hooks/git/parse/parse-file-diff.ts b/mods/diff/hooks/git/parse/parse-file-diff.ts
index 60c31d66..947de76c 100644
--- a/mods/diff/hooks/git/parse/parse-file-diff.ts
+++ b/mods/diff/hooks/git/parse/parse-file-diff.ts
@@ -3,6 +3,7 @@ import { cutHunks } from '../cut-hunks'
import { hunkOf } from '../hunk-of'
import { LARGE_FILE_HUNKS } from '../large-file-hunks'
import type Types from '../types'
+import { bodyLineOf } from './body-line-of'
import { HUNK_HEADER } from './hunk-header'
import { isBodyLine } from './is-body-line'
@@ -32,7 +33,10 @@ export function parseFileDiff(stdout: string): Types.FileHunks {
headerRows.map((at, ordinal) =>
hunkOf(
HUNK_HEADER.exec(rows[at] ?? '')?.groups,
- rows.slice(at + 1, headerRows[ordinal + 1]).filter(isBodyLine),
+ rows
+ .slice(at + 1, headerRows[ordinal + 1])
+ .filter(isBodyLine)
+ .map(bodyLineOf),
),
),
),
diff --git a/mods/diff/hooks/git/probes/dirty-paths-of/dirty-paths-of.ts b/mods/diff/hooks/git/probes/dirty-paths-of/dirty-paths-of.ts
new file mode 100644
index 00000000..b5e80d90
--- /dev/null
+++ b/mods/diff/hooks/git/probes/dirty-paths-of/dirty-paths-of.ts
@@ -0,0 +1,35 @@
+import Argv from '../../argv'
+import Parse from '../../parse'
+import type Types from '../../types'
+import { STATUS_PATH_OFFSET } from './status-path-offset'
+
+/**
+ * Every path dirty in the working tree right now, tracked changes and
+ * untracked files alike, as `git status -z` lists them; null unread.
+ *
+ * Read once when the backend is pinned, so a fetch can tell a path that
+ * was already dirty when the session began from one that turned up since
+ * (GitDeps `baseline`). Renames are not paired: each side is its own path.
+ *
+ * @param run runs git against the pinned repository
+ * @returns the paths, root-relative, or null on a failed or cut listing
+ */
+export async function dirtyPathsOf(
+ run: Types.GitRun,
+): Promise<ReadonlySet<string> | null> {
+ const listing = await run([
+ Argv.NO_OPTIONAL_LOCKS,
+ 'status',
+ '--porcelain',
+ '-z',
+ '--untracked-files=all',
+ '--no-renames',
+ '--ignore-submodules=dirty',
+ ])
+
+ const records = listing.stdout.split('\0').filter(record => record !== '')
+
+ return Parse.isWholeAnswer(listing)
+ ? new Set(records.map(record => record.slice(STATUS_PATH_OFFSET)))
+ : null
+}
diff --git a/mods/diff/hooks/git/probes/dirty-paths-of/index.ts b/mods/diff/hooks/git/probes/dirty-paths-of/index.ts
new file mode 100644
index 00000000..dd0d03bb
--- /dev/null
+++ b/mods/diff/hooks/git/probes/dirty-paths-of/index.ts
@@ -0,0 +1,4 @@
+export * from './dirty-paths-of.js'
+export * from './status-path-offset'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/git/probes/dirty-paths-of/status-path-offset/index.ts b/mods/diff/hooks/git/probes/dirty-paths-of/status-path-offset/index.ts
new file mode 100644
index 00000000..9693541f
--- /dev/null
+++ b/mods/diff/hooks/git/probes/dirty-paths-of/status-path-offset/index.ts
@@ -0,0 +1,3 @@
+export * from './status-path-offset.js'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/git/probes/dirty-paths-of/status-path-offset/status-path-offset.ts b/mods/diff/hooks/git/probes/dirty-paths-of/status-path-offset/status-path-offset.ts
new file mode 100644
index 00000000..ef55dee1
--- /dev/null
+++ b/mods/diff/hooks/git/probes/dirty-paths-of/status-path-offset/status-path-offset.ts
@@ -0,0 +1,5 @@
+/**
+ * Where the path starts in a `status --porcelain -z` record: after the two
+ * status letters and a space.
+ */
+export const STATUS_PATH_OFFSET = 3
diff --git a/mods/diff/hooks/git/probes/index.ts b/mods/diff/hooks/git/probes/index.ts
index e1ed4508..5df9909e 100644
--- a/mods/diff/hooks/git/probes/index.ts
+++ b/mods/diff/hooks/git/probes/index.ts
@@ -3,6 +3,7 @@ export * from './conventional-default-branches'
export * from './current-branch-of'
export * from './datings-of'
export * from './default-branch-of'
+export * from './dirty-paths-of'
export * from './head-key-of'
export * from './is-absolute-path'
export * from './is-safe-ref-name'
@@ -24,5 +25,6 @@ export * from './unstaged-numstat.js'
export * from './untracked-files'
export * from './untracked-place'
export * from './untracked-rows-of'
+export * from './was-dirty-at-start'
export * as default from '.'
diff --git a/mods/diff/hooks/git/probes/tag-pre-session.ts b/mods/diff/hooks/git/probes/tag-pre-session.ts
index 356a70a0..9e80a34e 100644
--- a/mods/diff/hooks/git/probes/tag-pre-session.ts
+++ b/mods/diff/hooks/git/probes/tag-pre-session.ts
@@ -1,14 +1,16 @@
import type Types from '../types'
import { datingsOf } from './datings-of'
+import { wasDirtyAtStart } from './was-dirty-at-start'
/**
* The tracked rows, `isPreSession` set on each whose file was modified
- * before the session began, by mtime alone as the built-in panel tags them.
+ * before the session began and was already dirty then (the built-in's tag).
*
* A file that cannot be dated (deleted this session, a symbolic link, past
- * the listing budget) stays session work, so nothing real is hidden.
+ * the listing budget), or that turned up since the start (wasDirtyAtStart),
+ * stays session work, so nothing real is hidden.
*
- * @param context the fetch: its stamp probe and the session's start
+ * @param context the fetch: its stamp probe, the session's start, its baseline
* @param files the tracked rows
* @returns the rows, tagged
*/
@@ -23,6 +25,8 @@ export async function tagPreSession(
return files.map(file => ({
...file,
- isPreSession: datings.get(file.path) === 'pre-session',
+ isPreSession:
+ datings.get(file.path) === 'pre-session' &&
+ wasDirtyAtStart(context, file.path),
}))
}
diff --git a/mods/diff/hooks/git/probes/untracked-rows-of/untracked-rows-of.ts b/mods/diff/hooks/git/probes/untracked-rows-of/untracked-rows-of.ts
index a290c92f..de683a4c 100644
--- a/mods/diff/hooks/git/probes/untracked-rows-of/untracked-rows-of.ts
+++ b/mods/diff/hooks/git/probes/untracked-rows-of/untracked-rows-of.ts
@@ -2,14 +2,15 @@ import Limits from '../../../limits'
import type Types from '../../types'
import { datingsOf } from '../datings-of'
import type { UntrackedPlace } from '../untracked-place'
+import { wasDirtyAtStart } from '../was-dirty-at-start'
/**
* Listed untracked paths as 0/0 rows: the session's own first, then,
* when the scope asks, the tagged pre-session ones, up to the slots.
*
* At most MAX_UNTRACKED_PROBES are dated; a file past that or past the
- * listing budget reads as pre-session, an undatable one as the session's
- * (the built-in panel's rule).
+ * listing budget reads as pre-session, an undatable one or one that turned
+ * up since the start (wasDirtyAtStart) as the session's (the built-in's).
*
* @param context the fetch's stamp probe and session start
* @param paths the untracked paths, root-relative, as the lister printed them
@@ -24,9 +25,13 @@ export async function untrackedRowsOf(
const probedPaths = paths.slice(0, Limits.MAX_UNTRACKED_PROBES)
const datings = await datingsOf(context, probedPaths)
+ const isPreSessionOf = (path: string, dating: Types.Dating | undefined) =>
+ dating === 'unlisted' ||
+ (dating === 'pre-session' && wasDirtyAtStart(context, path))
+
const probed = probedPaths.map(path => ({
path,
- isPreSession: datings.get(path) !== 'session',
+ isPreSession: isPreSessionOf(path, datings.get(path)),
}))
const isWithPreSession = place.scope === 'with-pre-session'
diff --git a/mods/diff/hooks/git/probes/was-dirty-at-start/index.ts b/mods/diff/hooks/git/probes/was-dirty-at-start/index.ts
new file mode 100644
index 00000000..0db2a69e
--- /dev/null
+++ b/mods/diff/hooks/git/probes/was-dirty-at-start/index.ts
@@ -0,0 +1,3 @@
+export * from './was-dirty-at-start.js'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/git/probes/was-dirty-at-start/was-dirty-at-start.ts b/mods/diff/hooks/git/probes/was-dirty-at-start/was-dirty-at-start.ts
new file mode 100644
index 00000000..ebab19bd
--- /dev/null
+++ b/mods/diff/hooks/git/probes/was-dirty-at-start/was-dirty-at-start.ts
@@ -0,0 +1,18 @@
+import type Types from '../../types'
+
+/**
+ * Whether a path may predate the session at all: it was dirty when the
+ * session began, or nothing says otherwise (no baseline was read).
+ *
+ * A path missing from the baseline turned up since, so its old timestamp
+ * (a rename keeps the file's, a move keeps the source's) does not date it,
+ * as the built-in's change-time check does not let it.
+ *
+ * @param context the fetch's dating context
+ * @param path the root-relative path
+ * @returns false only when a baseline exists and lacks the path
+ */
+export const wasDirtyAtStart = (
+ context: Types.DatingContext,
+ path: string,
+): boolean => context.deps.baseline?.has(path) ?? true
diff --git a/mods/diff/hooks/git/rename-display-of/index.ts b/mods/diff/hooks/git/rename-display-of/index.ts
new file mode 100644
index 00000000..fba9eb84
--- /dev/null
+++ b/mods/diff/hooks/git/rename-display-of/index.ts
@@ -0,0 +1,3 @@
+export * from './rename-display-of.js'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/git/rename-display-of/rename-display-of.ts b/mods/diff/hooks/git/rename-display-of/rename-display-of.ts
new file mode 100644
index 00000000..c1896eb8
--- /dev/null
+++ b/mods/diff/hooks/git/rename-display-of/rename-display-of.ts
@@ -0,0 +1,54 @@
+/**
+ * A rename as `git diff --stat` prints it: the shared leading and trailing
+ * directories once, the differing middles in braces, `old => new` between.
+ *
+ * `docs/{notes => renamed}`, `{src => lib}/main`, or with nothing shared
+ * `old => new`; a prefix or suffix only counts up to a slash, as git counts
+ * it (pprint_rename).
+ *
+ * @param oldPath the path before the rename
+ * @param newPath the path after it
+ * @returns the one-line rename
+ */
+export function renameDisplayOf(oldPath: string, newPath: string): string {
+ let prefix = 0
+ let suffix = 0
+
+ for (let at = 0; at < oldPath.length && at < newPath.length; at++) {
+ if (oldPath[at] !== newPath[at]) {
+ break
+ }
+
+ if (oldPath[at] === '/') {
+ prefix = at + 1
+ }
+ }
+
+ const floor = prefix - (prefix > 0 ? 1 : 0)
+
+ for (
+ let back = 1;
+ oldPath.length - back >= floor && newPath.length - back >= floor;
+ back++
+ ) {
+ const character = oldPath[oldPath.length - back]
+
+ if (character !== newPath[newPath.length - back]) {
+ break
+ }
+
+ if (character === '/') {
+ suffix = back
+ }
+ }
+
+ const middleOf = (path: string) =>
+ path.slice(prefix, Math.max(prefix, path.length - suffix))
+
+ const middle = `${middleOf(oldPath)} => ${middleOf(newPath)}`
+ const isBraced = prefix + suffix > 0
+ const head = oldPath.slice(0, prefix)
+ const tail = oldPath.slice(oldPath.length - suffix)
+
+ return isBraced ? `${head}{${middle}}${tail}` : middle
+}
diff --git a/mods/diff/hooks/git/types/dating-context/dating-context.ts b/mods/diff/hooks/git/types/dating-context/dating-context.ts
index bc61d4b5..9e410c00 100644
--- a/mods/diff/hooks/git/types/dating-context/dating-context.ts
+++ b/mods/diff/hooks/git/types/dating-context/dating-context.ts
@@ -2,10 +2,10 @@ import type { GitDeps } from '../git-deps'
import type { StampProbe } from '../stamp-probe'
/**
- * What dating a path against the session takes: the session's start and
- * one stamp probe over the working tree's top.
+ * What dating a path against the session takes: the session's start, one
+ * stamp probe over the tree's top, and when read, the paths dirty then.
*/
export type DatingContext = {
- deps: Pick<GitDeps, 'sessionStartMs'>
+ deps: Pick<GitDeps, 'sessionStartMs'> & Partial<Pick<GitDeps, 'baseline'>>
stamps: StampProbe
}
diff --git a/mods/diff/hooks/git/types/git-deps/git-deps.ts b/mods/diff/hooks/git/types/git-deps/git-deps.ts
index 930c8705..56bb4276 100644
--- a/mods/diff/hooks/git/types/git-deps/git-deps.ts
+++ b/mods/diff/hooks/git/types/git-deps/git-deps.ts
@@ -32,6 +32,15 @@ export type GitDeps = {
sessionStartMs: number
+ /**
+ * The paths that were dirty when the backend was pinned, at the session's
+ * start (Probes.dirtyPathsOf), or null when git could not list them.
+ *
+ * A path absent from it turned up during the session (a rename's new
+ * name, a file moved in), whatever timestamp it carries.
+ */
+ baseline: ReadonlySet<string> | null
+
/**
* Told each time branch mode resolves its base, for telemetry.
*/