MCPcopy Index your code
hub / github.com/promptfoo/promptfoo / generatePatchForFile

Function generatePatchForFile

src/codeScan/git/diffProcessor.ts:370–422  ·  view source on GitHub ↗
(
  repoPath: string,
  base: string,
  compare: string,
  filePath: string,
)

Source from the content-addressed store, hash-verified

368}
369
370async function generatePatchForFile(
371 repoPath: string,
372 base: string,
373 compare: string,
374 filePath: string,
375): Promise<PatchResult> {
376 try {
377 const result = await execa(
378 'git',
379 [
380 'diff',
381 '--patch',
382 '--unified=3',
383 '--no-color',
384 '--no-ext-diff',
385 `${base}...${compare}`,
386 '--',
387 filePath,
388 ],
389 {
390 cwd: repoPath,
391 maxBuffer: MAX_PATCH_SIZE_BYTES,
392 },
393 );
394
395 const patch = result.stdout;
396
397 // Double check patch size
398 const patchSize = Buffer.byteLength(patch, 'utf8');
399 if (patchSize > MAX_PATCH_SIZE_BYTES) {
400 return { success: false, skipReason: 'patch too large' };
401 }
402
403 // Annotate the patch with line numbers and extract valid line ranges
404 const { annotatedDiff, lineRanges } = annotateDiffWithLineRanges(patch);
405
406 return { success: true, patch: annotatedDiff, lineRanges };
407 } catch (err) {
408 const errorMessage = err instanceof Error ? err.message : String(err);
409
410 // Check if error is due to maxBuffer exceeded
411 if (errorMessage.includes('maxBuffer') || errorMessage.includes('stdout maxBuffer')) {
412 logger.debug(
413 `git diff --patch ${filePath} exceeded maxBuffer (${MAX_PATCH_SIZE_BYTES} bytes) - patch too large`,
414 );
415 return { success: false, skipReason: 'patch too large' };
416 }
417
418 // Other git diff errors
419 logger.debug(`git diff --patch ${filePath} failed: ${errorMessage} - skipping file`);
420 return { success: false, skipReason: 'diff error' };
421 }
422}
423
424async function generatePatches(
425 repoPath: string,

Callers 1

generatePatchesFunction · 0.85

Calls 1

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…