use std::path::Path;
use std::process::Command;
use std::str;
const GIT_HEAD_PATH: &str = "../.git/HEAD";
const JJ_OP_HEADS_PATH: &str = "../.jj/repo/op_heads/heads";
fn main() {
let version = std::env::var("CARGO_PKG_VERSION").unwrap();
if Path::new(GIT_HEAD_PATH).exists() {
println!("cargo:rerun-if-changed={GIT_HEAD_PATH}");
} else if Path::new(JJ_OP_HEADS_PATH).exists() {
println!("cargo:rerun-if-changed={JJ_OP_HEADS_PATH}");
}
println!("cargo:rerun-if-env-changed=NIX_JJ_GIT_HASH");
if let Some(git_hash) = get_git_hash() {
println!("cargo:rustc-env=JJ_VERSION={version}-{git_hash}");
} else {
println!("cargo:rustc-env=JJ_VERSION={version}");
}
let docs_symlink_path = Path::new("docs");
println!("cargo:rerun-if-changed={}", docs_symlink_path.display());
if docs_symlink_path.join("index.md").exists() {
println!("cargo:rustc-env=JJ_DOCS_DIR=docs/");
} else {
println!("cargo:rustc-env=JJ_DOCS_DIR=../docs/");
}
}
fn get_git_hash() -> Option<String> {
if let Some(nix_hash) = std::env::var("NIX_JJ_GIT_HASH")
.ok()
.filter(|s| !s.is_empty())
{
return Some(nix_hash);
}
if let Ok(output) = Command::new("jj")
.args([
"--ignore-working-copy",
"--color=never",
"log",
"--no-graph",
"-r=@-",
"-T=commit_id",
])
.output()
{
if output.status.success() {
return Some(String::from_utf8(output.stdout).unwrap());
}
}
if let Ok(output) = Command::new("git").args(["rev-parse", "HEAD"]).output() {
if output.status.success() {
let line = str::from_utf8(&output.stdout).unwrap();
return Some(line.trim_end().to_owned());
}
}
None
}