From 4725170c1fa92a06a3acc151d004402e1c1491d1 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Mon, 27 Feb 2023 22:11:52 +0800 Subject: [PATCH 1/4] add last tag --- src/git.rs | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/git.rs b/src/git.rs index f0747d2..650a4c1 100644 --- a/src/git.rs +++ b/src/git.rs @@ -10,6 +10,7 @@ use std::process::{Command, Stdio}; pub const BRANCH: ShadowConst = "BRANCH"; pub const TAG: ShadowConst = "TAG"; +pub const LAST_TAG: ShadowConst = "LAST_TAG"; pub const SHORT_COMMIT: ShadowConst = "SHORT_COMMIT"; pub const COMMIT_HASH: ShadowConst = "COMMIT_HASH"; pub const COMMIT_DATE: ShadowConst = "COMMIT_DATE"; @@ -67,6 +68,11 @@ impl Git { self.update_str(TAG, x) } + // use command get last tag + if let Some(x) = command_last_tag() { + self.update_str(LAST_TAG, x) + } + // try use ci branch,tag self.ci_branch_tag(std_env); Ok(()) @@ -89,9 +95,11 @@ impl Git { //get HEAD branch let tag = command_current_tag().unwrap_or_default(); - + let last_tag = command_last_tag().unwrap_or_default(); self.update_str(BRANCH, branch); self.update_str(TAG, tag); + self.update_str(LAST_TAG, last_tag); + if let Some(v) = reference.target() { let commit = v.to_string(); self.update_str(COMMIT_HASH, commit.clone()); @@ -199,7 +207,8 @@ impl Git { } if let Some(x) = tag { - self.update_str(TAG, x); + self.update_str(TAG, x.clone()); + self.update_str(LAST_TAG, x); } } } @@ -218,6 +227,8 @@ pub fn new_git( git.map.insert(TAG, ConstVal::new("display current tag")); + git.map.insert(LAST_TAG, ConstVal::new("display last tag")); + git.map .insert(COMMIT_HASH, ConstVal::new("display current commit_id")); @@ -355,6 +366,17 @@ fn command_current_tag() -> Option { .unwrap_or(None) } +/// git describe --tags --abbrev=0 HEAD +/// Command exec git last tag +fn command_last_tag() -> Option { + Command::new("git") + .args(["describe", "--tags", "--abbrev=0", "HEAD"]) + .output() + .map(|x| String::from_utf8(x.stdout).ok()) + .map(|x| x.map(|x| x.trim().to_string())) + .unwrap_or(None) +} + /// git clean:git status --porcelain /// check repository git status is clean fn command_git_clean() -> bool { @@ -487,4 +509,10 @@ mod tests { assert_eq!(Some(branch()), command_current_branch()); } + + #[test] + fn test_command_last_tag() { + let opt_last_tag = command_last_tag(); + assert!(opt_last_tag.is_some()) + } } From d5c484c6a9a6ec42a4043b70c382e078e1530b13 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Mon, 27 Feb 2023 22:20:42 +0800 Subject: [PATCH 2/4] add last tag --- Cargo.toml | 2 +- src/git.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e57e525..8267cb2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "shadow-rs" -version = "0.20.1" +version = "0.21.0" authors = ["baoyachi "] edition = "2021" description = "A build-time information stored in your rust project" diff --git a/src/git.rs b/src/git.rs index 650a4c1..73a11af 100644 --- a/src/git.rs +++ b/src/git.rs @@ -433,7 +433,7 @@ fn command_git_status_file() -> String { &[r#"^[A|M|D|R]"#], &["{print $2}"], ) - .unwrap_or_default(); + .unwrap_or_default(); filter_git_dirty_stage(dirty, stage) } @@ -473,6 +473,7 @@ mod tests { let env_map = get_std_env(); let map = new_git(Path::new("./"), CiType::Github, &env_map); for (k, v) in map { + println!("k:{},v:{:?}", k, v); assert!(!v.desc.is_empty()); if !k.eq(TAG) && !k.eq(BRANCH) && !k.eq(GIT_STATUS_FILE) { assert!(!v.v.is_empty()); From 44493258104669db1abe8905fda0d5cee1ed64c9 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Mon, 27 Feb 2023 22:20:56 +0800 Subject: [PATCH 3/4] add last tag --- src/git.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/git.rs b/src/git.rs index 73a11af..c3bdcf2 100644 --- a/src/git.rs +++ b/src/git.rs @@ -433,7 +433,7 @@ fn command_git_status_file() -> String { &[r#"^[A|M|D|R]"#], &["{print $2}"], ) - .unwrap_or_default(); + .unwrap_or_default(); filter_git_dirty_stage(dirty, stage) } From 6326348014fee4f156dca70e9a1c19ecfae17fc3 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Mon, 27 Feb 2023 22:27:42 +0800 Subject: [PATCH 4/4] fix unit test --- src/git.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/git.rs b/src/git.rs index c3bdcf2..e86d031 100644 --- a/src/git.rs +++ b/src/git.rs @@ -475,7 +475,7 @@ mod tests { for (k, v) in map { println!("k:{},v:{:?}", k, v); assert!(!v.desc.is_empty()); - if !k.eq(TAG) && !k.eq(BRANCH) && !k.eq(GIT_STATUS_FILE) { + if !k.eq(TAG) && !k.eq(LAST_TAG) && !k.eq(BRANCH) && !k.eq(GIT_STATUS_FILE) { assert!(!v.v.is_empty()); continue; }