| 769 | /* ── .gitattributes setup ────────────────────────────────────────── */ |
| 770 | |
| 771 | static void ensure_gitattributes(const char *repo_path) { |
| 772 | char ga_path[CBM_SZ_4K]; |
| 773 | artifact_path(ga_path, sizeof(ga_path), repo_path, ".gitattributes"); |
| 774 | |
| 775 | /* Atomic create-only-if-absent: the "x" mode (O_CREAT|O_EXCL) closes the |
| 776 | * TOCTOU window between checking existence and writing. If the file |
| 777 | * exists, the open fails with EEXIST and we leave it untouched. |
| 778 | * cbm_fopen, not open(): the ANSI CRT open() cannot create the file under |
| 779 | * a repo path outside the active code page on Windows, which silently |
| 780 | * dropped the merge=ours protection there (#1171). */ |
| 781 | errno = 0; |
| 782 | FILE *fp = cbm_fopen(ga_path, "wbx"); |
| 783 | if (!fp) { |
| 784 | int open_errno = errno; |
| 785 | if (open_errno != EEXIST) { |
| 786 | cbm_log_warn("artifact.gitattributes.open", "path", ga_path, "err", |
| 787 | strerror(open_errno)); |
| 788 | } |
| 789 | /* fall through to merge driver setup either way */ |
| 790 | } else { |
| 791 | /* Order matters: attributes apply left to right and the `binary` |
| 792 | * macro expands to `-diff -merge -text`, so a trailing `binary` |
| 793 | * unsets `merge=ours` and the conflict prevention this file |
| 794 | * exists for never engages. The macro must come first. */ |
| 795 | (void)fputs("# Auto-generated by codebase-memory-mcp\n" |
| 796 | "# Prevent merge conflicts on compressed artifact\n" CBM_ARTIFACT_FILENAME |
| 797 | " binary merge=ours\n", |
| 798 | fp); |
| 799 | (void)fclose(fp); |
| 800 | } |
| 801 | |
| 802 | /* Best-effort: configure merge driver */ |
| 803 | if (!cbm_artifact_repo_path_is_shell_safe(repo_path)) { |
| 804 | return; |
| 805 | } |
| 806 | char cmd[CBM_SZ_1K]; |
| 807 | int n = snprintf(cmd, sizeof(cmd), |
| 808 | "git -C \"%s\" config merge.ours.driver true 2>" ARTIFACT_NULL_DEV, repo_path); |
| 809 | if (n < 0 || (size_t)n >= sizeof(cmd)) { |
| 810 | return; /* truncated command → skip (parity with git_context.c) */ |
| 811 | } |
| 812 | FILE *p = cbm_popen_git(cmd); |
| 813 | if (p) { |
| 814 | (void)cbm_pclose(p); |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | /* ── Index stripping ─────────────────────────────────────────────── */ |
| 819 |
no test coverage detected