summaryrefslogtreecommitdiffstats
path: root/src/modules/git_commit.rs
diff options
context:
space:
mode:
authorDavid Knaack <davidkna@users.noreply.github.com>2022-11-22 10:15:31 +0100
committerGitHub <noreply@github.com>2022-11-22 10:15:31 +0100
commitac37792c19d7c545d4c51cf712f13e5e81559511 (patch)
tree972e0e25ef2c79719632f1adf597b106c74a6234 /src/modules/git_commit.rs
parente4dbff0fc7e88f792b90703f03f83e31d401b90e (diff)
feat(git_commit): support showing lightweight tags (#4632)
Diffstat (limited to 'src/modules/git_commit.rs')
-rw-r--r--src/modules/git_commit.rs89
1 files changed, 82 insertions, 7 deletions
diff --git a/src/modules/git_commit.rs b/src/modules/git_commit.rs
index ca63a0c01..bb49dc89e 100644
--- a/src/modules/git_commit.rs
+++ b/src/modules/git_commit.rs
@@ -1,5 +1,5 @@
use super::{Context, Module, ModuleConfig};
-use git_repository::commit::describe::SelectRef::AnnotatedTags;
+use git_repository::commit::describe::SelectRef::AllTags;
use crate::configs::git_commit::GitCommitConfig;
use crate::context::Repo;
@@ -58,7 +58,7 @@ fn git_tag(repo: &Repo, config: &GitCommitConfig) -> Option<String> {
let describe_platform = head_commit
.describe()
- .names(AnnotatedTags)
+ .names(AllTags)
.max_candidates(config.tag_max_candidates)
.traverse_first_parent(true);
let formatter = describe_platform.try_format().ok()??;
@@ -337,8 +337,6 @@ mod tests {
#[test]
fn test_latest_tag_shown_with_tag_enabled() -> io::Result<()> {
- use std::{thread, time};
-
let repo_dir = fixture_repo(FixtureProvider::Git)?;
let mut git_commit = create_command("git")?
@@ -351,19 +349,96 @@ mod tests {
create_command("git")?
.args(["tag", "v2", "-m", "Testing tags v2"])
+ .env("GIT_COMMITTER_DATE", "2022-01-01 00:00:00 +0000")
.current_dir(repo_dir.path())
.output()?;
- // Wait one second between tags
- thread::sleep(time::Duration::from_millis(1000));
-
create_command("git")?
.args(["tag", "v0", "-m", "Testing tags v0", "HEAD~1"])
+ .env("GIT_COMMITTER_DATE", "2022-01-01 00:00:01 +0000")
.current_dir(repo_dir.path())
.output()?;
create_command("git")?
.args(["tag", "v1", "-m", "Testing tags v1"])
+ .env("GIT_COMMITTER_DATE", "2022-01-01 00:00:01 +0000")
+ .current_dir(repo_dir.path())
+ .output()?;
+
+ // Annotaged tags are preferred over lightweight tags
+ create_command("git")?
+ .args(["tag", "l0"])
+ .env("GIT_COMMITTER_DATE", "2022-01-01 00:00:02 +0000")
+ .current_dir(repo_dir.path())
+ .output()?;
+
+ let git_tag = create_command("git")?
+ .args([
+ "for-each-ref",
+ "--contains",
+ "HEAD",
+ "--sort=-taggerdate",
+ "--count=1",
+ "--format",
+ "%(refname:short)",
+ "refs/tags",
+ ])
+ .current_dir(repo_dir.path())
+ .output()?
+ .stdout;
+ let tag_output = str::from_utf8(&git_tag).unwrap().trim();
+
+ let expected_output = format!("{commit_output} {tag_output}");
+
+ let actual = ModuleRenderer::new("git_commit")
+ .config(toml::toml! {
+ [git_commit]
+ only_detached = false
+ tag_disabled = false
+ tag_symbol = " "
+ })
+ .path(repo_dir.path())
+ .collect();
+
+ let expected = Some(format!(
+ "{} ",
+ Color::Green
+ .bold()
+ .paint(format!("({})", expected_output.trim()))
+ ));
+
+ assert_eq!(expected, actual);
+ Ok(())
+ }
+
+ #[test]
+ fn test_latest_tag_shown_with_tag_enabled_lightweight() -> io::Result<()> {
+ let repo_dir = fixture_repo(FixtureProvider::Git)?;
+
+ let mut git_commit = create_command("git")?
+ .args(["rev-parse", "HEAD"])
+ .current_dir(repo_dir.path())
+ .output()?
+ .stdout;
+ git_commit.truncate(7);
+ let commit_output = str::from_utf8(&git_commit).unwrap().trim();
+
+ // Lightweight tags are chosen lexicographically
+ create_command("git")?
+ .args(["tag", "v1"])
+ .env("GIT_COMMITTER_DATE", "2022-01-01 00:00:00 +0000")
+ .current_dir(repo_dir.path())
+ .output()?;
+
+ create_command("git")?
+ .args(["tag", "v0", "HEAD~1"])
+ .env("GIT_COMMITTER_DATE", "2022-01-01 00:00:01 +0000")
+ .current_dir(repo_dir.path())
+ .output()?;
+
+ create_command("git")?
+ .args(["tag", "v2"])
+ .env("GIT_COMMITTER_DATE", "2022-01-01 00:00:01 +0000")
.current_dir(repo_dir.path())
.output()?;