|
| 1 | +type GitChangeKind = 'added' | 'modified' | 'deleted' | 'renamed' |
| 2 | + |
| 3 | +type GitChange = { |
| 4 | + path: string |
| 5 | + kind: GitChangeKind |
| 6 | +} |
| 7 | + |
| 8 | +export type GitCommitSummaryInput = { |
| 9 | + status?: string |
| 10 | + diff?: string |
| 11 | + diffCached?: string |
| 12 | + maxFiles?: number |
| 13 | +} |
| 14 | + |
| 15 | +const DEFAULT_SUMMARY = 'Update project files' |
| 16 | + |
| 17 | +const normalizePath = (path: string) => |
| 18 | + path.trim().replace(/^"|"$/g, '').replace(/^a\//, '').replace(/^b\//, '') |
| 19 | + |
| 20 | +const basename = (path: string) => |
| 21 | + path.split('/').filter(Boolean).at(-1) ?? path |
| 22 | + |
| 23 | +const stripExtension = (fileName: string) => fileName.replace(/\.[^.]+$/, '') |
| 24 | + |
| 25 | +const humanizeFileName = (fileName: string) => |
| 26 | + stripExtension(fileName) |
| 27 | + .replace(/[-_]+/g, ' ') |
| 28 | + .replace(/\b\w/g, (char) => char.toUpperCase()) |
| 29 | + |
| 30 | +const parseStatusKind = (code: string): GitChangeKind => { |
| 31 | + if (code.includes('A') || code.includes('?')) return 'added' |
| 32 | + if (code.includes('D')) return 'deleted' |
| 33 | + if (code.includes('R')) return 'renamed' |
| 34 | + return 'modified' |
| 35 | +} |
| 36 | + |
| 37 | +const parseStatusChanges = (status: string): GitChange[] => { |
| 38 | + return status |
| 39 | + .split('\n') |
| 40 | + .map((line) => line.trimEnd()) |
| 41 | + .filter(Boolean) |
| 42 | + .flatMap((line) => { |
| 43 | + const porcelain = line.match(/^([ MADRCU?!]{1,2})\s+(.+)$/) |
| 44 | + if (!porcelain) return [] |
| 45 | + |
| 46 | + const [, code, rawPath] = porcelain |
| 47 | + const path = rawPath.includes(' -> ') |
| 48 | + ? rawPath.split(' -> ').at(-1)! |
| 49 | + : rawPath |
| 50 | + |
| 51 | + return [{ path: normalizePath(path), kind: parseStatusKind(code) }] |
| 52 | + }) |
| 53 | +} |
| 54 | + |
| 55 | +const parseDiffChanges = (diff: string): GitChange[] => { |
| 56 | + const changes: GitChange[] = [] |
| 57 | + let currentPath: string | null = null |
| 58 | + let currentKind: GitChangeKind = 'modified' |
| 59 | + |
| 60 | + const flush = () => { |
| 61 | + if (currentPath) { |
| 62 | + changes.push({ path: normalizePath(currentPath), kind: currentKind }) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + for (const line of diff.split('\n')) { |
| 67 | + const header = line.match(/^diff --git a\/(.+?) b\/(.+)$/) |
| 68 | + if (header) { |
| 69 | + flush() |
| 70 | + currentPath = header[2] |
| 71 | + currentKind = 'modified' |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + if (line.startsWith('new file mode')) { |
| 76 | + currentKind = 'added' |
| 77 | + } else if (line.startsWith('deleted file mode')) { |
| 78 | + currentKind = 'deleted' |
| 79 | + } else if (line.startsWith('rename to ')) { |
| 80 | + currentPath = line.slice('rename to '.length) |
| 81 | + currentKind = 'renamed' |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + flush() |
| 86 | + return changes |
| 87 | +} |
| 88 | + |
| 89 | +const dedupeChanges = (changes: GitChange[]): GitChange[] => { |
| 90 | + const byPath = new Map<string, GitChange>() |
| 91 | + for (const change of changes) { |
| 92 | + if (!change.path) continue |
| 93 | + byPath.set(change.path, change) |
| 94 | + } |
| 95 | + return [...byPath.values()] |
| 96 | +} |
| 97 | + |
| 98 | +const listNames = (names: string[]) => { |
| 99 | + if (names.length === 1) return names[0] |
| 100 | + if (names.length === 2) return `${names[0]} and ${names[1]}` |
| 101 | + return `${names.slice(0, -1).join(', ')}, and ${names.at(-1)}` |
| 102 | +} |
| 103 | + |
| 104 | +const summarizeSpecialChanges = (changes: GitChange[]) => { |
| 105 | + const paths = new Set(changes.map((change) => change.path.toLowerCase())) |
| 106 | + const summaries: string[] = [] |
| 107 | + |
| 108 | + if ( |
| 109 | + [...paths].some((path) => |
| 110 | + /(^|\/)(agents|ai-instructions|instructions)\.md$/.test(path), |
| 111 | + ) |
| 112 | + ) { |
| 113 | + summaries.push('update AI agent instructions') |
| 114 | + } |
| 115 | + |
| 116 | + if ( |
| 117 | + paths.has('changelog.md') || |
| 118 | + [...paths].some((path) => path.endsWith('/changelog.md')) |
| 119 | + ) { |
| 120 | + summaries.push('add changelog') |
| 121 | + } |
| 122 | + |
| 123 | + if ( |
| 124 | + [...paths].some((path) => /(^|\/)(package\.json|bun\.lock)$/.test(path)) |
| 125 | + ) { |
| 126 | + summaries.push('update dependencies') |
| 127 | + } |
| 128 | + |
| 129 | + return summaries |
| 130 | +} |
| 131 | + |
| 132 | +const getAction = (changes: GitChange[]) => { |
| 133 | + if (changes.every((change) => change.kind === 'added')) return 'Add' |
| 134 | + if (changes.every((change) => change.kind === 'deleted')) return 'Remove' |
| 135 | + if (changes.every((change) => change.kind === 'renamed')) return 'Rename' |
| 136 | + return 'Update' |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Builds a concise commit title from actual git changes. This is intentionally |
| 141 | + * deterministic so push flows can avoid falling back to the user's prompt. |
| 142 | + */ |
| 143 | +export const summarizeGitChangesForCommit = ({ |
| 144 | + status = '', |
| 145 | + diff = '', |
| 146 | + diffCached = '', |
| 147 | + maxFiles = 3, |
| 148 | +}: GitCommitSummaryInput) => { |
| 149 | + const changes = dedupeChanges([ |
| 150 | + ...parseStatusChanges(status), |
| 151 | + ...parseDiffChanges(diffCached), |
| 152 | + ...parseDiffChanges(diff), |
| 153 | + ]) |
| 154 | + |
| 155 | + if (changes.length === 0) { |
| 156 | + return DEFAULT_SUMMARY |
| 157 | + } |
| 158 | + |
| 159 | + const specialSummaries = summarizeSpecialChanges(changes) |
| 160 | + if (specialSummaries.length > 0) { |
| 161 | + return specialSummaries |
| 162 | + .map((summary, index) => |
| 163 | + index === 0 ? summary[0].toUpperCase() + summary.slice(1) : summary, |
| 164 | + ) |
| 165 | + .join(' and ') |
| 166 | + } |
| 167 | + |
| 168 | + const names = changes |
| 169 | + .slice(0, maxFiles) |
| 170 | + .map((change) => humanizeFileName(basename(change.path))) |
| 171 | + const suffix = |
| 172 | + changes.length > maxFiles ? ` and ${changes.length - maxFiles} more` : '' |
| 173 | + |
| 174 | + return `${getAction(changes)} ${listNames(names)}${suffix}` |
| 175 | +} |
0 commit comments