Skip to content

Commit f1b9382

Browse files
committed
fix(discover): honor info/exclude and excludesfile in linked worktrees
In a linked worktree, <repo>/.git is a regular file ("gitdir: <path>") rather than a directory. cbm_discover_ex gated all per-clone ignore sources on .git being a directory, so inside a worktree it skipped .git/info/exclude and the global core.excludesfile entirely and walked into directories the user had excluded there (e.g. build/, twister-out/). Committed .gitignore still applied via the issue DeusData#510 path, which is why the gap was easy to miss. Resolve the git common dir up front: when .git is a gitlink file, parse 'gitdir:', then follow <gitdir>/commondir to the shared dir where git keeps info/exclude and config (falling back to the gitdir when no commondir file is present). info/exclude and config are then read from that common dir, so a worktree behaves like an ordinary checkout. Adds discover_worktree_info_exclude (fails before this change) and discover_worktree_committed_gitignore. Signed-off-by: Nahim El Atmani <2959826+Naam@users.noreply.github.com>
1 parent 9169f46 commit f1b9382

2 files changed

Lines changed: 183 additions & 11 deletions

File tree

‎src/discover/discover.c‎

Lines changed: 116 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,107 @@ static void walk_dir(const char *dir_path, const char *rel_prefix, const cbm_dis
737737

738738
/* ── Public API ───────────────────────────────── */
739739

740+
static bool discover_path_is_absolute(const char *path) {
741+
if (!path || !path[0]) {
742+
return false;
743+
}
744+
if (path[0] == '/' || path[0] == '\\') {
745+
return true;
746+
}
747+
#ifdef _WIN32
748+
return isalpha((unsigned char)path[0]) && path[1] == ':';
749+
#else
750+
return false;
751+
#endif
752+
}
753+
754+
/* Resolve the shared "common" git directory for repo_path.
755+
* Handles three layouts:
756+
* 1. <repo>/.git is a directory - ordinary repo; common_dir == <repo>/.git
757+
* 2. <repo>/.git is a regular file - linked worktree gitlink "gitdir: <path>";
758+
* the common dir is read from <git_dir>/commondir (git stores info/exclude +
759+
* config there, shared across worktrees). Falls back to git_dir when no
760+
* commondir file exists.
761+
* 3. neither - not a git repo.
762+
* Returns true when a git dir was resolved. Fixes the worktree case where
763+
* .git/info/exclude and core.excludesfile were silently dropped because the old
764+
* check required .git to be a directory (issue #489 only covered ordinary repos). */
765+
static bool resolve_git_common_dir(const char *repo_path, char *common_dir, size_t cd_sz) {
766+
char dot_git[CBM_SZ_4K];
767+
snprintf(dot_git, sizeof(dot_git), "%s/.git", repo_path);
768+
struct stat st;
769+
if (wide_stat(dot_git, &st) != 0) {
770+
return false;
771+
}
772+
if (S_ISDIR(st.st_mode)) {
773+
snprintf(common_dir, cd_sz, "%s", dot_git);
774+
cbm_normalize_path_sep(common_dir);
775+
return true;
776+
}
777+
if (!S_ISREG(st.st_mode)) {
778+
return false;
779+
}
780+
781+
/* Linked worktree: parse "gitdir: <path>" from the gitlink file. */
782+
FILE *f = fopen(dot_git, "r");
783+
if (!f) {
784+
return false;
785+
}
786+
char git_dir[CBM_SZ_4K];
787+
bool got_git_dir = false;
788+
char line[CBM_SZ_4K];
789+
while (fgets(line, sizeof(line), f)) {
790+
char *gs = trim_ws(line);
791+
if (strncmp(gs, "gitdir:", 7) == 0) {
792+
char *val = trim_ws(gs + 7);
793+
if (val[0] != '\0') {
794+
if (discover_path_is_absolute(val)) {
795+
snprintf(git_dir, sizeof(git_dir), "%s", val);
796+
cbm_normalize_path_sep(git_dir);
797+
} else {
798+
path_join(git_dir, sizeof(git_dir), repo_path, val);
799+
}
800+
got_git_dir = true;
801+
}
802+
break;
803+
}
804+
}
805+
fclose(f);
806+
if (!got_git_dir) {
807+
return false;
808+
}
809+
810+
/* The shared dir holding info/exclude + config is named in <git_dir>/commondir
811+
* (typically a relative path like "../.."). Absent in single-worktree gitdirs. */
812+
char commondir_path[CBM_SZ_4K];
813+
path_join(commondir_path, sizeof(commondir_path), git_dir, "commondir");
814+
FILE *cf = fopen(commondir_path, "r");
815+
if (cf) {
816+
char cbuf[CBM_SZ_4K];
817+
bool resolved = false;
818+
if (fgets(cbuf, sizeof(cbuf), cf)) {
819+
char *cs = trim_ws(cbuf);
820+
if (cs[0] != '\0') {
821+
if (discover_path_is_absolute(cs)) {
822+
snprintf(common_dir, cd_sz, "%s", cs);
823+
cbm_normalize_path_sep(common_dir);
824+
} else {
825+
path_join(common_dir, cd_sz, git_dir, cs);
826+
}
827+
resolved = true;
828+
}
829+
}
830+
fclose(cf);
831+
if (resolved) {
832+
return true;
833+
}
834+
}
835+
836+
snprintf(common_dir, cd_sz, "%s", git_dir);
837+
cbm_normalize_path_sep(common_dir);
838+
return true;
839+
}
840+
740841
int cbm_discover(const char *repo_path, const cbm_discover_opts_t *opts, cbm_file_info_t **out,
741842
int *count) {
742843
return cbm_discover_ex(repo_path, opts, out, count, NULL, NULL);
@@ -763,31 +864,35 @@ int cbm_discover_ex(const char *repo_path, const cbm_discover_opts_t *opts, cbm_
763864
return CBM_NOT_FOUND;
764865
}
765866

766-
/* Load gitignore sources when a .git directory is present.
867+
/* Load gitignore sources for ordinary repos AND linked worktrees.
767868
* Sources merged in order (later patterns win on conflict):
768-
* 1. <repo>/.gitignore — committed exclusions
769-
* 2. <repo>/.git/info/exclude — per-clone exclusions, not committed
770-
* Both are folded into a single matcher so all downstream call paths
771-
* remain unchanged. Fixes issue #489: OOM on repos whose worktrees
772-
* are excluded only via .git/info/exclude (e.g. Sandcastle). */
869+
* 1. <repo>/.gitignore — committed exclusions
870+
* 2. <common>/info/exclude — per-clone exclusions, not committed
871+
* <common> is the git common dir, resolved via resolve_git_common_dir() so a
872+
* worktree (where .git is a gitlink file) reads the shared info/exclude/config
873+
* just like a normal checkout. Both are folded into a single matcher so all
874+
* downstream call paths remain unchanged. Fixes issue #489: OOM on repos whose
875+
* worktrees are excluded only via .git/info/exclude (e.g. Sandcastle). */
773876
cbm_gitignore_t *gitignore = NULL;
774877
char gi_path[CBM_SZ_4K];
775-
snprintf(gi_path, sizeof(gi_path), "%s/.git", repo_path);
776878
struct stat gi_stat;
777-
bool is_git_repo = wide_stat(gi_path, &gi_stat) == 0 && S_ISDIR(gi_stat.st_mode);
879+
/* Resolve the git common dir, transparently following a worktree gitlink so the
880+
* .git/info/exclude and core.excludesfile sources are honoured inside linked
881+
* worktrees too (where .git is a file pointing at the shared dir, not a directory). */
882+
char git_common_dir[CBM_SZ_4K];
883+
bool is_git_repo = resolve_git_common_dir(repo_path, git_common_dir, sizeof(git_common_dir));
778884
bool has_git_config = false;
779885
/* Always honour the .gitignore at the indexed-directory root, even when the
780886
* directory is not a git repo root (e.g. indexing a sub-package directly).
781-
* The .git/info/exclude and global-excludes sources still require .git/.
782887
* Fixes issue #510: a root .gitignore was silently ignored without .git/. */
783888
snprintf(gi_path, sizeof(gi_path), "%s/.gitignore", repo_path);
784889
gitignore = cbm_gitignore_load(gi_path);
785890
if (is_git_repo) {
786-
snprintf(gi_path, sizeof(gi_path), "%s/.git/config", repo_path);
891+
path_join(gi_path, sizeof(gi_path), git_common_dir, "config");
787892
has_git_config = wide_stat(gi_path, &gi_stat) == 0 && S_ISREG(gi_stat.st_mode);
788893

789894
char exc_path[CBM_SZ_4K];
790-
snprintf(exc_path, sizeof(exc_path), "%s/.git/info/exclude", repo_path);
895+
path_join(exc_path, sizeof(exc_path), git_common_dir, "info/exclude");
791896
cbm_gitignore_t *git_exclude = cbm_gitignore_load(exc_path);
792897
if (git_exclude) {
793898
if (!gitignore) {

‎tests/test_discover.c‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,69 @@ TEST(discover_git_info_exclude_stacks_with_gitignore) {
932932
PASS();
933933
}
934934

935+
/* ── Linked-worktree ignore tests ──────────────────────────────── */
936+
937+
/* In a linked worktree, <repo>/.git is a regular file ("gitdir: <path>"), not a
938+
* directory, and info/exclude + config live in the shared common dir named by
939+
* <gitdir>/commondir. The discover step must follow the gitlink so per-clone
940+
* excludes are honored exactly as in an ordinary checkout. Regression for the
941+
* worktree gap left by issue #489 (which only handled .git as a directory). */
942+
TEST(discover_worktree_info_exclude) {
943+
char *base = th_mktempdir("cbm_disc_wt_exc");
944+
ASSERT(base != NULL);
945+
946+
/* Worktree gitlink -> per-worktree gitdir (relative to the worktree root). */
947+
th_write_file(TH_PATH(base, ".git"), "gitdir: maingit/worktrees/wt\n");
948+
/* Per-worktree gitdir points back to the shared common dir via commondir. */
949+
th_mkdir_p(TH_PATH(base, "maingit/worktrees/wt"));
950+
th_write_file(TH_PATH(base, "maingit/worktrees/wt/commondir"), "../..\n");
951+
/* Shared exclude lives in the common dir, not the per-worktree gitdir. */
952+
th_mkdir_p(TH_PATH(base, "maingit/info"));
953+
th_write_file(TH_PATH(base, "maingit/info/exclude"), "build/\n");
954+
955+
th_write_file(TH_PATH(base, "src/main.go"), "package main\n");
956+
th_write_file(TH_PATH(base, "build/gen.go"), "package build\n");
957+
958+
cbm_discover_opts_t opts = {0};
959+
cbm_file_info_t *files = NULL;
960+
int count = 0;
961+
int rc = cbm_discover(base, &opts, &files, &count);
962+
ASSERT_EQ(rc, 0);
963+
ASSERT_EQ(count, 1);
964+
ASSERT_TRUE(strstr(files[0].rel_path, "main.go") != NULL);
965+
966+
cbm_discover_free(files, count);
967+
th_cleanup(base);
968+
PASS();
969+
}
970+
971+
/* Committed .gitignore must still apply in a worktree even when commondir is
972+
* absent (older gitdir layouts): the gitlink resolver falls back to the gitdir
973+
* itself, and the root .gitignore is loaded unconditionally (issue #510). */
974+
TEST(discover_worktree_committed_gitignore) {
975+
char *base = th_mktempdir("cbm_disc_wt_gi");
976+
ASSERT(base != NULL);
977+
978+
th_write_file(TH_PATH(base, ".git"), "gitdir: maingit/worktrees/wt\n");
979+
th_mkdir_p(TH_PATH(base, "maingit/worktrees/wt"));
980+
th_write_file(TH_PATH(base, ".gitignore"), "build/\n");
981+
982+
th_write_file(TH_PATH(base, "src/main.go"), "package main\n");
983+
th_write_file(TH_PATH(base, "build/gen.go"), "package build\n");
984+
985+
cbm_discover_opts_t opts = {0};
986+
cbm_file_info_t *files = NULL;
987+
int count = 0;
988+
int rc = cbm_discover(base, &opts, &files, &count);
989+
ASSERT_EQ(rc, 0);
990+
ASSERT_EQ(count, 1);
991+
ASSERT_TRUE(strstr(files[0].rel_path, "main.go") != NULL);
992+
993+
cbm_discover_free(files, count);
994+
th_cleanup(base);
995+
PASS();
996+
}
997+
935998
/* ── Nested .gitignore tests (issue #178) ──────────────────────── */
936999

9371000
TEST(discover_nested_gitignore) {
@@ -1109,6 +1172,10 @@ SUITE(discover) {
11091172
RUN_TEST(discover_git_info_exclude);
11101173
RUN_TEST(discover_git_info_exclude_stacks_with_gitignore);
11111174

1175+
/* Linked-worktree ignore resolution (gitlink + commondir) */
1176+
RUN_TEST(discover_worktree_info_exclude);
1177+
RUN_TEST(discover_worktree_committed_gitignore);
1178+
11121179
/* Nested .gitignore tests (issue #178) */
11131180
RUN_TEST(discover_nested_gitignore);
11141181
RUN_TEST(discover_nested_gitignore_stacks_with_root);

0 commit comments

Comments
 (0)