-
Notifications
You must be signed in to change notification settings - Fork 343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cli version
: Show date as part of the version next to the commit id, shorten commit id
#2034
base: main
Are you sure you want to change the base?
Changes from all commits
e47d19c
03d1080
af658f9
4a7d58c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,11 +12,14 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
extern crate chrono; | ||
|
||
use std::path::Path; | ||
use std::process::Command; | ||
use std::str; | ||
|
||
use cargo_metadata::MetadataCommand; | ||
use chrono::prelude::*; | ||
|
||
const GIT_HEAD_PATH: &str = "../.git/HEAD"; | ||
const JJ_OP_HEADS_PATH: &str = "../.jj/repo/op_heads/heads"; | ||
|
@@ -41,21 +44,51 @@ fn main() -> std::io::Result<()> { | |
} | ||
println!("cargo:rerun-if-env-changed=NIX_JJ_GIT_HASH"); | ||
|
||
if let Some(git_hash) = get_git_hash() { | ||
println!("cargo:rustc-env=JJ_VERSION={}-{}", version, git_hash); | ||
if let Some((mut git_hash, maybe_date)) = get_git_timestamp_and_hash() { | ||
git_hash.truncate(16); | ||
println!( | ||
"cargo:rustc-env=JJ_VERSION={}+{}-{}", | ||
version, | ||
maybe_date | ||
.map(|d| d.format("%Y%m%d").to_string()) | ||
.unwrap_or_else(|| "dateunknown".to_string()), | ||
git_hash | ||
); | ||
} else { | ||
println!("cargo:rustc-env=JJ_VERSION={}", version); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn get_git_hash() -> Option<String> { | ||
if let Some(nix_hash) = std::env::var("NIX_JJ_GIT_HASH") | ||
/// Convert a string with a unix timestamp to a date | ||
fn timestamp_to_date(ts_str: &str) -> Option<DateTime<Utc>> { | ||
ts_str | ||
.parse::<i64>() | ||
.ok() | ||
.filter(|s| !s.is_empty()) | ||
{ | ||
return Some(nix_hash); | ||
.and_then(|ts| DateTime::<Utc>::from_timestamp(ts, 0)) | ||
} | ||
|
||
/// Return the UTC committer date (maybe) and the git hash | ||
fn get_git_timestamp_and_hash() -> Option<(String, Option<DateTime<Utc>>)> { | ||
if let (Ok(nix_hash), maybe_nix_timestamp) = ( | ||
std::env::var("NIX_JJ_GIT_HASH"), | ||
std::env::var("NIX_JJ_GIT_TIMESTAMP"), | ||
Comment on lines
+74
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is missing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you have it for |
||
) { | ||
if !nix_hash.is_empty() { | ||
return Some(( | ||
nix_hash, | ||
maybe_nix_timestamp | ||
.ok() | ||
.and_then(|ts_str| timestamp_to_date(&ts_str)), | ||
)); | ||
} | ||
} | ||
|
||
fn parse_timestamp_vbar_hash(bytes: &[u8]) -> (String, Option<DateTime<Utc>>) { | ||
let s = str::from_utf8(bytes).unwrap().trim_end(); | ||
let (ts_str, id) = s.split_once('|').unwrap(); | ||
(id.to_owned(), timestamp_to_date(ts_str)) | ||
} | ||
if let Ok(output) = Command::new("jj") | ||
.args([ | ||
|
@@ -64,19 +97,21 @@ fn get_git_hash() -> Option<String> { | |
"log", | ||
"--no-graph", | ||
"-r=@-", | ||
"-T=commit_id", | ||
r#"-T=committer.timestamp().utc().format("%s") ++ "|" ++ commit_id"#, | ||
]) | ||
.output() | ||
{ | ||
if output.status.success() { | ||
return Some(String::from_utf8(output.stdout).unwrap()); | ||
return Some(parse_timestamp_vbar_hash(&output.stdout)); | ||
} | ||
} | ||
|
||
if let Ok(output) = Command::new("git").args(["rev-parse", "HEAD"]).output() { | ||
if let Ok(output) = Command::new("git") | ||
.args(["log", "-1", "--format=%ct|%H", "HEAD"]) | ||
.output() | ||
{ | ||
if output.status.success() { | ||
let line = str::from_utf8(&output.stdout).unwrap(); | ||
return Some(line.trim_end().to_owned()); | ||
return Some(parse_timestamp_vbar_hash(&output.stdout)); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ""; | ||
gitTimestamp = toString self.lastModified; | ||
|
||
pkgs = import nixpkgs { | ||
inherit system; | ||
overlays = [ | ||
|
@@ -100,7 +105,8 @@ | |
ZSTD_SYS_USE_PKG_CONFIG = "1"; | ||
LIBSSH2_SYS_USE_PKG_CONFIG = "1"; | ||
RUSTFLAGS = pkgs.lib.optionalString useMoldLinker "-C link-arg=-fuse-ld=mold"; | ||
NIX_JJ_GIT_HASH = self.rev or ""; | ||
NIX_JJ_GIT_HASH = gitRev; | ||
NIX_JJ_GIT_TIMESTAMP = gitTimestamp; | ||
CARGO_INCREMENTAL = "0"; | ||
|
||
preCheck = '' | ||
|
@@ -175,8 +181,10 @@ | |
export RUST_BACKTRACE=1 | ||
export ZSTD_SYS_USE_PKG_CONFIG=1 | ||
export LIBSSH2_SYS_USE_PKG_CONFIG=1 | ||
|
||
export RUSTFLAGS="-Zthreads=0" | ||
|
||
export NIX_JJ_GIT_HASH="${gitRev}" | ||
export NIX_JJ_GIT_TIMESTAMP="${gitTimestamp}"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this will ensure To fix this we'd need to have a third case where we set something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally, these variables would only populate in the case when jj is compelled outside a git repo by nix. It's beyond the scope of my comfort level with nix, though. Somebody else would have to test these things and make another PR, I think. |
||
'' + pkgs.lib.optionalString useMoldLinker '' | ||
export RUSTFLAGS+=" -C link-arg=-fuse-ld=mold -C link-arg=-Wl,--compress-debug-sections=zstd" | ||
'' + darwinNextestHack; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unrelated to this PR, but it would be simpler to use
CARGO_PKG_VERSION
instead of parsing the version fromcargo metadata
https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts (seeCARGO_PKG_<var>
)