Skip to content
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

add last tag #131

Merged
merged 4 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shadow-rs"
version = "0.20.1"
version = "0.21.0"
authors = ["baoyachi <[email protected]>"]
edition = "2021"
description = "A build-time information stored in your rust project"
Expand Down
35 changes: 32 additions & 3 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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(())
Expand All @@ -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());
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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"));

Expand Down Expand Up @@ -355,6 +366,17 @@ fn command_current_tag() -> Option<String> {
.unwrap_or(None)
}

/// git describe --tags --abbrev=0 HEAD
/// Command exec git last tag
fn command_last_tag() -> Option<String> {
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 {
Expand Down Expand Up @@ -451,8 +473,9 @@ 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) {
if !k.eq(TAG) && !k.eq(LAST_TAG) && !k.eq(BRANCH) && !k.eq(GIT_STATUS_FILE) {
assert!(!v.v.is_empty());
continue;
}
Expand Down Expand Up @@ -487,4 +510,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())
}
}