Skip to content

Commit 8e20b55

Browse files
committed
feat: Refactor diff threshold logic to use total changes
This commit refactors the logic for determining which language model to use based on the size of the git diff. Instead of counting the number of lines in the diff, it now calculates the total number of insertions and deletions using `git diff --shortstat`. The `diff.threshold` default value has also been adjusted from 500 to 200 to better reflect the new calculation method. auto-commit-msg{Version:0.4.0-dev Model:gemini-2.5-flash-lite ResponseTime:1.427025958s ExecutionTime:1.435464833s}
1 parent 0286d1f commit 8e20b55

1 file changed

Lines changed: 33 additions & 8 deletions

File tree

cmd/root.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"log"
66
"os"
77
"os/exec"
8+
"regexp"
9+
"strconv"
810
"strings"
911
"time"
1012

@@ -77,22 +79,45 @@ var rootCmd = &cobra.Command{
7779
cobra.CheckErr("git diff is empty")
7880
}
7981

80-
gitDiffStr := string(gitDiff)
81-
gitDiffLoc := strings.Count(gitDiffStr, "\n")
82-
diffThreshold := config.Diff.Threshold
82+
shortStat, err := exec.Command("git", "diff", "--cached", "--shortstat").Output()
83+
if err != nil {
84+
cobra.CheckErr(err)
85+
}
86+
shortStatStr := string(shortStat)
87+
88+
insertionMatches := regexp.MustCompile(`(\d+)\s+insertions?\(\+\)`).FindStringSubmatch(shortStatStr)
89+
insertions := 0
90+
if len(insertionMatches) > 1 {
91+
insertions, err = strconv.Atoi(insertionMatches[1])
92+
if err != nil {
93+
cobra.CheckErr(err)
94+
}
95+
}
96+
97+
deletionMatches := regexp.MustCompile(`(\d+)\s+deletions?\(\-\)`).FindStringSubmatch(shortStatStr)
98+
deletions := 0
99+
if len(deletionMatches) > 1 {
100+
deletions, err = strconv.Atoi(deletionMatches[1])
101+
if err != nil {
102+
cobra.CheckErr(err)
103+
}
104+
}
105+
106+
totalChanges := insertions + deletions
107+
totalChangesThreshold := config.Diff.Threshold
83108
var model string
84-
if gitDiffLoc < diffThreshold {
109+
if totalChanges < totalChangesThreshold {
85110
model = config.Diff.ShortModel
86111
if model == "" {
87112
cobra.CheckErr("short_model cannot be empty")
88113
}
89-
log.Printf("git diff LOC %d under %d threshold, using model for short diffs: %s\n", gitDiffLoc, diffThreshold, model)
114+
log.Printf("git diff total changes %d under %d threshold, using model for short diffs: %s\n", totalChanges, totalChangesThreshold, model)
90115
} else {
91116
model = config.Diff.LongModel
92117
if model == "" {
93118
cobra.CheckErr("long_model cannot be empty")
94119
}
95-
log.Printf("git diff LOC %d over %d threshold, using model for long diffs: %s\n", gitDiffLoc, diffThreshold, model)
120+
log.Printf("git diff total changes %d over %d threshold, using model for long diffs: %s\n", totalChanges, totalChangesThreshold, model)
96121
}
97122
if config.Provider.ApiKey == "" {
98123
cobra.CheckErr("api_key environment variable name cannot be empty")
@@ -118,7 +143,7 @@ var rootCmd = &cobra.Command{
118143
},
119144
{
120145
Role: "user",
121-
Content: gitDiffStr,
146+
Content: string(gitDiff),
122147
},
123148
})
124149
var responseDuration time.Duration
@@ -195,7 +220,7 @@ func initConfig() {
195220
viper.SetDefault("provider.api_key", "GEMINI_API_KEY")
196221
viper.SetDefault("diff.short_model", "gemini-2.5-flash-lite")
197222
viper.SetDefault("diff.long_model", "gemini-2.5-flash")
198-
viper.SetDefault("diff.threshold", 500)
223+
viper.SetDefault("diff.threshold", 200)
199224
viper.AutomaticEnv() // read in environment variables that match
200225

201226
// If a config file is found, read it in.

0 commit comments

Comments
 (0)