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

Function cbm_git_context_resolve

src/git/git_context.c:242–305  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

240}
241
242int cbm_git_context_resolve(const char *path, cbm_git_context_t *out) {
243 if (!out) {
244 return CBM_NOT_FOUND;
245 }
246
247 memset(out, 0, sizeof(*out));
248 if (!path || !path[0]) {
249 return CBM_NOT_FOUND;
250 }
251
252 out->input_path = git_strdup(path);
253 if (!out->input_path) {
254 return CBM_NOT_FOUND;
255 }
256
257 struct stat st;
258 out->root_exists = (stat(path, &st) == 0);
259 if (!out->root_exists) {
260 return 0;
261 }
262
263 if (git_capture(path, "rev-parse --show-toplevel", &out->worktree_root) != 0) {
264 out->is_git = false;
265 return 0;
266 }
267 out->is_git = true;
268
269 if (git_capture(path, "rev-parse --git-dir", &out->git_dir) != 0) {
270 out->git_dir = git_strdup("");
271 }
272 if (git_capture(path, "rev-parse --git-common-dir", &out->git_common_dir) != 0) {
273 out->git_common_dir = git_strdup("");
274 }
275 if (git_capture(path, "rev-parse --verify HEAD", &out->head_sha) != 0) {
276 out->head_sha = git_strdup("");
277 }
278
279 if (git_capture(path, "symbolic-ref --quiet --short HEAD", &out->branch) != 0) {
280 out->branch = git_strdup("DETACHED");
281 out->is_detached = true;
282 }
283
284 out->is_worktree =
285 out->git_dir && out->git_common_dir && strcmp(out->git_dir, out->git_common_dir) != 0;
286 /* git 2.31+ canonical absolute common-dir (best-effort; NULL on older git,
287 * where derive_canonical_root falls back to the relative common-dir). */
288 char *abs_common_dir = NULL;
289 (void)git_capture(path, "rev-parse --path-format=absolute --git-common-dir", &abs_common_dir);
290 out->canonical_root =
291 derive_canonical_root(path, out->worktree_root, out->git_common_dir, abs_common_dir);
292 free(abs_common_dir);
293 out->branch_slug = slug_from_branch(out->branch, out->is_detached);
294 if (git_capture(path, "merge-base HEAD @{upstream}", &out->base_sha) != 0) {
295 out->base_sha = git_strdup("");
296 }
297
298 if (!out->git_dir || !out->git_common_dir || !out->head_sha || !out->branch ||
299 !out->canonical_root || !out->branch_slug || !out->base_sha) {

Callers 9

add_git_context_jsonFunction · 0.85
build_project_json_entryFunction · 0.85
handle_repo_infoFunction · 0.85
cbm_pipeline_newFunction · 0.85
test_git_context.cFile · 0.85
th_gitctx_probe_threadFunction · 0.85
test_pipeline.cFile · 0.85

Calls 6

git_strdupFunction · 0.85
git_captureFunction · 0.85
derive_canonical_rootFunction · 0.85
slug_from_branchFunction · 0.85
cbm_git_context_freeFunction · 0.85
statClass · 0.70

Tested by 1

th_gitctx_probe_threadFunction · 0.68