Skip to content

Commit 84d0fb5

Browse files
committed
feat(git): add parents' digests in commit information
1 parent a202e66 commit 84d0fb5

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

commitizen/git.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,20 @@ def __eq__(self, other) -> bool:
4444

4545
class GitCommit(GitObject):
4646
def __init__(
47-
self, rev, title, body: str = "", author: str = "", author_email: str = ""
47+
self,
48+
rev,
49+
title,
50+
body: str = "",
51+
author: str = "",
52+
author_email: str = "",
53+
parents: list[str] = [],
4854
):
4955
self.rev = rev.strip()
5056
self.title = title.strip()
5157
self.body = body.strip()
5258
self.author = author.strip()
5359
self.author_email = author_email.strip()
60+
self.parents = parents
5461

5562
@property
5663
def message(self):
@@ -137,14 +144,17 @@ def get_commits(
137144
for rev_and_commit in git_log_entries:
138145
if not rev_and_commit:
139146
continue
140-
rev, title, author, author_email, *body_list = rev_and_commit.split("\n")
147+
rev, parents, title, author, author_email, *body_list = rev_and_commit.split(
148+
"\n"
149+
)
141150
if rev_and_commit:
142151
git_commit = GitCommit(
143152
rev=rev.strip(),
144153
title=title.strip(),
145154
body="\n".join(body_list).strip(),
146155
author=author,
147156
author_email=author_email,
157+
parents=[p for p in parents.strip().split(" ") if p],
148158
)
149159
git_commits.append(git_commit)
150160
return git_commits
@@ -286,7 +296,7 @@ def smart_open(*args, **kargs):
286296
def _get_log_as_str_list(start: str | None, end: str, args: str) -> list[str]:
287297
"""Get string representation of each log entry"""
288298
delimiter = "----------commit-delimiter----------"
289-
log_format: str = "%H%n%s%n%an%n%ae%n%b"
299+
log_format: str = "%H%n%P%n%s%n%an%n%ae%n%b"
290300
git_log_cmd = (
291301
f"git -c log.showSignature=False log --pretty={log_format}{delimiter} {args}"
292302
)

tests/test_git.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
def test_git_object_eq():
2222
git_commit = git.GitCommit(
23-
rev="sha1-code", title="this is title", body="this is body"
23+
rev="sha1-code",
24+
title="this is title",
25+
body="this is body",
2426
)
2527
git_tag = git.GitTag(rev="sha1-code", name="0.0.1", date="2020-01-21")
2628

@@ -132,11 +134,13 @@ def test_get_commits_author_and_email():
132134
def test_get_commits_without_email(mocker: MockFixture):
133135
raw_commit = (
134136
"a515bb8f71c403f6f7d1c17b9d8ebf2ce3959395\n"
137+
"95bbfc703eb99cb49ba0d6ffd8469911303dbe63 12d3b4bdaa996ea7067a07660bb5df4772297bdd\n"
135138
"\n"
136139
"user name\n"
137140
"\n"
138141
"----------commit-delimiter----------\n"
139142
"12d3b4bdaa996ea7067a07660bb5df4772297bdd\n"
143+
"de33bc5070de19600f2f00262b3c15efea762408\n"
140144
"feat(users): add username\n"
141145
"user name\n"
142146
"\n"
@@ -159,16 +163,19 @@ def test_get_commits_without_email(mocker: MockFixture):
159163
def test_get_commits_without_breakline_in_each_commit(mocker: MockFixture):
160164
raw_commit = (
161165
"ae9ba6fc5526cf478f52ef901418d85505109744\n"
166+
"ff2f56ca844de72a9d59590831087bf5a97bac84\n"
162167
"bump: version 2.13.0 → 2.14.0\n"
163168
"GitHub Action\n"
164169
"action@github.com\n"
165170
"----------commit-delimiter----------\n"
166171
"ff2f56ca844de72a9d59590831087bf5a97bac84\n"
172+
"b4dc83284dc8c9729032a774a037df1d1f2397d5 20a54bf1b82cd7b573351db4d1e8814dd0be205d\n"
167173
"Merge pull request #332 from cliles/feature/271-redux\n"
168174
"User\n"
169175
"user@email.com\n"
170176
"Feature/271 redux----------commit-delimiter----------\n"
171177
"20a54bf1b82cd7b573351db4d1e8814dd0be205d\n"
178+
"658f38c3fe832cdab63ed4fb1f7b3a0969a583be\n"
172179
"feat(#271): enable creation of annotated tags when bumping\n"
173180
"User 2\n"
174181
"user@email.edu\n"
@@ -193,6 +200,55 @@ def test_get_commits_without_breakline_in_each_commit(mocker: MockFixture):
193200
)
194201

195202

203+
def test_get_commits_with_and_without_parents(mocker: MockFixture):
204+
raw_commit = (
205+
"4206e661bacf9643373255965f34bbdb382cb2b9\n"
206+
"ae9ba6fc5526cf478f52ef901418d85505109744 bf8479e7aa1a5b9d2f491b79e3a4d4015519903e\n"
207+
"Merge pull request from someone\n"
208+
"Maintainer\n"
209+
"maintainer@email.com\n"
210+
"This is a much needed feature----------commit-delimiter----------\n"
211+
"ae9ba6fc5526cf478f52ef901418d85505109744\n"
212+
"ff2f56ca844de72a9d59590831087bf5a97bac84\n"
213+
"Release 0.1.0\n"
214+
"GitHub Action\n"
215+
"action@github.com\n"
216+
"----------commit-delimiter----------\n"
217+
"ff2f56ca844de72a9d59590831087bf5a97bac84\n"
218+
"\n"
219+
"Initial commit\n"
220+
"User\n"
221+
"user@email.com\n"
222+
"----------commit-delimiter----------\n"
223+
)
224+
mocker.patch("commitizen.cmd.run", return_value=FakeCommand(out=raw_commit))
225+
226+
commits = git.get_commits()
227+
228+
assert commits[0].author == "Maintainer"
229+
assert commits[1].author == "GitHub Action"
230+
assert commits[2].author == "User"
231+
232+
assert commits[0].author_email == "maintainer@email.com"
233+
assert commits[1].author_email == "action@github.com"
234+
assert commits[2].author_email == "user@email.com"
235+
236+
assert commits[0].title == "Merge pull request from someone"
237+
assert commits[1].title == "Release 0.1.0"
238+
assert commits[2].title == "Initial commit"
239+
240+
assert commits[0].body == "This is a much needed feature"
241+
assert commits[1].body == ""
242+
assert commits[2].body == ""
243+
244+
assert commits[0].parents == [
245+
"ae9ba6fc5526cf478f52ef901418d85505109744",
246+
"bf8479e7aa1a5b9d2f491b79e3a4d4015519903e",
247+
]
248+
assert commits[1].parents == ["ff2f56ca844de72a9d59590831087bf5a97bac84"]
249+
assert commits[2].parents == []
250+
251+
196252
def test_get_commits_with_signature():
197253
config_file = ".git/config"
198254
config_backup = ".git/config.bak"

0 commit comments

Comments
 (0)