From 7d7da9212e42bfe91044c24e3fa41f10c571fba2 Mon Sep 17 00:00:00 2001 From: "S. Kozyr" Date: Sat, 11 Feb 2023 17:27:10 +0200 Subject: [PATCH] Last tag is now optional. Timestamp is added to version string. --- build.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/build.rs b/build.rs index 1b171fb4..b6ba0b45 100644 --- a/build.rs +++ b/build.rs @@ -57,8 +57,10 @@ fn version_from_git_info() -> Result { // The last available tag, equal to exact_tag when // the current commit is tagged - let last_tag = run(&["git", "describe", "--abbrev=0", "--tags"])?; - println!("cargo:rustc-env=GIT_LAST_TAG={last_tag}"); + let last_tag_maybe = run(&["git", "describe", "--abbrev=0", "--tags"]).ok(); + if let Some(ref last_tag) = last_tag_maybe { + println!("cargo:rustc-env=GIT_LAST_TAG={last_tag}"); + } // The current branch name let branch = run(&["git", "rev-parse", "--abbrev-ref", "HEAD"])?; @@ -69,12 +71,25 @@ fn version_from_git_info() -> Result { let rev_short = rev.get(..8).unwrap_or_default(); println!("cargo:rustc-env=GIT_REV={rev_short}"); + // The current git commit time + let commit_time = run(&["git", "log", "-1", "--format=%cd", "--date=format:%Y-%m-%d %H:%M:%S"])?; + // Combined version if let Some(exact) = exact_tag { Ok(exact) } else if &branch != "main" && &branch != "master" { - Ok(format!("{last_tag}-{rev_short} ({branch})")) + if let Some(ref last_tag) = last_tag_maybe { + Ok(format!("{last_tag}-{rev_short} ({branch}) {commit_time}")) + } + else { + Ok(format!("{rev_short} ({branch}) {commit_time}")) + } } else { - Ok(format!("{last_tag}-{rev_short}")) + if let Some(ref last_tag) = last_tag_maybe { + Ok(format!("{last_tag}-{rev_short} {commit_time}")) + } + else { + Ok(format!("main-{rev_short} {commit_time}")) + } } }