on main help sign in
fix: only enable watcher for git repos (#1060)
e2e952bRaphael Amorim
internal/app/app.go+19−3
@@ -7,6 +7,8 @@ import (
7 "fmt"
7 "fmt"
8 "log/slog"
8 "log/slog"
9 "maps"
9 "maps"
10+ "os/exec"
11+ "strings"
10 "sync"
12 "sync"
11 "time"
13 "time"
12
14
@@ -54,6 +56,16 @@ type App struct {
54 cleanupFuncs []func() error
56 cleanupFuncs []func() error
55}
57}
56
58
59+// isGitRepo checks if the current directory is a git repository
60+func isGitRepo() bool {
61+ bts, err := exec.CommandContext(
62+ context.Background(),
63+ "git", "rev-parse",
64+ "--is-inside-work-tree",
65+ ).CombinedOutput()
66+ return err == nil && strings.TrimSpace(string(bts)) == "true"
67+}
68+
57// New initializes a new applcation instance.
69// New initializes a new applcation instance.
58func New(ctx context.Context, conn *sql.DB, cfg *config.Config) (*App, error) {
70func New(ctx context.Context, conn *sql.DB, cfg *config.Config) (*App, error) {
59 q := db.New(conn)
71 q := db.New(conn)
@@ -86,9 +98,13 @@ func New(ctx context.Context, conn *sql.DB, cfg *config.Config) (*App, error) {
86
98
87 app.setupEvents()
99 app.setupEvents()
88
100
89− // Start the global watcher
101+ // Start the global watcher only if this is a git repository
102+ if isGitRepo() {
90− if err := watcher.Start(); err != nil {
103+ if err := watcher.Start(); err != nil {
91− return nil, fmt.Errorf("app: %w", err)
104+ return nil, fmt.Errorf("app: %w", err)
105+ }
106+ } else {
107+ slog.Warn("Not starting global watcher: not a git repository")
92 }
108 }
93
109
94 // Initialize LSP clients in the background.
110 // Initialize LSP clients in the background.