All files / src/diff context.ts

91.78% Statements 67/73
86.04% Branches 37/43
92.3% Functions 12/13
91.78% Lines 67/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290                  19x                               18x 6x           12x             36x 36x       28x 28x 28x       8x   8x 4x     4x 4x 4x                       2x       2x             2x       2x                 36x 36x 26x     10x         10x       36x 36x                                           18x 18x                                                               18x                           36x 36x 22x     14x                       18x     18x       18x     18x                 18x               18x                                   9x 14x               17x   17x 17x 17x   17x 1x     17x     17x 1x 1x 1x 1x 1x       17x 17x 17x 17x 17x     17x 1x 1x 1x 1x 1x 1x     17x    
import { spawnSync } from 'node:child_process';
import { readFileSync, existsSync } from 'node:fs';
import { join, resolve } from 'node:path';
import type { DiffHunk, ParsedDiff } from './parser.js';
import { getExpandedLineRange } from './parser.js';
import type { DiffContextSource } from '../types/index.js';
import { GIT_NON_INTERACTIVE_ENV } from '../utils/exec.js';
 
/** Cache for file contents to avoid repeated reads */
const fileCache = new Map<string, string[] | null>();
 
export interface ExpandContextOptions {
  /** Number of context lines to read before and after each hunk */
  contextLines?: number;
  /** Source tree to read hunk context from */
  contentSource?: DiffContextSource;
}
 
/** Clear the file cache (useful for testing or long-running processes) */
export function clearFileCache(): void {
  fileCache.clear();
}
 
/** Get cached file lines or read and cache them */
function normalizeOptions(options: number | ExpandContextOptions): Required<ExpandContextOptions> {
  if (typeof options === 'number') {
    return {
      contextLines: options,
      contentSource: { type: 'working-tree' },
    };
  }
 
  return {
    contextLines: options.contextLines ?? 20,
    contentSource: options.contentSource ?? { type: 'working-tree' },
  };
}
 
function cacheKey(repoPath: string, filename: string, source: DiffContextSource): string {
  const sourceKey = source.type === 'git-ref' ? `${source.type}:${source.ref}` : source.type;
  return `${sourceKey}:${repoPath}:${filename}`;
}
 
function isInsideRepo(repoPath: string, filename: string): boolean {
  const resolvedRepo = resolve(repoPath);
  const resolvedFile = resolve(join(repoPath, filename));
  return resolvedFile === resolvedRepo || resolvedFile.startsWith(resolvedRepo + '/');
}
 
function readWorkingTreeLines(repoPath: string, filename: string): string[] | null {
  const filePath = join(repoPath, filename);
 
  if (!existsSync(filePath)) {
    return null;
  }
 
  try {
    const content = readFileSync(filePath, 'utf-8');
    return content.split('\n');
  } catch {
    // Binary file or read error
    return null;
  }
}
 
function readGitSourceLines(
  repoPath: string,
  filename: string,
  source: Extract<DiffContextSource, { type: 'git-index' | 'git-ref' }>
): string[] | null {
  const refPath = source.type === 'git-index'
    ? `:${filename}`
    : `${source.ref}:${filename}`;
 
  const result = spawnSync('git', ['show', refPath], {
    cwd: repoPath,
    encoding: 'utf-8',
    env: { ...process.env, ...GIT_NON_INTERACTIVE_ENV },
    stdio: ['ignore', 'pipe', 'pipe'],
  });
 
  Iif (result.error || result.status !== 0 || typeof result.stdout !== 'string') {
    return null;
  }
 
  return result.stdout.split('\n');
}
 
/** Get cached file lines or read and cache them */
function getCachedFileLines(
  repoPath: string,
  filename: string,
  source: DiffContextSource
): string[] | null {
  const key = cacheKey(repoPath, filename, source);
  if (fileCache.has(key)) {
    return fileCache.get(key) ?? null;
  }
 
  Iif (!isInsideRepo(repoPath, filename)) {
    fileCache.set(key, null);
    return null;
  }
 
  const lines = source.type === 'working-tree'
    ? readWorkingTreeLines(repoPath, filename)
    : readGitSourceLines(repoPath, filename, source);
 
  fileCache.set(key, lines);
  return lines;
}
 
export interface HunkWithContext {
  /** File path */
  filename: string;
  /** The hunk being analyzed */
  hunk: DiffHunk;
  /** Lines before the hunk (from actual file) */
  contextBefore: string[];
  /** Lines after the hunk (from actual file) */
  contextAfter: string[];
  /** Start line of contextBefore */
  contextStartLine: number;
  /** Detected language from file extension */
  language: string;
}
 
/**
 * Detect language from filename.
 */
function detectLanguage(filename: string): string {
  const ext = filename.split('.').pop()?.toLowerCase() ?? '';
  const languageMap: Record<string, string> = {
    ts: 'typescript',
    tsx: 'typescript',
    js: 'javascript',
    jsx: 'javascript',
    py: 'python',
    rb: 'ruby',
    go: 'go',
    rs: 'rust',
    java: 'java',
    kt: 'kotlin',
    cs: 'csharp',
    cpp: 'cpp',
    c: 'c',
    h: 'c',
    hpp: 'cpp',
    swift: 'swift',
    php: 'php',
    sh: 'bash',
    bash: 'bash',
    zsh: 'bash',
    yml: 'yaml',
    yaml: 'yaml',
    json: 'json',
    toml: 'toml',
    md: 'markdown',
    sql: 'sql',
    html: 'html',
    css: 'css',
    scss: 'scss',
    less: 'less',
  };
  return languageMap[ext] ?? ext;
}
 
/**
 * Read specific lines from a file using the cache.
 * Returns empty array if file doesn't exist or is binary.
 */
function readFileLines(
  repoPath: string,
  filename: string,
  source: DiffContextSource,
  startLine: number,
  endLine: number
): string[] {
  const lines = getCachedFileLines(repoPath, filename, source);
  if (!lines) {
    return [];
  }
  // Lines are 1-indexed, arrays are 0-indexed
  return lines.slice(startLine - 1, endLine);
}
 
/**
 * Expand a hunk with surrounding context from the actual file.
 */
export function expandHunkContext(
  repoPath: string,
  filename: string,
  hunk: DiffHunk,
  options: number | ExpandContextOptions = 20
): HunkWithContext {
  const { contextLines, contentSource } = normalizeOptions(options);
 
  // Defense-in-depth: ensure filename doesn't escape repo directory
  Iif (!isInsideRepo(repoPath, filename)) {
    return { filename, hunk, contextBefore: [], contextAfter: [], contextStartLine: 1, language: detectLanguage(filename) };
  }
 
  const expandedRange = getExpandedLineRange(hunk, contextLines);
 
  // Read context before the hunk
  const contextBefore = readFileLines(
    repoPath,
    filename,
    contentSource,
    expandedRange.start,
    hunk.newStart - 1
  );
 
  // Read context after the hunk
  const contextAfter = readFileLines(
    repoPath,
    filename,
    contentSource,
    hunk.newStart + hunk.newCount,
    expandedRange.end
  );
 
  return {
    filename,
    hunk,
    contextBefore,
    contextAfter,
    contextStartLine: expandedRange.start,
    language: detectLanguage(filename),
  };
}
 
/**
 * Expand all hunks in a parsed diff with context.
 */
export function expandDiffContext(
  repoPath: string,
  diff: ParsedDiff,
  options: number | ExpandContextOptions = 20
): HunkWithContext[] {
  return diff.hunks.map((hunk) =>
    expandHunkContext(repoPath, diff.filename, hunk, options)
  );
}
 
/**
 * Format a hunk with context for LLM analysis.
 */
export function formatHunkForAnalysis(hunkCtx: HunkWithContext): string {
  const lines: string[] = [];
 
  lines.push(`## File: ${hunkCtx.filename}`);
  lines.push(`## Language: ${hunkCtx.language}`);
  lines.push(`## Hunk: lines ${hunkCtx.hunk.newStart}-${hunkCtx.hunk.newStart + hunkCtx.hunk.newCount - 1}`);
 
  if (hunkCtx.hunk.header) {
    lines.push(`## Scope: ${hunkCtx.hunk.header}`);
  }
 
  lines.push('');
 
  // Context before
  if (hunkCtx.contextBefore.length > 0) {
    lines.push(`### Context Before (lines ${hunkCtx.contextStartLine}-${hunkCtx.hunk.newStart - 1})`);
    lines.push('```' + hunkCtx.language);
    lines.push(hunkCtx.contextBefore.join('\n'));
    lines.push('```');
    lines.push('');
  }
 
  // The actual changes
  lines.push(`### Changes`);
  lines.push('```diff');
  lines.push(hunkCtx.hunk.content);
  lines.push('```');
  lines.push('');
 
  // Context after
  if (hunkCtx.contextAfter.length > 0) {
    const afterStart = hunkCtx.hunk.newStart + hunkCtx.hunk.newCount;
    const afterEnd = afterStart + hunkCtx.contextAfter.length - 1;
    lines.push(`### Context After (lines ${afterStart}-${afterEnd})`);
    lines.push('```' + hunkCtx.language);
    lines.push(hunkCtx.contextAfter.join('\n'));
    lines.push('```');
  }
 
  return lines.join('\n');
}