From 378e21a426781a9d41d34f96f222d7281a1ac87b 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 --- appview/repo/repo_util.go | 3 --- types/commit.go | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/appview/repo/repo_util.go b/appview/repo/repo_util.go index b6a9507a..5217500d 100644 --- a/appview/repo/repo_util.go +++ b/appview/repo/repo_util.go @@ -1,7 +1,6 @@ package repo import ( - "regexp" "slices" "sort" "strings" @@ -9,8 +8,6 @@ import ( "tangled.org/core/appview/db" "tangled.org/core/appview/models" "tangled.org/core/types" - - "github.com/go-git/go-git/v5/plumbing/object" ) func sortFiles(files []types.NiceTree) { 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