From 146cbdaffdd1b551e6689f162e26226d5a351d6e Mon Sep 17 00:00:00 2001 From: Twist Date: Mon, 22 Aug 2022 18:00:37 +0100 Subject: Add co_authors property to the Commit object, which parses the commit message for designated co-authors, include a simple test. --- git/objects/commit.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'git/objects/commit.py') diff --git a/git/objects/commit.py b/git/objects/commit.py index 66cb9191..65c94f23 100644 --- a/git/objects/commit.py +++ b/git/objects/commit.py @@ -4,6 +4,7 @@ # This module is part of GitPython and is released under # the BSD License: http://www.opensource.org/licenses/bsd-license.php import datetime +import re from subprocess import Popen, PIPE from gitdb import IStream from git.util import hex_to_bin, Actor, Stats, finalize_process @@ -738,3 +739,24 @@ class Commit(base.Object, TraversableIterableObj, Diffable, Serializable): return self # } END serializable implementation + + @property + def co_authors(self) -> List[Actor]: + """ + Search the commit message for any co-authors of this commit. + Details on co-authors: https://github.blog/2018-01-29-commit-together-with-co-authors/ + + :return: List of co-authors for this commit (as Actor objects). + """ + co_authors = [] + + if self.message: + results = re.findall( + r"^Co-authored-by: ((?:\w|\-| ){0,38}) <(\S*)>$", + self.message, + re.MULTILINE, + ) + for author in results: + co_authors.append(Actor(*author)) + + return co_authors -- cgit v1.2.1 From c2fd97e374f9c1187f165b18651928c011e6f041 Mon Sep 17 00:00:00 2001 From: Twist Date: Tue, 23 Aug 2022 19:52:11 +0100 Subject: Update regex to extract the author string, and create the Actor using the _from_string classmethod. --- git/objects/commit.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'git/objects/commit.py') diff --git a/git/objects/commit.py b/git/objects/commit.py index 65c94f23..58f0bde7 100644 --- a/git/objects/commit.py +++ b/git/objects/commit.py @@ -752,11 +752,11 @@ class Commit(base.Object, TraversableIterableObj, Diffable, Serializable): if self.message: results = re.findall( - r"^Co-authored-by: ((?:\w|\-| ){0,38}) <(\S*)>$", + r"^Co-authored-by: ((?:\w|\-| ){0,38} <\S*>)$", self.message, re.MULTILINE, ) - for author in results: - co_authors.append(Actor(*author)) + for author_string in results: + co_authors.append(Actor._from_string(author_string)) return co_authors -- cgit v1.2.1 From 09f8a1b7b674d6138b872643b13ffc5444fab24f Mon Sep 17 00:00:00 2001 From: Twist Date: Wed, 24 Aug 2022 19:05:45 +0100 Subject: Use the same regex as the Actor class when determining co-authors. --- git/objects/commit.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'git/objects/commit.py') diff --git a/git/objects/commit.py b/git/objects/commit.py index 58f0bde7..cf7d9aaa 100644 --- a/git/objects/commit.py +++ b/git/objects/commit.py @@ -752,11 +752,11 @@ class Commit(base.Object, TraversableIterableObj, Diffable, Serializable): if self.message: results = re.findall( - r"^Co-authored-by: ((?:\w|\-| ){0,38} <\S*>)$", + r"^Co-authored-by: (.*) <(.*?)>$", self.message, re.MULTILINE, ) - for author_string in results: - co_authors.append(Actor._from_string(author_string)) + for author in results: + co_authors.append(Actor(*author)) return co_authors -- cgit v1.2.1