@@ -1168,6 +1168,37 @@ static bool resolve_git_common_dir(const char *repo_path, char *common_dir, size
11681168 return true;
11691169}
11701170
1171+ /* When repo_path itself carries no .git (resolve_git_common_dir already
1172+ * returned false for it), walk upward looking for an enclosing repository,
1173+ * exactly as `git` itself would when run from a subfolder. Bounded by the
1174+ * filesystem/drive root: dir is truncated at each iteration, so the loop
1175+ * cannot run more times than repo_path is characters long. Returns true and
1176+ * fills ancestor_root (for the enclosing repo's own .gitignore) plus
1177+ * common_dir (for its info/exclude + config, via the same resolution
1178+ * resolve_git_common_dir already applies to an ordinary repo root). Fixes
1179+ * the remaining half of issue #510: only the indexed directory's own
1180+ * .gitignore was ever consulted, never an enclosing repo's. */
1181+ static bool resolve_enclosing_git_root (const char * repo_path , char * ancestor_root , size_t ar_sz ,
1182+ char * common_dir , size_t cd_sz ) {
1183+ char dir [CBM_SZ_4K ];
1184+ snprintf (dir , sizeof (dir ), "%s" , repo_path );
1185+ cbm_normalize_path_sep (dir );
1186+
1187+ for (;;) {
1188+ char * slash = strrchr (dir , '/' );
1189+ /* No separator left, or only the root separator (POSIX "/") or a
1190+ * bare drive prefix (Windows "C:/"): nothing above this to check. */
1191+ if (!slash || slash == dir || (slash > dir && * (slash - 1 ) == ':' )) {
1192+ return false;
1193+ }
1194+ * slash = '\0' ;
1195+ if (resolve_git_common_dir (dir , common_dir , cd_sz )) {
1196+ snprintf (ancestor_root , ar_sz , "%s" , dir );
1197+ return true;
1198+ }
1199+ }
1200+ }
1201+
11711202int cbm_discover (const char * repo_path , const cbm_discover_opts_t * opts , cbm_file_info_t * * out ,
11721203 int * count ) {
11731204 return cbm_discover_ex (repo_path , opts , out , count , NULL , NULL );
@@ -1215,11 +1246,13 @@ static cbm_discover_status_t discover_impl(const char *repo_path, const cbm_disc
12151246
12161247 /* Load gitignore sources for ordinary repos AND linked worktrees.
12171248 * Sources merged in order (later patterns win on conflict):
1218- * 1. <repo>/.gitignore — committed exclusions
1219- * 2. <common>/info/exclude — per-clone exclusions, not committed
1249+ * 1. <enclosing>/.gitignore: enclosing repo's root exclusions, only when
1250+ * repo_path itself has no .git of its own (see below)
1251+ * 2. <repo>/.gitignore: committed exclusions
1252+ * 3. <common>/info/exclude: per-clone exclusions, not committed
12201253 * <common> is the git common dir, resolved via resolve_git_common_dir() so a
12211254 * worktree (where .git is a gitlink file) reads the shared info/exclude/config
1222- * just like a normal checkout. Both are folded into a single matcher so all
1255+ * just like a normal checkout. All are folded into a single matcher so all
12231256 * downstream call paths remain unchanged. Fixes issue #489: OOM on repos whose
12241257 * worktrees are excluded only via .git/info/exclude (e.g. Sandcastle). */
12251258 cbm_gitignore_t * gitignore = NULL ;
@@ -1231,11 +1264,34 @@ static cbm_discover_status_t discover_impl(const char *repo_path, const cbm_disc
12311264 char git_common_dir [CBM_SZ_4K ];
12321265 bool is_git_repo = resolve_git_common_dir (repo_path , git_common_dir , sizeof (git_common_dir ));
12331266 bool has_git_config = false;
1267+ /* repo_path itself is not a git repo root: walk upward for an enclosing one,
1268+ * exactly as `git` does when run from a subfolder. Its root .gitignore is
1269+ * loaded FIRST below (least specific), so the indexed directory's own
1270+ * .gitignore and info/exclude (both more specific) still override it on
1271+ * conflict. Fixes the remaining half of issue #510: only the indexed
1272+ * directory's own .gitignore was ever consulted, never an enclosing repo's. */
1273+ char enclosing_root [CBM_SZ_4K ];
1274+ if (!is_git_repo &&
1275+ resolve_enclosing_git_root (repo_path , enclosing_root , sizeof (enclosing_root ),
1276+ git_common_dir , sizeof (git_common_dir ))) {
1277+ is_git_repo = true;
1278+ char enclosing_gi_path [CBM_SZ_4K ];
1279+ path_join (enclosing_gi_path , sizeof (enclosing_gi_path ), enclosing_root , ".gitignore" );
1280+ gitignore = cbm_gitignore_load (enclosing_gi_path );
1281+ }
12341282 /* Always honour the .gitignore at the indexed-directory root, even when the
12351283 * directory is not a git repo root (e.g. indexing a sub-package directly).
12361284 * Fixes issue #510: a root .gitignore was silently ignored without .git/. */
12371285 snprintf (gi_path , sizeof (gi_path ), "%s/.gitignore" , repo_path );
1238- gitignore = cbm_gitignore_load (gi_path );
1286+ cbm_gitignore_t * local_gitignore = cbm_gitignore_load (gi_path );
1287+ if (local_gitignore ) {
1288+ if (!gitignore ) {
1289+ gitignore = local_gitignore ;
1290+ } else {
1291+ (void )cbm_gitignore_merge (gitignore , local_gitignore );
1292+ cbm_gitignore_free (local_gitignore );
1293+ }
1294+ }
12391295 if (is_git_repo ) {
12401296 path_join (gi_path , sizeof (gi_path ), git_common_dir , "config" );
12411297 has_git_config = wide_stat (gi_path , & gi_stat ) == 0 && S_ISREG (gi_stat .st_mode );
0 commit comments