Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Oct 29, 2023
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
2 changes: 1 addition & 1 deletion cliff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion config/cliff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/cocogitto.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/detailed.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/keepachangelog.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/scoped.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/scopesorted.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/unconventional.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions git-cliff-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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);
Expand All @@ -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)),
Expand Down
53 changes: 50 additions & 3 deletions git-cliff-core/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use git2::{
};
use glob::Pattern;
use indexmap::IndexMap;
use regex::Regex;
use std::io;
use std::path::PathBuf;

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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(())
}
}
10 changes: 2 additions & 8 deletions website/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
Loading