Skip to content

Commit 76362a7

Browse files
fpozzobontimvink
authored andcommitted
Adding co-author
1 parent 2d7722c commit 76362a7

File tree

2 files changed

+78
-9
lines changed

2 files changed

+78
-9
lines changed

src/mkdocs_git_authors_plugin/git/page.py

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,82 @@ def _process_git_blame(self) -> None:
189189
author_tz=commit_data.get("author-tz"),
190190
summary=commit_data.get("summary"),
191191
)
192-
if commit.author().email() not in ignore_authors and (
193-
len(line) > 1 or self.repo().config("count_empty_lines")
194-
):
195-
author = commit.author()
196-
if author not in self._authors:
197-
self._authors.append(author)
198-
author.add_lines(self, commit)
199-
self.add_total_lines()
200-
self.repo().add_total_lines()
192+
if len(line) > 1 or self.repo().config("count_empty_lines"):
193+
if commit.author().email() not in ignore_authors:
194+
author = commit.author()
195+
if author not in self._authors:
196+
self._authors.append(author)
197+
author.add_lines(self, commit)
198+
self.add_total_lines()
199+
self.repo().add_total_lines()
200+
# Process co-authors if present
201+
self._process_git_log(commit_data.get("sha"), commit)
202+
203+
def _process_git_log(self, sha, commit) -> None:
204+
"""
205+
Execute git log and parse the results.
206+
207+
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.
208+
Each line will be associated with a Commit object and counted
209+
to its co-author's "account".
210+
Whether empty lines are counted is determined by the
211+
count_empty_lines configuration option.
212+
213+
git log -1 <sha> will produce output like the following
214+
for each line in a file:
215+
216+
When a commit does not contain co-authors:
217+
commit ca8b32b24af1ce97fb29c5139b2f80e0c2ad9d1c
218+
Author: John Doe <[email protected]>
219+
Date: Sun Dec 22 11:10:32 2019 +0100
220+
221+
add plugin skeleton
222+
223+
When a commit contains co-authors:
224+
commit ca8b32b24af1ce97fb29c5139b2f80e0c2ad9d1c
225+
Author: John Doe <[email protected]>
226+
Date: Sun Dec 22 11:10:32 2019 +0100
227+
228+
add plugin skeleton
229+
230+
Co-authored-by: John Doe <[email protected]>
231+
Co-authored-by: Rock Smith <[email protected]>
232+
233+
In this case we skip the original author as redundant using email address to detect it.
234+
235+
Args:
236+
sha: the SHA of the commit to process
237+
Returns:
238+
--- (this method works through side effects)
239+
"""
240+
241+
args = []
242+
args.append("-1") # Only existing sha
243+
args.append(sha)
244+
cmd = GitCommand("log", args)
245+
cmd.run()
246+
247+
lines = cmd.stdout()
248+
249+
# in case of empty, non-committed files, raise error
250+
if len(lines) == 0:
251+
raise GitCommandError
252+
253+
ignore_authors = self.repo().config("ignore_authors")
254+
for line in lines:
255+
if line.startswith("Author: "):
256+
# skip author as already available in Commit object
257+
continue
258+
259+
result = re.search(r"Co-authored-by:(.*) <(.*)>", line)
260+
if result is not None and result.group(1) != "" and result.group(2) != "":
261+
# Extract co-authors from the commit
262+
co_author = self.repo().author(result.group(1), result.group(2))
263+
if co_author.email() not in ignore_authors and co_author.email() != commit.author().email():
264+
# Create the co-author
265+
if co_author not in self._authors:
266+
self._authors.append(co_author)
267+
co_author.add_lines(self, commit)
201268

202269
def path(self) -> Path:
203270
"""

tests/test_util.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ def setup_commit_history(testproject_path):
127127
repo.git.commit(message="homepage", author=author)
128128
repo.git.add("docs/page_with_tag.md")
129129
repo.git.commit(message="homepage", author=author)
130+
repo.git.add("docs/page_with_co_authors.md")
131+
repo.git.commit(message="co-authors commit\nCo-authored-by: Test Person <[email protected]>\nCo-authored-by: John Doe <[email protected]>", author=author)
130132
os.chdir(str(cwd))
131133
except:
132134
os.chdir(str(cwd))

0 commit comments

Comments
 (0)