@@ -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+
740841int 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 ) {
0 commit comments