|
| 1 | +/* |
| 2 | + * test_git_context.c — Tests for cbm_git_context_resolve(), focusing on |
| 3 | + * the canonical_root derivation for git worktrees and subdirectory projects. |
| 4 | + * |
| 5 | + * Issue #659: canonical_root was computed incorrectly for linked worktrees |
| 6 | + * and projects indexed from a subdirectory of the repository root. |
| 7 | + * git rev-parse --git-common-dir outputs a path relative to the -C directory |
| 8 | + * (input_path), not to worktree_root. Joining it with worktree_root and then |
| 9 | + * string-stripping "/.git" left unresolved ".." components in the result. |
| 10 | + * |
| 11 | + * These tests shell out to `git`, so they SKIP_PLATFORM on Windows (the CI |
| 12 | + * shell there cannot init a repo via system()). |
| 13 | + * |
| 14 | + * Reproduce-first guard: canonical_root_subdir is the genuine RED-without-the-fix |
| 15 | + * guard — a repo indexed from a subdirectory yields a relative --git-common-dir |
| 16 | + * ("../.git"), so the unfixed code returns an un-normalized "<root>/subdir/.." |
| 17 | + * (verified FAIL on the unfixed derive_canonical_root; GREEN with the realpath |
| 18 | + * normalization). canonical_root_linked_worktree is a SUPPORTING INVARIANT, not |
| 19 | + * the #659 reproducer: on git that emits an *absolute* --git-common-dir for a |
| 20 | + * linked worktree (e.g. 2.48.x) the bug does not manifest there, so that test |
| 21 | + * passes with or without the fix. It still enforces the worktree->main-root |
| 22 | + * invariant and would catch the bug on git builds that emit a relative |
| 23 | + * worktree common-dir. canonical_root_repo_root is a baseline (no `..` to |
| 24 | + * normalize), not a guard. |
| 25 | + */ |
| 26 | +#include "test_framework.h" |
| 27 | +#include "test_helpers.h" |
| 28 | +#include "git/git_context.h" |
| 29 | + |
| 30 | +#include <stdio.h> |
| 31 | +#include <string.h> |
| 32 | + |
| 33 | +#ifndef _WIN32 |
| 34 | +#include <limits.h> |
| 35 | +#endif |
| 36 | + |
| 37 | +/* Run a git command inside dir, return 0 on success. */ |
| 38 | +static int git_run(const char *dir, const char *args) { |
| 39 | + char cmd[1024]; |
| 40 | + snprintf(cmd, sizeof(cmd), "git -C \"%s\" %s >/dev/null 2>&1", dir, args); |
| 41 | + return system(cmd); |
| 42 | +} |
| 43 | + |
| 44 | +/* Create a minimal git repo at dir (init + empty commit so HEAD exists). */ |
| 45 | +static int make_git_repo(const char *dir) { |
| 46 | + if (th_mkdir_p(dir) != 0) return -1; |
| 47 | + if (git_run(dir, "init -q") != 0) return -1; |
| 48 | + if (git_run(dir, "config user.email test@example.com") != 0) return -1; |
| 49 | + if (git_run(dir, "config user.name Test") != 0) return -1; |
| 50 | + /* Create a file so HEAD points to a real commit. */ |
| 51 | + char path[1024]; |
| 52 | + snprintf(path, sizeof(path), "%s/.keep", dir); |
| 53 | + th_write_file(path, ""); |
| 54 | + if (git_run(dir, "add .keep") != 0) return -1; |
| 55 | + if (git_run(dir, "commit -q -m init") != 0) return -1; |
| 56 | + return 0; |
| 57 | +} |
| 58 | + |
| 59 | +/* ── canonical_root: normal repo indexed from its root ──────────── */ |
| 60 | + |
| 61 | +TEST(canonical_root_repo_root) { |
| 62 | +#ifdef _WIN32 |
| 63 | + SKIP_PLATFORM("git-based canonical_root test not supported on Windows CI"); |
| 64 | +#else |
| 65 | + char *tmp = th_mktempdir("cbm_gitctx"); |
| 66 | + if (!tmp) FAIL("th_mktempdir returned NULL"); |
| 67 | + |
| 68 | + if (make_git_repo(tmp) != 0) { |
| 69 | + th_rmtree(tmp); |
| 70 | + SKIP_PLATFORM("git not available to init a repo"); |
| 71 | + } |
| 72 | + |
| 73 | + cbm_git_context_t ctx = {0}; |
| 74 | + int rc = cbm_git_context_resolve(tmp, &ctx); |
| 75 | + if (rc != 0 || !ctx.is_git) { |
| 76 | + cbm_git_context_free(&ctx); |
| 77 | + th_rmtree(tmp); |
| 78 | + FAIL("cbm_git_context_resolve failed or not a git repo"); |
| 79 | + } |
| 80 | + |
| 81 | + char expected[4096]; |
| 82 | + if (realpath(tmp, expected) == NULL) { |
| 83 | + cbm_git_context_free(&ctx); |
| 84 | + th_rmtree(tmp); |
| 85 | + FAIL("realpath(tmp) failed"); |
| 86 | + } |
| 87 | + |
| 88 | + ASSERT_STR_EQ(ctx.canonical_root, expected); |
| 89 | + |
| 90 | + cbm_git_context_free(&ctx); |
| 91 | + th_rmtree(tmp); |
| 92 | + PASS(); |
| 93 | +#endif /* _WIN32 */ |
| 94 | +} |
| 95 | + |
| 96 | +/* ── canonical_root: indexed from a subdirectory (issue #659) ───── |
| 97 | + * THE reproduce-first guard: from a subdir, --git-common-dir is relative, so the |
| 98 | + * unfixed derive_canonical_root joins it against worktree_root and strips "/.git" |
| 99 | + * textually, leaving canonical_root = "<root>/subdir/.." (or "<root>/..") instead |
| 100 | + * of "<root>". Verified RED on the unfixed code, GREEN with the realpath fix. */ |
| 101 | + |
| 102 | +TEST(canonical_root_subdir) { |
| 103 | +#ifdef _WIN32 |
| 104 | + SKIP_PLATFORM("git-based canonical_root test not supported on Windows CI"); |
| 105 | +#else |
| 106 | + char *tmp = th_mktempdir("cbm_gitctx"); |
| 107 | + if (!tmp) FAIL("th_mktempdir returned NULL"); |
| 108 | + |
| 109 | + if (make_git_repo(tmp) != 0) { |
| 110 | + th_rmtree(tmp); |
| 111 | + SKIP_PLATFORM("git not available to init a repo"); |
| 112 | + } |
| 113 | + |
| 114 | + /* Create a subdirectory inside the repo. */ |
| 115 | + char subdir[1024]; |
| 116 | + snprintf(subdir, sizeof(subdir), "%s/scripts", tmp); |
| 117 | + if (th_mkdir_p(subdir) != 0) { |
| 118 | + th_rmtree(tmp); |
| 119 | + FAIL("failed to create subdir"); |
| 120 | + } |
| 121 | + |
| 122 | + cbm_git_context_t ctx = {0}; |
| 123 | + int rc = cbm_git_context_resolve(subdir, &ctx); |
| 124 | + if (rc != 0 || !ctx.is_git) { |
| 125 | + cbm_git_context_free(&ctx); |
| 126 | + th_rmtree(tmp); |
| 127 | + FAIL("cbm_git_context_resolve on subdir failed or not a git repo"); |
| 128 | + } |
| 129 | + |
| 130 | + /* canonical_root must equal the repo root, NOT "<repo>/.." or "<subdir>/..". */ |
| 131 | + char expected[4096]; |
| 132 | + if (realpath(tmp, expected) == NULL) { |
| 133 | + cbm_git_context_free(&ctx); |
| 134 | + th_rmtree(tmp); |
| 135 | + FAIL("realpath(tmp) failed"); |
| 136 | + } |
| 137 | + |
| 138 | + ASSERT_STR_EQ(ctx.canonical_root, expected); |
| 139 | + |
| 140 | + /* Sanity: canonical_root must not contain ".." or end with a slash. */ |
| 141 | + ASSERT(strstr(ctx.canonical_root, "..") == NULL); |
| 142 | + ASSERT(ctx.canonical_root[strlen(ctx.canonical_root) - 1] != '/'); |
| 143 | + |
| 144 | + cbm_git_context_free(&ctx); |
| 145 | + th_rmtree(tmp); |
| 146 | + PASS(); |
| 147 | +#endif /* _WIN32 */ |
| 148 | +} |
| 149 | + |
| 150 | +/* ── canonical_root: linked git worktree (supporting invariant) ──── |
| 151 | + * NOT the #659 reproducer on modern git: git that emits an *absolute* |
| 152 | + * --git-common-dir for a linked worktree (e.g. 2.48.x) takes the path_is_absolute |
| 153 | + * branch, so the bug does not manifest and this passes with or without the fix. |
| 154 | + * It is kept as an invariant — canonical_root of a linked worktree must equal the |
| 155 | + * MAIN repo root (never the worktree root or its parent) — and would fail on a git |
| 156 | + * build that emits a *relative* worktree common-dir. The genuine RED-without-fix |
| 157 | + * guard for #659 is canonical_root_subdir above. */ |
| 158 | + |
| 159 | +TEST(canonical_root_linked_worktree) { |
| 160 | +#ifdef _WIN32 |
| 161 | + SKIP_PLATFORM("git worktree test not implemented for Windows"); |
| 162 | +#else |
| 163 | + /* th_mktempdir() returns a static buffer — copy before the second call. */ |
| 164 | + char main_tmp[256]; |
| 165 | + char *raw = th_mktempdir("cbm_main"); |
| 166 | + if (!raw) FAIL("th_mktempdir returned NULL"); |
| 167 | + strncpy(main_tmp, raw, sizeof(main_tmp) - 1); |
| 168 | + main_tmp[sizeof(main_tmp) - 1] = '\0'; |
| 169 | + |
| 170 | + char wt_tmp[256]; |
| 171 | + raw = th_mktempdir("cbm_worktree"); |
| 172 | + if (!raw) FAIL("th_mktempdir returned NULL"); |
| 173 | + strncpy(wt_tmp, raw, sizeof(wt_tmp) - 1); |
| 174 | + wt_tmp[sizeof(wt_tmp) - 1] = '\0'; |
| 175 | + |
| 176 | + /* Remove the worktree dir first — git worktree add creates it. */ |
| 177 | + th_rmtree(wt_tmp); |
| 178 | + |
| 179 | + if (make_git_repo(main_tmp) != 0) { |
| 180 | + th_rmtree(main_tmp); |
| 181 | + SKIP_PLATFORM("git not available to init a repo"); |
| 182 | + } |
| 183 | + |
| 184 | + /* Create a branch for the worktree. */ |
| 185 | + if (git_run(main_tmp, "branch wt-branch") != 0) { |
| 186 | + th_rmtree(main_tmp); |
| 187 | + FAIL("failed to create branch for worktree"); |
| 188 | + } |
| 189 | + |
| 190 | + /* Add a linked worktree. */ |
| 191 | + char wt_cmd[1024]; |
| 192 | + snprintf(wt_cmd, sizeof(wt_cmd), "worktree add \"%s\" wt-branch", wt_tmp); |
| 193 | + if (git_run(main_tmp, wt_cmd) != 0) { |
| 194 | + th_rmtree(wt_tmp); |
| 195 | + th_rmtree(main_tmp); |
| 196 | + SKIP_PLATFORM("git worktree add unavailable (git 2.5+ required)"); |
| 197 | + } |
| 198 | + |
| 199 | + cbm_git_context_t ctx = {0}; |
| 200 | + int rc = cbm_git_context_resolve(wt_tmp, &ctx); |
| 201 | + if (rc != 0 || !ctx.is_git) { |
| 202 | + cbm_git_context_free(&ctx); |
| 203 | + git_run(main_tmp, "worktree prune"); |
| 204 | + th_rmtree(main_tmp); |
| 205 | + th_rmtree(wt_tmp); |
| 206 | + FAIL("cbm_git_context_resolve on linked worktree failed"); |
| 207 | + } |
| 208 | + |
| 209 | + /* canonical_root must be the MAIN repo root, not the worktree root or its parent. */ |
| 210 | + char expected[4096]; |
| 211 | + if (realpath(main_tmp, expected) == NULL) { |
| 212 | + cbm_git_context_free(&ctx); |
| 213 | + git_run(main_tmp, "worktree prune"); |
| 214 | + th_rmtree(main_tmp); |
| 215 | + th_rmtree(wt_tmp); |
| 216 | + FAIL("realpath(main_tmp) failed"); |
| 217 | + } |
| 218 | + |
| 219 | + ASSERT_STR_EQ(ctx.canonical_root, expected); |
| 220 | + ASSERT(strstr(ctx.canonical_root, "..") == NULL); |
| 221 | + |
| 222 | + cbm_git_context_free(&ctx); |
| 223 | + git_run(main_tmp, "worktree prune"); |
| 224 | + th_rmtree(main_tmp); |
| 225 | + th_rmtree(wt_tmp); |
| 226 | + PASS(); |
| 227 | +#endif /* _WIN32 */ |
| 228 | +} |
| 229 | + |
| 230 | +/* ── Suite ──────────────────────────────────────────────────────── */ |
| 231 | + |
| 232 | +SUITE(git_context) { |
| 233 | + RUN_TEST(canonical_root_repo_root); |
| 234 | + RUN_TEST(canonical_root_subdir); |
| 235 | + RUN_TEST(canonical_root_linked_worktree); |
| 236 | +} |
0 commit comments