Skip to content

Commit 12b8d33

Browse files
authored
Merge pull request #119 from aqsaaqeel/feat/truncate-big-diffs
feat: handle large diffs by truncating before LLM input
2 parents 2ccd8c8 + 82b2807 commit 12b8d33

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

cmd/cli/createMsg.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,41 @@ func CreateCommitMsg(Store *store.StoreMethods, dryRun bool, autoCommit bool) {
9191
return
9292
}
9393

94+
// Large diff handling
95+
const maxDiffChars = 8000 // can change as needed
96+
const maxDiffLines = 300
97+
98+
diffLines := strings.Split(changes, "\n")
99+
diffTooLarge := len(changes) > maxDiffChars || len(diffLines) > maxDiffLines
100+
101+
if diffTooLarge {
102+
pterm.Warning.Println("The diff is very large and may exceed the LLM's context window.")
103+
pterm.Info.Printf("Diff size: %d lines, %d characters.\n", len(diffLines), len(changes))
104+
pterm.Info.Println("Only the first part of the diff will be used for commit message generation.")
105+
106+
// Truncate the diff for LLM input, preserving whole lines and UTF-8 safety
107+
truncatedLines := make([]string, 0, len(diffLines))
108+
totalChars := 0
109+
110+
for i, line := range diffLines {
111+
lineLen := len([]rune(line)) + 1 // +1 for newline, using rune count for UTF-8 safety
112+
113+
// Stop if we've reached max lines or adding this line would exceed max chars
114+
if i >= maxDiffLines || (totalChars+lineLen) > maxDiffChars {
115+
break
116+
}
117+
118+
truncatedLines = append(truncatedLines, line)
119+
totalChars += lineLen
120+
}
121+
122+
changes = strings.Join(truncatedLines, "\n")
123+
actualLineCount := len(truncatedLines)
124+
125+
pterm.Info.Printf("Truncated diff to %d lines, %d characters.\n", actualLineCount, len(changes))
126+
pterm.Info.Println("Consider committing smaller changes for more accurate commit messages.")
127+
}
128+
94129
// Handle dry-run mode: display what would be sent to LLM without making API call
95130
if dryRun {
96131
pterm.Println()

0 commit comments

Comments
 (0)