From 6b6b50e47c7205b4d9048a9a2635b920aec59603 Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Fri, 5 Dec 2025 04:12:11 +0000 Subject: [PATCH] types: add utility to extract co-authors from a commit Change-Id: qxwspuynkpoknnskvqktnomzkryqyzxu uses a regex to detect & extract a object.Signature from the Co-authored-by header. Signed-off-by: oppiliappan --- types/commit.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/types/commit.go b/types/commit.go index d52e2066..fd5bc595 100644 --- a/types/commit.go +++ b/types/commit.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "maps" + "regexp" "strings" "github.com/go-git/go-git/v5/plumbing" @@ -166,3 +167,28 @@ func (c *Commit) Payload() string { return payload.String() } + +var ( + coAuthorRegex = regexp.MustCompile(`(?im)^Co-authored-by:\s*(.+?)\s*<([^>]+)>`) +) + +func (commit *Commit) CoAuthors() []object.Signature { + var coAuthors []object.Signature + + matches := coAuthorRegex.FindAllStringSubmatch(commit.Message, -1) + + for _, match := range matches { + if len(match) >= 3 { + name := strings.TrimSpace(match[1]) + email := strings.TrimSpace(match[2]) + + coAuthors = append(coAuthors, object.Signature{ + Name: name, + Email: email, + When: commit.Committer.When, + }) + } + } + + return coAuthors +} -- 2.43.0