From 136a89b8f57ed785cfe4e5e34398f36078399b35 Mon Sep 17 00:00:00 2001 From: Ilya Grigoriev Date: Fri, 11 Aug 2023 20:20:12 -0700 Subject: [PATCH] nix: send the Git date to `build.rs` Thanks to @thoughtpolice for providing the original version of the nix code; I edited it a bit. --- cli/build.rs | 12 +++++++----- cli/tests/test_global_opts.rs | 2 +- flake.nix | 12 ++++++++++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/cli/build.rs b/cli/build.rs index a317221560..e285444f50 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -56,11 +56,13 @@ fn main() -> std::io::Result<()> { /// Return the committer date in YYYYMMDD format and the git hash fn get_git_date_and_hash() -> Option<(String, String)> { - if let Some(nix_hash) = std::env::var("NIX_JJ_GIT_HASH") - .ok() - .filter(|s| !s.is_empty()) - { - return Some(("nix".to_string(), nix_hash)); + if let (Ok(nix_hash), Ok(nix_date)) = ( + std::env::var("NIX_JJ_GIT_HASH"), + std::env::var("NIX_JJ_GIT_DATE"), + ) { + if !nix_hash.is_empty() && !nix_date.is_empty() { + return Some((nix_date, nix_hash)); + } } fn trim_and_split_on_vbar(bytes: &[u8]) -> (String, String) { diff --git a/cli/tests/test_global_opts.rs b/cli/tests/test_global_opts.rs index aaba75b496..7be53d0237 100644 --- a/cli/tests/test_global_opts.rs +++ b/cli/tests/test_global_opts.rs @@ -71,7 +71,7 @@ fn test_no_subcommand() { let sanitized = stdout.replace(|c: char| c.is_ascii_hexdigit(), "?"); assert_matches!( sanitized.as_str(), - "jj ?.?.?\n" | "jj ?.?.?-????????-????????????????\n" | "jj ?.?.?-nix-????????????????\n" + "jj ?.?.?\n" | "jj ?.?.?-????????-????????????????\n" ); let stdout = test_env.jj_cmd_success(test_env.env_root(), &["-R", "repo"]); diff --git a/flake.nix b/flake.nix index 49dcd9056a..c42c4b10a4 100644 --- a/flake.nix +++ b/flake.nix @@ -18,6 +18,11 @@ } // (flake-utils.lib.eachDefaultSystem (system: let + # TODO(aseipp): Use dirtyRev and dirtyShortRev to record dirty checkout + # when we update `build.rs` to understand dirty checkouts. + gitRev = self.rev or ""; + gitDate = builtins.substring 0 8 (self.lastModifiedDate or self.lastModified or ""); + pkgs = import nixpkgs { inherit system; overlays = [ @@ -73,10 +78,11 @@ darwin.apple_sdk.frameworks.Security darwin.apple_sdk.frameworks.SystemConfiguration libiconv - ]; + ]; ZSTD_SYS_USE_PKG_CONFIG = "1"; LIBSSH2_SYS_USE_PKG_CONFIG = "1"; - NIX_JJ_GIT_HASH = self.rev or ""; + NIX_JJ_GIT_HASH = gitRev; + NIX_JJ_GIT_DATE = gitDate; CARGO_INCREMENTAL = "0"; postInstall = '' $out/bin/jj util mangen > ./jj.1 @@ -134,6 +140,8 @@ export RUST_BACKTRACE=1 export ZSTD_SYS_USE_PKG_CONFIG=1 export LIBSSH2_SYS_USE_PKG_CONFIG=1 + export NIX_JJ_GIT_HASH="${gitRev}" + export NIX_JJ_GIT_DATE="${gitDate}; ''; }; }));