Allow passing in GIT_DESCRIBE/GIT_REV to avoid git repo

This should allow building inside a flake
This commit is contained in:
Kovacsics Robert
2024-11-20 11:55:53 +00:00
parent 2e3a3397a5
commit c36e72b5f6
3 changed files with 25 additions and 7 deletions
+20 -5
View File
@@ -3,11 +3,26 @@ use std::env;
use git2::Repository;
fn main() {
if let Ok(repo) = Repository::open(".") {
let head = repo.head().expect("should get git HEAD");
let commit = head.peel_to_commit().expect("should get git HEAD commit");
println!("cargo::rustc-env=GIT_REV={}", commit.id());
}
let branch = if let Ok(describe) = env::var("GIT_DESCRIBE") {
describe
} else {
let repo = Repository::open(".").expect("should open git repository");
let head = repo.head().expect("should get HEAD");
head.shorthand()
.expect("should get branch name")
.to_string()
};
println!("cargo::rustc-env=GIT_DESCRIBE={branch}");
let rev = if let Ok(rev) = env::var("GIT_REV") {
rev
} else {
let repo = Repository::open(".").expect("should open git repository");
let head = repo.head().expect("should get HEAD");
let commit = head.peel_to_commit().expect("should get HEAD commit");
commit.id().to_string()
};
println!("cargo::rustc-env=GIT_REV={rev}");
let os = env::var("CARGO_CFG_TARGET_OS").expect("should get CARGO_CFG_TARGET_OS");
println!("cargo::rustc-env=TARGET_OS={os}");
+2 -1
View File
@@ -134,7 +134,8 @@
nativeBuildInputs = with pkgs; [ pkg-config ];
CARGO_BUILD_TARGET = targetConfig.rustTarget;
CARGO_BUILD_RUSTFLAGS = [ "-Ctarget-feature=+crt-static" ];
GIT_REV = self.rev or self.dirtyRev or "dirty";
GIT_REV = self.rev or self.dirtyRev or "unknown-rev";
GIT_DESCRIBE = "flake-" + self.shortRev or self.dirtyShortRev or "unknown";
postInstall = ''
export WINEPREFIX="$(mktemp -d)"
+3 -1
View File
@@ -84,7 +84,9 @@ impl Cli {
env!("TARGET_ENV"),
" ",
env!("TARGET_ARCH"),
", git rev ",
", git ",
env!("GIT_DESCRIBE"),
" rev ",
env!("GIT_REV"),
);
}