Skip to content

Commit b582ea1

Browse files
fpozzobontimvink
authored andcommitted
Code review:
- moving responsibility of deducing co-author to repo - added co-author inside commit
1 parent 927324d commit b582ea1

File tree

3 files changed

+89
-82
lines changed

3 files changed

+89
-82
lines changed

src/mkdocs_git_authors_plugin/git/commit.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import re
2-
from typing import Union
2+
from typing import Any, Union
33

44
from mkdocs_git_authors_plugin import util
55
from mkdocs_git_authors_plugin.git.repo import AbstractRepoObject, Repo
@@ -24,6 +24,7 @@ def __init__(
2424
author_time: str,
2525
author_tz: str,
2626
summary: str,
27+
co_authors: list[Any],
2728
):
2829
"""Initialize a commit from its SHA.
2930
@@ -48,6 +49,7 @@ def __init__(
4849
self._datetime = util.commit_datetime(author_time, author_tz)
4950
self._datetime_string = util.commit_datetime_string(self._datetime)
5051
self._summary = summary
52+
self._co_authors = co_authors
5153

5254
def author(self):
5355
"""
@@ -60,6 +62,17 @@ def author(self):
6062
"""
6163
return self._author
6264

65+
def co_authors(self):
66+
"""
67+
The commit's co-author.
68+
69+
Args:
70+
71+
Returns:
72+
Co-Authors list
73+
"""
74+
return self._co_authors
75+
6376
def datetime(self, _type=str) -> Union[str, util.datetime]:
6477
"""
6578
The commit's commit time.

src/mkdocs_git_authors_plugin/git/page.py

Lines changed: 5 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ def __init__(self, repo: Repo, path: Path, strict: bool) -> None:
3232
self._total_lines = 0
3333
self._authors: List[dict] = list()
3434
self._strict = strict
35-
# Cache Logs, indexed by 40 char SHA
36-
self._cached_logs = {}
3735

3836
try:
3937
self._process_git_blame()
@@ -200,85 +198,11 @@ def _process_git_blame(self) -> None:
200198
self.add_total_lines()
201199
self.repo().add_total_lines()
202200
# Process co-authors if present
203-
self._process_git_log(commit_data.get("sha"), commit)
204-
205-
def _process_git_log(self, sha, commit) -> None:
206-
"""
207-
Execute git log and parse the results.
208-
209-
This retrieves [co-authors](https://docs.github.com/en/pull-requests/committing-changes-to-your-project/creating-and-editing-commits/creating-a-commit-with-multiple-authors) from comment.
210-
Each line will be associated with a Commit object and counted
211-
to its co-author's "account".
212-
Whether empty lines are counted is determined by the
213-
count_empty_lines configuration option.
214-
215-
git log -1 <sha> will produce output like the following
216-
for each line in a file:
217-
218-
When a commit does not contain co-authors:
219-
commit ca8b32b24af1ce97fb29c5139b2f80e0c2ad9d1c
220-
Author: John Doe <jdoe@john.com>
221-
Date: Sun Dec 22 11:10:32 2019 +0100
222-
223-
add plugin skeleton
224-
225-
When a commit contains co-authors:
226-
commit ca8b32b24af1ce97fb29c5139b2f80e0c2ad9d1c
227-
Author: John Doe <jdoe@john.com>
228-
Date: Sun Dec 22 11:10:32 2019 +0100
229-
230-
add plugin skeleton
231-
232-
Co-authored-by: John Doe <jdoe@john.com>
233-
Co-authored-by: Rock Smith <rsmith@smith.com>
234-
235-
In this case we skip the original author as redundant using email address to detect it.
236-
237-
Args:
238-
sha: the SHA of the commit to process
239-
Returns:
240-
--- (this method works through side effects)
241-
"""
242-
243-
co_authors = self._get_git_log(sha, commit)
244-
for co_author in co_authors:
245-
# Create the co-author
246-
if co_author not in self._authors:
247-
self._authors.append(co_author)
248-
co_author.add_lines(self, commit)
249-
250-
def _get_git_log(self, sha, commit):
251-
if self._cached_logs.get(sha) is None:
252-
args = ["-1", sha]
253-
cmd = GitCommand("log", args)
254-
cmd.run()
255-
256-
lines = cmd.stdout()
257-
258-
# in case of empty, non-committed files, raise error
259-
if len(lines) == 0:
260-
raise GitCommandError
261-
self._cached_logs[sha] = self._parse_git_log(lines, commit)
262-
return self._cached_logs.get(sha)
263-
264-
def _parse_git_log(self, lines, commit):
265-
parsed_logs = []
266-
ignore_authors = self.repo().config("ignore_authors")
267-
for line in lines:
268-
if line.startswith("Author: "):
269-
# skip author as already available in Commit object
270-
continue
271-
272-
result = re.search(r"Co-authored-by: (.*) <(.*)>", line)
273-
if result is not None and result.group(1) != "" and result.group(2) != "":
274-
# Extract co-authors from the commit
275-
co_author = self.repo().author(result.group(1), result.group(2))
276-
if (
277-
co_author.email() not in ignore_authors
278-
and co_author.email() != commit.author().email()
279-
):
280-
parsed_logs.append(co_author)
281-
return parsed_logs
201+
for co_author in commit.co_authors():
202+
# Create the co-author
203+
if co_author not in self._authors:
204+
self._authors.append(co_author)
205+
co_author.add_lines(self, commit)
282206

283207
def path(self) -> Path:
284208
"""

src/mkdocs_git_authors_plugin/git/repo.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import re
12
from pathlib import Path
23
from typing import Any, Union
34

45
from mkdocs_git_authors_plugin.git.command import GitCommand
56

7+
from mkdocs_git_authors_plugin.git.command import GitCommandError
8+
69

710
class Repo(object):
811
"""
@@ -113,9 +116,76 @@ def get_commit(self, sha: str, **kwargs) -> Union[Any, None]:
113116
if not self._commits.get(sha):
114117
from .commit import Commit
115118

119+
kwargs["co_authors"] = self._get_co_authors(sha, kwargs.get("author_email"))
116120
self._commits[sha] = Commit(self, sha, **kwargs)
117121
return self._commits.get(sha)
118122

123+
def _get_co_authors(self, sha, author_email) -> list[Any]:
124+
"""
125+
Execute git log and parse the results.
126+
127+
This retrieves [co-authors](https://docs.github.com/en/pull-requests/committing-changes-to-your-project/creating-and-editing-commits/creating-a-commit-with-multiple-authors) from comment.
128+
Each line will be associated with a Commit object and counted
129+
to its co-author's "account".
130+
Whether empty lines are counted is determined by the
131+
count_empty_lines configuration option.
132+
133+
git log -1 <sha> will produce output like the following
134+
for each line in a file:
135+
136+
When a commit does not contain co-authors:
137+
commit ca8b32b24af1ce97fb29c5139b2f80e0c2ad9d1c
138+
Author: John Doe <jdoe@john.com>
139+
Date: Sun Dec 22 11:10:32 2019 +0100
140+
141+
add plugin skeleton
142+
143+
When a commit contains co-authors:
144+
commit ca8b32b24af1ce97fb29c5139b2f80e0c2ad9d1c
145+
Author: John Doe <jdoe@john.com>
146+
Date: Sun Dec 22 11:10:32 2019 +0100
147+
148+
add plugin skeleton
149+
150+
Co-authored-by: John Doe <jdoe@john.com>
151+
Co-authored-by: Rock Smith <rsmith@smith.com>
152+
153+
In this case we skip the original author as redundant using email address to detect it.
154+
155+
Args:
156+
sha: the SHA of the commit to process
157+
author_email: email of the author
158+
Returns:
159+
List of co-authors excluding commit author
160+
"""
161+
args = ["-1", sha]
162+
cmd = GitCommand("log", args)
163+
cmd.run()
164+
165+
lines = cmd.stdout()
166+
167+
# in case of empty, non-committed files, raise error
168+
if len(lines) == 0:
169+
raise GitCommandError
170+
co_authors = []
171+
ignore_authors = self.config("ignore_authors")
172+
173+
for line in lines:
174+
if line.startswith("Author: "):
175+
# skip author as already available in Commit object
176+
continue
177+
178+
result = re.search(r"Co-authored-by: (.*) <(.*)>", line)
179+
if result is not None and result.group(1) != "" and result.group(2) != "":
180+
# Extract co-authors from the commit
181+
co_author = self.author(result.group(1), result.group(2))
182+
if (
183+
co_author.email() not in ignore_authors
184+
and co_author.email() != author_email
185+
):
186+
co_authors.append(co_author)
187+
return co_authors
188+
119189
def page(self, path):
120190
"""
121191
Return the (cached) Page object for given path.

0 commit comments

Comments
 (0)