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(changelog): expose commit parents #1376

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions commitizen/changelog.py
Original file line number Diff line number Diff line change
@@ -172,6 +172,7 @@ def process_commit_message(
):
message: dict = {
"sha1": commit.rev,
"parents": commit.parents,
"author": commit.author,
"author_email": commit.author_email,
**parsed.groupdict(),
16 changes: 13 additions & 3 deletions commitizen/git.py
Original file line number Diff line number Diff line change
@@ -44,13 +44,20 @@ def __eq__(self, other) -> bool:

class GitCommit(GitObject):
def __init__(
self, rev, title, body: str = "", author: str = "", author_email: str = ""
self,
rev,
title,
body: str = "",
author: str = "",
author_email: str = "",
parents: list[str] | None = None,
):
self.rev = rev.strip()
self.title = title.strip()
self.body = body.strip()
self.author = author.strip()
self.author_email = author_email.strip()
self.parents = parents or []

@property
def message(self):
@@ -137,14 +144,17 @@ def get_commits(
for rev_and_commit in git_log_entries:
if not rev_and_commit:
continue
rev, title, author, author_email, *body_list = rev_and_commit.split("\n")
rev, parents, title, author, author_email, *body_list = rev_and_commit.split(
"\n"
)
if rev_and_commit:
git_commit = GitCommit(
rev=rev.strip(),
title=title.strip(),
body="\n".join(body_list).strip(),
author=author,
author_email=author_email,
parents=[p for p in parents.strip().split(" ") if p],
)
git_commits.append(git_commit)
return git_commits
@@ -286,7 +296,7 @@ def smart_open(*args, **kargs):
def _get_log_as_str_list(start: str | None, end: str, args: str) -> list[str]:
"""Get string representation of each log entry"""
delimiter = "----------commit-delimiter----------"
log_format: str = "%H%n%s%n%an%n%ae%n%b"
log_format: str = "%H%n%P%n%s%n%an%n%ae%n%b"
git_log_cmd = (
f"git -c log.showSignature=False log --pretty={log_format}{delimiter} {args}"
)
4 changes: 4 additions & 0 deletions docs/customization.md
Original file line number Diff line number Diff line change
@@ -507,12 +507,16 @@ Each `Change` has the following fields:
| scope | `str | None` | An optional scope |
| message | `str` | The commit message body |
| sha1 | `str` | The commit `sha1` |
| parents | `list[str]` | The parent commit(s) `sha1`(s) |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add more information to this section, specially what's it useful for:

Example usage: generate changelogs based on merge commits only, or listing commits that belong to the same PR under its own section.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added similar phrasing just below this table after the note. Let me know if that's ok.

| author | `str` | The commit author name |
| author_email | `str` | The commit author email |

!!! Note
The field values depend on the customization class and/or the settings you provide

The `parents` field can be used to identify merge commits and generate a changelog based on those. Another use case
is listing commits that belong to the same pull request.

When using another template (either provided by a plugin or by yourself), you can also pass extra template variables
by:

Loading
Oops, something went wrong.