1
1
mirror of https://github.com/orhun/git-cliff.git synced 2024-11-29 15:42:15 +03:00

feat(config)!: support regex in 'tag_pattern' configuration (#318)

resolves #289

BREAKING CHANGE: `tag_pattern` now takes a valid regular expression instead of a glob pattern
This commit is contained in:
Jean SIMARD 2023-10-29 13:05:38 +01:00 committed by GitHub
parent 213f383b54
commit 3c2fb60726
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 67 additions and 22 deletions

View File

@ -91,7 +91,7 @@ protect_breaking_commits = false
# filter out the commits that are not matched by commit parsers
filter_commits = false
# glob pattern for matching git tags
tag_pattern = "v[0-9]*"
tag_pattern = "^v[0-9].*"
# regex for skipping tags
skip_tags = "beta|alpha"
# regex for ignoring tags

View File

@ -69,7 +69,7 @@ protect_breaking_commits = false
# filter out the commits that are not matched by commit parsers
filter_commits = false
# glob pattern for matching git tags
tag_pattern = "v[0-9]*"
tag_pattern = "^v[0-9].*"
# regex for skipping tags
skip_tags = "v0.1.0-beta.1"
# regex for ignoring tags

View File

@ -79,7 +79,7 @@ protect_breaking_commits = false
# filter out the commits that are not matched by commit parsers
filter_commits = false
# glob pattern for matching git tags
tag_pattern = "v[0-9]*"
tag_pattern = "^v[0-9].*"
# regex for skipping tags
skip_tags = "v0.1.0-beta.1"
# regex for ignoring tags

View File

@ -66,7 +66,7 @@ protect_breaking_commits = false
# filter out the commits that are not matched by commit parsers
filter_commits = false
# glob pattern for matching git tags
tag_pattern = "v[0-9]*"
tag_pattern = "^v[0-9].*"
# regex for skipping tags
skip_tags = "v0.1.0-beta.1"
# regex for ignoring tags

View File

@ -55,7 +55,7 @@ protect_breaking_commits = false
# filter out the commits that are not matched by commit parsers
filter_commits = true
# glob pattern for matching git tags
tag_pattern = "v[0-9]*"
tag_pattern = "^v[0-9].*"
# regex for skipping tags
skip_tags = "v0.1.0-beta.1"
# regex for ignoring tags

View File

@ -57,7 +57,7 @@ protect_breaking_commits = false
# filter out the commits that are not matched by commit parsers
filter_commits = false
# glob pattern for matching git tags
tag_pattern = "v[0-9]*"
tag_pattern = "^v[0-9].*"
# regex for skipping tags
skip_tags = "v0.1.0-beta.1"
# regex for ignoring tags

View File

@ -70,7 +70,7 @@ protect_breaking_commits = false
# filter out the commits that are not matched by commit parsers
filter_commits = false
# glob pattern for matching git tags
tag_pattern = "v[0-9]*"
tag_pattern = "^v[0-9].*"
# regex for skipping tags
skip_tags = "v0.1.0-beta.1"
# regex for ignoring tags

View File

@ -55,7 +55,7 @@ protect_breaking_commits = false
# filter out the commits that are not matched by commit parsers
filter_commits = false
# glob pattern for matching git tags
tag_pattern = "v[0-9]*"
tag_pattern = "^v[0-9].*"
# regex for skipping tags
skip_tags = "v0.1.0-beta.1"
# regex for ignoring tags

View File

@ -68,7 +68,8 @@ pub struct GitConfig {
/// Whether to filter out commits.
pub filter_commits: Option<bool>,
/// Blob pattern for git tags.
pub tag_pattern: Option<String>,
#[serde(with = "serde_regex", default)]
pub tag_pattern: Option<Regex>,
/// Regex to skip matched tags.
#[serde(with = "serde_regex", default)]
pub skip_tags: Option<Regex>,
@ -199,7 +200,7 @@ mod test {
.join(crate::DEFAULT_CONFIG);
const FOOTER_VALUE: &str = "test";
const TAG_PATTERN_VALUE: &str = "*[0-9]*";
const TAG_PATTERN_VALUE: &str = ".*[0-9].*";
const IGNORE_TAGS_VALUE: &str = "v[0-9]+.[0-9]+.[0-9]+-rc[0-9]+";
env::set_var("GIT_CLIFF__CHANGELOG__FOOTER", FOOTER_VALUE);
@ -211,7 +212,10 @@ mod test {
assert_eq!(Some(String::from(FOOTER_VALUE)), config.changelog.footer);
assert_eq!(
Some(String::from(TAG_PATTERN_VALUE)),
config.git.tag_pattern
config
.git
.tag_pattern
.map(|tag_pattern| tag_pattern.to_string())
);
assert_eq!(
Some(String::from(IGNORE_TAGS_VALUE)),

View File

@ -10,6 +10,7 @@ use git2::{
};
use glob::Pattern;
use indexmap::IndexMap;
use regex::Regex;
use std::io;
use std::path::PathBuf;
@ -102,12 +103,19 @@ impl Repository {
/// It collects lightweight and annotated tags.
pub fn tags(
&self,
pattern: &Option<String>,
pattern: &Option<Regex>,
topo_order: bool,
) -> Result<IndexMap<String, String>> {
let mut tags: Vec<(Commit, String)> = Vec::new();
let tag_names = self.inner.tag_names(pattern.as_deref())?;
for name in tag_names.iter().flatten().map(String::from) {
let tag_names = self.inner.tag_names(None)?;
for name in tag_names
.iter()
.flatten()
.filter(|tag_name| {
pattern.as_ref().map_or(true, |pat| pat.is_match(tag_name))
})
.map(String::from)
{
let obj = self.inner.revparse_single(&name)?;
if let Ok(commit) = obj.clone().into_commit() {
tags.push((commit, name));
@ -191,4 +199,43 @@ mod test {
assert_eq!(&get_last_tag()?, tags.last().expect("no tags found").1);
Ok(())
}
#[test]
fn git_tags() -> Result<()> {
let repository = Repository::init(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("parent directory not found")
.to_path_buf(),
)?;
let tags = repository.tags(&None, true)?;
assert_eq!(
tags.get("2b8b4d3535f29231e05c3572e919634b9af907b6").expect(
"the commit hash does not exist in the repository (tag v0.1.0)"
),
"v0.1.0"
);
assert_eq!(
tags.get("4ddef08debfff48117586296e49d5caa0800d1b5").expect(
"the commit hash does not exist in the repository (tag \
v0.1.0-beta.4)"
),
"v0.1.0-beta.4"
);
let tags = repository.tags(
&Some(
Regex::new("^v[0-9]+\\.[0-9]+\\.[0-9]$")
.expect("the regex is not valid"),
),
true,
)?;
assert_eq!(
tags.get("2b8b4d3535f29231e05c3572e919634b9af907b6").expect(
"the commit hash does not exist in the repository (tag v0.1.0)"
),
"v0.1.0"
);
assert!(!tags.contains_key("4ddef08debfff48117586296e49d5caa0800d1b5"));
Ok(())
}
}

View File

@ -83,7 +83,7 @@ commit_parsers = [
]
protect_breaking_commits = false
filter_commits = false
tag_pattern = "v[0-9]*"
tag_pattern = "^v[0-9].*"
skip_tags = "v0.1.0-beta.1"
ignore_tags = ""
topo_order = false
@ -241,13 +241,7 @@ If set to `true`, commits that are not matched by [`commit_parsers`](#commit_par
### tag_pattern
A glob pattern for matching the git tags.
e.g. It processes the same tags as the output of the following git command:
```bash
git tag --list 'v[0-9]*'
```
A regular expression for matching the git tags.
### skip_tags