Skip to content

Commit

Permalink
feat(parser): support using SHA1 of the commit (#385)
Browse files Browse the repository at this point in the history
* feat(parser): support using SHA1 of the commit

* fix(parser): make the SHA1 matcher case insensitive
  • Loading branch information
orhun committed Dec 18, 2023
1 parent dd27a9a commit 1039f85
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 7 deletions.
64 changes: 64 additions & 0 deletions git-cliff-core/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,40 @@ mod test {
}]),
commit_parsers: Some(vec![
CommitParser {
sha: Some(String::from("tea")),
message: None,
body: None,
group: Some(String::from("I love tea")),
default_scope: None,
scope: None,
skip: None,
field: None,
pattern: None,
},
CommitParser {
sha: Some(String::from("coffee")),
message: None,
body: None,
group: None,
default_scope: None,
scope: None,
skip: Some(true),
field: None,
pattern: None,
},
CommitParser {
sha: Some(String::from("coffee2")),
message: None,
body: None,
group: None,
default_scope: None,
scope: None,
skip: Some(true),
field: None,
pattern: None,
},
CommitParser {
sha: None,
message: Regex::new(r".*merge.*").ok(),
body: None,
group: None,
Expand All @@ -259,6 +293,7 @@ mod test {
pattern: None,
},
CommitParser {
sha: None,
message: Regex::new("feat*").ok(),
body: None,
group: Some(String::from("New features")),
Expand All @@ -269,6 +304,7 @@ mod test {
pattern: None,
},
CommitParser {
sha: None,
message: Regex::new("^fix*").ok(),
body: None,
group: Some(String::from("Bug Fixes")),
Expand All @@ -279,6 +315,7 @@ mod test {
pattern: None,
},
CommitParser {
sha: None,
message: Regex::new("doc:").ok(),
body: None,
group: Some(String::from("Documentation")),
Expand All @@ -289,6 +326,7 @@ mod test {
pattern: None,
},
CommitParser {
sha: None,
message: Regex::new("docs:").ok(),
body: None,
group: Some(String::from("Documentation")),
Expand All @@ -299,6 +337,7 @@ mod test {
pattern: None,
},
CommitParser {
sha: None,
message: Regex::new(r"match\((.*)\):.*").ok(),
body: None,
group: Some(String::from("Matched ($1)")),
Expand All @@ -309,6 +348,7 @@ mod test {
pattern: None,
},
CommitParser {
sha: None,
message: Regex::new(".*").ok(),
body: None,
group: Some(String::from("Other")),
Expand All @@ -333,6 +373,14 @@ mod test {
let test_release = Release {
version: Some(String::from("v1.0.0")),
commits: vec![
Commit::new(
String::from("coffee"),
String::from("revert(app): skip this commit"),
),
Commit::new(
String::from("tea"),
String::from("feat(app): damn right"),
),
Commit::new(
String::from("0bc123"),
String::from("feat(app): add cool features"),
Expand Down Expand Up @@ -377,6 +425,10 @@ mod test {
String::from("qwert0"),
String::from("match(group): support regex-replace for groups"),
),
Commit::new(
String::from("coffee"),
String::from("revert(app): skip this commit"),
),
],
commit_id: Some(String::from("0bc123")),
timestamp: 50000000,
Expand Down Expand Up @@ -416,6 +468,10 @@ mod test {
String::from("hjkl12"),
String::from("chore(ui): do boring stuff"),
),
Commit::new(
String::from("coffee2"),
String::from("revert(app): skip this commit"),
),
],
commit_id: None,
timestamp: 1000,
Expand Down Expand Up @@ -463,6 +519,10 @@ mod test {
- update docs
- add some documentation
### I love tea
#### app
- damn right
### Matched (group)
#### group
- support regex-replace for groups
Expand Down Expand Up @@ -574,6 +634,10 @@ chore(deps): fix broken deps
- update docs
- add some documentation
### I love tea
#### app
- damn right
### Matched (group)
#### group
- support regex-replace for groups
Expand Down
64 changes: 64 additions & 0 deletions git-cliff-core/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,21 @@ impl Commit<'_> {
})?,
));
}
if parser.sha.clone().map(|v| v.to_lowercase()).as_deref() ==
Some(&self.id)
{
if self.skip_commit(parser, protect_breaking) {
return Err(AppError::GroupError(String::from(
"Skipping commit",
)));
} else {
self.group = parser.group.clone().or(self.group);
self.scope = parser.scope.clone().or(self.scope);
self.default_scope =
parser.default_scope.clone().or(self.default_scope);
return Ok(self);
}
}
for (regex, text) in regex_checks {
if regex.is_match(&text) {
if self.skip_commit(parser, protect_breaking) {
Expand Down Expand Up @@ -446,6 +461,7 @@ mod test {
}
let commit = test_cases[0].0.clone().parse(
&[CommitParser {
sha: None,
message: Regex::new("test*").ok(),
body: None,
group: Some(String::from("test_group")),
Expand Down Expand Up @@ -618,6 +634,7 @@ mod test {

let parsed_commit = commit.parse(
&[CommitParser {
sha: None,
message: None,
body: None,
group: Some(String::from("Test group")),
Expand All @@ -634,4 +651,51 @@ mod test {
assert_eq!(Some(String::from("Test group")), parsed_commit.group);
Ok(())
}

#[test]
fn commit_sha() -> Result<()> {
let commit = Commit::new(
String::from("8f55e69eba6e6ce811ace32bd84cc82215673cb6"),
String::from("feat: do something"),
);
let parsed_commit = commit.clone().parse(
&[CommitParser {
sha: Some(String::from(
"8f55e69eba6e6ce811ace32bd84cc82215673cb6",
)),
message: None,
body: None,
group: None,
default_scope: None,
scope: None,
skip: Some(true),
field: None,
pattern: None,
}],
false,
false,
);
assert!(parsed_commit.is_err());

let parsed_commit = commit.parse(
&[CommitParser {
sha: Some(String::from(
"8f55e69eba6e6ce811ace32bd84cc82215673cb6",
)),
message: None,
body: None,
group: Some(String::from("Test group")),
default_scope: None,
scope: None,
skip: None,
field: None,
pattern: None,
}],
false,
false,
)?;
assert_eq!(Some(String::from("Test group")), parsed_commit.group);

Ok(())
}
}
2 changes: 2 additions & 0 deletions git-cliff-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ pub struct GitConfig {
/// Parser for grouping commits.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommitParser {
/// SHA1 of the commit.
pub sha: Option<String>,
/// Regex for matching the commit message.
#[serde(with = "serde_regex", default)]
pub message: Option<Regex>,
Expand Down
16 changes: 16 additions & 0 deletions git-cliff-core/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ fn generate_changelog() -> Result<()> {
}]),
commit_parsers: Some(vec![
CommitParser {
sha: Some(String::from("coffee")),
message: None,
body: None,
group: Some(String::from("I love coffee")),
default_scope: None,
scope: None,
skip: None,
field: None,
pattern: None,
},
CommitParser {
sha: None,
message: Regex::new("^feat").ok(),
body: None,
group: Some(String::from("shiny features")),
Expand All @@ -62,6 +74,7 @@ fn generate_changelog() -> Result<()> {
pattern: None,
},
CommitParser {
sha: None,
message: Regex::new("^fix").ok(),
body: None,
group: Some(String::from("fix bugs")),
Expand All @@ -72,6 +85,7 @@ fn generate_changelog() -> Result<()> {
pattern: None,
},
CommitParser {
sha: None,
message: Regex::new("^test").ok(),
body: None,
group: None,
Expand All @@ -82,6 +96,7 @@ fn generate_changelog() -> Result<()> {
pattern: None,
},
CommitParser {
sha: None,
message: None,
body: None,
group: Some(String::from("docs")),
Expand Down Expand Up @@ -129,6 +144,7 @@ fn generate_changelog() -> Result<()> {
Release {
version: Some(String::from("v2.0.0")),
commits: vec![

Commit::new(
String::from("000abc"),
String::from("Add unconventional commit"),
Expand Down
19 changes: 12 additions & 7 deletions website/docs/configuration.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
sidebar_position: 4
---

# Configuration

**git-cliff** configuration file supports [TOML](https://github.com/toml-lang/toml) (preferred) and [YAML](https://yaml.org) formats.
Expand Down Expand Up @@ -213,21 +214,25 @@ An array of commit parsers for determining the commit groups by using regex.

Examples:

- `{ message = "^feat", group = "Features"}`
- `{ message = "^feat", group = "Features" }`
- Group the commit as "Features" if the commit message (description) starts with "feat".
- `{ body = ".*security", group = "Security"}`
- `{ body = ".*security", group = "Security" }`
- Group the commit as "Security" if the commit body contains "security".
- `{ message = '^fix\((.*)\)', group = 'Fix (${1})' }`
- Use the matched scope value from the commit message in the group name.
- `{ message = ".*deprecated", body = ".*deprecated", group = "Deprecation"}`
- `{ message = ".*deprecated", body = ".*deprecated", group = "Deprecation" }`
- Group the commit as "Deprecation" if the commit body and message contains "deprecated".
- `{ message = "^revert", skip = true}`
- `{ message = "^revert", skip = true }`
- Skip processing the commit if the commit message (description) starts with "revert".
- `{ message = "^doc", group = "Documentation", default_scope = "other"},`
- `{ message = "^doc", group = "Documentation", default_scope = "other" },`
- If the commit starts with "doc", group the commit as "Documentation" and set the default scope to "other". (e.g. `docs: xyz` will be processed as `docs(other): xyz`)
- `{ message = "(www)", scope = "Application"}`
- `{ message = "(www)", scope = "Application" }`
- If the commit contains "(www)", override the scope with "Application". Scoping order is: scope specification, conventional commit's scope and default scope.
- `{ field = "author.name", pattern = "John Doe", group = "John's stuff"}`
- `{ sha = "f6f2472bdf0bbb5f9fcaf2d72c1fa9f98f772bb2", skip = true }`
- Skip a specific commit by using its SHA1.
- `{ sha = "f6f2472bdf0bbb5f9fcaf2d72c1fa9f98f772bb2", group = "Stuff" }`
- Set the group of the commit by using its SHA1.
- `{ 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' stuff". Supported commit attributes are:
- `id`
- `message`
Expand Down

0 comments on commit 1039f85

Please sign in to comment.