Skip to main content

worktree_root_for_git_dir

Function worktree_root_for_git_dir 

Source
pub fn worktree_root_for_git_dir(git_dir: &Path) -> Result<Option<PathBuf>>
Expand description

Resolve the working-tree root for a repository identified by its git directory, returning Ok(None) for a bare repository.

This is the repository-intrinsic worktree resolution (it does not consult GIT_WORK_TREE/GIT_DIR or CLI overrides — those are the caller’s job):

  1. for a linked worktree (a git directory that has both a commondir and a gitdir administrative file), the directory containing the worktree’s .git link, canonicalised;
  2. otherwise, if core.bare is true the repository is bare and Ok(None) is returned immediately — core.bare takes precedence for the main repo, so a bare repo ignores core.worktree and the .git-parent fallback;
  3. otherwise, a core.worktree setting in <git_dir>/config (absolute, or relative to the git directory), canonicalised;
  4. otherwise, when the git directory is a .git directory, its parent (the ordinary non-bare layout) — returned verbatim, not canonicalised;
  5. otherwise the repository is bare and Ok(None) is returned.

Ok(None) means specifically “bare” (case 0 or case 4). A GitError::Io is returned if a path that should exist cannot be canonicalised, and a GitError::InvalidPath if a .git directory has no parent (a malformed layout).