8000 Add co_authors property to the Commit object, which parses the commit… · kasthula21/GitPythonchandra@146cbda · GitHub
[go: up one dir, main page]

Skip to content
< 8000 header class="HeaderMktg header-logged-out js-details-container js-header Details f4 py-3" role="banner" data-is-top="true" data-color-mode=light data-light-theme=light data-dark-theme=dark>

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 146cbda

Browse files
committed
Add co_authors property to the Commit object, which parses the commit message for designated co-authors, include a simple test.
1 parent 12d91c6 commit 146cbda

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ Contributors are:
4848
-Hiroki Tokunaga <tokusan441 _at_ gmail.com>
4949
-Julien Mauroy <pro.julien.mauroy _at_ gmail.com>
5050
-Patrick Gerard
51+
-Luke Twist <itsluketwist@gmail.com>
5152
Portions derived from other open source works and are clearly marked.

git/objects/commit.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# This module is part of GitPython and is released under
55
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
66
import datetime
7+
import re
78
from subprocess import Popen, PIPE
89
from gitdb import IStream
910
from git.util import hex_to_bin, Actor, Stats, finalize_process
@@ -738,3 +739,24 @@ def _deserialize(self, stream: BytesIO) -> "Commit":
738739
return self
739740

740741
# } END serializable implementation
742+
743+
@property
744+
def co_authors(self) -> List[Actor]:
745+
"""
746+
Search the commit message for any co-authors of this commit.
747+
Details on co-authors: https://github.blog/2018-01-29-commit-together-with-co-authors/
748+
749+
:return: List of co-authors for this commit (as Actor objects).
750+
"""
751+
co_authors = []
752+
753+
if self.message:
754+
results = re.findall(
755+
r"^Co-authored-by: ((?:\w|\-| ){0,38}) <(\S*)>$",
756+
self.message,
757+
re.MULTILINE,
758+
)
759+
for author in results:
760+
co_authors.append(Actor(*author))
761+
762+
return co_authors

test/test_commit.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,3 +509,14 @@ def test_trailers(self):
509509
assert KEY_1 not in commit.trailers.keys()
510510
assert KEY_2 in commit.trailers.keys()
511511
assert commit.trailers[KEY_2] == VALUE_2
512+
513+
def test_commit_co_authors(self):
514+
commit = copy.copy(self.rorepo.commit("4251bd5"))
515+
commit.message = """Commit message
516+
517+
Co-authored-by: Test User 1 <602352+test@users.noreply.github.com>
518+
Co-authored-by: test_user_2 <another_user-email@.github.com>"""
519+
assert commit.co_authors == [
520+
Actor("Test User 1", "602352+test@users.noreply.github.com"),
521+
Actor("test_user_2", "another_user-email@.github.com"),
522+
]

0 commit comments

Comments
 (0)
0