Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions internal/generator/heuristic/heuristic.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ func New() *Generator {

func (g *Generator) Name() string { return "heuristic" }

// largeChangeThreshold is the total number of changed lines (additions +
// deletions) across modified-only files above which we treat the change
// as substantial enough to be "feat" rather than "fix". A one-line typo
// fix and a 200-line rewrite should not both be labeled "fix".
const largeChangeThreshold = 50

// Generate drafts a Conventional Commits message from the staged diff
// using only path/status heuristics — no network, no LLM.
func (g *Generator) Generate(diff *gitutil.StagedDiff) (generator.Message, error) {
Expand All @@ -43,11 +49,12 @@ func (g *Generator) Generate(diff *gitutil.StagedDiff) (generator.Message, error
// files. Rules are checked in priority order; the first match wins.
func inferType(files []gitutil.FileChange) string {
var (
allTest = true
allDocs = true
hasNewFile = false
hasDeleted = false
hasCI = false
allTest = true
allDocs = true
hasNewFile = false
hasDeleted = false
hasCI = false
totalModifiedLines = 0
)

for _, f := range files {
Expand All @@ -68,6 +75,8 @@ func inferType(files []gitutil.FileChange) string {
hasNewFile = true
case "D":
hasDeleted = true
case "M":
totalModifiedLines += f.Additions + f.Deletions
}
}

Expand All @@ -82,6 +91,8 @@ func inferType(files []gitutil.FileChange) string {
return "chore"
case hasNewFile:
return "feat"
case totalModifiedLines >= largeChangeThreshold:
return "feat"
default:
return "fix"
}
Expand Down
28 changes: 28 additions & 0 deletions internal/generator/heuristic/heuristic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,34 @@ func TestGenerate(t *testing.T) {
},
wantPrefix: "feat(a): add 1, update 1, remove 1 files",
},
{
name: "small modification stays fix",
files: []gitutil.FileChange{
{Path: "src/app.py", Status: "M", Additions: 10, Deletions: 5},
},
wantPrefix: "fix",
},
{
name: "large modification becomes feat",
files: []gitutil.FileChange{
{Path: "src/app.py", Status: "M", Additions: 200, Deletions: 24},
},
wantPrefix: "feat(src): update src/app.py",
},
{
name: "modification exactly at threshold becomes feat",
files: []gitutil.FileChange{
{Path: "src/app.py", Status: "M", Additions: 40, Deletions: 10},
},
wantPrefix: "feat",
},
{
name: "modification just under threshold stays fix",
files: []gitutil.FileChange{
{Path: "src/app.py", Status: "M", Additions: 30, Deletions: 19},
},
wantPrefix: "fix",
},
}

g := New()
Expand Down
Loading