| 1 |
// Copyright 2023 The Jujutsu Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
use std::path::Path; |
| 16 |
use std::process::Command; |
| 17 |
|
| 18 |
const GIT_HEAD_PATH: &str = "../.git/HEAD"; |
| 19 |
const JJ_OP_HEADS_PATH: &str = "../.jj/repo/op_heads/heads"; |
| 20 |
|
| 21 |
fn main() { |
| 22 |
let version = std::env::var("CARGO_PKG_VERSION").unwrap(); |
| 23 |
|
| 24 |
println!("cargo:rerun-if-env-changed=NIX_JJ_GIT_HASH"); |
| 25 |
let git_hash = get_git_hash_from_nix().or_else(|| { |
| 26 |
if Path::new(GIT_HEAD_PATH).exists() { |
| 27 |
// In colocated workspace, .git/HEAD should reflect the working-copy parent. |
| 28 |
println!("cargo:rerun-if-changed={GIT_HEAD_PATH}"); |
| 29 |
} else if Path::new(JJ_OP_HEADS_PATH).exists() { |
| 30 |
// op_heads changes when working-copy files are mutated, which is way more |
| 31 |
// frequent than .git/HEAD. |
| 32 |
println!("cargo:rerun-if-changed={JJ_OP_HEADS_PATH}"); |
| 33 |
} |
| 34 |
get_git_hash_from_jj().or_else(get_git_hash_from_git) |
| 35 |
}); |
| 36 |
|
| 37 |
if let Some(git_hash) = git_hash { |
| 38 |
println!("cargo:rustc-env=JJ_VERSION={version}-{git_hash}"); |
| 39 |
} else { |
| 40 |
println!("cargo:rustc-env=JJ_VERSION={version}"); |
| 41 |
} |
| 42 |
|
| 43 |
let docs_symlink_path = Path::new("docs"); |
| 44 |
println!("cargo:rerun-if-changed={}", docs_symlink_path.display()); |
| 45 |
if docs_symlink_path.join("index.md").exists() { |
| 46 |
println!("cargo:rustc-env=JJ_DOCS_DIR=docs/"); |
| 47 |
} else { |
| 48 |
println!("cargo:rustc-env=JJ_DOCS_DIR=../docs/"); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
fn get_git_hash_from_nix() -> Option<String> { |
| 53 |
std::env::var("NIX_JJ_GIT_HASH") |
| 54 |
.ok() |
| 55 |
.filter(|s| !s.is_empty()) |
| 56 |
} |
| 57 |
|
| 58 |
fn get_git_hash_from_jj() -> Option<String> { |
| 59 |
Command::new("jj") |
| 60 |
.args([ |
| 61 |
"--ignore-working-copy", |
| 62 |
"--color=never", |
| 63 |
"log", |
| 64 |
"--no-graph", |
| 65 |
"-r=@-", |
| 66 |
"-T=commit_id ++ '-'", |
| 67 |
]) |
| 68 |
.output() |
| 69 |
.ok() |
| 70 |
.filter(|output| output.status.success()) |
| 71 |
.map(|output| { |
| 72 |
let mut parent_commits = String::from_utf8(output.stdout).unwrap(); |
| 73 |
// If a development version of `jj` is compiled at a merge commit, this will |
| 74 |
// result in several commit ids separated by `-`s. |
| 75 |
parent_commits.truncate(parent_commits.trim_end_matches('-').len()); |
| 76 |
parent_commits |
| 77 |
}) |
| 78 |
} |
| 79 |
|
| 80 |
fn get_git_hash_from_git() -> Option<String> { |
| 81 |
Command::new("git") |
| 82 |
.args(["rev-parse", "HEAD"]) |
| 83 |
.output() |
| 84 |
.ok() |
| 85 |
.filter(|output| output.status.success()) |
| 86 |
.map(|output| { |
| 87 |
str::from_utf8(&output.stdout) |
| 88 |
.unwrap() |
| 89 |
.trim_end() |
| 90 |
.to_owned() |
| 91 |
}) |
| 92 |
} |