Skip to content

Commit e29fd6d

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

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

commitizen/git.py

+13-3
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

+54
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,13 @@ def test_get_commits_author_and_email():
132132
def test_get_commits_without_email(mocker: MockFixture):
133133
raw_commit = (
134134
"a515bb8f71c403f6f7d1c17b9d8ebf2ce3959395\n"
135+
"95bbfc703eb99cb49ba0d6ffd8469911303dbe63 12d3b4bdaa996ea7067a07660bb5df4772297bdd\n"
135136
"\n"
136137
"user name\n"
137138
"\n"
138139
"----------commit-delimiter----------\n"
139140
"12d3b4bdaa996ea7067a07660bb5df4772297bdd\n"
141+
"de33bc5070de19600f2f00262b3c15efea762408\n"
140142
"feat(users): add username\n"
141143
"user name\n"
142144
"\n"
@@ -159,16 +161,19 @@ def test_get_commits_without_email(mocker: MockFixture):
159161
def test_get_commits_without_breakline_in_each_commit(mocker: MockFixture):
160162
raw_commit = (
161163
"ae9ba6fc5526cf478f52ef901418d85505109744\n"
164+
"ff2f56ca844de72a9d59590831087bf5a97bac84\n"
162165
"bump: version 2.13.0 → 2.14.0\n"
163166
"GitHub Action\n"
164167
165168
"----------commit-delimiter----------\n"
166169
"ff2f56ca844de72a9d59590831087bf5a97bac84\n"
170+
"b4dc83284dc8c9729032a774a037df1d1f2397d5 20a54bf1b82cd7b573351db4d1e8814dd0be205d\n"
167171
"Merge pull request #332 from cliles/feature/271-redux\n"
168172
"User\n"
169173
170174
"Feature/271 redux----------commit-delimiter----------\n"
171175
"20a54bf1b82cd7b573351db4d1e8814dd0be205d\n"
176+
"658f38c3fe832cdab63ed4fb1f7b3a0969a583be\n"
172177
"feat(#271): enable creation of annotated tags when bumping\n"
173178
"User 2\n"
174179
@@ -193,6 +198,55 @@ def test_get_commits_without_breakline_in_each_commit(mocker: MockFixture):
193198
)
194199

195200

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

0 commit comments

Comments
 (0)