Skip to content

Commit

Permalink
cli version: Show date as part of the version next to the commit id
Browse files Browse the repository at this point in the history
The date comes from the commiter date of the commit. This is so that
it's easy to tell at a glance how old a version is.
  • Loading branch information
ilyagr committed Aug 21, 2023
1 parent c43a306 commit 3ce3299
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
31 changes: 22 additions & 9 deletions cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,31 @@ 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((date, git_hash)) = get_git_date_and_hash() {
println!(
"cargo:rustc-env=JJ_VERSION={}-{}-{}",
version, date, git_hash
);
} else {
println!("cargo:rustc-env=JJ_VERSION={}", version);
}

Ok(())
}

fn get_git_hash() -> Option<String> {
/// 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_hash);
return Some(("nix".to_string(), nix_hash));
}

fn trim_and_split_on_vbar(bytes: &[u8]) -> (String, String) {
let s = str::from_utf8(bytes).unwrap().trim_end();
let (date, id) = s.split_once('|').unwrap();
(date.to_owned(), id.to_owned())
}
if let Ok(output) = Command::new("jj")
.args([
Expand All @@ -64,19 +74,22 @@ fn get_git_hash() -> Option<String> {
"log",
"--no-graph",
"-r=@-",
"-T=commit_id",
r#"-T=committer.timestamp().format("%Y%m%d") ++ "|" ++ commit_id"#,
])
.output()
{
if output.status.success() {
return Some(String::from_utf8(output.stdout).unwrap());
return Some(trim_and_split_on_vbar(&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=%cs|%H", "HEAD"])
.output()
{
if output.status.success() {
let line = str::from_utf8(&output.stdout).unwrap();
return Some(line.trim_end().to_owned());
let (date, hash) = trim_and_split_on_vbar(&output.stdout);
return Some((date.replace('-', ""), hash));
}
}

Expand Down
11 changes: 7 additions & 4 deletions cli/tests/test_global_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

use std::ffi::OsString;

use assert_matches::assert_matches;

use crate::common::{get_stderr_string, TestEnvironment};

pub mod common;
Expand Down Expand Up @@ -67,10 +69,11 @@ fn test_no_subcommand() {

let stdout = test_env.jj_cmd_success(test_env.env_root(), &["--version"]);
let sanitized = stdout.replace(|c: char| c.is_ascii_hexdigit(), "?");
assert!(
sanitized == "jj ?.?.?\n"
|| sanitized == "jj ?.?.?-????????????????????????????????????????\n",
"{sanitized}"
assert_matches!(
sanitized.as_str(),
"jj ?.?.?\n"
| "jj ?.?.?-????????-????????????????????????????????????????\n"
| "jj ?.?.?-nix-????????????????????????????????????????\n"
);

let stdout = test_env.jj_cmd_success(test_env.env_root(), &["-R", "repo"]);
Expand Down

0 comments on commit 3ce3299

Please sign in to comment.