MCPcopy Index your code
hub / github.com/claude-code-best/claude-code / computeFileModificationState

Function computeFileModificationState

src/utils/commitAttribution.ts:326–381  ·  view source on GitHub ↗

* Compute the character contribution for a file modification. * Returns the FileAttributionState to store, or null if tracking failed.

(
  existingFileStates: Map<string, FileAttributionState>,
  filePath: string,
  oldContent: string,
  newContent: string,
  mtime: number,
)

Source from the content-addressed store, hash-verified

324 * Returns the FileAttributionState to store, or null if tracking failed.
325 */
326function computeFileModificationState(
327 existingFileStates: Map<string, FileAttributionState>,
328 filePath: string,
329 oldContent: string,
330 newContent: string,
331 mtime: number,
332): FileAttributionState | null {
333 const normalizedPath = normalizeFilePath(filePath)
334
335 try {
336 // Calculate Claude's character contribution
337 let claudeContribution: number
338
339 if (oldContent === '' || newContent === '') {
340 // New file or full deletion - contribution is the content length
341 claudeContribution =
342 oldContent === '' ? newContent.length : oldContent.length
343 } else {
344 // Find actual changed region via common prefix/suffix matching.
345 // This correctly handles same-length replacements (e.g., "Esc" → "esc")
346 // where Math.abs(newLen - oldLen) would be 0.
347 const minLen = Math.min(oldContent.length, newContent.length)
348 let prefixEnd = 0
349 while (
350 prefixEnd < minLen &&
351 oldContent[prefixEnd] === newContent[prefixEnd]
352 ) {
353 prefixEnd++
354 }
355 let suffixLen = 0
356 while (
357 suffixLen < minLen - prefixEnd &&
358 oldContent[oldContent.length - 1 - suffixLen] ===
359 newContent[newContent.length - 1 - suffixLen]
360 ) {
361 suffixLen++
362 }
363 const oldChangedLen = oldContent.length - prefixEnd - suffixLen
364 const newChangedLen = newContent.length - prefixEnd - suffixLen
365 claudeContribution = Math.max(oldChangedLen, newChangedLen)
366 }
367
368 // Get current file state if it exists
369 const existingState = existingFileStates.get(normalizedPath)
370 const existingContribution = existingState?.claudeContribution ?? 0
371
372 return {
373 contentHash: computeContentHash(newContent),
374 claudeContribution: existingContribution + claudeContribution,
375 mtime,
376 }
377 } catch (error) {
378 logError(error as Error)
379 return null
380 }
381}
382
383/**

Callers 2

trackFileModificationFunction · 0.85
trackBulkFileChangesFunction · 0.85

Calls 5

normalizeFilePathFunction · 0.85
computeContentHashFunction · 0.85
maxMethod · 0.80
logErrorFunction · 0.70
getMethod · 0.65

Tested by

no test coverage detected