Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 40 additions & 31 deletions cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ 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() {
// In colocated repo, .git/HEAD should reflect the working-copy parent.
println!("cargo:rerun-if-changed={GIT_HEAD_PATH}");
} else if Path::new(JJ_OP_HEADS_PATH).exists() {
// op_heads changes when working-copy files are mutated, which is way more
// frequent than .git/HEAD.
println!("cargo:rerun-if-changed={JJ_OP_HEADS_PATH}");
}
println!("cargo:rerun-if-env-changed=NIX_JJ_GIT_HASH");
let git_hash = get_git_hash_from_nix().or_else(|| {
if Path::new(GIT_HEAD_PATH).exists() {
// In colocated repo, .git/HEAD should reflect the working-copy parent.
println!("cargo:rerun-if-changed={GIT_HEAD_PATH}");
} else if Path::new(JJ_OP_HEADS_PATH).exists() {
// op_heads changes when working-copy files are mutated, which is way more
// frequent than .git/HEAD.
println!("cargo:rerun-if-changed={JJ_OP_HEADS_PATH}");
}
get_git_hash_from_jj().or_else(get_git_hash_from_git)
});

if let Some(git_hash) = get_git_hash() {
if let Some(git_hash) = git_hash {
println!("cargo:rustc-env=JJ_VERSION={version}-{git_hash}");
} else {
println!("cargo:rustc-env=JJ_VERSION={version}");
Expand All @@ -47,14 +50,14 @@ fn main() {
}
}

fn get_git_hash() -> Option<String> {
if let Some(nix_hash) = std::env::var("NIX_JJ_GIT_HASH")
fn get_git_hash_from_nix() -> Option<String> {
std::env::var("NIX_JJ_GIT_HASH")
.ok()
.filter(|s| !s.is_empty())
{
return Some(nix_hash);
}
if let Ok(output) = Command::new("jj")
}

fn get_git_hash_from_jj() -> Option<String> {
Command::new("jj")
.args([
"--ignore-working-copy",
"--color=never",
Expand All @@ -64,21 +67,27 @@ fn get_git_hash() -> Option<String> {
"-T=commit_id ++ '-'",
])
.output()
&& output.status.success()
{
let mut parent_commits = String::from_utf8(output.stdout).unwrap();
// If a development version of `jj` is compiled at a merge commit, this will
// result in several commit ids separated by `-`s.
parent_commits.truncate(parent_commits.trim_end_matches('-').len());
return Some(parent_commits);
}

if let Ok(output) = Command::new("git").args(["rev-parse", "HEAD"]).output()
&& output.status.success()
{
let line = str::from_utf8(&output.stdout).unwrap();
return Some(line.trim_end().to_owned());
}
.ok()
.filter(|output| output.status.success())
.map(|output| {
let mut parent_commits = String::from_utf8(output.stdout).unwrap();
// If a development version of `jj` is compiled at a merge commit, this will
// result in several commit ids separated by `-`s.
parent_commits.truncate(parent_commits.trim_end_matches('-').len());
parent_commits
})
}

None
fn get_git_hash_from_git() -> Option<String> {
Command::new("git")
.args(["rev-parse", "HEAD"])
.output()
.ok()
.filter(|output| output.status.success())
.map(|output| {
str::from_utf8(&output.stdout)
.unwrap()
.trim_end()
.to_owned()
})
}