Skip to content

Commit 5c51d71

Browse files
committed
fix(git): run the worktree status probe through cbm_popen_git
The freshness verdict reads cbm_git_worktree_status(), which spawned git with plain cbm_popen. A GIT_DIR/GIT_WORK_TREE inherited from a git hook or an editor overrides `git -C <root>`, so a clean checkout was reported stale with the other repository's changes. Every other git child already goes through cbm_popen_git since DeusData#2003; this probe now does too. worktree_status_ignores_inherited_repo_env pins it: a dirty decoy in the hook environment must not leak into a clean fixture's status. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent b44a22c commit 5c51d71

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎src/git/git_context.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,9 @@ int cbm_git_worktree_status(const char *validated_root, int max_samples,
628628
return CBM_NOT_FOUND;
629629
}
630630

631-
FILE *fp = cbm_popen(cmd, "r");
631+
/* An inherited GIT_DIR/GIT_WORK_TREE (a git hook, an editor) overrides
632+
* `git -C`, which would report another repository's worktree here. */
633+
FILE *fp = cbm_popen_git(cmd);
632634
if (!fp) {
633635
return 0; /* out is zeroed: available=false */
634636
}

‎tests/test_git_context.c‎

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,10 +497,54 @@ TEST(git_branch_resolve_matches_full_context) {
497497
#endif /* _WIN32 */
498498
}
499499

500+
/* The freshness verdict reads this snapshot, so an inherited GIT_DIR /
501+
* GIT_WORK_TREE pointing at a dirty decoy must not make a clean fixture
502+
* report the decoy's changes as its own (#2003). */
503+
TEST(worktree_status_ignores_inherited_repo_env) {
504+
#ifdef _WIN32
505+
SKIP_PLATFORM("git-based env-isolation test not supported on Windows CI");
506+
#else
507+
char root[256];
508+
char *raw = th_mktempdir("cbm_gitenv_status");
509+
if (!raw)
510+
FAIL("th_mktempdir returned NULL");
511+
snprintf(root, sizeof(root), "%s", raw);
512+
if (make_env_isolation_tree(root) != 0) {
513+
th_rmtree(root);
514+
SKIP_PLATFORM("git not available to init a repo");
515+
}
516+
char decoy[512], fixture[512], path[600];
517+
snprintf(decoy, sizeof(decoy), "%s/decoy", root);
518+
snprintf(fixture, sizeof(fixture), "%s/fixture", root);
519+
/* Dirty only the decoy: one tracked edit and one untracked file. */
520+
snprintf(path, sizeof(path), "%s/a.go", decoy);
521+
th_write_file(path, "package a // dirty\n");
522+
snprintf(path, sizeof(path), "%s/untracked.go", decoy);
523+
th_write_file(path, "package a\n");
524+
525+
git_env_snapshot_t snap;
526+
git_env_hook_enter(&snap, decoy);
527+
cbm_worktree_status_t st;
528+
int rc = cbm_git_worktree_status(fixture, 4, &st);
529+
git_env_hook_leave(&snap);
530+
bool available = st.available;
531+
int tracked = st.tracked_count;
532+
int untracked = st.untracked_count;
533+
cbm_git_worktree_status_free(&st);
534+
th_rmtree(root);
535+
536+
ASSERT_EQ(rc, 0);
537+
ASSERT_TRUE(available);
538+
ASSERT_EQ(tracked, 0); /* the fixture is clean; the decoy is not */
539+
ASSERT_EQ(untracked, 0);
540+
PASS();
541+
#endif /* _WIN32 */
542+
}
543+
500544
/* ── Bounded worktree status (BT-240 increment 3) ──────────────────────
501545
* These run on EVERY platform, not just POSIX: they shell out through
502-
* cbm_popen (the same isolated spawn production git_context uses) instead of
503-
* system(), so they work whenever git is on PATH — Windows CI included. The
546+
* cbm_popen instead of system(), so they work whenever git is on PATH —
547+
* Windows CI included. The
504548
* core status parser is platform-neutral and must be exercised everywhere git
505549
* is available; only a genuinely missing git skips. */
506550

@@ -769,6 +813,7 @@ SUITE(git_context) {
769813
RUN_TEST(canonical_root_linked_worktree);
770814
RUN_TEST(git_context_ignores_inherited_repo_env);
771815
RUN_TEST(githistory_ignores_inherited_repo_env);
816+
RUN_TEST(worktree_status_ignores_inherited_repo_env);
772817
RUN_TEST(worktree_status_clean);
773818
RUN_TEST(worktree_status_tracked_modified);
774819
RUN_TEST(worktree_status_untracked);

0 commit comments

Comments
 (0)