Skip to content

Commit d7d6368

Browse files
committed
feat: verify git repository before running git diff
1 parent 6fc14d7 commit d7d6368

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

cmd/hepler.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ func check() error {
1717
return errors.New("git command not found in your system's PATH. Please install Git and try again")
1818
}
1919

20+
// Check if current working directory is inside a Git repository.
21+
if !util.IsGitRepo() {
22+
return errors.New("not a git repository")
23+
}
24+
2025
// Apply configuration values from CLI flags to Viper
2126
if diffUnified != 3 {
2227
viper.Set("git.diff_unified", diffUnified)

util/util.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package util
22

33
import (
4+
"bytes"
45
"os/exec"
56
"strings"
67
)
@@ -35,3 +36,16 @@ func ConvertToMap(args []string) Data {
3536
}
3637
return m
3738
}
39+
40+
// IsGitRepo returns true if the current working directory is inside a Git work tree.
41+
func IsGitRepo() bool {
42+
cmd := exec.Command("git", "rev-parse", "--is-inside-work-tree")
43+
44+
var out bytes.Buffer
45+
cmd.Stdout = &out
46+
if err := cmd.Run(); err != nil {
47+
return false
48+
}
49+
50+
return strings.TrimSpace(out.String()) == "true"
51+
}

0 commit comments

Comments
 (0)