@@ -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