2022-03-26 09:42:19 +00:00
|
|
|
use super::{Context, Module, ModuleConfig};
|
2022-11-22 09:15:31 +00:00
|
|
|
use git_repository::commit::describe::SelectRef::AllTags;
|
2019-12-06 16:57:42 +00:00
|
|
|
|
|
|
|
use crate::configs::git_commit::GitCommitConfig;
|
2022-08-09 02:33:00 +00:00
|
|
|
use crate::context::Repo;
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::formatter::StringFormatter;
|
2019-12-06 16:57:42 +00:00
|
|
|
|
|
|
|
/// Creates a module with the Git commit in the current directory
|
|
|
|
///
|
|
|
|
/// Will display the commit hash if the current directory is a git repo
|
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
|
|
|
let mut module = context.new_module("git_commit");
|
2020-07-07 22:45:32 +00:00
|
|
|
let config: GitCommitConfig = GitCommitConfig::try_load(module.config);
|
2019-12-06 16:57:42 +00:00
|
|
|
|
|
|
|
let repo = context.get_repo().ok()?;
|
2022-08-09 02:33:00 +00:00
|
|
|
let git_repo = repo.open();
|
|
|
|
let git_head = git_repo.head().ok()?;
|
2019-12-06 16:57:42 +00:00
|
|
|
|
2022-08-09 02:33:00 +00:00
|
|
|
let is_detached = git_head.is_detached();
|
2020-02-12 19:56:29 +00:00
|
|
|
if config.only_detached && !is_detached {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
2021-07-04 13:29:21 +00:00
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
2022-08-09 02:33:00 +00:00
|
|
|
"hash" => Some(Ok(git_hash(context.get_repo().ok()?, &config)?)),
|
2022-11-06 21:43:50 +00:00
|
|
|
"tag" if !config.tag_disabled => Some(Ok(format!(
|
2022-08-09 02:33:00 +00:00
|
|
|
"{}{}",
|
|
|
|
config.tag_symbol,
|
2022-08-19 06:27:41 +00:00
|
|
|
git_tag(context.get_repo().ok()?, &config)?
|
2021-07-04 13:29:21 +00:00
|
|
|
))),
|
|
|
|
_ => None,
|
|
|
|
})
|
2021-11-01 21:18:45 +00:00
|
|
|
.parse(None, Some(context))
|
2021-07-04 13:29:21 +00:00
|
|
|
});
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `git_commit`:\n{}", error);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
});
|
2019-12-06 16:57:42 +00:00
|
|
|
|
|
|
|
Some(module)
|
|
|
|
}
|
|
|
|
|
2022-08-19 06:27:41 +00:00
|
|
|
fn git_tag(repo: &Repo, config: &GitCommitConfig) -> Option<String> {
|
2022-08-18 06:22:51 +00:00
|
|
|
// allow environment variables like GITOXIDE_OBJECT_CACHE_MEMORY and GITOXIDE_DISABLE_PACK_CACHE to speed up operation for some repos
|
|
|
|
let mut git_repo = repo.open().apply_environment();
|
|
|
|
git_repo.object_cache_size_if_unset(4 * 1024 * 1024);
|
2022-08-09 02:33:00 +00:00
|
|
|
let head_commit = git_repo.head_commit().ok()?;
|
|
|
|
|
2022-08-19 06:27:41 +00:00
|
|
|
let describe_platform = head_commit
|
|
|
|
.describe()
|
2022-11-22 09:15:31 +00:00
|
|
|
.names(AllTags)
|
2022-08-19 06:27:41 +00:00
|
|
|
.max_candidates(config.tag_max_candidates)
|
|
|
|
.traverse_first_parent(true);
|
2022-08-09 02:33:00 +00:00
|
|
|
let formatter = describe_platform.try_format().ok()??;
|
|
|
|
|
|
|
|
Some(formatter.name?.to_string())
|
2021-07-04 13:29:21 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 02:33:00 +00:00
|
|
|
fn git_hash(repo: &Repo, config: &GitCommitConfig) -> Option<String> {
|
|
|
|
let git_repo = repo.open();
|
|
|
|
let head_id = git_repo.head_id().ok()?;
|
|
|
|
|
|
|
|
Some(format!(
|
|
|
|
"{}",
|
|
|
|
head_id.to_hex_with_len(config.commit_hash_length)
|
|
|
|
))
|
2019-12-06 16:57:42 +00:00
|
|
|
}
|
2020-08-07 19:13:12 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-09-04 16:44:54 +00:00
|
|
|
use nu_ansi_term::Color;
|
2020-08-07 19:13:12 +00:00
|
|
|
use std::{io, str};
|
|
|
|
|
|
|
|
use crate::test::{fixture_repo, FixtureProvider, ModuleRenderer};
|
2021-07-16 19:20:59 +00:00
|
|
|
use crate::utils::create_command;
|
2020-08-07 19:13:12 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn show_nothing_on_empty_dir() -> io::Result<()> {
|
|
|
|
let repo_dir = tempfile::tempdir()?;
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("git_commit")
|
2022-11-05 11:40:46 +00:00
|
|
|
.path(repo_dir.path())
|
2020-08-07 19:13:12 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = None;
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
repo_dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_render_commit_hash() -> io::Result<()> {
|
2021-03-25 20:03:19 +00:00
|
|
|
let repo_dir = fixture_repo(FixtureProvider::Git)?;
|
2020-08-07 19:13:12 +00:00
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
let mut git_output = create_command("git")?
|
2022-11-05 11:40:46 +00:00
|
|
|
.args(["rev-parse", "HEAD"])
|
|
|
|
.current_dir(repo_dir.path())
|
2020-08-07 19:13:12 +00:00
|
|
|
.output()?
|
|
|
|
.stdout;
|
|
|
|
git_output.truncate(7);
|
|
|
|
let expected_hash = str::from_utf8(&git_output).unwrap();
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("git_commit")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[git_commit]
|
|
|
|
only_detached = false
|
|
|
|
})
|
2022-11-05 11:40:46 +00:00
|
|
|
.path(repo_dir.path())
|
2020-08-07 19:13:12 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!(
|
|
|
|
"{} ",
|
2022-11-05 11:40:46 +00:00
|
|
|
Color::Green.bold().paint(format!("({expected_hash})"))
|
2020-08-07 19:13:12 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
repo_dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_render_commit_hash_len_override() -> io::Result<()> {
|
2021-03-25 20:03:19 +00:00
|
|
|
let repo_dir = fixture_repo(FixtureProvider::Git)?;
|
2020-08-07 19:13:12 +00:00
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
let mut git_output = create_command("git")?
|
2022-11-05 11:40:46 +00:00
|
|
|
.args(["rev-parse", "HEAD"])
|
|
|
|
.current_dir(repo_dir.path())
|
2020-08-07 19:13:12 +00:00
|
|
|
.output()?
|
|
|
|
.stdout;
|
|
|
|
git_output.truncate(14);
|
|
|
|
let expected_hash = str::from_utf8(&git_output).unwrap();
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("git_commit")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[git_commit]
|
|
|
|
only_detached = false
|
|
|
|
commit_hash_length = 14
|
|
|
|
})
|
2022-11-05 11:40:46 +00:00
|
|
|
.path(repo_dir.path())
|
2020-08-07 19:13:12 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!(
|
|
|
|
"{} ",
|
2022-11-05 11:40:46 +00:00
|
|
|
Color::Green.bold().paint(format!("({expected_hash})"))
|
2020-08-07 19:13:12 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
repo_dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_render_commit_hash_only_detached_on_branch() -> io::Result<()> {
|
2021-03-25 20:03:19 +00:00
|
|
|
let repo_dir = fixture_repo(FixtureProvider::Git)?;
|
2020-08-07 19:13:12 +00:00
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("git_commit")
|
2022-11-05 11:40:46 +00:00
|
|
|
.path(repo_dir.path())
|
2020-08-07 19:13:12 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = None;
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
repo_dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_render_commit_hash_only_detached_on_detached() -> io::Result<()> {
|
2021-03-25 20:03:19 +00:00
|
|
|
let repo_dir = fixture_repo(FixtureProvider::Git)?;
|
2020-08-07 19:13:12 +00:00
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
create_command("git")?
|
2022-11-05 11:40:46 +00:00
|
|
|
.args(["checkout", "@~1"])
|
|
|
|
.current_dir(repo_dir.path())
|
2020-08-07 19:13:12 +00:00
|
|
|
.output()?;
|
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
let mut git_output = create_command("git")?
|
2022-11-05 11:40:46 +00:00
|
|
|
.args(["rev-parse", "HEAD"])
|
|
|
|
.current_dir(repo_dir.path())
|
2020-08-07 19:13:12 +00:00
|
|
|
.output()?
|
|
|
|
.stdout;
|
|
|
|
git_output.truncate(7);
|
|
|
|
let expected_hash = str::from_utf8(&git_output).unwrap();
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("git_commit")
|
2022-11-05 11:40:46 +00:00
|
|
|
.path(repo_dir.path())
|
2020-08-07 19:13:12 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!(
|
|
|
|
"{} ",
|
2022-11-05 11:40:46 +00:00
|
|
|
Color::Green.bold().paint(format!("({expected_hash})"))
|
2020-08-07 19:13:12 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
repo_dir.close()
|
|
|
|
}
|
2020-10-23 15:49:19 +00:00
|
|
|
|
2022-11-06 21:43:50 +00:00
|
|
|
#[test]
|
|
|
|
fn test_render_commit_hash_with_tag_disabled() -> io::Result<()> {
|
|
|
|
let repo_dir = fixture_repo(FixtureProvider::Git)?;
|
|
|
|
|
|
|
|
create_command("git")?
|
2022-11-12 16:07:49 +00:00
|
|
|
.args(["tag", "v1", "-m", "Testing tags"])
|
|
|
|
.current_dir(repo_dir.path())
|
2022-11-06 21:43:50 +00:00
|
|
|
.output()?;
|
|
|
|
|
|
|
|
let mut git_commit = create_command("git")?
|
2022-11-12 16:07:49 +00:00
|
|
|
.args(["rev-parse", "HEAD"])
|
|
|
|
.current_dir(repo_dir.path())
|
2022-11-06 21:43:50 +00:00
|
|
|
.output()?
|
|
|
|
.stdout;
|
|
|
|
git_commit.truncate(7);
|
|
|
|
let commit_output = str::from_utf8(&git_commit).unwrap().trim();
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("git_commit")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[git_commit]
|
|
|
|
only_detached = false
|
|
|
|
})
|
2022-11-12 16:07:49 +00:00
|
|
|
.path(repo_dir.path())
|
2022-11-06 21:43:50 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!(
|
|
|
|
"{} ",
|
|
|
|
Color::Green
|
|
|
|
.bold()
|
|
|
|
.paint(format!("({})", commit_output.trim()))
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-10-23 15:49:19 +00:00
|
|
|
#[test]
|
|
|
|
fn test_render_commit_hash_with_tag_enabled() -> io::Result<()> {
|
2021-03-25 20:03:19 +00:00
|
|
|
let repo_dir = fixture_repo(FixtureProvider::Git)?;
|
2020-10-23 15:49:19 +00:00
|
|
|
|
2022-11-06 21:43:50 +00:00
|
|
|
create_command("git")?
|
2022-11-12 16:07:49 +00:00
|
|
|
.args(["tag", "v1", "-m", "Testing tags"])
|
|
|
|
.current_dir(repo_dir.path())
|
2022-11-06 21:43:50 +00:00
|
|
|
.output()?;
|
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
let mut git_commit = create_command("git")?
|
2022-11-05 11:40:46 +00:00
|
|
|
.args(["rev-parse", "HEAD"])
|
|
|
|
.current_dir(repo_dir.path())
|
2020-10-23 15:49:19 +00:00
|
|
|
.output()?
|
|
|
|
.stdout;
|
|
|
|
git_commit.truncate(7);
|
|
|
|
let commit_output = str::from_utf8(&git_commit).unwrap().trim();
|
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
let git_tag = create_command("git")?
|
2022-11-05 11:40:46 +00:00
|
|
|
.args(["describe", "--tags", "--exact-match", "HEAD"])
|
|
|
|
.current_dir(repo_dir.path())
|
2020-10-23 15:49:19 +00:00
|
|
|
.output()?
|
|
|
|
.stdout;
|
|
|
|
let tag_output = str::from_utf8(&git_tag).unwrap().trim();
|
|
|
|
|
2022-11-05 11:40:46 +00:00
|
|
|
let expected_output = format!("{commit_output} {tag_output}");
|
2020-10-23 15:49:19 +00:00
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("git_commit")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[git_commit]
|
|
|
|
only_detached = false
|
|
|
|
tag_disabled = false
|
2022-11-06 21:43:50 +00:00
|
|
|
tag_symbol = " "
|
2020-10-23 15:49:19 +00:00
|
|
|
})
|
2022-11-05 11:40:46 +00:00
|
|
|
.path(repo_dir.path())
|
2020-10-23 15:49:19 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!(
|
|
|
|
"{} ",
|
|
|
|
Color::Green
|
|
|
|
.bold()
|
|
|
|
.paint(format!("({})", expected_output.trim()))
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_render_commit_hash_only_detached_on_detached_with_tag_enabled() -> io::Result<()> {
|
2021-03-25 20:03:19 +00:00
|
|
|
let repo_dir = fixture_repo(FixtureProvider::Git)?;
|
2020-10-23 15:49:19 +00:00
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
create_command("git")?
|
2022-11-05 11:40:46 +00:00
|
|
|
.args(["checkout", "@~1"])
|
|
|
|
.current_dir(repo_dir.path())
|
2020-10-23 15:49:19 +00:00
|
|
|
.output()?;
|
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
create_command("git")?
|
2022-11-05 11:40:46 +00:00
|
|
|
.args(["tag", "tagOnDetached", "-m", "Testing tags on detached"])
|
|
|
|
.current_dir(repo_dir.path())
|
2020-10-23 15:49:19 +00:00
|
|
|
.output()?;
|
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
let mut git_commit = create_command("git")?
|
2022-11-05 11:40:46 +00:00
|
|
|
.args(["rev-parse", "HEAD"])
|
|
|
|
.current_dir(repo_dir.path())
|
2020-10-23 15:49:19 +00:00
|
|
|
.output()?
|
|
|
|
.stdout;
|
|
|
|
git_commit.truncate(7);
|
|
|
|
let commit_output = str::from_utf8(&git_commit).unwrap().trim();
|
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
let git_tag = create_command("git")?
|
2022-11-05 11:40:46 +00:00
|
|
|
.args(["describe", "--tags", "--exact-match", "HEAD"])
|
|
|
|
.current_dir(repo_dir.path())
|
2020-10-23 15:49:19 +00:00
|
|
|
.output()?
|
|
|
|
.stdout;
|
|
|
|
let tag_output = str::from_utf8(&git_tag).unwrap().trim();
|
|
|
|
|
2022-11-05 11:40:46 +00:00
|
|
|
let expected_output = format!("{commit_output} {tag_output}");
|
2020-10-23 15:49:19 +00:00
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("git_commit")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[git_commit]
|
|
|
|
tag_disabled = false
|
2021-05-08 19:01:00 +00:00
|
|
|
tag_symbol = " "
|
2020-10-23 15:49:19 +00:00
|
|
|
})
|
2022-11-05 11:40:46 +00:00
|
|
|
.path(repo_dir.path())
|
2020-10-23 15:49:19 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!(
|
|
|
|
"{} ",
|
|
|
|
Color::Green
|
|
|
|
.bold()
|
|
|
|
.paint(format!("({})", expected_output.trim()))
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-12-07 13:56:22 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_latest_tag_shown_with_tag_enabled() -> io::Result<()> {
|
2021-03-25 20:03:19 +00:00
|
|
|
let repo_dir = fixture_repo(FixtureProvider::Git)?;
|
2020-12-07 13:56:22 +00:00
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
let mut git_commit = create_command("git")?
|
2022-11-05 11:40:46 +00:00
|
|
|
.args(["rev-parse", "HEAD"])
|
|
|
|
.current_dir(repo_dir.path())
|
2020-12-07 13:56:22 +00:00
|
|
|
.output()?
|
|
|
|
.stdout;
|
|
|
|
git_commit.truncate(7);
|
|
|
|
let commit_output = str::from_utf8(&git_commit).unwrap().trim();
|
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
create_command("git")?
|
2022-11-05 11:40:46 +00:00
|
|
|
.args(["tag", "v2", "-m", "Testing tags v2"])
|
2022-11-22 09:15:31 +00:00
|
|
|
.env("GIT_COMMITTER_DATE", "2022-01-01 00:00:00 +0000")
|
2022-11-05 11:40:46 +00:00
|
|
|
.current_dir(repo_dir.path())
|
2020-12-07 13:56:22 +00:00
|
|
|
.output()?;
|
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
create_command("git")?
|
2022-11-05 11:40:46 +00:00
|
|
|
.args(["tag", "v0", "-m", "Testing tags v0", "HEAD~1"])
|
2022-11-22 09:15:31 +00:00
|
|
|
.env("GIT_COMMITTER_DATE", "2022-01-01 00:00:01 +0000")
|
2022-11-05 11:40:46 +00:00
|
|
|
.current_dir(repo_dir.path())
|
2020-12-07 13:56:22 +00:00
|
|
|
.output()?;
|
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
create_command("git")?
|
2022-11-05 11:40:46 +00:00
|
|
|
.args(["tag", "v1", "-m", "Testing tags v1"])
|
2022-11-22 09:15:31 +00:00
|
|
|
.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")
|
2022-11-05 11:40:46 +00:00
|
|
|
.current_dir(repo_dir.path())
|
2020-12-07 13:56:22 +00:00
|
|
|
.output()?;
|
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
let git_tag = create_command("git")?
|
2022-11-05 11:40:46 +00:00
|
|
|
.args([
|
2020-12-07 13:56:22 +00:00
|
|
|
"for-each-ref",
|
|
|
|
"--contains",
|
|
|
|
"HEAD",
|
|
|
|
"--sort=-taggerdate",
|
|
|
|
"--count=1",
|
|
|
|
"--format",
|
|
|
|
"%(refname:short)",
|
|
|
|
"refs/tags",
|
|
|
|
])
|
2022-11-05 11:40:46 +00:00
|
|
|
.current_dir(repo_dir.path())
|
2020-12-07 13:56:22 +00:00
|
|
|
.output()?
|
|
|
|
.stdout;
|
|
|
|
let tag_output = str::from_utf8(&git_tag).unwrap().trim();
|
|
|
|
|
2022-11-05 11:40:46 +00:00
|
|
|
let expected_output = format!("{commit_output} {tag_output}");
|
2020-12-07 13:56:22 +00:00
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("git_commit")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[git_commit]
|
|
|
|
only_detached = false
|
|
|
|
tag_disabled = false
|
2021-05-08 19:01:00 +00:00
|
|
|
tag_symbol = " "
|
2020-12-07 13:56:22 +00:00
|
|
|
})
|
2022-11-05 11:40:46 +00:00
|
|
|
.path(repo_dir.path())
|
2020-12-07 13:56:22 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!(
|
|
|
|
"{} ",
|
|
|
|
Color::Green
|
|
|
|
.bold()
|
|
|
|
.paint(format!("({})", expected_output.trim()))
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-08-07 19:13:12 +00:00
|
|
|
}
|