MCPcopy
hub / github.com/codeaashu/claude-code / fetchSingleFileGitDiff

Function fetchSingleFileGitDiff

src/utils/gitDiff.ts:405–441  ·  view source on GitHub ↗
(
  absoluteFilePath: string,
)

Source from the content-addressed store, hash-verified

403 * Returns null if not in a git repo or if git commands fail.
404 */
405export async function fetchSingleFileGitDiff(
406 absoluteFilePath: string,
407): Promise<ToolUseDiff | null> {
408 const gitRoot = findGitRoot(dirname(absoluteFilePath))
409 if (!gitRoot) return null
410
411 const gitPath = relative(gitRoot, absoluteFilePath).split(sep).join('/')
412 const repository = getCachedRepository()
413
414 // Check if the file is tracked by git
415 const { code: lsFilesCode } = await execFileNoThrowWithCwd(
416 gitExe(),
417 ['--no-optional-locks', 'ls-files', '--error-unmatch', gitPath],
418 { cwd: gitRoot, timeout: SINGLE_FILE_DIFF_TIMEOUT_MS },
419 )
420
421 if (lsFilesCode === 0) {
422 // File is tracked - diff against merge base for PR-like view
423 const diffRef = await getDiffRef(gitRoot)
424 const { stdout, code } = await execFileNoThrowWithCwd(
425 gitExe(),
426 ['--no-optional-locks', 'diff', diffRef, '--', gitPath],
427 { cwd: gitRoot, timeout: SINGLE_FILE_DIFF_TIMEOUT_MS },
428 )
429 if (code !== 0) return null
430 if (!stdout) return null
431 return {
432 ...parseRawDiffToToolUseDiff(gitPath, stdout, 'modified'),
433 repository,
434 }
435 }
436
437 // File is untracked - generate synthetic diff
438 const syntheticDiff = await generateSyntheticDiff(gitPath, absoluteFilePath)
439 if (!syntheticDiff) return null
440 return { ...syntheticDiff, repository }
441}
442
443/**
444 * Parse raw unified diff output into the structured ToolUseDiff format.

Callers 2

callFunction · 0.85
callFunction · 0.85

Calls 5

getCachedRepositoryFunction · 0.85
execFileNoThrowWithCwdFunction · 0.85
getDiffRefFunction · 0.85
generateSyntheticDiffFunction · 0.85

Tested by

no test coverage detected