Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(artifact): create .gitattributes through the UTF-8 path layer

Martin Vogel402dd7c59b72
diff --git a/src/pipeline/artifact.c b/src/pipeline/artifact.c
index 9594c8a09..b1a1ac6c7 100644
--- a/src/pipeline/artifact.c
+++ b/src/pipeline/artifact.c
@@ -772,30 +772,31 @@ static void ensure_gitattributes(const char *repo_path) {
char ga_path[CBM_SZ_4K];
artifact_path(ga_path, sizeof(ga_path), repo_path, ".gitattributes");
- /* Atomic create-only-if-absent: O_EXCL closes the TOCTOU window
- * between checking existence and writing. If the file exists, open
- * fails with EEXIST and we leave it untouched. */
- int fd = open(ga_path, O_WRONLY | O_CREAT | O_EXCL, 0644);
- if (fd < 0) {
- if (errno != EEXIST) {
- cbm_log_warn("artifact.gitattributes.open path=%s err=%s", ga_path, strerror(errno));
+ /* Atomic create-only-if-absent: the "x" mode (O_CREAT|O_EXCL) closes the
+ * TOCTOU window between checking existence and writing. If the file
+ * exists, the open fails with EEXIST and we leave it untouched.
+ * cbm_fopen, not open(): the ANSI CRT open() cannot create the file under
+ * a repo path outside the active code page on Windows, which silently
+ * dropped the merge=ours protection there (#1171). */
+ errno = 0;
+ FILE *fp = cbm_fopen(ga_path, "wbx");
+ if (!fp) {
+ int open_errno = errno;
+ if (open_errno != EEXIST) {
+ cbm_log_warn("artifact.gitattributes.open", "path", ga_path, "err",
+ strerror(open_errno));
}
/* fall through to merge driver setup either way */
} else {
- FILE *fp = fdopen(fd, "w");
- if (fp) {
- /* Order matters: attributes apply left to right and the `binary`
- * macro expands to `-diff -merge -text`, so a trailing `binary`
- * unsets `merge=ours` and the conflict prevention this file
- * exists for never engages. The macro must come first. */
- (void)fputs("# Auto-generated by codebase-memory-mcp\n"
- "# Prevent merge conflicts on compressed artifact\n" CBM_ARTIFACT_FILENAME
- " binary merge=ours\n",
- fp);
- (void)fclose(fp);
- } else {
- (void)close(fd);
- }
+ /* Order matters: attributes apply left to right and the `binary`
+ * macro expands to `-diff -merge -text`, so a trailing `binary`
+ * unsets `merge=ours` and the conflict prevention this file
+ * exists for never engages. The macro must come first. */
+ (void)fputs("# Auto-generated by codebase-memory-mcp\n"
+ "# Prevent merge conflicts on compressed artifact\n" CBM_ARTIFACT_FILENAME
+ " binary merge=ours\n",
+ fp);
+ (void)fclose(fp);
}
/* Best-effort: configure merge driver */
diff --git a/tests/test_artifact.c b/tests/test_artifact.c
index 6206463e3..8bbec658d 100644
--- a/tests/test_artifact.c
+++ b/tests/test_artifact.c
@@ -334,12 +334,30 @@ TEST(artifact_gitattributes_created) {
PASS();
}
+/* Read a small file through the UTF-8 path layer (wide on Windows), so a
+ * non-ASCII path is never misread by the test's own ANSI CRT call. Returns the
+ * byte count, or 0 when the file cannot be opened or is empty. */
+static size_t read_small_file_utf8(const char *path, char *buf, size_t cap) {
+ buf[0] = '\0';
+ FILE *fp = cbm_fopen(path, "rb");
+ if (!fp) {
+ return 0;
+ }
+ size_t rd = fread(buf, 1, cap - 1, fp);
+ (void)fclose(fp);
+ buf[rd] = '\0';
+ return rd;
+}
+
/* #1171: a repository (and a cache directory) under a non-ASCII path must
* export, report the artifact present, and import — also onto an existing
* cache db. On Windows the atomic writer used the ANSI CRT fopen + MoveFileExA
* and the existence check the ANSI stat, so CJK characters outside the active
- * code page made every step fail. The bytes are escaped UTF-8 for a CJK repo
- * directory name and a CJK cache directory name (kept ASCII in source). */
+ * code page made every step fail. The same export also writes the
+ * .gitattributes merge=ours protection, which ensure_gitattributes created
+ * with the ANSI CRT open(): on Windows it was silently missing. The bytes are
+ * escaped UTF-8 for a CJK repo directory name and a CJK cache directory name
+ * (kept ASCII in source). */
#define ART_CJK_REPO "\xe6\x8c\x81\xe4\xb9\x85\xe5\x8c\x96\xe5\xa4\x8d\xe7\x8e\xb0"
#define ART_CJK_CACHE "\xe5\xaf\xbc\xe5\x85\xa5"
TEST(artifact_roundtrip_non_ascii_paths) {
@@ -356,6 +374,23 @@ TEST(artifact_roundtrip_non_ascii_paths) {
ASSERT_TRUE(cbm_file_exists(zst));
ASSERT_TRUE(cbm_artifact_exists(repo));
+ char ga[1024];
+ snprintf(ga, sizeof(ga), "%s/.codebase-memory/.gitattributes", repo);
+ char ga_content[512];
+ ASSERT_TRUE(read_small_file_utf8(ga, ga_content, sizeof(ga_content)) > 0);
+ ASSERT_NOT_NULL(strstr(ga_content, CBM_ARTIFACT_FILENAME " binary merge=ours"));
+
+ /* Create-only-if-absent: a re-export never rewrites an existing
+ * .gitattributes the user may have edited. */
+ static const char user_ga[] = "# user-owned\n";
+ FILE *uga = cbm_fopen(ga, "wb");
+ ASSERT_NOT_NULL(uga);
+ ASSERT_TRUE(fputs(user_ga, uga) >= 0);
+ ASSERT_EQ(fclose(uga), 0);
+ ASSERT_EQ(cbm_artifact_export(g_db, repo, "test-proj", CBM_ARTIFACT_FAST), 0);
+ ASSERT_EQ(read_small_file_utf8(ga, ga_content, sizeof(ga_content)), strlen(user_ga));
+ ASSERT_STR_EQ(ga_content, user_ga);
+
char cache_dir[1024];
snprintf(cache_dir, sizeof(cache_dir), "%s/" ART_CJK_CACHE, g_tmpdir);
ASSERT_TRUE(cbm_mkdir_p(cache_dir, 0755));