diff --git a/internal/git/root.go b/internal/git/root.go index 71b4351..3742cb3 100644 --- a/internal/git/root.go +++ b/internal/git/root.go @@ -1,7 +1,6 @@ package git import ( - "errors" "os/exec" "regexp" "strconv" @@ -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 } @@ -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 +} diff --git a/internal/git/root_test.go b/internal/git/root_test.go new file mode 100644 index 0000000..f04f1de --- /dev/null +++ b/internal/git/root_test.go @@ -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) + } + }) + } +} diff --git a/secretspec.toml b/secretspec.toml index ed950bb..1d12d48 100644 --- a/secretspec.toml +++ b/secretspec.toml @@ -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" }