From 0153579a4c9efe655f098d5d5723bd1c55eab1bc Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Mon, 23 Dec 2024 15:03:40 +0100 Subject: [PATCH] Fix build in non-colocated jj workspaces --- build.rs | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/build.rs b/build.rs index 983b3d382..a4f5c906d 100644 --- a/build.rs +++ b/build.rs @@ -317,16 +317,35 @@ fn get_version(src_dir: &Path) -> String { // So we read the SHA ourselves. fn get_git_hash() -> Result> { let gitdir = Path::new(env!("CARGO_MANIFEST_DIR")).join(".git"); + let jjdir = Path::new(env!("CARGO_MANIFEST_DIR")).join(".jj"); + let commit_id = if gitdir.exists() { + // .git/HEAD contains ref: refs/heads/branch + let headpath = gitdir.join("HEAD"); + let headstr = read_to_string(headpath)?; + let headref = headstr.split(' ').collect::>()[1].trim(); - // .git/HEAD contains ref: refs/heads/branch - let headpath = gitdir.join("HEAD"); - let headstr = read_to_string(headpath)?; - let headref = headstr.split(' ').collect::>()[1].trim(); - - // .git/refs/heads/branch contains the SHA - let refpath = gitdir.join(headref); - // Shorten to 9 characters (what git describe does currently) - let refstr = &read_to_string(refpath)?[0..9]; + // .git/refs/heads/branch contains the SHA + let refpath = gitdir.join(headref); + // Shorten to 9 characters (what git describe does currently) + read_to_string(refpath)? + } else if jjdir.exists() { + let output = Command::new("jj") + .args([ + "log", + "--revisions", + "@", + "--no-graph", + "--ignore-working-copy", + "--template", + "commit_id", + ]) + .output() + .unwrap(); + String::from_utf8_lossy(&output.stdout).to_string() + } else { + return Err("did not find either of .git or .jj".into()); + }; + let refstr = &commit_id[0..9]; let refstr = refstr.trim(); let version = env!("CARGO_PKG_VERSION").to_owned();