import { CommitDiff } from '../interfaces/CommitDiff'; import { FileDiff } from '../interfaces/FileDiff'; function generateHumanReadableCommitDiffDocument(commitDiff: CommitDiff): string { const output: string[] = []; output.push(`Commit title: ${commitDiff.title}`); if (commitDiff.message !== null) { output.push(`Commit message: ${commitDiff.message}`); } for (const fileDiff of commitDiff.diffs) { output.push(generateHumanReadableFileDiffText(fileDiff)); } return output.join('\n'); } /** * Generates a human-readable string that represents a git diff chunk. * Adds leading line numbers for code lines. */ function generateHumanReadableFileDiffText(fileDiff: FileDiff): string { const output: string[] = []; // Determine the maximum line number to calculate the width for padding const maxLineNum = fileDiff.lines.at(-1)?.lineNum; if (maxLineNum === undefined) { throw new Error('Could not determine the maximum line number of file diff'); } const maxLineWidth = maxLineNum.toString().length; output.push(`File: ${fileDiff.fileName}`); output.push(`Changes:`); for (const line of fileDiff.lines) { // Pad the line number with spaces to ensure consistent alignment const paddedLineNum = line.lineNum.toString().padStart(maxLineWidth, ' '); output.push(`${paddedLineNum}: ${line.content}`); } return output.join('\n'); } export { generateHumanReadableCommitDiffDocument };