Skip to content

Commit

Permalink
feat(changelog): support setting commit SHA while using --with-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Aug 12, 2022
1 parent 1b5dcc1 commit d453d4c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 4 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ git cliff --with-commit "$commit_msg" -o CHANGELOG.md
git add CHANGELOG.md && git commit -m "$commit_msg"
```

> The commit SHA will be empty as default when `--with-commit` is used. Specify the hash with a message separated by single whitespace for setting the commit SHA. e.g. "--with-commit "8f55e69eba6e6ce811ace32bd84cc82215673cb6 feat: add X""
Sort the commits inside sections:

```sh
Expand Down
1 change: 1 addition & 0 deletions git-cliff-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ serde_regex = "1.1.0"
indexmap = "1.9.1"
toml = "0.5.9"
glob = "0.3.0"
lazy-regex = "2.3.0"

[dependencies.git2]
version = "0.14.4"
Expand Down
56 changes: 56 additions & 0 deletions git-cliff-core/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@ use crate::error::{
};
use git2::Commit as GitCommit;
use git_conventional::Commit as ConventionalCommit;
use lazy_regex::{
lazy_regex,
Lazy,
Regex,
};
use serde::ser::{
Serialize,
SerializeStruct,
Serializer,
};

/// Regular expression for matching SHA1 and a following commit message
/// separated by a whitespace.
static SHA1_REGEX: Lazy<Regex> = lazy_regex!(r#"^\b([a-f0-9]{40})\b (.*)$"#);

/// Common commit object that is parsed from a repository.
#[derive(Debug, Clone, PartialEq, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -78,6 +87,20 @@ impl<'a> From<&'a git_conventional::Footer<'a>> for Footer<'a> {
}
}

impl<'a> From<String> for Commit<'a> {
fn from(s: String) -> Self {
if let Some(captures) = SHA1_REGEX.captures(&s) {
if let (Some(id), Some(message)) = (
captures.get(1).map(|v| v.as_str()),
captures.get(2).map(|v| v.as_str()),
) {
return Commit::new(id.to_string(), message.to_string());
}
}
Commit::new(String::new(), s)
}
}

impl<'a> From<&GitCommit<'a>> for Commit<'a> {
fn from(commit: &GitCommit<'a>) -> Self {
Self::new(
Expand Down Expand Up @@ -454,4 +477,37 @@ mod test {
);
Ok(())
}

#[test]
fn parse_commit() {
assert_eq!(
Commit::new(String::new(), String::from("test: no sha1 given")),
Commit::from(String::from("test: no sha1 given"))
);
assert_eq!(
Commit::new(
String::from("8f55e69eba6e6ce811ace32bd84cc82215673cb6"),
String::from("feat: do something")
),
Commit::from(String::from(
"8f55e69eba6e6ce811ace32bd84cc82215673cb6 feat: do something"
))
);
assert_eq!(
Commit::new(
String::from("3bdd0e690c4cd5bd00e5201cc8ef3ce3fb235853"),
String::from("chore: do something")
),
Commit::from(String::from(
"3bdd0e690c4cd5bd00e5201cc8ef3ce3fb235853 chore: do something"
))
);
assert_eq!(
Commit::new(
String::new(),
String::from("thisisinvalidsha1 style: add formatting")
),
Commit::from(String::from("thisisinvalidsha1 style: add formatting"))
);
}
}
6 changes: 2 additions & 4 deletions git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,8 @@ pub fn run(mut args: Opt) -> Result<()> {
// Add custom commit messages to the latest release.
if let Some(custom_commits) = args.with_commit {
if let Some(latest_release) = releases.iter_mut().last() {
custom_commits.iter().for_each(|commit_message| {
latest_release
.commits
.push(Commit::new(String::new(), commit_message.to_string()))
custom_commits.into_iter().for_each(|message| {
latest_release.commits.push(Commit::from(message))
});
}
}
Expand Down

0 comments on commit d453d4c

Please sign in to comment.