diff options
author | Michael Gattozzi <mgattozzi@gmail.com> | 2019-11-21 10:59:44 -0500 |
---|---|---|
committer | Michael Gattozzi <mgattozzi@gmail.com> | 2019-11-21 14:21:07 -0500 |
commit | 2cd27b8dd40c7361f6f2b8832e210a58f30f3639 (patch) | |
tree | f52f9c39570e227f58ee498c3943e50d2e3f732c /shared | |
parent | 5cec4e3f5c551731d1202d892da570ad62d9682d (diff) |
Move find_root function into the new shared crate
This cleans up the init function using the modified find_root function
for ticket and moves it into a new shared crate so that other tools that
might be built can use it. This means we can easily find the root of
a git repo no matter where in the repo one is and build paths relative
to it.
Closes #3
Diffstat (limited to 'shared')
-rw-r--r-- | shared/Cargo.toml | 10 | ||||
-rw-r--r-- | shared/src/lib.rs | 23 |
2 files changed, 33 insertions, 0 deletions
diff --git a/shared/Cargo.toml b/shared/Cargo.toml new file mode 100644 index 0000000..a072d12 --- /dev/null +++ b/shared/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "shared" +version = "0.1.0" +authors = ["Michael Gattozzi <mgattozzi@gmail.com>"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0" diff --git a/shared/src/lib.rs b/shared/src/lib.rs new file mode 100644 index 0000000..9fdcc27 --- /dev/null +++ b/shared/src/lib.rs @@ -0,0 +1,23 @@ +use anyhow::{bail, Result}; +use std::{env, path::PathBuf}; + +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() { + loc.pop(); + found_root = true; + location = loc.canonicalize()?; + break; + } + } + + if found_root { + Ok(location) + } else { + bail!("Unable to find a valid git repo"); + } +} |