Canonicalize paths for git (fixes #39)
This commit is contained in:
35
src/git.rs
35
src/git.rs
@@ -1,6 +1,7 @@
|
|||||||
use super::utils::{which, Check};
|
use super::utils::{which, Check};
|
||||||
use failure::Error;
|
use failure::Error;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
use std::io;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
@@ -19,24 +20,30 @@ impl Git {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_repo_root<P: AsRef<Path>>(&self, path: P) -> Option<String> {
|
pub fn get_repo_root<P: AsRef<Path>>(&self, path: P) -> Option<String> {
|
||||||
if !path.as_ref().exists() {
|
match path.as_ref().canonicalize() {
|
||||||
return None;
|
Ok(path) => {
|
||||||
}
|
debug_assert!(path.exists());
|
||||||
|
|
||||||
if let Some(git) = &self.git {
|
if let Some(git) = &self.git {
|
||||||
let output = Command::new(&git)
|
let output = Command::new(&git)
|
||||||
.arg("rev-parse")
|
.arg("rev-parse")
|
||||||
.arg("--show-toplevel")
|
.arg("--show-toplevel")
|
||||||
.current_dir(path)
|
.current_dir(path)
|
||||||
.output();
|
.output();
|
||||||
|
|
||||||
if let Ok(output) = output {
|
if let Ok(output) = output {
|
||||||
if !output.status.success() {
|
if !output.status.success() {
|
||||||
return None;
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Some(String::from_utf8_lossy(&output.stdout).trim().to_string());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Some(String::from_utf8_lossy(&output.stdout).trim().to_string());
|
|
||||||
}
|
}
|
||||||
|
Err(e) => match e.kind() {
|
||||||
|
io::ErrorKind::NotFound => debug!("{} does not exists", path.as_ref().display()),
|
||||||
|
_ => error!("Error looking for {}: {}", path.as_ref().display(), e),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
|
|||||||
Reference in New Issue
Block a user