Skip to content

Commit 1c0c5df

Browse files
committed
refactor: extract git diff logic to internal/git
This commit extracts the logic for retrieving and parsing git diff information from the root command into a new `internal/git` package. This change improves code organization and modularity by separating concerns. The `internal/git` package now encapsulates all git-related operations, making the root command cleaner and more focused on its core responsibilities. --- {"auto-commit-msg":{"language":"rust","version":"0.4.0-dev","model":"gemini-2.5-flash-lite","response_time":1.83,"execution_time":1.85}}
1 parent 9cb8b34 commit 1c0c5df

3 files changed

Lines changed: 64 additions & 28 deletions

File tree

cmd/root.go

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ import (
66
"log"
77
"math"
88
"os"
9-
"os/exec"
10-
"regexp"
11-
"strconv"
129
"strings"
1310
"time"
1411

12+
"github.com/sestrella/auto-commit-msg/internal/git"
1513
"github.com/sestrella/auto-commit-msg/internal/openai"
1614
"github.com/spf13/cobra"
1715
"github.com/spf13/viper"
@@ -78,40 +76,21 @@ var rootCmd = &cobra.Command{
7876
return
7977
}
8078

81-
gitDiff, err := exec.Command("git", "diff", "--cached").Output()
79+
cachedGitDiff, err := git.DiffCached()
8280
if err != nil {
8381
cobra.CheckErr(err)
8482
}
85-
if len(gitDiff) == 0 {
83+
84+
if cachedGitDiff == "" {
8685
cobra.CheckErr("git diff is empty")
8786
}
8887

89-
shortStat, err := exec.Command("git", "diff", "--cached", "--shortstat").Output()
88+
stats, err := git.DiffCachedStats()
9089
if err != nil {
9190
cobra.CheckErr(err)
9291
}
93-
shortStatStr := string(shortStat)
94-
95-
// TODO: Get insertions and deletions using a single regex, take into account num of files?
96-
insertionMatches := regexp.MustCompile(`(\d+)\s+insertions?\(\+\)`).FindStringSubmatch(shortStatStr)
97-
insertions := 0
98-
if len(insertionMatches) > 1 {
99-
insertions, err = strconv.Atoi(insertionMatches[1])
100-
if err != nil {
101-
cobra.CheckErr(err)
102-
}
103-
}
104-
105-
deletionMatches := regexp.MustCompile(`(\d+)\s+deletions?\(\-\)`).FindStringSubmatch(shortStatStr)
106-
deletions := 0
107-
if len(deletionMatches) > 1 {
108-
deletions, err = strconv.Atoi(deletionMatches[1])
109-
if err != nil {
110-
cobra.CheckErr(err)
111-
}
112-
}
11392

114-
totalChanges := insertions + deletions
93+
totalChanges := stats.Insertions + stats.Deletions
11594
totalChangesThreshold := config.Diff.Threshold
11695
var model string
11796
if totalChanges < totalChangesThreshold {
@@ -151,7 +130,7 @@ var rootCmd = &cobra.Command{
151130
},
152131
{
153132
Role: "user",
154-
Content: string(gitDiff),
133+
Content: cachedGitDiff,
155134
},
156135
})
157136
var responseDuration time.Duration

internal/git/root.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package git
2+
3+
import (
4+
"errors"
5+
"os/exec"
6+
"regexp"
7+
"strconv"
8+
)
9+
10+
type Stats struct {
11+
FileChanged int
12+
Insertions int
13+
Deletions int
14+
}
15+
16+
func DiffCached() (string, error) {
17+
output, err := exec.Command("git", "diff", "--cached").Output()
18+
if err != nil {
19+
return "", err
20+
}
21+
22+
return string(output), nil
23+
}
24+
25+
func DiffCachedStats() (*Stats, error) {
26+
output, err := exec.Command("git", "diff", "--cached", "--shortstat").Output()
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
re := regexp.MustCompile(`\s+(\d+)\s+files? changed,\s+(\d+)\s+insertions\(\+\),\s+(\d+)\s+deletions\(\-\)`)
32+
matches := re.FindStringSubmatch(string(output))
33+
if len(matches) < 4 {
34+
return nil, errors.New("TODO")
35+
}
36+
37+
fileChanged, err := strconv.Atoi(matches[1])
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
insertions, err := strconv.Atoi(matches[2])
43+
if err != nil {
44+
return nil, err
45+
}
46+
47+
deletions, err := strconv.Atoi(matches[3])
48+
if err != nil {
49+
return nil, err
50+
}
51+
52+
return &Stats{
53+
FileChanged: fileChanged,
54+
Insertions: insertions,
55+
Deletions: deletions,
56+
}, nil
57+
}

0 commit comments

Comments
 (0)