From bdf3c33865eef6390d8077389830afdde0e3be3b Mon Sep 17 00:00:00 2001 From: unsc9 <438590997@qq.com> Date: Sat, 10 Oct 2026 20:07:32 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix(changes):=20=E5=8F=98=E6=9B=B4=E9=9D=A2?= =?UTF-8?q?=E6=9D=BF=E4=B8=A4=E4=BE=A7=E8=A1=8C=E6=95=B0=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E7=9B=B8=E5=8A=A0=EF=BC=8C=E5=90=84=E5=88=86=E7=BB=84=E5=90=84?= =?UTF-8?q?=E8=AF=BB=E8=87=AA=E5=B7=B1=E9=82=A3=E4=B8=80=E4=BE=A7=EF=BC=88?= =?UTF-8?q?#131=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 同一文件同时有已暂存与未暂存改动(porcelain 'MM')时,status() 把 `git diff --cached --numstat`(index 对 HEAD)与 `git diff --numstat` (worktree 对 index)按路径相加成一个数。两次 diff 的基线不同,相加会把 两者都看到的那几行算两遍:改写一行并暂存、再改同一行,两组各自显示 `+2 −2`,真值只有 `+1 −1`;面板里该文件出现两次,于是同一个虚高值被印 两遍、也被两个分组头各合计一遍。原注释写的「两侧都是对 HEAD 量的」前提 不成立——不带 --cached 的 git diff 量的是 worktree 对 index。 改为让两侧各自保留读数,一路传到分组: - src/git.ts / src/client/api.ts:`counts` 拆成 `staged` / `unstaged`, 各查各的 numstat 表;删掉 addLineCounts - src/client/changes/change-tree.ts:`buildChangeTree(entries, side)` 按分组取自己那一侧,叶子上的 `counts` 因此只代表所在分组 - src/client/changes/GitLens.tsx:两个分组各传自己的 side;内容键改成 同时含两侧,否则「只动了一侧的行数」不会重建树 - src/client/ui/git-status.ts:快照比较逐侧比,语义与原来一致 用例:`tests/git.spec.ts` 新增真实仓库用例钉住 `MM` 的两侧读数并断言不再 出现相加值(原用例正好钉着旧行为,一并改);`tests/change-tree-counts.spec.ts` 与 `tests/git-view-worktree.spec.tsx` 钉住每个分组只读自己那一侧。 --- src/client/api.ts | 12 +++-- src/client/changes/GitLens.tsx | 18 ++++--- src/client/changes/change-tree.ts | 19 +++++-- src/client/ui/git-status.ts | 16 +++--- src/git.ts | 69 ++++++++++++------------ tests/change-tree-counts.spec.ts | 87 ++++++++++++++++++++----------- tests/changes-ops.spec.ts | 28 +++++----- tests/changes-tab.spec.tsx | 8 +-- tests/git-view-worktree.spec.tsx | 46 +++++++++++++--- tests/git-worktree.spec.ts | 5 +- tests/git.spec.ts | 53 ++++++++++++++++--- 11 files changed, 241 insertions(+), 120 deletions(-) diff --git a/src/client/api.ts b/src/client/api.ts index e8da82ef..da4b3934 100644 --- a/src/client/api.ts +++ b/src/client/api.ts @@ -63,10 +63,14 @@ export type GitLineCounts = { additions: number; deletions: number } | { binary: export interface GitStatusEntry { path: string xy: string - /** Lines gained/lost, index and worktree sides summed. ABSENT when git has - * no numstat row for the path — untracked files never have one — so the row - * must not read that absence as `0`. */ - counts?: GitLineCounts + /** Lines gained/lost on the INDEX side (index vs HEAD). ABSENT when git has + * no numstat row for the path on that side — an untracked file has none — so + * the row must not read that absence as `0`. */ + staged?: GitLineCounts + /** Lines gained/lost on the WORKTREE side (worktree vs index). The two sides + * are separate readings, never one summed number: a path changed on both + * sides ('MM') is rendered once per band, each with its own. */ + unstaged?: GitLineCounts } /** Git status snapshot. */ diff --git a/src/client/changes/GitLens.tsx b/src/client/changes/GitLens.tsx index ea2792b6..20ac838d 100644 --- a/src/client/changes/GitLens.tsx +++ b/src/client/changes/GitLens.tsx @@ -145,15 +145,18 @@ const NO_ENTRIES: readonly GitStatusEntry[] = [] function entriesKey(entries: readonly GitStatusEntry[]): string { let key = '' for (const entry of entries) { - const counts = entry.counts - const lines = counts === undefined - ? '' - : 'additions' in counts ? `${counts.additions}/${counts.deletions}` : 'binary' + const lines = `${sideKey(entry.staged)}/${sideKey(entry.unstaged)}` key += `${entry.xy}\u0000${entry.path}\u0000${lines}\n` } return key } +/** One side's counts as a key fragment ('' when that side has none). */ +function sideKey(counts: GitLineCounts | undefined): string { + if (counts === undefined) return '' + return 'additions' in counts ? `${counts.additions}/${counts.deletions}` : 'binary' +} + /** The `+N` / `−M` pair, ink and sign included: shared by a file row and its * group header so the two can never drift apart. A zero side is omitted (the * diff pane's own convention), and both numbers stay on the element as @@ -825,8 +828,11 @@ export function GitLens(props: GitLensProps) { return { stagedEntries: staged, unstagedEntries: unstaged, - stagedTree: buildChangeTree(staged), - unstagedTree: buildChangeTree(unstaged), + // Each band's rows carry THAT band's reading: a path changed on both + // sides ('MM') sits in both trees, and the two diffs have different + // bases, so there is no single number to hand to either. + stagedTree: buildChangeTree(staged, 'staged'), + unstagedTree: buildChangeTree(unstaged, 'unstaged'), } // The key stands in for `entries`: same content ⇒ same tree objects ⇒ the // memoized rows below keep their props and never re-render. diff --git a/src/client/changes/change-tree.ts b/src/client/changes/change-tree.ts index 992ed75a..419221c9 100644 --- a/src/client/changes/change-tree.ts +++ b/src/client/changes/change-tree.ts @@ -15,6 +15,10 @@ import type { GitLineCounts, GitStatusEntry } from '../api.ts' import { statusOfXY, type GitFileStatus } from '../ui/index.ts' +/** Which band's reading a tree carries: the index side or the worktree side. + * Named after `GitStatusEntry`'s own fields so `entry[side]` is the reading. */ +export type ChangeSide = 'staged' | 'unstaged' + /** One changed file (a leaf), carrying its porcelain status. */ export interface ChangeFile { kind: 'file' @@ -23,8 +27,11 @@ export interface ChangeFile { /** The repo-relative path exactly as git reported it (the row's identity). */ path: string status: GitFileStatus - /** The path's line counts from the SAME status answer (#131). Absent when - * git has no numstat row for the path — untracked files never have one. */ + /** THIS GROUP's line counts for the path (#131) — the index side inside the + * staged group, the worktree side inside the unstaged one. A path changed on + * both sides ('MM') lands in both groups with a different reading each, and + * the sum of the two is nobody's number. Absent when git has no numstat row + * for the path on this side — untracked files never have one. */ counts?: GitLineCounts } @@ -110,9 +117,12 @@ function finishChildren(draft: DirDraft): ChangeNode[] { * Rows without a porcelain status (clean, ignored) are skipped, a repeated * path counts once, and an empty input yields an empty list. * @param entries - the group's `git status` entries. + * @param side - which band's reading to put on the leaves. The entries carry + * both sides (a path can be in both groups), so the tree must be told which + * one its rows stand for. * @returns the tree's roots, ready to render in order. */ -export function buildChangeTree(entries: readonly GitStatusEntry[]): ChangeNode[] { +export function buildChangeTree(entries: readonly GitStatusEntry[], side: ChangeSide): ChangeNode[] { const root: DirDraft = { name: '', path: '', dirs: new Map(), files: [] } const seen = new Set() for (const entry of entries) { @@ -135,12 +145,13 @@ export function buildChangeTree(entries: readonly GitStatusEntry[]): ChangeNode[ } // The FILE keeps git's own spelling (the lane previews, copies and menus // all resolve this path); only the directory rows use the split form. + const counts = entry[side] at.files.push({ kind: 'file', name: segments[segments.length - 1]!, path: entry.path, status, - ...(entry.counts !== undefined ? { counts: entry.counts } : {}), + ...(counts !== undefined ? { counts } : {}), }) } return finishChildren(root) diff --git a/src/client/ui/git-status.ts b/src/client/ui/git-status.ts index d0811359..7f9ba5a4 100644 --- a/src/client/ui/git-status.ts +++ b/src/client/ui/git-status.ts @@ -91,11 +91,8 @@ function joinRoot(root: string, rel: string): string { } /** - * Whether two entries carry the same line counts. A numstat-only change — an - * edit that adds lines without touching either porcelain letter, e.g. more - * lines appended to an already-modified file — must still publish a NEW - * snapshot: keeping the previous identity would freeze the row's numbers at - * their first reading for as long as the porcelain codes stay put. + * Whether two sides carry the same line counts (both absent counts as equal — + * the entry simply has no reading on that side). */ function sameCounts(left: GitLineCounts | undefined, right: GitLineCounts | undefined): boolean { if (left === right) return true @@ -110,6 +107,12 @@ function sameCounts(left: GitLineCounts | undefined, right: GitLineCounts | unde * Whether two status snapshots carry the same information. Compared field by * field (never by serializing the whole object): the entries are the payload * and their order is stable, so a length + pairwise compare is enough. + * + * Both sides' counts are compared, each on its own: a numstat-only change — an + * edit that adds lines without touching either porcelain letter, e.g. more + * lines appended to an already-modified file — must still publish a NEW + * snapshot, because keeping the previous identity would freeze the row's + * numbers at their first reading for as long as the porcelain codes stay put. */ function sameStatus(a: GitStatusResult, b: GitStatusResult): boolean { if (a === b) return true @@ -120,7 +123,8 @@ function sameStatus(a: GitStatusResult, b: GitStatusResult): boolean { && a.repositories.some((root, index) => root !== b.repositories![index])) return false return a.entries.every((entry, index) => { const other = b.entries[index]! - return entry.path === other.path && entry.xy === other.xy && sameCounts(entry.counts, other.counts) + return entry.path === other.path && entry.xy === other.xy + && sameCounts(entry.staged, other.staged) && sameCounts(entry.unstaged, other.unstaged) }) } diff --git a/src/git.ts b/src/git.ts index b66a567e..f4f58d53 100644 --- a/src/git.ts +++ b/src/git.ts @@ -21,11 +21,21 @@ export interface GitStatusEntry { path: string /** Two-letter index/worktree status (X Y), e.g. 'M ', ' M', 'A ', '??'. */ xy: string - /** How many lines this path gained and lost, index and worktree sides - * SUMMED (both are measured against HEAD). Absent when git has no numstat - * row for the path — an untracked file never gets one — so "no counts" is - * never rendered as a fabricated `0`. */ - counts?: GitLineCounts + /** How many lines the INDEX side gained and lost (`git diff --cached`, index + * against HEAD). Absent when git has no numstat row for the path on that + * side — an untracked file has none — so "no counts" is never rendered as a + * fabricated `0`. */ + staged?: GitLineCounts + /** How many lines the WORKTREE side gained and lost (`git diff`, worktree + * against the index). Absent for the same reason as {@link staged}. + * + * The two sides are kept APART, never added up: a path can hold a change on + * each side ('MM'), and the change panel renders such a path once per band + * with that band's own reading. Summing the two readings would double-count + * every line both diffs see — e.g. stage a line and keep editing the same + * region, and `1 + 1` lines would be reported as a 2-line change on BOTH + * the staged row and the unstaged one. */ + unstaged?: GitLineCounts } /** One path's line-count summary from `git diff --numstat`. A binary change is @@ -493,30 +503,6 @@ export async function currentBranch(cwd: string): Promise { * freeze the browser main thread on JSON parse or list render (#369). */ const GIT_STATUS_LIMIT = 2_000 -/** - * Fold one path's counts from both numstat sides into a running map. A file - * changed in the index AND in the worktree is measured on each side against - * HEAD, so the two readings ADD UP; a binary reading on either side wins, - * because then there is no line count to add. - */ -function addLineCounts(into: Map, from: Map): void { - for (const [path, counts] of from) { - const previous = into.get(path) - if (previous === undefined) { - into.set(path, counts) - continue - } - if (!('additions' in previous) || !('additions' in counts)) { - into.set(path, { binary: true }) - continue - } - into.set(path, { - additions: previous.additions + counts.additions, - deletions: previous.deletions + counts.deletions, - }) - } -} - /** * Working-tree status (untracked included). `--untracked-files=all` lists * the contents of new directories as individual entries, while preserving @@ -527,6 +513,12 @@ function addLineCounts(into: Map, from: Map { const repositories = await repoRoots(cwd) @@ -539,19 +531,24 @@ export async function status(cwd: string, selected?: string): Promise ''), ]) const parsed = parsePorcelainZ(raw) - const lineCounts = new Map() - addLineCounts(lineCounts, parseNumstat(stagedNumstat)) - addLineCounts(lineCounts, parseNumstat(unstagedNumstat)) + const stagedCounts = parseNumstat(stagedNumstat) + const unstagedCounts = parseNumstat(unstagedNumstat) const truncated = parsed.length > GIT_STATUS_LIMIT const bounded = truncated ? parsed.slice(0, GIT_STATUS_LIMIT) : parsed return { isRepo: true, branch, - // Paths git has no numstat row for (untracked files) keep their entry - // untouched: the absence IS the information. + // Paths git has no numstat row for on either side (untracked files) keep + // their entry untouched: the absence IS the information. entries: bounded.map((entry): GitStatusEntry => { - const counts = lineCounts.get(entry.path) - return counts === undefined ? entry : { ...entry, counts } + const staged = stagedCounts.get(entry.path) + const unstaged = unstagedCounts.get(entry.path) + if (staged === undefined && unstaged === undefined) return entry + return { + ...entry, + ...(staged !== undefined ? { staged } : {}), + ...(unstaged !== undefined ? { unstaged } : {}), + } }), truncated, root, diff --git a/tests/change-tree-counts.spec.ts b/tests/change-tree-counts.spec.ts index 03877117..c40d90e2 100644 --- a/tests/change-tree-counts.spec.ts +++ b/tests/change-tree-counts.spec.ts @@ -1,29 +1,36 @@ /** * Line counts in the change tree (#131). * - * The status answer carries each path's added/deleted lines; the tree keeps - * them on its leaves so a row prints its own numbers, and a group's header - * total is the sum of exactly those leaves. Binary rows and rows without - * counts (untracked files — git diffs nothing for them) contribute NOTHING: - * the alternative, counting them as 0, would make the header disagree with the - * numbers printed under it. + * The status answer carries each path's added/deleted lines PER SIDE; the tree + * is built once per band and keeps that band's reading on its leaves, so a row + * prints its own numbers and a band's header total is the sum of exactly those + * leaves. Binary rows and rows without counts (untracked files — git diffs + * nothing for them) contribute NOTHING: the alternative, counting them as 0, + * would make the header disagree with the numbers printed under it. */ import { describe, expect, it } from 'vitest' import { buildChangeTree, sumLineCounts } from '../src/client/changes/change-tree.ts' import type { GitStatusEntry } from '../src/client/api.ts' -/** One status entry with counts. */ -function entry(path: string, xy: string, counts?: GitStatusEntry['counts']): GitStatusEntry { - return counts === undefined ? { path, xy } : { path, xy, counts } +/** + * One status entry with the given sides' counts. `xy` decides which bands the + * entry lands in; each band's tree reads its own side off the entry. + */ +function entry( + path: string, + xy: string, + sides: { staged?: GitStatusEntry['staged']; unstaged?: GitStatusEntry['unstaged'] } = {}, +): GitStatusEntry { + return { path, xy, ...sides } } describe('change tree line counts', () => { it('keeps each entry counts on its own leaf', () => { const tree = buildChangeTree([ - entry('src/a.ts', ' M', { additions: 4, deletions: 2 }), - entry('src/deep/b.ts', ' M', { binary: true }), + entry('src/a.ts', ' M', { unstaged: { additions: 4, deletions: 2 } }), + entry('src/deep/b.ts', ' M', { unstaged: { binary: true } }), entry('new.txt', '??'), - ]) + ], 'unstaged') const src = tree.find(node => node.kind === 'dir' && node.path === 'src')! expect(src.kind).toBe('dir') const leaves = src.kind === 'dir' ? src.children : [] @@ -38,26 +45,42 @@ describe('change tree line counts', () => { expect(untracked.kind === 'file' && 'counts' in untracked).toBe(false) }) + it('reads ONLY the band\'s own side off a path that changed on both sides', () => { + // The double-count regression (#131): the two diffs have different bases, + // so `MM` carries two readings. Each band's tree must print its own, not + // the sum — summing reported the same edited line as `+2 −2` in BOTH bands. + const both = { staged: { additions: 1, deletions: 1 }, unstaged: { additions: 1, deletions: 1 } } + expect(sumLineCounts(buildChangeTree([entry('a.txt', 'MM', both)], 'staged'))) + .toEqual({ additions: 1, deletions: 1, files: 1 }) + expect(sumLineCounts(buildChangeTree([entry('a.txt', 'MM', both)], 'unstaged'))) + .toEqual({ additions: 1, deletions: 1, files: 1 }) + // A side with no reading stays count-less in that band only. + const stagedOnly = entry('b.txt', 'M ', { staged: { additions: 3, deletions: 0 } }) + expect(buildChangeTree([stagedOnly], 'unstaged')[0]).not.toHaveProperty('counts') + expect(buildChangeTree([stagedOnly], 'staged')[0]).toMatchObject({ counts: { additions: 3, deletions: 0 } }) + }) + it('sums a group to exactly the numbers its members carry', () => { const tree = buildChangeTree([ - entry('src/a.ts', ' M', { additions: 4, deletions: 2 }), - entry('src/b.ts', ' M', { additions: 1, deletions: 0 }), - entry('docs/c.md', ' M', { additions: 3, deletions: 3 }), - ]) + entry('src/a.ts', ' M', { unstaged: { additions: 4, deletions: 2 } }), + entry('src/b.ts', ' M', { unstaged: { additions: 1, deletions: 0 } }), + entry('docs/c.md', ' M', { unstaged: { additions: 3, deletions: 3 } }), + ], 'unstaged') expect(sumLineCounts(tree)).toEqual({ additions: 8, deletions: 5, files: 3 }) }) it('counts only the rows that HAVE numbers (binary and untracked add nothing)', () => { const tree = buildChangeTree([ - entry('src/a.ts', ' M', { additions: 4, deletions: 2 }), - entry('blob.bin', ' M', { binary: true }), + entry('src/a.ts', ' M', { unstaged: { additions: 4, deletions: 2 } }), + entry('blob.bin', ' M', { unstaged: { binary: true } }), entry('new.txt', '??'), - ]) + ], 'unstaged') // 4/2 — not 4/2 plus a fabricated zero from either count-less row. expect(sumLineCounts(tree)).toEqual({ additions: 4, deletions: 2, files: 1 }) // A group with nothing to sum reports zero files, so its header stays bare. - expect(sumLineCounts(buildChangeTree([entry('blob.bin', ' M', { binary: true }), entry('n.txt', '??')]))) - .toEqual({ additions: 0, deletions: 0, files: 0 }) + expect(sumLineCounts(buildChangeTree([ + entry('blob.bin', ' M', { unstaged: { binary: true } }), entry('n.txt', '??'), + ], 'unstaged'))).toEqual({ additions: 0, deletions: 0, files: 0 }) }) it('counts only the rows that PRINT numbers: a 0/0 rename has none to sum (#131)', () => { @@ -65,24 +88,26 @@ describe('change tree line counts', () => { // reads the same way): its row prints nothing at all, so it must not make // a group's total claim it contributed numbers — otherwise a group holding // nothing but renames renders an empty `+N −M` cluster in its header. - expect(sumLineCounts(buildChangeTree([entry('renamed.ts', 'R ', { additions: 0, deletions: 0 })]))) - .toEqual({ additions: 0, deletions: 0, files: 0 }) - expect(sumLineCounts(buildChangeTree([entry('mode-only.ts', ' M', { additions: 0, deletions: 0 })]))) - .toEqual({ additions: 0, deletions: 0, files: 0 }) + expect(sumLineCounts(buildChangeTree([ + entry('renamed.ts', 'R ', { staged: { additions: 0, deletions: 0 } }), + ], 'staged'))).toEqual({ additions: 0, deletions: 0, files: 0 }) + expect(sumLineCounts(buildChangeTree([ + entry('mode-only.ts', ' M', { unstaged: { additions: 0, deletions: 0 } }), + ], 'unstaged'))).toEqual({ additions: 0, deletions: 0, files: 0 }) // A rename that DID move lines keeps contributing them... expect(sumLineCounts(buildChangeTree([ - entry('edited.ts', 'R ', { additions: 2, deletions: 1 }), - entry('renamed.ts', 'R ', { additions: 0, deletions: 0 }), - ]))).toEqual({ additions: 2, deletions: 1, files: 1 }) + entry('edited.ts', 'R ', { staged: { additions: 2, deletions: 1 } }), + entry('renamed.ts', 'R ', { staged: { additions: 0, deletions: 0 } }), + ], 'staged'))).toEqual({ additions: 2, deletions: 1, files: 1 }) }) it('counts every member regardless of how deep the directory chain folds', () => { // One compressed chain row stands for `src/client/changes`; the total must // still be the sum of the leaves under it. const tree = buildChangeTree([ - entry('src/client/changes/a.ts', ' M', { additions: 2, deletions: 1 }), - entry('src/client/changes/b.ts', ' M', { additions: 5, deletions: 0 }), - ]) + entry('src/client/changes/a.ts', ' M', { unstaged: { additions: 2, deletions: 1 } }), + entry('src/client/changes/b.ts', ' M', { unstaged: { additions: 5, deletions: 0 } }), + ], 'unstaged') expect(tree).toHaveLength(1) expect(tree[0]).toMatchObject({ kind: 'dir', name: 'src/client/changes' }) expect(sumLineCounts(tree)).toEqual({ additions: 7, deletions: 1, files: 2 }) diff --git a/tests/changes-ops.spec.ts b/tests/changes-ops.spec.ts index 1723cdec..e0b80220 100644 --- a/tests/changes-ops.spec.ts +++ b/tests/changes-ops.spec.ts @@ -164,19 +164,23 @@ describe('change tree (the Git lens reads its groups through it)', () => { /** One `git status` row from a path and its porcelain code. */ const entry = (path: string, xy = ' M'): GitStatusEntry => ({ path, xy }) + /** The tree of one band. These cases exercise the SHAPE only, so the side is + * fixed; the per-side readings have their own suite. */ + const band = (entries: readonly GitStatusEntry[]): ChangeNode[] => buildChangeTree(entries, 'unstaged') + /** A compact, assertion-friendly projection of one tree. */ const shape = (nodes: readonly ChangeNode[]): unknown[] => nodes.map(node => node.kind === 'dir' ? { dir: node.name, path: node.path, changes: node.changes, children: shape(node.children) } : { file: node.name, path: node.path, letter: node.status.letter, tone: node.status.tone }) it('keeps a single root-level file as one leaf', () => { - expect(shape(buildChangeTree([entry('readme.md')]))).toEqual([ + expect(shape(band([entry('readme.md')]))).toEqual([ { file: 'readme.md', path: 'readme.md', letter: 'M', tone: 'modified' }, ]) }) it('nests a deep path and compresses its single-child chain into one row', () => { - const tree = buildChangeTree([entry('src/client/changes/a.ts')]) + const tree = band([entry('src/client/changes/a.ts')]) expect(shape(tree)).toEqual([ { dir: 'src/client/changes', @@ -189,7 +193,7 @@ describe('change tree (the Git lens reads its groups through it)', () => { it('stops compressing where the path branches or a directory holds its own file', () => { // Two child directories: 'src' keeps its own row. - expect(shape(buildChangeTree([entry('src/a/x.ts'), entry('src/b/y.ts')]))).toEqual([ + expect(shape(band([entry('src/a/x.ts'), entry('src/b/y.ts')]))).toEqual([ { dir: 'src', path: 'src', @@ -201,7 +205,7 @@ describe('change tree (the Git lens reads its groups through it)', () => { }, ]) // A file AT this level: 'a' keeps its own row (and its file leads the child). - expect(shape(buildChangeTree([entry('a/x.ts'), entry('a/b/y.ts')]))).toEqual([ + expect(shape(band([entry('a/x.ts'), entry('a/b/y.ts')]))).toEqual([ { dir: 'a', path: 'a', @@ -215,7 +219,7 @@ describe('change tree (the Git lens reads its groups through it)', () => { }) it('orders directories before files, each by name case-insensitively', () => { - const tree = buildChangeTree([ + const tree = band([ entry('Zed.ts'), entry('B.ts'), entry('a.ts'), entry('zdir/f.ts'), entry('Adir/f.ts'), ]) expect(shape(tree).map(node => (node as { dir?: string; file?: string }).dir ?? (node as { file: string }).file)) @@ -223,7 +227,7 @@ describe('change tree (the Git lens reads its groups through it)', () => { }) it('mixes root-level files with nested paths and keeps same-named files apart', () => { - expect(shape(buildChangeTree([ + expect(shape(band([ entry('b/index.ts'), entry('index.ts'), entry('a/index.ts'), ]))).toEqual([ { dir: 'a', path: 'a', changes: 1, children: [{ file: 'index.ts', path: 'a/index.ts', letter: 'M', tone: 'modified' }] }, @@ -233,7 +237,7 @@ describe('change tree (the Git lens reads its groups through it)', () => { }) it('carries every file porcelain status and counts the files under each row', () => { - const tree = buildChangeTree([ + const tree = band([ entry('src/a.ts', ' M'), entry('src/new.ts', '??'), entry('src/gone.ts', ' D'), entry('src/kept.ts', ' '), ]) expect(shape(tree)).toEqual([ @@ -257,14 +261,14 @@ describe('change tree (the Git lens reads its groups through it)', () => { // reorder the same change list on another machine. const names = ['b.ts', 'A.ts', 'ä.ts', 'Z.ts', 'a.ts', 'Ä.ts', '_x.ts', '1.ts'] const order = (list: readonly string[]): string[] => - buildChangeTree(list.map(path => entry(path))).map(node => node.name) + band(list.map(path => entry(path))).map(node => node.name) expect(order(names)).toEqual(['_x.ts', '1.ts', 'A.ts', 'a.ts', 'Ä.ts', 'ä.ts', 'b.ts', 'Z.ts']) // …and it is a TOTAL order: reversing the input cannot move a row. expect(order([...names].reverse())).toEqual(order(names)) const dirs = ['Zdir/f.ts', 'adir/f.ts', 'Ädir/f.ts', '_dir/f.ts'] const dirOrder = (list: readonly string[]): string[] => - buildChangeTree(list.map(path => entry(path))).map(node => node.name) + band(list.map(path => entry(path))).map(node => node.name) expect(dirOrder(dirs)).toEqual(['_dir', 'adir', 'Ädir', 'Zdir']) expect(dirOrder([...dirs].reverse())).toEqual(dirOrder(dirs)) @@ -276,12 +280,12 @@ describe('change tree (the Git lens reads its groups through it)', () => { }) it('returns no nodes for an empty or all-clean list', () => { - expect(buildChangeTree([])).toEqual([]) - expect(buildChangeTree([entry('a.ts', ' '), entry('b.ts', '!!')])).toEqual([]) + expect(band([])).toEqual([]) + expect(band([entry('a.ts', ' '), entry('b.ts', '!!')])).toEqual([]) }) it('counts a repeated path once', () => { - expect(shape(buildChangeTree([entry('a.ts'), entry('a.ts', '??')]))).toEqual([ + expect(shape(band([entry('a.ts'), entry('a.ts', '??')]))).toEqual([ { file: 'a.ts', path: 'a.ts', letter: 'M', tone: 'modified' }, ]) }) diff --git a/tests/changes-tab.spec.tsx b/tests/changes-tab.spec.tsx index d8cef920..69d950ef 100644 --- a/tests/changes-tab.spec.tsx +++ b/tests/changes-tab.spec.tsx @@ -146,8 +146,8 @@ describe('ChangesTab', () => { */ it('renders no count cluster for a group whose only changes are pure renames (#131)', async () => { mockGit([ - { path: 'renamed.ts', xy: 'R ', counts: { additions: 0, deletions: 0 } }, - { path: 'moved.md', xy: ' R', counts: { additions: 0, deletions: 0 } }, + { path: 'renamed.ts', xy: 'R ', staged: { additions: 0, deletions: 0 } }, + { path: 'moved.md', xy: ' R', unstaged: { additions: 0, deletions: 0 } }, ]) const container = document.createElement('div') @@ -176,8 +176,8 @@ describe('ChangesTab', () => { it('keeps the group total when a row in the group really has numbers (#131)', async () => { mockGit([ - { path: 'mode-only.ts', xy: ' M', counts: { additions: 0, deletions: 0 } }, - { path: 'edited.ts', xy: ' M', counts: { additions: 3, deletions: 1 } }, + { path: 'mode-only.ts', xy: ' M', unstaged: { additions: 0, deletions: 0 } }, + { path: 'edited.ts', xy: ' M', unstaged: { additions: 3, deletions: 1 } }, ]) const container = document.createElement('div') diff --git a/tests/git-view-worktree.spec.tsx b/tests/git-view-worktree.spec.tsx index 6fad842e..08915e5a 100644 --- a/tests/git-view-worktree.spec.tsx +++ b/tests/git-view-worktree.spec.tsx @@ -455,7 +455,7 @@ describe('GitLens (changes tab, git lens) change tree', () => { async function mountTree( container: HTMLElement, root: Root, - entries: Array<{ path: string; xy: string; counts?: GitStatusEntry['counts'] }>, + entries: Array<{ path: string; xy: string; staged?: GitStatusEntry['staged']; unstaged?: GitStatusEntry['unstaged'] }>, ): Promise { const changes = entries.filter(row => row.xy !== ' ').length vi.spyOn(api, 'gitWorktrees').mockResolvedValue([{ path: MAIN, branch: 'main', current: true, changes }]) @@ -708,9 +708,9 @@ describe('GitLens (changes tab, git lens) change tree', () => { const { container, root } = makeRoot() try { await mountTree(container, root, [ - { path: 'src/a.ts', xy: ' M', counts: { additions: 4, deletions: 2 } }, - { path: 'src/b.ts', xy: ' M', counts: { additions: 1, deletions: 0 } }, - { path: 'docs/c.md', xy: 'M ', counts: { additions: 3, deletions: 3 } }, + { path: 'src/a.ts', xy: ' M', unstaged: { additions: 4, deletions: 2 } }, + { path: 'src/b.ts', xy: ' M', unstaged: { additions: 1, deletions: 0 } }, + { path: 'docs/c.md', xy: 'M ', staged: { additions: 3, deletions: 3 } }, ]) expect(counted(container, '[data-path="src/a.ts"] [data-lines="count"]')) @@ -744,7 +744,7 @@ describe('GitLens (changes tab, git lens) change tree', () => { try { await mountTree(container, root, [ { path: 'new.txt', xy: '??' }, - { path: 'blob.bin', xy: ' M', counts: { binary: true } }, + { path: 'blob.bin', xy: ' M', unstaged: { binary: true } }, ]) expect(container.querySelector('[data-path="new.txt"] [data-lines="new"]')?.textContent) @@ -773,10 +773,10 @@ describe('GitLens (changes tab, git lens) change tree', () => { // line counts moved (more lines appended to an already-modified file). vi.spyOn(api, 'gitStatus') .mockResolvedValueOnce({ - isRepo: true, branch: 'main', entries: [{ path: 'src/a.ts', xy: ' M', counts: { additions: 1, deletions: 0 } }], + isRepo: true, branch: 'main', entries: [{ path: 'src/a.ts', xy: ' M', unstaged: { additions: 1, deletions: 0 } }], }) .mockResolvedValue({ - isRepo: true, branch: 'main', entries: [{ path: 'src/a.ts', xy: ' M', counts: { additions: 7, deletions: 0 } }], + isRepo: true, branch: 'main', entries: [{ path: 'src/a.ts', xy: ' M', unstaged: { additions: 7, deletions: 0 } }], }) vi.spyOn(api, 'gitBranch').mockResolvedValue({ current: 'main', names: ['main'] }) vi.spyOn(api, 'gitLog').mockResolvedValue([]) @@ -798,6 +798,38 @@ describe('GitLens (changes tab, git lens) change tree', () => { vi.useRealTimers() } }) + + it('gives each band its OWN side of a file changed on both sides, never the sum (#131 regression)', async () => { + const { container, root } = makeRoot() + try { + // One file, changed on both sides ('MM'): a one-line rewrite staged, + // then five lines appended in the worktree. The two diffs have + // different bases, so the path is listed once per band with its own + // reading — and the bands' totals are those readings. Summing them + // printed `+6 −1` on the staged band AND on the unstaged one. + await mountTree(container, root, [ + { + path: 'a.txt', + xy: 'MM', + staged: { additions: 1, deletions: 1 }, + unstaged: { additions: 5, deletions: 0 }, + }, + ]) + + expect(counted(container, '[data-group="unstaged"] [data-lines="group"]')) + .toEqual({ added: '5', deleted: '0', text: '+5' }) + expect(counted(container, '[data-group="staged"] [data-lines="group"]')) + .toEqual({ added: '1', deleted: '1', text: '+1−1' }) + // The path really is listed in both bands, each row printing its own + // side's numbers (unstaged band first, staged second). + const rows = [...container.querySelectorAll('button[data-path="a.txt"] [data-lines="count"]')] + expect(rows.map(node => `${node.getAttribute('data-added')}/${node.getAttribute('data-deleted')}`)) + .toEqual(['5/0', '1/1']) + } finally { + act(() => { root.unmount() }) + container.remove() + } + }) }) }) diff --git a/tests/git-worktree.spec.ts b/tests/git-worktree.spec.ts index 7a5e91f6..a7df12f1 100644 --- a/tests/git-worktree.spec.ts +++ b/tests/git-worktree.spec.ts @@ -80,9 +80,10 @@ describe('linked Git worktrees', () => { expect(listed.find(entry => !entry.current)).toMatchObject({ branch: 'agent', changes: 1 }) expect(resolve(await resolveWorktree(main, agentPath))).toBe(resolve(agentPath)) // The line counts ride the same status answer (#131): 'base' → - // 'changed by agent' is exactly one line replaced. + // 'changed by agent' is exactly one line replaced, on the worktree side + // (nothing is staged here, so the index side has no reading at all). expect((await status(await resolveWorktree(main, agentPath))).entries).toEqual([ - { path: 'tracked.txt', xy: ' M', counts: { additions: 1, deletions: 1 } }, + { path: 'tracked.txt', xy: ' M', unstaged: { additions: 1, deletions: 1 } }, ]) await expect(resolveWorktree(main, root)).rejects.toThrow('unknown linked worktree') diff --git a/tests/git.spec.ts b/tests/git.spec.ts index 316bb20a..301d09b7 100644 --- a/tests/git.spec.ts +++ b/tests/git.spec.ts @@ -361,7 +361,7 @@ describe('numstat line counts (#131)', () => { expect(parseNumstat('not a numstat record\nanother one\n')).toEqual(new Map()) }) - it('carries counts, the binary marker and the untracked absence through status()', async () => { + it('carries each side\'s counts, the binary marker and the untracked absence through status()', async () => { const root = mkdtempSync(join(tmpdir(), 'dsh-git-counts-')) try { execFileSync('git', ['init', '-q'], { cwd: root }) @@ -376,8 +376,9 @@ describe('numstat line counts (#131)', () => { execFileSync('git', ['add', '-A'], { cwd: root }) execFileSync('git', ['commit', '-q', '-m', 'init'], { cwd: root }) - // ONE path changed on BOTH sides: one line staged, then one more in the - // worktree — the entry must report the sum of the two readings. + // ONE path changed on BOTH sides (porcelain 'MM'): one line staged, then + // one more in the worktree. The two diffs have different bases, so the + // entry carries two readings — never one number that adds them up. writeFileSync(join(root, 'tracked.txt'), 'one\ntwo\nthree\n') execFileSync('git', ['add', 'tracked.txt'], { cwd: root }) writeFileSync(join(root, 'tracked.txt'), 'one\ntwo\nthree\nfour\n') @@ -391,20 +392,56 @@ describe('numstat line counts (#131)', () => { const result = await status(root) const byPath = new Map(result.entries.map(entry => [entry.path, entry])) expect(byPath.get('tracked.txt')).toEqual({ - path: 'tracked.txt', xy: 'MM', counts: { additions: 2, deletions: 0 }, + path: 'tracked.txt', + xy: 'MM', + staged: { additions: 1, deletions: 0 }, + unstaged: { additions: 1, deletions: 0 }, }) expect(byPath.get('moved.txt')).toEqual({ - path: 'moved.txt', xy: 'R ', counts: { additions: 0, deletions: 0 }, + path: 'moved.txt', xy: 'R ', staged: { additions: 0, deletions: 0 }, }) expect(byPath.get('blob.bin')).toEqual({ - path: 'blob.bin', xy: 'A ', counts: { binary: true }, + path: 'blob.bin', xy: 'A ', staged: { binary: true }, }) - // No numstat row exists for an untracked file: the entry keeps no counts - // at all, which is what the row renders as "new file". + // No numstat row exists for an untracked file on either side: the entry + // keeps no counts at all, which is what the row renders as "new file". expect(byPath.get('untracked.txt')).toEqual({ path: 'untracked.txt', xy: '??' }) expect(byPath.has('renamed.txt')).toBe(false) } finally { rmSync(root, { recursive: true, force: true }) } }) + + it('never adds the two sides up: a line both diffs see is counted once per side', async () => { + // The double-count regression. Editing the SAME line twice — staged once, + // then edited again in the worktree — makes both numstat sides report the + // same single line. Summing them reported a 2-line change on the staged row + // AND on the unstaged one, while `git diff HEAD --numstat` says 1. + const root = mkdtempSync(join(tmpdir(), 'dsh-git-both-sides-')) + try { + execFileSync('git', ['init', '-q'], { cwd: root }) + execFileSync('git', ['config', 'user.email', 't@t'], { cwd: root }) + execFileSync('git', ['config', 'user.name', 't'], { cwd: root }) + writeFileSync(join(root, 'a.txt'), 'one\ntwo\nthree\n') + execFileSync('git', ['add', '-A'], { cwd: root }) + execFileSync('git', ['commit', '-q', '-m', 'init'], { cwd: root }) + + writeFileSync(join(root, 'a.txt'), 'one\nTWO\nthree\n') + execFileSync('git', ['add', 'a.txt'], { cwd: root }) + writeFileSync(join(root, 'a.txt'), 'one\nTwo\nthree\n') + + const result = await status(root) + expect(result.entries).toEqual([{ + path: 'a.txt', + xy: 'MM', + staged: { additions: 1, deletions: 1 }, + unstaged: { additions: 1, deletions: 1 }, + }]) + // The reading the panel must never print: the sum of two diffs that saw + // the same line. + expect(JSON.stringify(result.entries)).not.toContain('"additions":2') + } finally { + rmSync(root, { recursive: true, force: true }) + } + }) }) From 42bfd9386e0a99526309095fdbb174c06015e498 Mon Sep 17 00:00:00 2001 From: unsc9 <438590997@qq.com> Date: Sun, 11 Oct 2026 17:59:32 +0800 Subject: [PATCH 2/3] =?UTF-8?q?refactor(changes):=20=E7=B2=BE=E7=AE=80?= =?UTF-8?q?=E4=B8=A4=E4=BE=A7=E8=A1=8C=E6=95=B0=E6=94=B9=E5=8A=A8=E7=9A=84?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=E3=80=81=E6=B5=8B=E8=AF=95=E5=A4=B9=E5=85=B7?= =?UTF-8?q?=E4=B8=8E=E5=90=88=E7=BA=A6=EF=BC=88=E8=A1=8C=E4=B8=BA=E4=B8=8D?= =?UTF-8?q?=E5=8F=98=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 上一个提交(bdf3c33)过了一轮「代码精简」审查(复用 / 简化 / 效率 / 修在哪一层 四个角度,四个只读子代理并发),这里只落地行为完全不变的那批。 - 同一个「两侧不能相加」的理由原先在 5 处源码注释与 4 处测试注释里各写一遍, 措辞已经开始分叉。完整理由只留在权威位置 `GitStatusEntry` 的字段文档; `status()`、`api.ts` 的镜像类型、`ChangeFile.counts`、GitLens 的调用处与三处 测试注释压成一行并指回去。 - 用例夹具:`change-tree-counts.spec.ts` 的 `entry()` 从「传一个 sides 对象」改成 「传一个读数 + 一个侧」,收掉 13 处 `{ unstaged: … }` 外壳;那个 `side` 用生产 侧导出的 `ChangeSide`——它此前导出了却没有任何消费者。 - 同文件「两侧不相加」的用例原先两侧读数对称(都是 1/1),两条断言的期望值逐字 相同:把两侧读反也照样绿。夹具改成不对称(staged 1/1、unstaged 5/0),「相加」 与「读错边」两种错误现在都会失败。 - `git.spec.ts` 里第 4 份「建仓引导」提成局部 `mkRepo(files, config)`,与紧邻用例 共用(少一次重复的 4 个 git 进程)。 - `git-view-worktree.spec.tsx` 的 `mountTree` 形参原先手写镜像了 `GitStatusEntry` 的四个成员,改回 `GitStatusEntry[]`(同文件上方本就 import 了它)。 按「超出本次 diff / 会改变行为」跳过的四条,留档:GitLens 里 band↔侧的六处穿线 (要重写两个本 diff 没动过、近乎逐字重复的渲染块)、「这行会不会印数字」的两处 判断(合一就必须让 0/0 行不再渲染那个空 span,那是改输出)、快照身份的两份实现 (造键 vs 判等,本就是两件事)、服务端 `git.suggest-message` 又推一遍「属于哪 一侧」(本 diff 之外,且与本次 bug 同类)。另:`GitStatusEntry` 在 `git.ts` 与 `api.ts` 的两份声明是刻意的镜像(浏览器侧声明图不能碰 `node:*`),不合并。 验证:`tsc --noEmit` 通过;eslint 对 7 个改动文件无输出;受影响的 6 个测试文件 173 条全绿(含真起 git 进程的用例);完整单测 189 文件 / 2234 用例,失败仍是与 改动前同一批 8 条环境性失败(3 条 Windows 建符号链接 EPERM、1 条 `tar` 在 Windows 输出 CRLF、4 条中文语言标签),无新增。 --- src/client/api.ts | 5 +-- src/client/changes/GitLens.tsx | 3 +- src/client/changes/change-tree.ts | 8 ++-- src/git.ts | 7 +--- tests/change-tree-counts.spec.ts | 63 ++++++++++++++++--------------- tests/git-view-worktree.spec.tsx | 10 ++--- tests/git.spec.ts | 47 +++++++++++------------ 7 files changed, 68 insertions(+), 75 deletions(-) diff --git a/src/client/api.ts b/src/client/api.ts index da4b3934..f954c28e 100644 --- a/src/client/api.ts +++ b/src/client/api.ts @@ -67,9 +67,8 @@ export interface GitStatusEntry { * no numstat row for the path on that side — an untracked file has none — so * the row must not read that absence as `0`. */ staged?: GitLineCounts - /** Lines gained/lost on the WORKTREE side (worktree vs index). The two sides - * are separate readings, never one summed number: a path changed on both - * sides ('MM') is rendered once per band, each with its own. */ + /** Lines gained/lost on the WORKTREE side (worktree vs index) — a separate + * reading, never one summed number. */ unstaged?: GitLineCounts } diff --git a/src/client/changes/GitLens.tsx b/src/client/changes/GitLens.tsx index 20ac838d..5fef64bf 100644 --- a/src/client/changes/GitLens.tsx +++ b/src/client/changes/GitLens.tsx @@ -829,8 +829,7 @@ export function GitLens(props: GitLensProps) { stagedEntries: staged, unstagedEntries: unstaged, // Each band's rows carry THAT band's reading: a path changed on both - // sides ('MM') sits in both trees, and the two diffs have different - // bases, so there is no single number to hand to either. + // sides sits in both trees, and neither may see the other's number. stagedTree: buildChangeTree(staged, 'staged'), unstagedTree: buildChangeTree(unstaged, 'unstaged'), } diff --git a/src/client/changes/change-tree.ts b/src/client/changes/change-tree.ts index 419221c9..54993504 100644 --- a/src/client/changes/change-tree.ts +++ b/src/client/changes/change-tree.ts @@ -28,10 +28,10 @@ export interface ChangeFile { path: string status: GitFileStatus /** THIS GROUP's line counts for the path (#131) — the index side inside the - * staged group, the worktree side inside the unstaged one. A path changed on - * both sides ('MM') lands in both groups with a different reading each, and - * the sum of the two is nobody's number. Absent when git has no numstat row - * for the path on this side — untracked files never have one. */ + * staged group, the worktree side inside the unstaged one, so a path changed + * on both sides lands in both groups with a reading of its own each. Absent + * when git has no numstat row for the path on this side — untracked files + * never have one. */ counts?: GitLineCounts } diff --git a/src/git.ts b/src/git.ts index f4f58d53..ad76882e 100644 --- a/src/git.ts +++ b/src/git.ts @@ -514,11 +514,8 @@ const GIT_STATUS_LIMIT = 2_000 * costs only the counts (the rows fall back to their count-less rendering) — * the status answer itself must never fail because of it. * - * Each side keeps its own reading ({@link GitStatusEntry.staged} / - * {@link GitStatusEntry.unstaged}); they are never folded into one number. The - * two diffs have different bases (index vs HEAD, worktree vs index), so a path - * changed on both sides has two honest answers and no sum of them equals - * either. + * Each side's reading lands on its own field of the entry + * ({@link GitStatusEntry} documents why the two are never summed). */ export async function status(cwd: string, selected?: string): Promise { const repositories = await repoRoots(cwd) diff --git a/tests/change-tree-counts.spec.ts b/tests/change-tree-counts.spec.ts index c40d90e2..886730ba 100644 --- a/tests/change-tree-counts.spec.ts +++ b/tests/change-tree-counts.spec.ts @@ -9,26 +9,21 @@ * would make the header disagree with the numbers printed under it. */ import { describe, expect, it } from 'vitest' -import { buildChangeTree, sumLineCounts } from '../src/client/changes/change-tree.ts' -import type { GitStatusEntry } from '../src/client/api.ts' +import { buildChangeTree, sumLineCounts, type ChangeSide } from '../src/client/changes/change-tree.ts' +import type { GitLineCounts, GitStatusEntry } from '../src/client/api.ts' -/** - * One status entry with the given sides' counts. `xy` decides which bands the - * entry lands in; each band's tree reads its own side off the entry. - */ -function entry( - path: string, - xy: string, - sides: { staged?: GitStatusEntry['staged']; unstaged?: GitStatusEntry['unstaged'] } = {}, -): GitStatusEntry { - return { path, xy, ...sides } +/** One status entry carrying a reading on ONE side (the worktree by default). + * `xy` decides which bands the entry lands in; each band's tree reads its own + * side off the entry. */ +function entry(path: string, xy: string, counts?: GitLineCounts, side: ChangeSide = 'unstaged'): GitStatusEntry { + return counts === undefined ? { path, xy } : { path, xy, [side]: counts } } describe('change tree line counts', () => { it('keeps each entry counts on its own leaf', () => { const tree = buildChangeTree([ - entry('src/a.ts', ' M', { unstaged: { additions: 4, deletions: 2 } }), - entry('src/deep/b.ts', ' M', { unstaged: { binary: true } }), + entry('src/a.ts', ' M', { additions: 4, deletions: 2 }), + entry('src/deep/b.ts', ' M', { binary: true }), entry('new.txt', '??'), ], 'unstaged') const src = tree.find(node => node.kind === 'dir' && node.path === 'src')! @@ -49,37 +44,43 @@ describe('change tree line counts', () => { // The double-count regression (#131): the two diffs have different bases, // so `MM` carries two readings. Each band's tree must print its own, not // the sum — summing reported the same edited line as `+2 −2` in BOTH bands. - const both = { staged: { additions: 1, deletions: 1 }, unstaged: { additions: 1, deletions: 1 } } - expect(sumLineCounts(buildChangeTree([entry('a.txt', 'MM', both)], 'staged'))) - .toEqual({ additions: 1, deletions: 1, files: 1 }) - expect(sumLineCounts(buildChangeTree([entry('a.txt', 'MM', both)], 'unstaged'))) + // The two readings differ, so a band reading the WRONG side fails too. + const both: GitStatusEntry = { + path: 'a.txt', + xy: 'MM', + staged: { additions: 1, deletions: 1 }, + unstaged: { additions: 5, deletions: 0 }, + } + expect(sumLineCounts(buildChangeTree([both], 'staged'))) .toEqual({ additions: 1, deletions: 1, files: 1 }) + expect(sumLineCounts(buildChangeTree([both], 'unstaged'))) + .toEqual({ additions: 5, deletions: 0, files: 1 }) // A side with no reading stays count-less in that band only. - const stagedOnly = entry('b.txt', 'M ', { staged: { additions: 3, deletions: 0 } }) + const stagedOnly = entry('b.txt', 'M ', { additions: 3, deletions: 0 }, 'staged') expect(buildChangeTree([stagedOnly], 'unstaged')[0]).not.toHaveProperty('counts') expect(buildChangeTree([stagedOnly], 'staged')[0]).toMatchObject({ counts: { additions: 3, deletions: 0 } }) }) it('sums a group to exactly the numbers its members carry', () => { const tree = buildChangeTree([ - entry('src/a.ts', ' M', { unstaged: { additions: 4, deletions: 2 } }), - entry('src/b.ts', ' M', { unstaged: { additions: 1, deletions: 0 } }), - entry('docs/c.md', ' M', { unstaged: { additions: 3, deletions: 3 } }), + entry('src/a.ts', ' M', { additions: 4, deletions: 2 }), + entry('src/b.ts', ' M', { additions: 1, deletions: 0 }), + entry('docs/c.md', ' M', { additions: 3, deletions: 3 }), ], 'unstaged') expect(sumLineCounts(tree)).toEqual({ additions: 8, deletions: 5, files: 3 }) }) it('counts only the rows that HAVE numbers (binary and untracked add nothing)', () => { const tree = buildChangeTree([ - entry('src/a.ts', ' M', { unstaged: { additions: 4, deletions: 2 } }), - entry('blob.bin', ' M', { unstaged: { binary: true } }), + entry('src/a.ts', ' M', { additions: 4, deletions: 2 }), + entry('blob.bin', ' M', { binary: true }), entry('new.txt', '??'), ], 'unstaged') // 4/2 — not 4/2 plus a fabricated zero from either count-less row. expect(sumLineCounts(tree)).toEqual({ additions: 4, deletions: 2, files: 1 }) // A group with nothing to sum reports zero files, so its header stays bare. expect(sumLineCounts(buildChangeTree([ - entry('blob.bin', ' M', { unstaged: { binary: true } }), entry('n.txt', '??'), + entry('blob.bin', ' M', { binary: true }), entry('n.txt', '??'), ], 'unstaged'))).toEqual({ additions: 0, deletions: 0, files: 0 }) }) @@ -89,15 +90,15 @@ describe('change tree line counts', () => { // a group's total claim it contributed numbers — otherwise a group holding // nothing but renames renders an empty `+N −M` cluster in its header. expect(sumLineCounts(buildChangeTree([ - entry('renamed.ts', 'R ', { staged: { additions: 0, deletions: 0 } }), + entry('renamed.ts', 'R ', { additions: 0, deletions: 0 }, 'staged'), ], 'staged'))).toEqual({ additions: 0, deletions: 0, files: 0 }) expect(sumLineCounts(buildChangeTree([ - entry('mode-only.ts', ' M', { unstaged: { additions: 0, deletions: 0 } }), + entry('mode-only.ts', ' M', { additions: 0, deletions: 0 }), ], 'unstaged'))).toEqual({ additions: 0, deletions: 0, files: 0 }) // A rename that DID move lines keeps contributing them... expect(sumLineCounts(buildChangeTree([ - entry('edited.ts', 'R ', { staged: { additions: 2, deletions: 1 } }), - entry('renamed.ts', 'R ', { staged: { additions: 0, deletions: 0 } }), + entry('edited.ts', 'R ', { additions: 2, deletions: 1 }, 'staged'), + entry('renamed.ts', 'R ', { additions: 0, deletions: 0 }, 'staged'), ], 'staged'))).toEqual({ additions: 2, deletions: 1, files: 1 }) }) @@ -105,8 +106,8 @@ describe('change tree line counts', () => { // One compressed chain row stands for `src/client/changes`; the total must // still be the sum of the leaves under it. const tree = buildChangeTree([ - entry('src/client/changes/a.ts', ' M', { unstaged: { additions: 2, deletions: 1 } }), - entry('src/client/changes/b.ts', ' M', { unstaged: { additions: 5, deletions: 0 } }), + entry('src/client/changes/a.ts', ' M', { additions: 2, deletions: 1 }), + entry('src/client/changes/b.ts', ' M', { additions: 5, deletions: 0 }), ], 'unstaged') expect(tree).toHaveLength(1) expect(tree[0]).toMatchObject({ kind: 'dir', name: 'src/client/changes' }) diff --git a/tests/git-view-worktree.spec.tsx b/tests/git-view-worktree.spec.tsx index 08915e5a..d0221062 100644 --- a/tests/git-view-worktree.spec.tsx +++ b/tests/git-view-worktree.spec.tsx @@ -455,7 +455,7 @@ describe('GitLens (changes tab, git lens) change tree', () => { async function mountTree( container: HTMLElement, root: Root, - entries: Array<{ path: string; xy: string; staged?: GitStatusEntry['staged']; unstaged?: GitStatusEntry['unstaged'] }>, + entries: GitStatusEntry[], ): Promise { const changes = entries.filter(row => row.xy !== ' ').length vi.spyOn(api, 'gitWorktrees').mockResolvedValue([{ path: MAIN, branch: 'main', current: true, changes }]) @@ -802,11 +802,9 @@ describe('GitLens (changes tab, git lens) change tree', () => { it('gives each band its OWN side of a file changed on both sides, never the sum (#131 regression)', async () => { const { container, root } = makeRoot() try { - // One file, changed on both sides ('MM'): a one-line rewrite staged, - // then five lines appended in the worktree. The two diffs have - // different bases, so the path is listed once per band with its own - // reading — and the bands' totals are those readings. Summing them - // printed `+6 −1` on the staged band AND on the unstaged one. + // One file changed on both sides ('MM'): a one-line rewrite staged, + // then five lines appended in the worktree. Each band must show its own + // reading — summing them printed `+6 −1` on BOTH bands. await mountTree(container, root, [ { path: 'a.txt', diff --git a/tests/git.spec.ts b/tests/git.spec.ts index 301d09b7..ea0ae210 100644 --- a/tests/git.spec.ts +++ b/tests/git.spec.ts @@ -361,21 +361,28 @@ describe('numstat line counts (#131)', () => { expect(parseNumstat('not a numstat record\nanother one\n')).toEqual(new Map()) }) - it('carries each side\'s counts, the binary marker and the untracked absence through status()', async () => { - const root = mkdtempSync(join(tmpdir(), 'dsh-git-counts-')) - try { - execFileSync('git', ['init', '-q'], { cwd: root }) - execFileSync('git', ['config', 'user.email', 't@t'], { cwd: root }) - execFileSync('git', ['config', 'user.name', 't'], { cwd: root }) - // Rename detection is a user config (diff.renames / status.renames); - // pin it so this fixture means the same thing on every machine. - execFileSync('git', ['config', 'diff.renames', 'true'], { cwd: root }) - execFileSync('git', ['config', 'status.renames', 'true'], { cwd: root }) - writeFileSync(join(root, 'tracked.txt'), 'one\ntwo\n') - writeFileSync(join(root, 'renamed.txt'), 'same\n') - execFileSync('git', ['add', '-A'], { cwd: root }) - execFileSync('git', ['commit', '-q', '-m', 'init'], { cwd: root }) + /** A fresh repo whose `files` (path → content) are committed as the baseline. + * `config` entries are applied before that first commit. */ + function mkRepo(files: Record, config: Record = {}): string { + const root = mkdtempSync(join(tmpdir(), 'dsh-git-repo-')) + execFileSync('git', ['init', '-q'], { cwd: root }) + execFileSync('git', ['config', 'user.email', 't@t'], { cwd: root }) + execFileSync('git', ['config', 'user.name', 't'], { cwd: root }) + for (const [key, value] of Object.entries(config)) execFileSync('git', ['config', key, value], { cwd: root }) + for (const [name, content] of Object.entries(files)) writeFileSync(join(root, name), content) + execFileSync('git', ['add', '-A'], { cwd: root }) + execFileSync('git', ['commit', '-q', '-m', 'init'], { cwd: root }) + return root + } + it('carries each side\'s counts, the binary marker and the untracked absence through status()', async () => { + // Rename detection is a user config (diff.renames / status.renames); pin it + // so this fixture means the same thing on every machine. + const root = mkRepo( + { 'tracked.txt': 'one\ntwo\n', 'renamed.txt': 'same\n' }, + { 'diff.renames': 'true', 'status.renames': 'true' }, + ) + try { // ONE path changed on BOTH sides (porcelain 'MM'): one line staged, then // one more in the worktree. The two diffs have different bases, so the // entry carries two readings — never one number that adds them up. @@ -415,17 +422,9 @@ describe('numstat line counts (#131)', () => { it('never adds the two sides up: a line both diffs see is counted once per side', async () => { // The double-count regression. Editing the SAME line twice — staged once, // then edited again in the worktree — makes both numstat sides report the - // same single line. Summing them reported a 2-line change on the staged row - // AND on the unstaged one, while `git diff HEAD --numstat` says 1. - const root = mkdtempSync(join(tmpdir(), 'dsh-git-both-sides-')) + // same single line; summing them reported a 2-line change on BOTH rows. + const root = mkRepo({ 'a.txt': 'one\ntwo\nthree\n' }) try { - execFileSync('git', ['init', '-q'], { cwd: root }) - execFileSync('git', ['config', 'user.email', 't@t'], { cwd: root }) - execFileSync('git', ['config', 'user.name', 't'], { cwd: root }) - writeFileSync(join(root, 'a.txt'), 'one\ntwo\nthree\n') - execFileSync('git', ['add', '-A'], { cwd: root }) - execFileSync('git', ['commit', '-q', '-m', 'init'], { cwd: root }) - writeFileSync(join(root, 'a.txt'), 'one\nTWO\nthree\n') execFileSync('git', ['add', 'a.txt'], { cwd: root }) writeFileSync(join(root, 'a.txt'), 'one\nTwo\nthree\n') From 1dc71458b98df5cfa8c13c501653dc5853c45de8 Mon Sep 17 00:00:00 2001 From: unsc9 <438590997@qq.com> Date: Sun, 11 Oct 2026 18:10:41 +0800 Subject: [PATCH 3/3] =?UTF-8?q?chore:=20=E9=87=8D=E8=B7=91=20CI=EF=BC=88ub?= =?UTF-8?q?untu=20lane=20=E7=9A=84=E5=B7=B2=E7=9F=A5=20flake=EF=BC=8C?= =?UTF-8?q?=E4=B8=8D=E5=90=AB=E4=BB=BB=E4=BD=95=E6=96=87=E4=BB=B6=E6=94=B9?= =?UTF-8?q?=E5=8A=A8=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `ci`(ubuntu)在 `tests/editor-host.spec.tsx` 的 `waitForTreeRow()` 条件等待上 跑满 10s 预算(10031ms,断言 `expected false to be true`)。该用例的文件里自带 这个 flake 的记录(`RENDER_WAIT_MS` 注释:饱和的 2 核 runner 上同时跑 184 个 文件时,文件树的异步层级加载会错过预算;2026-10-08 那次同样跑满,下一次运行 就过)。 同一次 run 的另两条 lane 支持「与代码无关」:`ci-windows` 的 Test / Build / 消费者类型 / 引擎断言全部 success(8m8s 通过),`plugin-mount` 通过。 本提交**不含任何文件改动**(`git show --stat` 为空),仅用于触发 `pull_request` 的 synchronize,让 `ci` 重新跑一遍。上一提交 `42bfd93` 对 `src/` 的改动逐行都是注释,且没有碰 `editor-host.spec.tsx`。作者在本仓库没有 admin 权限(`gh run rerun` 被拒:Must have admin rights),故用这种方式重跑。