use std::{
env, fs,
fs::File,
io,
io::Read,
path::{Path, PathBuf},
};
pub fn rerun_if_git_head_changed() {
let mut manifest_dir = PathBuf::from(
env::var("CARGO_MANIFEST_DIR").expect("`CARGO_MANIFEST_DIR` is always set by cargo."),
);
let manifest_dir_copy = manifest_dir.clone();
while manifest_dir.parent().is_some() {
match get_git_paths(&manifest_dir) {
Err(err) => {
eprintln!("cargo:warning=Unable to read the Git repository: {}", err);
return
},
Ok(None) => {},
Ok(Some(paths)) => {
for p in paths {
println!("cargo:rerun-if-changed={}", p.display());
}
return
},
}
manifest_dir.pop();
}
println!(
"cargo:warning=Could not find `.git/HEAD` searching from `{}` upwards!",
manifest_dir_copy.display(),
);
}
fn get_git_paths(path: &Path) -> Result<Option<Vec<PathBuf>>, io::Error> {
let git_dir_or_file = path.join(".git");
if let Ok(metadata) = fs::metadata(&git_dir_or_file) {
if metadata.is_dir() {
let git_head_path = git_dir_or_file.join("HEAD");
let mut f = File::open(&git_head_path)?;
let mut git_head_contents = String::new();
let _ = f.read_to_string(&mut git_head_contents)?;
let ref_vec: Vec<&str> = git_head_contents.split(": ").collect();
if ref_vec.len() == 2 {
let current_head_file = ref_vec[1];
let git_refs_path = git_dir_or_file.join(current_head_file);
Ok(Some(vec![git_head_path, git_refs_path]))
} else {
Err(io::Error::new(
io::ErrorKind::Other,
"You are most likely in a detached HEAD state",
))
}
} else if metadata.is_file() {
let mut git_file = File::open(&git_dir_or_file)?;
let mut git_contents = String::new();
let _ = git_file.read_to_string(&mut git_contents)?;
let dir_vec: Vec<&str> = git_contents.split(": ").collect();
let git_path = dir_vec[1].trim();
let git_head_path = PathBuf::from(git_path).join("HEAD");
let mut actual_git_dir = PathBuf::from(git_path);
actual_git_dir.pop();
actual_git_dir.pop();
let mut f = File::open(&git_head_path)?;
let mut git_head_contents = String::new();
let _ = f.read_to_string(&mut git_head_contents)?;
let ref_vec: Vec<&str> = git_head_contents.split(": ").collect();
if ref_vec.len() == 2 {
let current_head_file = ref_vec[1];
let git_refs_path = actual_git_dir.join(current_head_file);
Ok(Some(vec![git_head_path, git_refs_path]))
} else {
Err(io::Error::new(
io::ErrorKind::Other,
"You are most likely in a detached HEAD state",
))
}
} else {
Err(io::Error::new(
io::ErrorKind::Other,
"Invalid .git format (Not a directory or a file)",
))
}
} else {
Ok(None)
}
}