MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / fetchGitDiff

Function fetchGitDiff

source code/utils/gitDiff.ts:51–110  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

49 * made by the user.
50 */
51export async function fetchGitDiff(): Promise<GitDiffResult | null> {
52 const isGit = await getIsGit()
53 if (!isGit) return null
54
55 // Skip diff calculation during transient git states since the
56 // working tree contains incoming changes, not user-intentional edits
57 if (await isInTransientGitState()) {
58 return null
59 }
60
61 // Quick probe: use --shortstat to get totals without loading all content.
62 // This is O(1) memory and lets us detect massive diffs (e.g., jj workspaces)
63 // before committing to expensive operations.
64 const { stdout: shortstatOut, code: shortstatCode } = await execFileNoThrow(
65 gitExe(),
66 ['--no-optional-locks', 'diff', 'HEAD', '--shortstat'],
67 { timeout: GIT_TIMEOUT_MS, preserveOutputOnError: false },
68 )
69
70 if (shortstatCode === 0) {
71 const quickStats = parseShortstat(shortstatOut)
72 if (quickStats && quickStats.filesCount > MAX_FILES_FOR_DETAILS) {
73 // Too many files - return accurate totals but skip per-file details
74 // to avoid loading hundreds of MB into memory
75 return {
76 stats: quickStats,
77 perFileStats: new Map(),
78 hunks: new Map(),
79 }
80 }
81 }
82
83 // Get stats via --numstat (all uncommitted changes vs HEAD)
84 const { stdout: numstatOut, code: numstatCode } = await execFileNoThrow(
85 gitExe(),
86 ['--no-optional-locks', 'diff', 'HEAD', '--numstat'],
87 { timeout: GIT_TIMEOUT_MS, preserveOutputOnError: false },
88 )
89
90 if (numstatCode !== 0) return null
91
92 const { stats, perFileStats } = parseGitNumstat(numstatOut)
93
94 // Include untracked files (new files not yet staged)
95 // Just filenames - no content reading for performance
96 const remainingSlots = MAX_FILES - perFileStats.size
97 if (remainingSlots > 0) {
98 const untrackedStats = await fetchUntrackedFiles(remainingSlots)
99 if (untrackedStats) {
100 stats.filesCount += untrackedStats.size
101 for (const [path, fileStats] of untrackedStats) {
102 perFileStats.set(path, fileStats)
103 }
104 }
105 }
106
107 // Return stats only - hunks are fetched on-demand via fetchGitDiffHunks()
108 // to avoid expensive git diff HEAD call on every poll

Callers 1

loadDiffDataFunction · 0.85

Calls 6

isInTransientGitStateFunction · 0.85
execFileNoThrowFunction · 0.85
parseShortstatFunction · 0.85
parseGitNumstatFunction · 0.85
fetchUntrackedFilesFunction · 0.85
setMethod · 0.80

Tested by

no test coverage detected