Skip to content

Commit 6cdf221

Browse files
committed
Removed os/exec and added git.IsDirty check
1 parent 70b3313 commit 6cdf221

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

cmd/commit.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package cmd
66
import (
77
"fmt"
88
"os"
9-
"os/exec"
109

1110
"github.com/chand1012/ottodocs/pkg/ai"
1211
"github.com/chand1012/ottodocs/pkg/config"
@@ -34,16 +33,25 @@ var commitCmd = &cobra.Command{
3433
os.Exit(1)
3534
}
3635

36+
dirty, err := git.IsDirty()
37+
if err != nil {
38+
log.Error(err)
39+
os.Exit(1)
40+
}
41+
42+
if !dirty {
43+
log.Error("No changes to commit.")
44+
os.Exit(1)
45+
}
46+
3747
log.Info("Generating commit message...")
3848
log.Debug("Getting git diff...")
39-
c := exec.Command("git", "diff")
40-
diffBytes, err := c.Output()
49+
diff, err := git.Diff()
4150
if err != nil {
4251
log.Error(err)
4352
os.Exit(1)
4453
}
4554

46-
diff := string(diffBytes)
4755
log.Debug("Sending diff to ChatGPT...")
4856
msg, err := ai.CommitMessage(diff, conventional, conf)
4957
if err != nil {

pkg/git/diff.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package git
2+
3+
import "strings"
4+
5+
func Diff() (string, error) {
6+
return git("diff")
7+
}
8+
9+
func Status() (string, error) {
10+
return git("status")
11+
}
12+
13+
func IsDirty() (bool, error) {
14+
status, err := Status()
15+
if err != nil {
16+
return false, err
17+
}
18+
return !strings.Contains(status, "nothing to commit, working tree clean"), nil
19+
}

0 commit comments

Comments
 (0)