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,16 +317,35 @@ 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
let headpath = gitdir.join("HEAD");
let headstr = read_to_string(headpath)?;
let headref = headstr.split(' ').collect::<Vec<_>>()[1].trim();
// .git/HEAD contains ref: refs/heads/branch // .git/refs/heads/branch contains the SHA
let headpath = gitdir.join("HEAD"); let refpath = gitdir.join(headref);
let headstr = read_to_string(headpath)?; // Shorten to 9 characters (what git describe does currently)
let headref = headstr.split(' ').collect::<Vec<_>>()[1].trim(); read_to_string(refpath)?
} else if jjdir.exists() {
// .git/refs/heads/branch contains the SHA let output = Command::new("jj")
let refpath = gitdir.join(headref); .args([
// Shorten to 9 characters (what git describe does currently) "log",
let refstr = &read_to_string(refpath)?[0..9]; "--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();