From c36e72b5f6f1cb6fb60c0e2e384e885fb7e4d298 Mon Sep 17 00:00:00 2001 From: Kovacsics Robert Date: Wed, 20 Nov 2024 11:55:53 +0000 Subject: [PATCH] Allow passing in GIT_DESCRIBE/GIT_REV to avoid git repo This should allow building inside a flake --- build.rs | 25 ++++++++++++++++++++----- flake.nix | 3 ++- src/cli.rs | 4 +++- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/build.rs b/build.rs index afd452cc..288a0b17 100644 --- a/build.rs +++ b/build.rs @@ -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}"); diff --git a/flake.nix b/flake.nix index 4abdb9b0..d8521844 100644 --- a/flake.nix +++ b/flake.nix @@ -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)" diff --git a/src/cli.rs b/src/cli.rs index cc3f4d67..6d365a98 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -84,7 +84,9 @@ impl Cli { env!("TARGET_ENV"), " ", env!("TARGET_ARCH"), - ", git rev ", + ", git ", + env!("GIT_DESCRIBE"), + " rev ", env!("GIT_REV"), ); }