2022-03-26 09:42:19 +00:00
|
|
|
use super::{Context, Module, ModuleConfig};
|
2021-07-04 13:29:21 +00:00
|
|
|
use crate::formatter::string_formatter::StringFormatterError;
|
2020-12-07 13:56:22 +00:00
|
|
|
use git2::Time;
|
2019-12-06 16:57:42 +00:00
|
|
|
|
|
|
|
use crate::configs::git_commit::GitCommitConfig;
|
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()?;
|
2021-12-03 20:15:31 +00:00
|
|
|
let git_repo = repo.open().ok()?;
|
2019-12-06 16:57:42 +00:00
|
|
|
|
2020-02-12 19:56:29 +00:00
|
|
|
let is_detached = git_repo.head_detached().ok()?;
|
|
|
|
if config.only_detached && !is_detached {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
2019-12-06 16:57:42 +00:00
|
|
|
let git_head = git_repo.head().ok()?;
|
|
|
|
let head_commit = git_head.peel_to_commit().ok()?;
|
|
|
|
let commit_oid = head_commit.id();
|
2020-07-07 22:45:32 +00:00
|
|
|
|
2021-07-04 13:29:21 +00:00
|
|
|
let mut tag_name = String::new();
|
2020-10-23 15:49:19 +00:00
|
|
|
if !config.tag_disabled {
|
|
|
|
// Let's get repo tags names
|
|
|
|
let tag_names = git_repo.tag_names(None).ok()?;
|
2021-05-19 17:08:48 +00:00
|
|
|
let tag_and_refs = tag_names.iter().flatten().flat_map(|name| {
|
|
|
|
let full_tag = format!("refs/tags/{}", name);
|
2020-12-07 13:56:22 +00:00
|
|
|
let tag_obj = git_repo.find_reference(&full_tag)?.peel_to_tag()?;
|
2021-05-19 17:08:48 +00:00
|
|
|
let sig_obj = tag_obj.tagger();
|
|
|
|
git_repo.find_reference(&full_tag).map(|reference| {
|
|
|
|
(
|
|
|
|
String::from(name),
|
|
|
|
// fall back to oldest + 1s time if sig_obj is unavailable
|
|
|
|
sig_obj.map_or(git2::Time::new(1, 0), |s| s.when()),
|
|
|
|
reference,
|
|
|
|
)
|
|
|
|
})
|
2020-10-23 15:49:19 +00:00
|
|
|
});
|
|
|
|
|
2020-12-07 13:56:22 +00:00
|
|
|
let mut oldest = Time::new(0, 0);
|
|
|
|
// Let's check if HEAD has some tag. If several, gets last created one...
|
|
|
|
for (name, timestamp, reference) in tag_and_refs.rev() {
|
|
|
|
if commit_oid == reference.peel_to_commit().ok()?.id() && timestamp > oldest {
|
2020-10-23 15:49:19 +00:00
|
|
|
tag_name = name;
|
2020-12-07 13:56:22 +00:00
|
|
|
oldest = timestamp;
|
2020-10-23 15:49:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 {
|
|
|
|
"hash" => Some(Ok(id_to_hex_abbrev(
|
|
|
|
commit_oid.as_bytes(),
|
|
|
|
config.commit_hash_length,
|
|
|
|
))),
|
|
|
|
"tag" => format_tag(config.tag_symbol, &tag_name),
|
|
|
|
_ => 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)
|
|
|
|
}
|
|
|
|
|
2021-07-04 13:29:21 +00:00
|
|
|
fn format_tag(symbol: &str, tag_name: &str) -> Option<Result<String, StringFormatterError>> {
|
|
|
|
if tag_name.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(Ok(format!("{}{}", &symbol, &tag_name)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 16:57:42 +00:00
|
|
|
/// len specifies length of hex encoded string
|
2021-07-04 13:29:21 +00:00
|
|
|
fn id_to_hex_abbrev(bytes: &[u8], len: usize) -> String {
|
2019-12-06 16:57:42 +00:00
|
|
|
bytes
|
|
|
|
.iter()
|
|
|
|
.map(|b| format!("{:02x}", b))
|
2022-05-23 10:58:27 +00:00
|
|
|
.collect::<String>()
|
2019-12-06 16:57:42 +00:00
|
|
|
.chars()
|
|
|
|
.take(len)
|
|
|
|
.collect()
|
|
|
|
}
|
2020-08-07 19:13:12 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use ansi_term::Color;
|
|
|
|
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")
|
|
|
|
.path(&repo_dir.path())
|
|
|
|
.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")?
|
2020-08-07 19:13:12 +00:00
|
|
|
.args(&["rev-parse", "HEAD"])
|
|
|
|
.current_dir(&repo_dir.path())
|
|
|
|
.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
|
|
|
|
})
|
|
|
|
.path(&repo_dir.path())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!(
|
|
|
|
"{} ",
|
2021-12-03 06:54:56 +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")?
|
2020-08-07 19:13:12 +00:00
|
|
|
.args(&["rev-parse", "HEAD"])
|
|
|
|
.current_dir(&repo_dir.path())
|
|
|
|
.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
|
|
|
|
})
|
|
|
|
.path(&repo_dir.path())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!(
|
|
|
|
"{} ",
|
2021-12-03 06:54:56 +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")
|
|
|
|
.path(&repo_dir.path())
|
|
|
|
.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")?
|
2020-08-07 19:13:12 +00:00
|
|
|
.args(&["checkout", "@~1"])
|
|
|
|
.current_dir(&repo_dir.path())
|
|
|
|
.output()?;
|
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
let mut git_output = create_command("git")?
|
2020-08-07 19:13:12 +00:00
|
|
|
.args(&["rev-parse", "HEAD"])
|
|
|
|
.current_dir(&repo_dir.path())
|
|
|
|
.output()?
|
|
|
|
.stdout;
|
|
|
|
git_output.truncate(7);
|
|
|
|
let expected_hash = str::from_utf8(&git_output).unwrap();
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("git_commit")
|
|
|
|
.path(&repo_dir.path())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!(
|
|
|
|
"{} ",
|
2021-12-03 06:54:56 +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
|
|
|
|
|
|
|
#[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
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
let mut git_commit = create_command("git")?
|
2020-10-23 15:49:19 +00:00
|
|
|
.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();
|
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
let git_tag = create_command("git")?
|
2020-10-23 15:49:19 +00:00
|
|
|
.args(&["describe", "--tags", "--exact-match", "HEAD"])
|
|
|
|
.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_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")?
|
2020-10-23 15:49:19 +00:00
|
|
|
.args(&["checkout", "@~1"])
|
|
|
|
.current_dir(&repo_dir.path())
|
|
|
|
.output()?;
|
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
create_command("git")?
|
2020-10-23 15:49:19 +00:00
|
|
|
.args(&["tag", "tagOnDetached", "-m", "Testing tags on detached"])
|
|
|
|
.current_dir(&repo_dir.path())
|
|
|
|
.output()?;
|
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
let mut git_commit = create_command("git")?
|
2020-10-23 15:49:19 +00:00
|
|
|
.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();
|
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
let git_tag = create_command("git")?
|
2020-10-23 15:49:19 +00:00
|
|
|
.args(&["describe", "--tags", "--exact-match", "HEAD"])
|
|
|
|
.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]
|
|
|
|
tag_disabled = false
|
2021-05-08 19:01:00 +00:00
|
|
|
tag_symbol = " "
|
2020-10-23 15:49:19 +00:00
|
|
|
})
|
|
|
|
.path(&repo_dir.path())
|
|
|
|
.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<()> {
|
|
|
|
use std::{thread, time};
|
|
|
|
|
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")?
|
2020-12-07 13:56:22 +00:00
|
|
|
.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();
|
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
create_command("git")?
|
2020-12-07 13:56:22 +00:00
|
|
|
.args(&["tag", "v2", "-m", "Testing tags v2"])
|
|
|
|
.current_dir(&repo_dir.path())
|
|
|
|
.output()?;
|
|
|
|
|
|
|
|
// Wait one second between tags
|
|
|
|
thread::sleep(time::Duration::from_millis(1000));
|
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
create_command("git")?
|
2020-12-07 13:56:22 +00:00
|
|
|
.args(&["tag", "v0", "-m", "Testing tags v0", "HEAD~1"])
|
|
|
|
.current_dir(&repo_dir.path())
|
|
|
|
.output()?;
|
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
create_command("git")?
|
2020-12-07 13:56:22 +00:00
|
|
|
.args(&["tag", "v1", "-m", "Testing tags v1"])
|
|
|
|
.current_dir(&repo_dir.path())
|
|
|
|
.output()?;
|
|
|
|
|
2021-07-16 19:20:59 +00:00
|
|
|
let git_tag = create_command("git")?
|
2020-12-07 13:56:22 +00:00
|
|
|
.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
|
2021-05-08 19:01:00 +00:00
|
|
|
tag_symbol = " "
|
2020-12-07 13:56:22 +00:00
|
|
|
})
|
|
|
|
.path(&repo_dir.path())
|
|
|
|
.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
|
|
|
}
|