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
| 1496 | * .git/info/exclude and core.excludesfile were silently dropped because the old |
| 1497 | * check required .git to be a directory (issue #489 only covered ordinary repos). */ |
| 1498 | static bool resolve_git_common_dir(const char *repo_path, char *common_dir, size_t cd_sz) { |
| 1499 | char dot_git[CBM_SZ_4K]; |
| 1500 | snprintf(dot_git, sizeof(dot_git), "%s/.git", repo_path); |
| 1501 | struct stat st; |
| 1502 | if (wide_stat(dot_git, &st) != 0) { |
| 1503 | return false; |
| 1504 | } |
| 1505 | if (S_ISDIR(st.st_mode)) { |
| 1506 | snprintf(common_dir, cd_sz, "%s", dot_git); |
| 1507 | cbm_normalize_path_sep(common_dir); |
| 1508 | return true; |
| 1509 | } |
| 1510 | if (!S_ISREG(st.st_mode)) { |
| 1511 | return false; |
| 1512 | } |
| 1513 | |
| 1514 | /* Linked worktree: parse "gitdir: <path>" from the gitlink file. |
| 1515 | * cbm_fopen (not raw fopen) so non-ASCII repo paths open on Windows. */ |
| 1516 | FILE *f = cbm_fopen(dot_git, "r"); |
| 1517 | if (!f) { |
| 1518 | return false; |
| 1519 | } |
| 1520 | char git_dir[CBM_SZ_4K]; |
| 1521 | bool got_git_dir = false; |
| 1522 | char line[CBM_SZ_4K]; |
| 1523 | while (fgets(line, sizeof(line), f)) { |
| 1524 | char *gs = trim_ws(line); |
| 1525 | if (strncmp(gs, "gitdir:", 7) == 0) { |
| 1526 | char *val = trim_ws(gs + 7); |
| 1527 | if (val[0] != '\0') { |
| 1528 | if (discover_path_is_absolute(val)) { |
| 1529 | snprintf(git_dir, sizeof(git_dir), "%s", val); |
| 1530 | cbm_normalize_path_sep(git_dir); |
| 1531 | } else { |
| 1532 | path_join(git_dir, sizeof(git_dir), repo_path, val); |
| 1533 | } |
| 1534 | got_git_dir = true; |
| 1535 | } |
| 1536 | break; |
| 1537 | } |
| 1538 | } |
| 1539 | fclose(f); |
| 1540 | if (!got_git_dir) { |
| 1541 | return false; |
| 1542 | } |
| 1543 | |
| 1544 | /* The shared dir holding info/exclude + config is named in <git_dir>/commondir |
| 1545 | * (typically a relative path like "../.."). Absent in single-worktree gitdirs. */ |
| 1546 | char commondir_path[CBM_SZ_4K]; |
| 1547 | path_join(commondir_path, sizeof(commondir_path), git_dir, "commondir"); |
| 1548 | FILE *cf = cbm_fopen(commondir_path, "r"); |
| 1549 | if (cf) { |
| 1550 | char cbuf[CBM_SZ_4K]; |
| 1551 | bool resolved = false; |
| 1552 | if (fgets(cbuf, sizeof(cbuf), cf)) { |
| 1553 | char *cs = trim_ws(cbuf); |
| 1554 | if (cs[0] != '\0') { |
| 1555 | if (discover_path_is_absolute(cs)) { |
no test coverage detected