Files
codebase-memory-mcp/tests/test_gitignore.c
T
Shaurya 11009f2d94 fix(discover): honor .git/info/exclude during file discovery (#493)
* fix(discover): honor .git/info/exclude during file discovery

index_repository only loaded .gitignore when building the exclusion
matcher. .git/info/exclude — the per-clone exclude file that Git treats
as authoritative — was never read, so paths excluded only there were
walked in full. On repos with Sandcastle worktrees this caused the
indexer to traverse 661K files / 170 GB and OOM (issue #489).

Add cbm_gitignore_merge() to append patterns from one matcher into
another. In cbm_discover_ex, after loading .gitignore, also load
.git/info/exclude and merge its patterns in. If only the exclude file
exists, use it directly. No downstream call paths change.

Add three unit tests for cbm_gitignore_merge and two integration tests
that reproduce the issue scenario (exclude-only and exclude-stacked-
with-gitignore).

Signed-off-by: ShauryaaSharma <shauryasofficial27@gmail.com>

* style(discover): fix clang-format alignment in cbm_gitignore_merge

Signed-off-by: ShauryaaSharma <shauryasofficial27@gmail.com>

---------

Signed-off-by: ShauryaaSharma <shauryasofficial27@gmail.com>
2026-06-22 22:04:42 +02:00

257 lines
8.6 KiB
C

/*
* test_gitignore.c — Tests for gitignore-style pattern matching.
*
* RED phase: These tests define the expected pattern matching behavior.
*/
#include "../src/foundation/compat.h"
#include "test_framework.h"
#include "discover/discover.h"
/* ── Basic pattern matching ────────────────────────────────────── */
TEST(gi_empty_pattern) {
cbm_gitignore_t *gi = cbm_gitignore_parse("");
ASSERT_NOT_NULL(gi);
ASSERT_FALSE(cbm_gitignore_matches(gi, "foo.txt", false));
cbm_gitignore_free(gi);
PASS();
}
TEST(gi_exact_file) {
cbm_gitignore_t *gi = cbm_gitignore_parse("secret.key\n");
ASSERT_NOT_NULL(gi);
ASSERT_TRUE(cbm_gitignore_matches(gi, "secret.key", false));
ASSERT_FALSE(cbm_gitignore_matches(gi, "other.key", false));
cbm_gitignore_free(gi);
PASS();
}
TEST(gi_wildcard_star) {
cbm_gitignore_t *gi = cbm_gitignore_parse("*.log\n");
ASSERT_NOT_NULL(gi);
ASSERT_TRUE(cbm_gitignore_matches(gi, "error.log", false));
ASSERT_TRUE(cbm_gitignore_matches(gi, "access.log", false));
ASSERT_FALSE(cbm_gitignore_matches(gi, "error.txt", false));
/* Non-rooted pattern matches basename at any depth */
ASSERT_TRUE(cbm_gitignore_matches(gi, "logs/error.log", false));
cbm_gitignore_free(gi);
PASS();
}
TEST(gi_double_star_prefix) {
/* ** matches any number of directories */
cbm_gitignore_t *gi = cbm_gitignore_parse("**/build\n");
ASSERT_NOT_NULL(gi);
ASSERT_TRUE(cbm_gitignore_matches(gi, "build", true));
ASSERT_TRUE(cbm_gitignore_matches(gi, "src/build", true));
ASSERT_TRUE(cbm_gitignore_matches(gi, "a/b/c/build", true));
cbm_gitignore_free(gi);
PASS();
}
TEST(gi_double_star_suffix) {
cbm_gitignore_t *gi = cbm_gitignore_parse("logs/**\n");
ASSERT_NOT_NULL(gi);
ASSERT_TRUE(cbm_gitignore_matches(gi, "logs/debug.log", false));
ASSERT_TRUE(cbm_gitignore_matches(gi, "logs/sub/trace.log", false));
ASSERT_FALSE(cbm_gitignore_matches(gi, "src/logs", false));
cbm_gitignore_free(gi);
PASS();
}
TEST(gi_double_star_middle) {
cbm_gitignore_t *gi = cbm_gitignore_parse("a/**/b\n");
ASSERT_NOT_NULL(gi);
ASSERT_TRUE(cbm_gitignore_matches(gi, "a/b", false));
ASSERT_TRUE(cbm_gitignore_matches(gi, "a/x/b", false));
ASSERT_TRUE(cbm_gitignore_matches(gi, "a/x/y/z/b", false));
ASSERT_FALSE(cbm_gitignore_matches(gi, "c/a/b", false));
cbm_gitignore_free(gi);
PASS();
}
TEST(gi_directory_only) {
/* Trailing slash means match directories only */
cbm_gitignore_t *gi = cbm_gitignore_parse("build/\n");
ASSERT_NOT_NULL(gi);
ASSERT_TRUE(cbm_gitignore_matches(gi, "build", true));
ASSERT_FALSE(cbm_gitignore_matches(gi, "build", false)); /* not a directory */
/* Should match anywhere in tree */
ASSERT_TRUE(cbm_gitignore_matches(gi, "src/build", true));
cbm_gitignore_free(gi);
PASS();
}
TEST(gi_negation) {
/* ! prefix negates a pattern */
cbm_gitignore_t *gi = cbm_gitignore_parse("*.log\n!important.log\n");
ASSERT_NOT_NULL(gi);
ASSERT_TRUE(cbm_gitignore_matches(gi, "error.log", false));
ASSERT_FALSE(cbm_gitignore_matches(gi, "important.log", false));
cbm_gitignore_free(gi);
PASS();
}
TEST(gi_comment_and_blank) {
cbm_gitignore_t *gi = cbm_gitignore_parse("# This is a comment\n\n*.tmp\n");
ASSERT_NOT_NULL(gi);
ASSERT_TRUE(cbm_gitignore_matches(gi, "data.tmp", false));
ASSERT_FALSE(cbm_gitignore_matches(gi, "data.txt", false));
cbm_gitignore_free(gi);
PASS();
}
TEST(gi_rooted_pattern) {
/* Pattern with slash is anchored to the root */
cbm_gitignore_t *gi = cbm_gitignore_parse("/build\n");
ASSERT_NOT_NULL(gi);
ASSERT_TRUE(cbm_gitignore_matches(gi, "build", true));
ASSERT_FALSE(cbm_gitignore_matches(gi, "src/build", true));
cbm_gitignore_free(gi);
PASS();
}
TEST(gi_path_with_slash) {
/* Pattern containing / (not just leading) is rooted */
cbm_gitignore_t *gi = cbm_gitignore_parse("doc/frotz\n");
ASSERT_NOT_NULL(gi);
ASSERT_TRUE(cbm_gitignore_matches(gi, "doc/frotz", false));
ASSERT_FALSE(cbm_gitignore_matches(gi, "src/doc/frotz", false));
cbm_gitignore_free(gi);
PASS();
}
TEST(gi_question_mark) {
cbm_gitignore_t *gi = cbm_gitignore_parse("file?.txt\n");
ASSERT_NOT_NULL(gi);
ASSERT_TRUE(cbm_gitignore_matches(gi, "file1.txt", false));
ASSERT_TRUE(cbm_gitignore_matches(gi, "fileA.txt", false));
ASSERT_FALSE(cbm_gitignore_matches(gi, "file12.txt", false));
cbm_gitignore_free(gi);
PASS();
}
TEST(gi_bracket_range) {
cbm_gitignore_t *gi = cbm_gitignore_parse("file[0-9].txt\n");
ASSERT_NOT_NULL(gi);
ASSERT_TRUE(cbm_gitignore_matches(gi, "file3.txt", false));
ASSERT_FALSE(cbm_gitignore_matches(gi, "fileA.txt", false));
cbm_gitignore_free(gi);
PASS();
}
TEST(gi_multiple_patterns) {
cbm_gitignore_t *gi = cbm_gitignore_parse("*.pyc\n"
"__pycache__/\n"
".env\n"
"*.log\n");
ASSERT_NOT_NULL(gi);
ASSERT_TRUE(cbm_gitignore_matches(gi, "module.pyc", false));
ASSERT_TRUE(cbm_gitignore_matches(gi, "__pycache__", true));
ASSERT_TRUE(cbm_gitignore_matches(gi, ".env", false));
ASSERT_TRUE(cbm_gitignore_matches(gi, "app.log", false));
ASSERT_FALSE(cbm_gitignore_matches(gi, "main.py", false));
cbm_gitignore_free(gi);
PASS();
}
TEST(gi_null_safe_free) {
cbm_gitignore_free(NULL); /* should not crash */
PASS();
}
/* ── Load from file ────────────────────────────────────────────── */
TEST(gi_load_file) {
char path[256]; snprintf(path, sizeof(path), "%s/test_gitignore_file", cbm_tmpdir());
FILE *f = fopen(path, "w");
ASSERT_NOT_NULL(f);
fprintf(f, "*.o\nbuild/\n");
fclose(f);
cbm_gitignore_t *gi = cbm_gitignore_load(path);
ASSERT_NOT_NULL(gi);
ASSERT_TRUE(cbm_gitignore_matches(gi, "main.o", false));
ASSERT_TRUE(cbm_gitignore_matches(gi, "build", true));
cbm_gitignore_free(gi);
remove(path);
PASS();
}
TEST(gi_load_nonexistent) {
char np[256];
snprintf(np, sizeof(np), "%s/nonexistent_gitignore_12345", cbm_tmpdir());
cbm_gitignore_t *gi = cbm_gitignore_load(np);
ASSERT_NULL(gi);
PASS();
}
/* ── Merge ─────────────────────────────────────────────────────── */
TEST(gi_merge_patterns) {
cbm_gitignore_t *base_gi = cbm_gitignore_parse("*.log\n");
cbm_gitignore_t *extra = cbm_gitignore_parse("tmp/\nbuild/\n");
ASSERT_NOT_NULL(base_gi);
ASSERT_NOT_NULL(extra);
cbm_gitignore_merge(base_gi, extra);
cbm_gitignore_free(extra);
ASSERT_TRUE(cbm_gitignore_matches(base_gi, "error.log", false));
ASSERT_TRUE(cbm_gitignore_matches(base_gi, "tmp", true));
ASSERT_TRUE(cbm_gitignore_matches(base_gi, "build", true));
ASSERT_FALSE(cbm_gitignore_matches(base_gi, "main.go", false));
cbm_gitignore_free(base_gi);
PASS();
}
TEST(gi_merge_into_empty) {
cbm_gitignore_t *dst = cbm_gitignore_parse("");
cbm_gitignore_t *src = cbm_gitignore_parse("*.o\n");
ASSERT_NOT_NULL(dst);
ASSERT_NOT_NULL(src);
cbm_gitignore_merge(dst, src);
cbm_gitignore_free(src);
ASSERT_TRUE(cbm_gitignore_matches(dst, "main.o", false));
ASSERT_FALSE(cbm_gitignore_matches(dst, "main.c", false));
cbm_gitignore_free(dst);
PASS();
}
TEST(gi_merge_null_safe) {
cbm_gitignore_t *gi = cbm_gitignore_parse("*.log\n");
cbm_gitignore_merge(gi, NULL); /* should not crash */
cbm_gitignore_merge(NULL, gi); /* should not crash */
cbm_gitignore_free(gi);
PASS();
}
/* ── Suite ─────────────────────────────────────────────────────── */
SUITE(gitignore) {
RUN_TEST(gi_empty_pattern);
RUN_TEST(gi_exact_file);
RUN_TEST(gi_wildcard_star);
RUN_TEST(gi_double_star_prefix);
RUN_TEST(gi_double_star_suffix);
RUN_TEST(gi_double_star_middle);
RUN_TEST(gi_directory_only);
RUN_TEST(gi_negation);
RUN_TEST(gi_comment_and_blank);
RUN_TEST(gi_rooted_pattern);
RUN_TEST(gi_path_with_slash);
RUN_TEST(gi_question_mark);
RUN_TEST(gi_bracket_range);
RUN_TEST(gi_multiple_patterns);
RUN_TEST(gi_null_safe_free);
RUN_TEST(gi_load_file);
RUN_TEST(gi_load_nonexistent);
RUN_TEST(gi_merge_patterns);
RUN_TEST(gi_merge_into_empty);
RUN_TEST(gi_merge_null_safe);
}