Skip to main content

workdir_root

Function workdir_root 

Source
pub fn workdir_root(path: &Path) -> Option<PathBuf>
Available on crate feature vcs-git only.
Expand description

Discover the working-tree root of the repository containing path.

Returns the canonicalised work-tree directory of the repository that encloses path (a file or directory), or None when path is not inside a repository or the repository is bare (no work tree). Repository discovery walks upward from path the same way git itself does.

Front ends use this to coalesce a batch of files onto the repository each belongs to — two files in different subdirectories of the same checkout resolve to the same root, so a per-repo HistoryIndex (or PerFunctionBlame engine) can be built once and shared across them rather than rebuilt per directory (issue #670).

use std::path::Path;
// Two files in different subdirectories of the *same* checkout both
// resolve to that checkout's work-tree root: `workdir_root` walks
// upward from each file and lands on the same `Some(root)`. (Shown
// with absolute paths under a real checkout; `no_run` because no such
// repository exists at doctest time.)
let a = big_code_analysis::vcs::workdir_root(Path::new("/checkout/src/a.rs"));
let b = big_code_analysis::vcs::workdir_root(Path::new("/checkout/tests/b.rs"));
// When `/checkout` is a git work tree, both are `Some("/checkout")`, so a
// per-repo index can be built once and shared across the batch.
if let (Some(root_a), Some(root_b)) = (a, b) {
    assert_eq!(root_a, root_b);
}