Skip to content

Commit 3d9482c

Browse files
Divy13anshbakayu
andauthored
feat: restrict gitx execution to git repositories (#23)
Co-authored-by: Ayush Chauhan <[email protected]>
1 parent e7a1c7b commit 3d9482c

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

cmd/gitx/main.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"log"
77
"os"
8-
8+
"os/exec"
99
tea "github.com/charmbracelet/bubbletea"
1010
"github.com/gitxtui/gitx/internal/tui"
1111
zone "github.com/lrstanley/bubblezone"
@@ -14,6 +14,11 @@ import (
1414
var version = "dev"
1515

1616
func main() {
17+
if err := ensureGitRepo(); err != nil {
18+
fmt.Fprintln(os.Stderr, err) // print to stderr
19+
os.Exit(1)
20+
}
21+
1722
if len(os.Args) > 1 && (os.Args[1] == "--version" || os.Args[1] == "-v") {
1823
fmt.Printf("gitx version: %s\n", version)
1924
return
@@ -23,10 +28,19 @@ func main() {
2328
defer zone.Close()
2429

2530
app := tui.NewApp()
31+
2632
if err := app.Run(); err != nil {
2733
if !errors.Is(err, tea.ErrProgramKilled) {
28-
log.Fatalf("Error running application: %v", err)
34+
log.Fatalf("error running application: %v", err)
2935
}
3036
}
3137
fmt.Println("Bye from gitx! :)")
3238
}
39+
40+
func ensureGitRepo() error {
41+
cmd := exec.Command("git", "rev-parse", "--is-inside-work-tree")
42+
if err := cmd.Run(); err != nil {
43+
return fmt.Errorf("error: not a git repository")
44+
}
45+
return nil
46+
}

0 commit comments

Comments
 (0)