MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / derive_canonical_root

Function derive_canonical_root

src/git/git_context.c:160–212  ·  view source on GitHub ↗

Derive the canonical repo root. * * Preferred (git 2.31+): abs_common_dir is `git rev-parse --path-format=absolute * --git-common-dir` — git's OWN absolute, canonical common-dir. Because a main * repo and its linked worktree both ask the same git binary, they resolve to the * IDENTICAL path, so canonical_root is consistent across worktrees AND platforms * with no manual join or realpath/_ful

Source from the content-addressed store, hash-verified

158 * common-dir so this branch isn't reached in practice, and _fullpath there
159 * reintroduces the msys divergence. */
160static char *derive_canonical_root(const char *input_path, const char *worktree_root,
161 const char *git_common_dir, const char *abs_common_dir) {
162 char *root = NULL;
163 if (abs_common_dir && abs_common_dir[0] && path_is_absolute(abs_common_dir)) {
164 root = git_strdup(abs_common_dir);
165 if (!root) {
166 return NULL;
167 }
168 } else {
169 const char *src = git_common_dir && git_common_dir[0] ? git_common_dir : worktree_root;
170 if (!src) {
171 return git_strdup("");
172 }
173#ifndef _WIN32
174 root = path_is_absolute(src) ? git_strdup(src) : join_root_relative(input_path, src);
175 if (!root) {
176 return NULL;
177 }
178 {
179 char resolved[4096];
180 if (realpath(root, resolved) != NULL) {
181 free(root);
182 root = git_strdup(resolved);
183 if (!root) {
184 return NULL;
185 }
186 }
187 }
188#else
189 (void)input_path;
190 root = path_is_absolute(src) ? git_strdup(src) : join_root_relative(worktree_root, src);
191 if (!root) {
192 return NULL;
193 }
194#endif
195 }
196
197 size_t len = strlen(root);
198 while (len > 1 && (root[len - 1] == '/' || root[len - 1] == '\\')) {
199 root[--len] = '\0';
200 }
201
202 if (len >= 5 && strcmp(root + len - 5, "/.git") == 0) {
203 root[len - 5] = '\0';
204 }
205#ifdef _WIN32
206 else if (len >= 5 && strcmp(root + len - 5, "\\.git") == 0) {
207 root[len - 5] = '\0';
208 }
209#endif
210
211 return root;
212}
213
214static char *slug_from_branch(const char *branch, bool detached) {
215 const char *fallback = detached ? "detached" : "working-tree";

Callers 1

cbm_git_context_resolveFunction · 0.85

Calls 3

path_is_absoluteFunction · 0.85
git_strdupFunction · 0.85
join_root_relativeFunction · 0.85

Tested by

no test coverage detected