From 383ab92cf6ed30d39be3aa086775ea1900ec49cb 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 this; I edited it a bit. --- cli/build.rs | 14 ++++++++++---- cli/tests/test_global_opts.rs | 2 +- flake.nix | 15 ++++++++++++--- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/cli/build.rs b/cli/build.rs index a317221560..33b939120f 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -56,11 +56,17 @@ 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()) + if let Some((nix_hash, nix_date)) = std::iter::zip( + std::env::var("NIX_JJ_GIT_HASH") + .ok() + .filter(|s| !s.is_empty()), + std::env::var("NIX_JJ_GIT_DATE") + .ok() + .filter(|s| !s.is_empty()), + ) + .next() { - return Some(("nix".to_string(), nix_hash)); + 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 55cfefc98a..31626b1d7e 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..f03d83d3db 100644 --- a/flake.nix +++ b/flake.nix @@ -18,6 +18,12 @@ } // (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 ""; + gitShortRev = self.shortRev or ""; + gitDate = builtins.substring 0 8 (self.lastModifiedDate or self.lastModified or "19700101"); + pkgs = import nixpkgs { inherit system; overlays = [ @@ -47,7 +53,7 @@ packages = { jujutsu = ourRustPlatform.buildRustPackage rec { pname = "jujutsu"; - version = "unstable-${self.shortRev or "dirty"}"; + version = "unstable-${gitShortRev}"; buildNoDefaultFeatures = true; buildFeatures = [ "packaging" ]; cargoBuildFlags = ["--bin" "jj"]; # don't build and install the fake editors @@ -73,10 +79,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 +141,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}; ''; }; }));