Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

Make the watcher git helpers work under Windows cmd.exe

Martin Vogela2c980439710
diff --git a/src/watcher/watcher.c b/src/watcher/watcher.c
index 4d0794352..04f27f127 100644
--- a/src/watcher/watcher.c
+++ b/src/watcher/watcher.c
@@ -89,9 +89,19 @@ int cbm_watcher_poll_interval_ms(int file_count) {
/* ── Git helpers ────────────────────────────────────────────────── */
+/* Portable command pieces: cbm_popen runs through cmd.exe on Windows, which does
+ * NOT strip single quotes (git would receive a literal-quoted path → "cannot find
+ * the path") and has no /dev/null. Use double quotes (stripped by both cmd.exe and
+ * POSIX sh) and the platform null device. */
+#if defined(_WIN32)
+#define WATCHER_NULDEV "NUL"
+#else
+#define WATCHER_NULDEV "/dev/null"
+#endif
+
static bool is_git_repo(const char *root_path) {
char cmd[CBM_SZ_1K];
- snprintf(cmd, sizeof(cmd), "git -C '%s' rev-parse --git-dir 2>/dev/null", root_path);
+ snprintf(cmd, sizeof(cmd), "git -C \"%s\" rev-parse --git-dir 2>%s", root_path, WATCHER_NULDEV);
FILE *fp = cbm_popen(cmd, "r");
if (!fp) {
return false;
@@ -106,7 +116,7 @@ static bool is_git_repo(const char *root_path) {
static int git_head(const char *root_path, char *out, size_t out_size) {
char cmd[CBM_SZ_1K];
- snprintf(cmd, sizeof(cmd), "git -C '%s' rev-parse HEAD 2>/dev/null", root_path);
+ snprintf(cmd, sizeof(cmd), "git -C \"%s\" rev-parse HEAD 2>%s", root_path, WATCHER_NULDEV);
FILE *fp = cbm_popen(cmd, "r");
if (!fp) {
return CBM_NOT_FOUND;
@@ -130,9 +140,9 @@ static int git_head(const char *root_path, char *out, size_t out_size) {
static bool git_is_dirty(const char *root_path) {
char cmd[CBM_SZ_1K];
snprintf(cmd, sizeof(cmd),
- "git --no-optional-locks -C '%s' status --porcelain "
- "--untracked-files=normal 2>/dev/null",
- root_path);
+ "git --no-optional-locks -C \"%s\" status --porcelain "
+ "--untracked-files=normal 2>%s",
+ root_path, WATCHER_NULDEV);
FILE *fp = cbm_popen(cmd, "r");
if (!fp) {
return false;
@@ -155,9 +165,12 @@ static bool git_is_dirty(const char *root_path) {
return true;
}
+#if !defined(_WIN32)
/* Check submodules: uncommitted changes inside a submodule are invisible
* to the parent's git status. Use `git submodule foreach` as a portable
- * fallback (Apple Git lacks --recurse-submodules). */
+ * fallback (Apple Git lacks --recurse-submodules). POSIX-only: foreach takes
+ * an inner shell command that cmd.exe cannot pass intact; the parent-repo
+ * status check above already covers the common (non-submodule) case. */
snprintf(cmd, sizeof(cmd),
"git --no-optional-locks -C '%s' submodule foreach --quiet --recursive "
"'git status --porcelain --untracked-files=normal 2>/dev/null' "
@@ -177,22 +190,30 @@ static bool git_is_dirty(const char *root_path) {
}
}
cbm_pclose(fp);
+#endif
return dirty;
}
/* Count tracked files via git ls-files */
static int git_file_count(const char *root_path) {
char cmd[CBM_SZ_1K];
- snprintf(cmd, sizeof(cmd), "git -C '%s' ls-files 2>/dev/null | wc -l", root_path);
+ snprintf(cmd, sizeof(cmd), "git -C \"%s\" ls-files 2>%s", root_path, WATCHER_NULDEV);
FILE *fp = cbm_popen(cmd, "r");
if (!fp) {
return 0;
}
+ /* Count newlines (one tracked file per line). `wc -l` is unavailable on
+ * Windows, so count in C, robust to paths longer than the read buffer. */
int count = 0;
- char line[CBM_SZ_64];
- if (fgets(line, sizeof(line), fp)) {
- count = (int)strtol(line, NULL, CBM_DECIMAL_BASE);
+ char buf[CBM_SZ_1K];
+ size_t n;
+ while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) {
+ for (size_t i = 0; i < n; i++) {
+ if (buf[i] == '\n') {
+ count++;
+ }
+ }
}
cbm_pclose(fp);
return count;