just playing with tangled
at ig/vimdiffwarn 81 lines 2.7 kB view raw
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 15use std::path::Path; 16use std::process::Command; 17use std::str; 18 19const GIT_HEAD_PATH: &str = "../.git/HEAD"; 20const JJ_OP_HEADS_PATH: &str = "../.jj/repo/op_heads/heads"; 21 22fn main() { 23 let version = std::env::var("CARGO_PKG_VERSION").unwrap(); 24 25 if Path::new(GIT_HEAD_PATH).exists() { 26 // In colocated repo, .git/HEAD should reflect the working-copy parent. 27 println!("cargo:rerun-if-changed={GIT_HEAD_PATH}"); 28 } else if Path::new(JJ_OP_HEADS_PATH).exists() { 29 // op_heads changes when working-copy files are mutated, which is way more 30 // frequent than .git/HEAD. 31 println!("cargo:rerun-if-changed={JJ_OP_HEADS_PATH}"); 32 } 33 println!("cargo:rerun-if-env-changed=NIX_JJ_GIT_HASH"); 34 35 if let Some(git_hash) = get_git_hash() { 36 println!("cargo:rustc-env=JJ_VERSION={version}-{git_hash}"); 37 } else { 38 println!("cargo:rustc-env=JJ_VERSION={version}"); 39 } 40 41 let docs_symlink_path = Path::new("docs"); 42 println!("cargo:rerun-if-changed={}", docs_symlink_path.display()); 43 if docs_symlink_path.join("index.md").exists() { 44 println!("cargo:rustc-env=JJ_DOCS_DIR=docs/"); 45 } else { 46 println!("cargo:rustc-env=JJ_DOCS_DIR=../docs/"); 47 } 48} 49 50fn get_git_hash() -> Option<String> { 51 if let Some(nix_hash) = std::env::var("NIX_JJ_GIT_HASH") 52 .ok() 53 .filter(|s| !s.is_empty()) 54 { 55 return Some(nix_hash); 56 } 57 if let Ok(output) = Command::new("jj") 58 .args([ 59 "--ignore-working-copy", 60 "--color=never", 61 "log", 62 "--no-graph", 63 "-r=@-", 64 "-T=commit_id", 65 ]) 66 .output() 67 { 68 if output.status.success() { 69 return Some(String::from_utf8(output.stdout).unwrap()); 70 } 71 } 72 73 if let Ok(output) = Command::new("git").args(["rev-parse", "HEAD"]).output() { 74 if output.status.success() { 75 let line = str::from_utf8(&output.stdout).unwrap(); 76 return Some(line.trim_end().to_owned()); 77 } 78 } 79 80 None 81}