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

Function resolve_git_common_dir

src/discover/discover.c:1019–1094  ·  view source on GitHub ↗

Resolve the shared "common" git directory for repo_path. * Handles three layouts: * 1. /.git is a directory - ordinary repo; common_dir == /.git * 2. /.git is a regular file - linked worktree gitlink "gitdir: "; * the common dir is read from /commondir (git stores info/exclude + * config there, shared across worktrees). Falls back to git_dir w

Source from the content-addressed store, hash-verified

1017 * .git/info/exclude and core.excludesfile were silently dropped because the old
1018 * check required .git to be a directory (issue #489 only covered ordinary repos). */
1019static bool resolve_git_common_dir(const char *repo_path, char *common_dir, size_t cd_sz) {
1020 char dot_git[CBM_SZ_4K];
1021 snprintf(dot_git, sizeof(dot_git), "%s/.git", repo_path);
1022 struct stat st;
1023 if (wide_stat(dot_git, &st) != 0) {
1024 return false;
1025 }
1026 if (S_ISDIR(st.st_mode)) {
1027 snprintf(common_dir, cd_sz, "%s", dot_git);
1028 cbm_normalize_path_sep(common_dir);
1029 return true;
1030 }
1031 if (!S_ISREG(st.st_mode)) {
1032 return false;
1033 }
1034
1035 /* Linked worktree: parse "gitdir: <path>" from the gitlink file.
1036 * cbm_fopen (not raw fopen) so non-ASCII repo paths open on Windows. */
1037 FILE *f = cbm_fopen(dot_git, "r");
1038 if (!f) {
1039 return false;
1040 }
1041 char git_dir[CBM_SZ_4K];
1042 bool got_git_dir = false;
1043 char line[CBM_SZ_4K];
1044 while (fgets(line, sizeof(line), f)) {
1045 char *gs = trim_ws(line);
1046 if (strncmp(gs, "gitdir:", 7) == 0) {
1047 char *val = trim_ws(gs + 7);
1048 if (val[0] != '\0') {
1049 if (discover_path_is_absolute(val)) {
1050 snprintf(git_dir, sizeof(git_dir), "%s", val);
1051 cbm_normalize_path_sep(git_dir);
1052 } else {
1053 path_join(git_dir, sizeof(git_dir), repo_path, val);
1054 }
1055 got_git_dir = true;
1056 }
1057 break;
1058 }
1059 }
1060 fclose(f);
1061 if (!got_git_dir) {
1062 return false;
1063 }
1064
1065 /* The shared dir holding info/exclude + config is named in <git_dir>/commondir
1066 * (typically a relative path like "../.."). Absent in single-worktree gitdirs. */
1067 char commondir_path[CBM_SZ_4K];
1068 path_join(commondir_path, sizeof(commondir_path), git_dir, "commondir");
1069 FILE *cf = cbm_fopen(commondir_path, "r");
1070 if (cf) {
1071 char cbuf[CBM_SZ_4K];
1072 bool resolved = false;
1073 if (fgets(cbuf, sizeof(cbuf), cf)) {
1074 char *cs = trim_ws(cbuf);
1075 if (cs[0] != '\0') {
1076 if (discover_path_is_absolute(cs)) {

Callers 1

discover_implFunction · 0.85

Calls 6

wide_statFunction · 0.85
cbm_normalize_path_sepFunction · 0.85
cbm_fopenFunction · 0.85
trim_wsFunction · 0.85
path_joinFunction · 0.85

Tested by

no test coverage detected