fix(git-worktree): compute real line counts for untracked files in diff stats
This commit is contained in:
parent
7fdba56d8f
commit
edc3317e37
@ -7,6 +7,9 @@ const execSyncMock = mock(() => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const execFileSyncMock = mock((file: string, args: string[], _opts: { cwd?: string }) => {
|
const execFileSyncMock = mock((file: string, args: string[], _opts: { cwd?: string }) => {
|
||||||
|
if (file === "wc") {
|
||||||
|
return " 10 new-file.ts\n"
|
||||||
|
}
|
||||||
if (file !== "git") throw new Error(`unexpected file: ${file}`)
|
if (file !== "git") throw new Error(`unexpected file: ${file}`)
|
||||||
const subcommand = args[0]
|
const subcommand = args[0]
|
||||||
|
|
||||||
@ -42,7 +45,7 @@ describe("collectGitDiffStats", () => {
|
|||||||
|
|
||||||
//#then
|
//#then
|
||||||
expect(execSyncMock).not.toHaveBeenCalled()
|
expect(execSyncMock).not.toHaveBeenCalled()
|
||||||
expect(execFileSyncMock).toHaveBeenCalledTimes(3)
|
expect(execFileSyncMock).toHaveBeenCalledTimes(4)
|
||||||
|
|
||||||
const [firstCallFile, firstCallArgs, firstCallOpts] = execFileSyncMock.mock
|
const [firstCallFile, firstCallArgs, firstCallOpts] = execFileSyncMock.mock
|
||||||
.calls[0]! as unknown as [string, string[], { cwd?: string }]
|
.calls[0]! as unknown as [string, string[], { cwd?: string }]
|
||||||
@ -65,6 +68,12 @@ describe("collectGitDiffStats", () => {
|
|||||||
expect(thirdCallOpts.cwd).toBe(directory)
|
expect(thirdCallOpts.cwd).toBe(directory)
|
||||||
expect(thirdCallArgs.join(" ")).not.toContain(directory)
|
expect(thirdCallArgs.join(" ")).not.toContain(directory)
|
||||||
|
|
||||||
|
const [fourthCallFile, fourthCallArgs, fourthCallOpts] = execFileSyncMock.mock
|
||||||
|
.calls[3]! as unknown as [string, string[], { cwd?: string }]
|
||||||
|
expect(fourthCallFile).toBe("wc")
|
||||||
|
expect(fourthCallArgs).toEqual(["-l", "--", "new-file.ts"])
|
||||||
|
expect(fourthCallOpts.cwd).toBe(directory)
|
||||||
|
|
||||||
expect(result).toEqual([
|
expect(result).toEqual([
|
||||||
{
|
{
|
||||||
path: "file.ts",
|
path: "file.ts",
|
||||||
@ -74,7 +83,7 @@ describe("collectGitDiffStats", () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: "new-file.ts",
|
path: "new-file.ts",
|
||||||
added: 0,
|
added: 10,
|
||||||
removed: 0,
|
removed: 0,
|
||||||
status: "added",
|
status: "added",
|
||||||
},
|
},
|
||||||
|
|||||||
@ -30,7 +30,24 @@ export function collectGitDiffStats(directory: string): GitFileStat[] {
|
|||||||
? untrackedOutput
|
? untrackedOutput
|
||||||
.split("\n")
|
.split("\n")
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.map((filePath) => `0\t0\t${filePath}`)
|
.map((filePath) => {
|
||||||
|
try {
|
||||||
|
const wcOutput = execFileSync("wc", ["-l", "--", filePath], {
|
||||||
|
cwd: directory,
|
||||||
|
encoding: "utf-8",
|
||||||
|
timeout: 5000,
|
||||||
|
stdio: ["pipe", "pipe", "pipe"],
|
||||||
|
}).trim()
|
||||||
|
|
||||||
|
const [lineCountToken] = wcOutput.split(/\s+/)
|
||||||
|
const lineCount = Number(lineCountToken)
|
||||||
|
if (!Number.isFinite(lineCount)) return `0\t0\t${filePath}`
|
||||||
|
|
||||||
|
return `${lineCount}\t0\t${filePath}`
|
||||||
|
} catch {
|
||||||
|
return `0\t0\t${filePath}`
|
||||||
|
}
|
||||||
|
})
|
||||||
.join("\n")
|
.join("\n")
|
||||||
: ""
|
: ""
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user