1use std::path::Path;
2use std::path::PathBuf;
3
4use color_eyre::eyre::Context as _;
5use color_eyre::eyre::ContextCompat as _;
6use color_eyre::eyre::eyre;
7use git2::Repository;
8
9pub fn discover(path: &Path) -> color_eyre::Result<Repository> {
15 Repository::discover(path).wrap_err_with(|| eyre!("error discovering repo | path={}", path.display()))
16}
17
18pub fn get_root(repo: &Repository) -> PathBuf {
23 repo.commondir()
24 .components()
25 .filter(|c| c.as_os_str() != ".git")
26 .collect()
27}
28
29pub fn get_relative_path_to_repo(path: &Path, repo: &Repository) -> color_eyre::Result<PathBuf> {
35 let repo_workdir = repo.workdir().wrap_err_with(|| {
36 format!(
37 "error getting repository working directory | repo={:?}",
38 repo.path().display()
39 )
40 })?;
41 Ok(Path::new("/").join(path.strip_prefix(repo_workdir)?))
42}