Fix build in non-colocated jj workspaces

This commit is contained in:
Johannes Altmanninger 2024-12-23 15:03:40 +01:00
parent c1b460525c
commit 0153579a4c

View file

@ -317,7 +317,8 @@ fn get_version(src_dir: &Path) -> String {
// So we read the SHA ourselves. // So we read the SHA ourselves.
fn get_git_hash() -> Result<String, Box<dyn std::error::Error>> { fn get_git_hash() -> Result<String, Box<dyn std::error::Error>> {
let gitdir = Path::new(env!("CARGO_MANIFEST_DIR")).join(".git"); 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 // .git/HEAD contains ref: refs/heads/branch
let headpath = gitdir.join("HEAD"); let headpath = gitdir.join("HEAD");
let headstr = read_to_string(headpath)?; let headstr = read_to_string(headpath)?;
@ -326,7 +327,25 @@ fn get_version(src_dir: &Path) -> String {
// .git/refs/heads/branch contains the SHA // .git/refs/heads/branch contains the SHA
let refpath = gitdir.join(headref); let refpath = gitdir.join(headref);
// Shorten to 9 characters (what git describe does currently) // Shorten to 9 characters (what git describe does currently)
let refstr = &read_to_string(refpath)?[0..9]; 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 refstr = refstr.trim();
let version = env!("CARGO_PKG_VERSION").to_owned(); let version = env!("CARGO_PKG_VERSION").to_owned();