Skip to content

Commit f44f8b4

Browse files
authored
fix: Fix problem parsing diff stats (#19)
1 parent 4b3bb0d commit f44f8b4

3 files changed

Lines changed: 47 additions & 12 deletions

File tree

internal/git/root.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package git
22

33
import (
4-
"errors"
54
"os/exec"
65
"regexp"
76
"strconv"
@@ -28,23 +27,21 @@ func DiffCachedStats() (*Stats, error) {
2827
return nil, err
2928
}
3029

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-
}
30+
return parseDiffStats(string(output))
31+
}
3632

37-
fileChanged, err := strconv.Atoi(matches[1])
33+
func parseDiffStats(output string) (*Stats, error) {
34+
fileChanged, err := parseInt(output, `(\d+)\sfiles? changed`)
3835
if err != nil {
3936
return nil, err
4037
}
4138

42-
insertions, err := strconv.Atoi(matches[2])
39+
insertions, err := parseInt(output, `(\d+)\sinsertions?\(\+\)`)
4340
if err != nil {
4441
return nil, err
4542
}
4643

47-
deletions, err := strconv.Atoi(matches[3])
44+
deletions, err := parseInt(output, `(\d+)\sdeletions?\(\-\)`)
4845
if err != nil {
4946
return nil, err
5047
}
@@ -55,3 +52,17 @@ func DiffCachedStats() (*Stats, error) {
5552
Deletions: deletions,
5653
}, nil
5754
}
55+
56+
func parseInt(str string, re string) (int, error) {
57+
matches := regexp.MustCompile(re).FindStringSubmatch(str)
58+
if len(matches) != 2 {
59+
return 0, nil
60+
}
61+
62+
value, err := strconv.Atoi(matches[1])
63+
if err != nil {
64+
return 0, err
65+
}
66+
67+
return value, nil
68+
}

internal/git/root_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package git
2+
3+
import "testing"
4+
5+
func TestParseDiffStats(t *testing.T) {
6+
tests := map[string]Stats{
7+
"1 file changed, 3 insertions(+), 1 deletion(-)": {1, 3, 1},
8+
"2 files changed, 10 insertions(+)": {2, 10, 0},
9+
"5 files changed, 7 deletions(-)": {5, 0, 7},
10+
"3 files changed, 12 insertions(+), 8 deletions(-)": {3, 12, 8},
11+
"1 file changed, 0 insertions(+), 5 deletions(-)": {1, 0, 5},
12+
"12 files changed, 240 insertions(+), 198 deletions(-)": {12, 240, 198},
13+
}
14+
15+
for output, expectedStats := range tests {
16+
t.Run(output, func(t *testing.T) {
17+
stats, err := parseDiffStats(output)
18+
if err != nil {
19+
t.Fatal(err)
20+
}
21+
if *stats != expectedStats {
22+
t.Errorf("expected %v, got %v", expectedStats, stats)
23+
}
24+
})
25+
}
26+
}

secretspec.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,5 @@
22
name = "auto-commit-msg"
33
revision = "1.0"
44

5-
[profiles.default]
6-
GEMINI_API_KEY = { description = "Google Gemini API key" }
7-
85
[profiles.development]
6+
GEMINI_API_KEY = { description = "Google Gemini API key" }

0 commit comments

Comments
 (0)