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
29 changes: 20 additions & 9 deletions internal/git/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package git

import (
"errors"
"os/exec"
"regexp"
"strconv"
Expand All @@ -28,23 +27,21 @@ func DiffCachedStats() (*Stats, error) {
return nil, err
}

re := regexp.MustCompile(`\s+(\d+)\s+files? changed,\s+(\d+)\s+insertions?\(\+\),\s+(\d+)\s+deletions?\(\-\)`)
matches := re.FindStringSubmatch(string(output))
if len(matches) < 4 {
return nil, errors.New("TODO")
}
return parseDiffStats(string(output))
}

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

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

deletions, err := strconv.Atoi(matches[3])
deletions, err := parseInt(output, `(\d+)\sdeletions?\(\-\)`)
if err != nil {
return nil, err
}
Expand All @@ -55,3 +52,17 @@ func DiffCachedStats() (*Stats, error) {
Deletions: deletions,
}, nil
}

func parseInt(str string, re string) (int, error) {
matches := regexp.MustCompile(re).FindStringSubmatch(str)
if len(matches) != 2 {
return 0, nil
}

value, err := strconv.Atoi(matches[1])
if err != nil {
return 0, err
}

return value, nil
}
26 changes: 26 additions & 0 deletions internal/git/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package git

import "testing"

func TestParseDiffStats(t *testing.T) {
tests := map[string]Stats{
"1 file changed, 3 insertions(+), 1 deletion(-)": {1, 3, 1},
"2 files changed, 10 insertions(+)": {2, 10, 0},
"5 files changed, 7 deletions(-)": {5, 0, 7},
"3 files changed, 12 insertions(+), 8 deletions(-)": {3, 12, 8},
"1 file changed, 0 insertions(+), 5 deletions(-)": {1, 0, 5},
"12 files changed, 240 insertions(+), 198 deletions(-)": {12, 240, 198},
}

for output, expectedStats := range tests {
t.Run(output, func(t *testing.T) {
stats, err := parseDiffStats(output)
if err != nil {
t.Fatal(err)
}
if *stats != expectedStats {
t.Errorf("expected %v, got %v", expectedStats, stats)
}
})
}
}
4 changes: 1 addition & 3 deletions secretspec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@
name = "auto-commit-msg"
revision = "1.0"

[profiles.default]
GEMINI_API_KEY = { description = "Google Gemini API key" }

[profiles.development]
GEMINI_API_KEY = { description = "Google Gemini API key" }