Skip to content
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
109 changes: 108 additions & 1 deletion git-cliff-core/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io::{Read, Write};
use std::time::{SystemTime, UNIX_EPOCH};

use crate::config::Config;
use crate::error::Result;
use crate::error::{Error as AppError, Result};
use crate::process::CommitProcessor;
use crate::release::{Release, Releases};
#[cfg(feature = "azure_devops")]
Expand Down Expand Up @@ -117,6 +117,7 @@ impl<'a> Changelog<'a> {
fn process_commits(&mut self) -> Result<()> {
crate::set_progress_message!("Processing commits for the changelog");
tracing::debug!("Processing the commits");
self.expand_commit_parser_shas()?;

let mut summary = Summary::default();
for release in &mut self.releases {
Expand Down Expand Up @@ -147,6 +148,43 @@ impl<'a> Changelog<'a> {
Ok(())
}

/// Expands abbreviated SHAs (at least 4 characters) in the commit parsers
/// to the full SHA of the matching commit. Like git, this fails if an
/// abbreviated SHA matches more than one commit.
fn expand_commit_parser_shas(&mut self) -> Result<()> {
let ids = self
.releases
.iter()
.flat_map(|release| {
release
.commits
.iter()
.chain(release.submodule_commits.values().flatten())
})
.map(|commit| commit.id.as_str())
.collect::<HashSet<_>>();
for parser in &mut self.config.git.commit_parsers {
let Some(sha) = parser.sha.as_mut() else {
continue;
};
let prefix = sha.to_lowercase();
if prefix.len() < 4 || ids.contains(prefix.as_str()) {
continue;
}
let mut matches = ids.iter().filter(|id| id.starts_with(&prefix));
if let Some(id) = matches.next() {
if matches.next().is_some() {
let count = 2 + matches.count();
return Err(AppError::ChangelogError(format!(
"commit parser SHA `{sha}` is ambiguous, it matches {count} commits"
)));
}
*sha = (*id).to_string();
}
}
Ok(())
}

/// Processes the releases and filters them out based on the configuration.
#[cfg_attr(
feature = "tracing",
Expand Down Expand Up @@ -1433,6 +1471,75 @@ mod test {
(config, releases)
}

fn sha_parser(sha: &str, group: &str) -> CommitParser {
CommitParser {
sha: Some(String::from(sha)),
group: Some(String::from(group)),
..Default::default()
}
}

fn sha_test_release() -> Release<'static> {
Release {
version: Some(String::from("v1.0.0")),
commits: ["abc1234def", "abc1299", "fff0000"]
.into_iter()
.map(|id| Commit::new(String::from(id), String::from("feat: add feature")))
.collect(),
..Default::default()
}
}

#[test]
fn changelog_resolves_abbreviated_commit_parser_shas() -> Result<()> {
let (mut config, _) = get_test_data();
config.git.commit_parsers = vec![
sha_parser("ABC123", "Abbreviated"),
sha_parser("fff0000", "Full"),
sha_parser("abc", "Too short"),
sha_parser("dead", "No match"),
];
let changelog = Changelog::new(vec![sha_test_release()], config, None)?;
let shas = changelog
.config
.git
.commit_parsers
.iter()
.map(|parser| parser.sha.as_deref())
.collect::<Vec<_>>();
assert_eq!(shas, vec![
Some("abc1234def"),
Some("fff0000"),
Some("abc"),
Some("dead")
]);
let group_of = |id: &str| {
changelog.releases[0]
.commits
.iter()
.find(|commit| commit.id == id)
.and_then(|commit| commit.group.clone())
};
assert_eq!(group_of("abc1234def").as_deref(), Some("Abbreviated"));
assert_eq!(group_of("fff0000").as_deref(), Some("Full"));
Ok(())
}

#[test]
fn changelog_rejects_ambiguous_commit_parser_sha() {
let (mut config, _) = get_test_data();
config.git.commit_parsers = vec![sha_parser("abc12", "Ambiguous")];
let Err(error) = Changelog::new(vec![sha_test_release()], config, None) else {
panic!("ambiguous SHA must be rejected");
};
assert!(
error
.to_string()
.contains("`abc12` is ambiguous, it matches 2 commits"),
"{error}"
);
}

#[test]
fn changelog_generator() -> Result<()> {
let (config, releases) = get_test_data();
Expand Down
2 changes: 2 additions & 0 deletions website/docs/configuration/git.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ Examples:
- Skip a specific commit by using its SHA1.
- `{ sha = "f6f2472bdf0bbb5f9fcaf2d72c1fa9f98f772bb2", group = "Stuff" }`
- Set the group of the commit by using its SHA1.
- `{ sha = "f6f2472", group = "Stuff" }`
- SHAs can be abbreviated (at least 4 characters). An abbreviated SHA that matches more than one commit is an error.
- `{ field = "author.name", pattern = "John Doe", group = "John's stuff" }`
- If the author's name attribute of the commit matches the pattern "John Doe" (as a regex), override the scope with "John's stuff".
- All values that are part of the commit context can be used. Nested fields can be accessed via the [dot notation](https://keats.github.io/tera/#dot-notation). Some commonly used ones are:
Expand Down
Loading