Skip to content

Commit c8184ed

Browse files
committed
fix(discover): honor an enclosing repo's .gitignore for a git-less subfolder
resolve_git_common_dir() only stats repo_path/.git directly, so indexing a subfolder with no .git of its own never consulted any ancestor repo's .gitignore. Add resolve_enclosing_git_root() to walk up parent directories the way git itself does, and merge that ancestor's .gitignore (and info/exclude) ahead of the subfolder's own so the more specific file still wins on conflict. Fixes DeusData#510 Signed-off-by: Amir Fathi <amirfathi.me@gmail.com>
1 parent 055fbb7 commit c8184ed

2 files changed

Lines changed: 152 additions & 4 deletions

File tree

‎src/discover/discover.c‎

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
11711202
int 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);

‎tests/test_discover.c‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,93 @@ TEST(discover_worktree_committed_gitignore) {
13551355
PASS();
13561356
}
13571357

1358+
/* ── Enclosing-repo .gitignore tests (issue #510, second half) ──── */
1359+
1360+
/* repo_path itself has no .git (indexing a git-less subfolder of a larger
1361+
* repo). The enclosing repo's root .gitignore must still be honored, exactly
1362+
* as `git status`/`git check-ignore` run from that subfolder would. Before
1363+
* this fix, resolve_git_common_dir() only ever stat'd repo_path/.git
1364+
* directly and gave up, so the enclosing repo's rules were silently never
1365+
* consulted. */
1366+
TEST(discover_enclosing_repo_gitignore_issue510) {
1367+
char *base = th_mktempdir("cbm_disc_enc_gi");
1368+
ASSERT(base != NULL);
1369+
1370+
th_mkdir_p(TH_PATH(base, ".git"));
1371+
th_write_file(TH_PATH(base, ".gitignore"), "secret.py\n");
1372+
th_write_file(TH_PATH(base, "pkg/secret.py"), "TOKEN = 1\n");
1373+
th_write_file(TH_PATH(base, "pkg/keep.py"), "pass\n");
1374+
1375+
cbm_discover_opts_t opts = {0};
1376+
cbm_file_info_t *files = NULL;
1377+
int count = 0;
1378+
int rc = cbm_discover(TH_PATH(base, "pkg"), &opts, &files, &count);
1379+
ASSERT_EQ(rc, 0);
1380+
ASSERT_EQ(count, 1);
1381+
ASSERT_TRUE(strstr(files[0].rel_path, "keep.py") != NULL);
1382+
ASSERT_FALSE(discover_has_rel_path(files, count, "secret.py"));
1383+
1384+
cbm_discover_free(files, count);
1385+
th_cleanup(base);
1386+
PASS();
1387+
}
1388+
1389+
/* The enclosing repo's <common>/info/exclude (per-clone, uncommitted) must
1390+
* be honored the same way once the enclosing root is found, exactly as it
1391+
* already is for a repo_path that carries its own .git (issue #489). */
1392+
TEST(discover_enclosing_repo_info_exclude) {
1393+
char *base = th_mktempdir("cbm_disc_enc_exc");
1394+
ASSERT(base != NULL);
1395+
1396+
th_mkdir_p(TH_PATH(base, ".git/info"));
1397+
th_write_file(TH_PATH(base, ".git/info/exclude"), "scratch/\n");
1398+
th_write_file(TH_PATH(base, "pkg/main.py"), "pass\n");
1399+
th_write_file(TH_PATH(base, "pkg/scratch/tmp.py"), "pass\n");
1400+
1401+
cbm_discover_opts_t opts = {0};
1402+
cbm_file_info_t *files = NULL;
1403+
int count = 0;
1404+
int rc = cbm_discover(TH_PATH(base, "pkg"), &opts, &files, &count);
1405+
ASSERT_EQ(rc, 0);
1406+
ASSERT_EQ(count, 1);
1407+
ASSERT_TRUE(strstr(files[0].rel_path, "main.py") != NULL);
1408+
1409+
cbm_discover_free(files, count);
1410+
th_cleanup(base);
1411+
PASS();
1412+
}
1413+
1414+
/* Precedence: the indexed directory's own .gitignore is more specific than
1415+
* the enclosing repo's root .gitignore and must still win on conflict,
1416+
* matching git's shallow-to-deep rule (a later, deeper pattern overrides an
1417+
* earlier, shallower one). Without this, folding the enclosing root in
1418+
* ahead of repo_path's own .gitignore in the wrong order would let a root
1419+
* pattern silently re-ignore a file the subfolder's own .gitignore
1420+
* un-ignores. */
1421+
TEST(discover_enclosing_repo_gitignore_local_overrides) {
1422+
char *base = th_mktempdir("cbm_disc_enc_gi_ovr");
1423+
ASSERT(base != NULL);
1424+
1425+
th_mkdir_p(TH_PATH(base, ".git"));
1426+
th_write_file(TH_PATH(base, ".gitignore"), "*.py\n");
1427+
th_write_file(TH_PATH(base, "pkg/.gitignore"), "!keep.py\n");
1428+
th_write_file(TH_PATH(base, "pkg/keep.py"), "pass\n");
1429+
th_write_file(TH_PATH(base, "pkg/drop.py"), "pass\n");
1430+
1431+
cbm_discover_opts_t opts = {0};
1432+
cbm_file_info_t *files = NULL;
1433+
int count = 0;
1434+
int rc = cbm_discover(TH_PATH(base, "pkg"), &opts, &files, &count);
1435+
ASSERT_EQ(rc, 0);
1436+
ASSERT_EQ(count, 1);
1437+
ASSERT_TRUE(strstr(files[0].rel_path, "keep.py") != NULL);
1438+
ASSERT_FALSE(discover_has_rel_path(files, count, "drop.py"));
1439+
1440+
cbm_discover_free(files, count);
1441+
th_cleanup(base);
1442+
PASS();
1443+
}
1444+
13581445
/* ── Nested .gitignore tests (issue #178) ──────────────────────── */
13591446

13601447
TEST(discover_nested_gitignore) {
@@ -1971,6 +2058,11 @@ SUITE(discover) {
19712058
RUN_TEST(discover_worktree_info_exclude);
19722059
RUN_TEST(discover_worktree_committed_gitignore);
19732060

2061+
/* Enclosing-repo .gitignore resolution (issue #510, second half) */
2062+
RUN_TEST(discover_enclosing_repo_gitignore_issue510);
2063+
RUN_TEST(discover_enclosing_repo_info_exclude);
2064+
RUN_TEST(discover_enclosing_repo_gitignore_local_overrides);
2065+
19742066
/* Nested .gitignore tests (issue #178) */
19752067
RUN_TEST(discover_nested_gitignore);
19762068
RUN_TEST(discover_nested_gitignore_stacks_with_root);

0 commit comments

Comments
 (0)