Skip to content

Commit 416e4f2

Browse files
authored
Merge pull request DeusData#807 from DeusData/distill/543-cbmignore-neg
fix(discover): .cbmignore negation can un-skip built-in dirs (safety core excluded)
2 parents 185451a + 56f28ec commit 416e4f2

2 files changed

Lines changed: 199 additions & 1 deletion

File tree

‎src/discover/discover.c‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,14 +473,35 @@ static const char *local_rel_path(const char *rel_path, const char *local_prefix
473473
return rel_path;
474474
}
475475

476+
/* Non-negatable safety core: built-in skip dirs that a .cbmignore negation
477+
* can NEVER un-skip. A repo-committed .cbmignore must not be able to defeat
478+
* OOM/safety skips: .git holds VCS internals (and the info/exclude sources,
479+
* #489), node_modules explodes discovery, and the worktree-internal dirs
480+
* (.worktrees / .claude-worktrees, the worktree entries in ALWAYS_SKIP_DIRS)
481+
* contain parallel checkouts of the same repo whose indexing would duplicate
482+
* the whole codebase (#802). */
483+
static bool is_safety_core_dir(const char *name) {
484+
static const char *const SAFETY_CORE_DIRS[] = {".git", "node_modules", ".worktrees",
485+
".claude-worktrees", NULL};
486+
return str_in_list(name, SAFETY_CORE_DIRS);
487+
}
488+
476489
/* Check if a directory entry should be skipped (hardcoded dirs + gitignore). */
477490
static bool should_skip_directory(const char *entry_name, const char *rel_path,
478491
const cbm_discover_opts_t *opts, const cbm_gitignore_t *gitignore,
479492
const cbm_gitignore_t *global_gi,
480493
const cbm_gitignore_t *cbmignore, const cbm_gitignore_t *local_gi,
481494
const char *local_gi_prefix) {
482495
if (cbm_should_skip_dir(entry_name, opts ? opts->mode : CBM_MODE_FULL)) {
483-
return true;
496+
/* #500: a .cbmignore negation (e.g. "!obj/") whose rule is the last
497+
* match for this dir un-skips a built-in skip-list dir — except the
498+
* non-negatable safety core. Fall through so .gitignore/global/local
499+
* rules still apply to the un-skipped dir. */
500+
bool unskipped = cbmignore && !is_safety_core_dir(entry_name) &&
501+
cbm_gitignore_match_result(cbmignore, rel_path, true) < 0;
502+
if (!unskipped) {
503+
return true;
504+
}
484505
}
485506
if (gitignore && cbm_gitignore_matches(gitignore, rel_path, true)) {
486507
return true;

‎tests/test_discover.c‎

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,176 @@ TEST(discover_cbmignore_no_git) {
872872
PASS();
873873
}
874874

875+
/* ── .cbmignore negation vs built-in skip dirs (issue #500) ────── */
876+
877+
static bool discover_excluded_contains(char **excluded, int count, const char *rel_path) {
878+
for (int i = 0; i < count; i++) {
879+
if (strcmp(excluded[i], rel_path) == 0) {
880+
return true;
881+
}
882+
}
883+
return false;
884+
}
885+
886+
/* A "!obj/" negation in .cbmignore must un-skip the built-in ALWAYS_SKIP
887+
* "obj" dir so files inside it get discovered — and the un-skipped dir must
888+
* not be reported as an excluded subtree (#411 list stays coherent). */
889+
TEST(discover_cbmignore_negates_always_skip_dir) {
890+
char *base = th_mktempdir("cbm_disc_cbmi_neg_obj");
891+
ASSERT(base != NULL);
892+
893+
th_write_file(TH_PATH(base, ".cbmignore"), "!obj/\n");
894+
th_write_file(TH_PATH(base, "main.go"), "package main\n");
895+
th_write_file(TH_PATH(base, "obj/generated.go"), "package obj\n");
896+
897+
cbm_discover_opts_t opts = {0};
898+
cbm_file_info_t *files = NULL;
899+
int count = 0;
900+
char **excluded = NULL;
901+
int excluded_count = 0;
902+
903+
int rc = cbm_discover_ex(base, &opts, &files, &count, &excluded, &excluded_count);
904+
ASSERT_EQ(rc, 0);
905+
ASSERT_EQ(count, 2);
906+
ASSERT_TRUE(discover_has_rel_path(files, count, "main.go"));
907+
ASSERT_TRUE(discover_has_rel_path(files, count, "obj/generated.go"));
908+
ASSERT_FALSE(discover_excluded_contains(excluded, excluded_count, "obj"));
909+
910+
cbm_discover_free_excluded(excluded, excluded_count);
911+
cbm_discover_free(files, count);
912+
th_cleanup(base);
913+
PASS();
914+
}
915+
916+
/* An anchored negation ("!src/target/") un-skips only that nested dir; other
917+
* dirs with the same basename stay built-in-skipped. */
918+
TEST(discover_cbmignore_negates_only_nested_skip_dir) {
919+
char *base = th_mktempdir("cbm_disc_cbmi_neg_nested");
920+
ASSERT(base != NULL);
921+
922+
th_write_file(TH_PATH(base, ".cbmignore"), "!src/target/\n");
923+
th_write_file(TH_PATH(base, "src/main.go"), "package src\n");
924+
th_write_file(TH_PATH(base, "src/target/lib.go"), "package target\n");
925+
th_write_file(TH_PATH(base, "other/target/lib.go"), "package other\n");
926+
th_write_file(TH_PATH(base, "target/root.go"), "package root\n");
927+
928+
cbm_discover_opts_t opts = {0};
929+
cbm_file_info_t *files = NULL;
930+
int count = 0;
931+
932+
int rc = cbm_discover(base, &opts, &files, &count);
933+
ASSERT_EQ(rc, 0);
934+
ASSERT_EQ(count, 2);
935+
ASSERT_TRUE(discover_has_rel_path(files, count, "src/main.go"));
936+
ASSERT_TRUE(discover_has_rel_path(files, count, "src/target/lib.go"));
937+
ASSERT_FALSE(discover_has_rel_path(files, count, "other/target/lib.go"));
938+
ASSERT_FALSE(discover_has_rel_path(files, count, "target/root.go"));
939+
940+
cbm_discover_free(files, count);
941+
th_cleanup(base);
942+
PASS();
943+
}
944+
945+
/* Negation also un-skips FAST-mode skip dirs ("docs" is in FAST_SKIP_DIRS). */
946+
TEST(discover_cbmignore_negates_fast_skip_dir) {
947+
char *base = th_mktempdir("cbm_disc_cbmi_neg_fast");
948+
ASSERT(base != NULL);
949+
950+
th_write_file(TH_PATH(base, ".cbmignore"), "!docs/\n");
951+
th_write_file(TH_PATH(base, "main.go"), "package main\n");
952+
th_write_file(TH_PATH(base, "docs/guide.go"), "package docs\n");
953+
954+
cbm_discover_opts_t opts = {.mode = CBM_MODE_FAST};
955+
cbm_file_info_t *files = NULL;
956+
int count = 0;
957+
958+
int rc = cbm_discover(base, &opts, &files, &count);
959+
ASSERT_EQ(rc, 0);
960+
ASSERT_EQ(count, 2);
961+
ASSERT_TRUE(discover_has_rel_path(files, count, "main.go"));
962+
ASSERT_TRUE(discover_has_rel_path(files, count, "docs/guide.go"));
963+
964+
cbm_discover_free(files, count);
965+
th_cleanup(base);
966+
PASS();
967+
}
968+
969+
/* Last-match-wins ordering: "obj/" then "!obj/" un-skips; "!obj/" then
970+
* "obj/" re-ignores, so the built-in skip stands. */
971+
TEST(discover_cbmignore_negation_last_match_wins) {
972+
char *base = th_mktempdir("cbm_disc_cbmi_order1");
973+
ASSERT(base != NULL);
974+
975+
th_write_file(TH_PATH(base, ".cbmignore"), "obj/\n!obj/\n");
976+
th_write_file(TH_PATH(base, "main.go"), "package main\n");
977+
th_write_file(TH_PATH(base, "obj/generated.go"), "package obj\n");
978+
979+
cbm_discover_opts_t opts = {0};
980+
cbm_file_info_t *files = NULL;
981+
int count = 0;
982+
983+
int rc = cbm_discover(base, &opts, &files, &count);
984+
ASSERT_EQ(rc, 0);
985+
ASSERT_EQ(count, 2);
986+
ASSERT_TRUE(discover_has_rel_path(files, count, "obj/generated.go"));
987+
988+
cbm_discover_free(files, count);
989+
th_cleanup(base);
990+
991+
base = th_mktempdir("cbm_disc_cbmi_order2");
992+
ASSERT(base != NULL);
993+
994+
th_write_file(TH_PATH(base, ".cbmignore"), "!obj/\nobj/\n");
995+
th_write_file(TH_PATH(base, "main.go"), "package main\n");
996+
th_write_file(TH_PATH(base, "obj/generated.go"), "package obj\n");
997+
998+
files = NULL;
999+
count = 0;
1000+
1001+
rc = cbm_discover(base, &opts, &files, &count);
1002+
ASSERT_EQ(rc, 0);
1003+
ASSERT_EQ(count, 1);
1004+
ASSERT_TRUE(discover_has_rel_path(files, count, "main.go"));
1005+
ASSERT_FALSE(discover_has_rel_path(files, count, "obj/generated.go"));
1006+
1007+
cbm_discover_free(files, count);
1008+
th_cleanup(base);
1009+
PASS();
1010+
}
1011+
1012+
/* Safety-core policy (#489, #802): .git, node_modules, and the
1013+
* worktree-internal dirs can never be un-skipped, even by an explicit
1014+
* .cbmignore negation. Green by construction; guards the policy. */
1015+
TEST(discover_cbmignore_negation_cannot_unskip_safety_core) {
1016+
char *base = th_mktempdir("cbm_disc_cbmi_safety");
1017+
ASSERT(base != NULL);
1018+
1019+
th_write_file(TH_PATH(base, ".cbmignore"),
1020+
"!.git/\n!node_modules/\n!.worktrees/\n!.claude-worktrees/\n");
1021+
th_write_file(TH_PATH(base, "main.go"), "package main\n");
1022+
th_write_file(TH_PATH(base, ".git/hooks/hook.go"), "package hooks\n");
1023+
th_write_file(TH_PATH(base, "node_modules/pkg/index.js"), "module.exports = 1;\n");
1024+
th_write_file(TH_PATH(base, ".worktrees/wt/dup.go"), "package dup\n");
1025+
th_write_file(TH_PATH(base, ".claude-worktrees/wt/dup.go"), "package dup\n");
1026+
1027+
cbm_discover_opts_t opts = {0};
1028+
cbm_file_info_t *files = NULL;
1029+
int count = 0;
1030+
1031+
int rc = cbm_discover(base, &opts, &files, &count);
1032+
ASSERT_EQ(rc, 0);
1033+
ASSERT_EQ(count, 1);
1034+
ASSERT_TRUE(discover_has_rel_path(files, count, "main.go"));
1035+
ASSERT_FALSE(discover_has_rel_path(files, count, ".git/hooks/hook.go"));
1036+
ASSERT_FALSE(discover_has_rel_path(files, count, "node_modules/pkg/index.js"));
1037+
ASSERT_FALSE(discover_has_rel_path(files, count, ".worktrees/wt/dup.go"));
1038+
ASSERT_FALSE(discover_has_rel_path(files, count, ".claude-worktrees/wt/dup.go"));
1039+
1040+
cbm_discover_free(files, count);
1041+
th_cleanup(base);
1042+
PASS();
1043+
}
1044+
8751045
/* ── .git/info/exclude tests (issue #489) ─────────────────────── */
8761046

8771047
/* Per-clone excludes written to .git/info/exclude (not committed) must be
@@ -1168,6 +1338,13 @@ SUITE(discover) {
11681338
RUN_TEST(discover_deploy_excluded_full_mode);
11691339
RUN_TEST(discover_cbmignore_no_git);
11701340

1341+
/* .cbmignore negation vs built-in skip dirs (issue #500) */
1342+
RUN_TEST(discover_cbmignore_negates_always_skip_dir);
1343+
RUN_TEST(discover_cbmignore_negates_only_nested_skip_dir);
1344+
RUN_TEST(discover_cbmignore_negates_fast_skip_dir);
1345+
RUN_TEST(discover_cbmignore_negation_last_match_wins);
1346+
RUN_TEST(discover_cbmignore_negation_cannot_unskip_safety_core);
1347+
11711348
/* .git/info/exclude support (issue #489) */
11721349
RUN_TEST(discover_git_info_exclude);
11731350
RUN_TEST(discover_git_info_exclude_stacks_with_gitignore);

0 commit comments

Comments
 (0)