aboutsummaryrefslogtreecommitdiff
path: root/shared/src/lib.rs
blob: db28822a80e0501d93ead045100b486539f27715 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//! Common functionality needed in many of the tools

use anyhow::{
  bail,
  Result,
};
use std::{
  env,
  path::PathBuf,
};

/// Finds the top level folder of the repo and returns it's canonicalized path
pub fn find_root() -> Result<PathBuf> {
  let mut location = env::current_dir()?;
  let mut found_root = false;

  for loc in location.ancestors() {
    let mut loc = loc.join(".git");
    if loc.exists() {
      let _ = loc.pop();
      found_root = true;
      location = loc.canonicalize()?;
      break;
    }
  }

  if found_root {
    Ok(location)
  } else {
    bail!("Unable to find a valid git repo");
  }
}