types: add utility to extract co-authors from a commit #861

merged
opened by oppi.li targeting master from op/rkowwwsvpoqx

uses a regex to detect & extract a object.Signature from the Co-authored-by header.

Signed-off-by: oppiliappan me@oppi.li

Changed files
+26 -3
appview
types
-3
appview/repo/repo_util.go
··· 1 package repo 2 3 import ( 4 - "regexp" 5 "slices" 6 "sort" 7 "strings" ··· 9 "tangled.org/core/appview/db" 10 "tangled.org/core/appview/models" 11 "tangled.org/core/types" 12 - 13 - "github.com/go-git/go-git/v5/plumbing/object" 14 ) 15 16 func sortFiles(files []types.NiceTree) {
··· 1 package repo 2 3 import ( 4 "slices" 5 "sort" 6 "strings" ··· 8 "tangled.org/core/appview/db" 9 "tangled.org/core/appview/models" 10 "tangled.org/core/types" 11 ) 12 13 func sortFiles(files []types.NiceTree) {
+26
types/commit.go
··· 5 "encoding/json" 6 "fmt" 7 "maps" 8 "strings" 9 10 "github.com/go-git/go-git/v5/plumbing" ··· 166 167 return payload.String() 168 }
··· 5 "encoding/json" 6 "fmt" 7 "maps" 8 + "regexp" 9 "strings" 10 11 "github.com/go-git/go-git/v5/plumbing" ··· 167 168 return payload.String() 169 } 170 + 171 + var ( 172 + coAuthorRegex = regexp.MustCompile(`(?im)^Co-authored-by:\s*(.+?)\s*<([^>]+)>`) 173 + ) 174 + 175 + func (commit *Commit) CoAuthors() []object.Signature { 176 + var coAuthors []object.Signature 177 + 178 + matches := coAuthorRegex.FindAllStringSubmatch(commit.Message, -1) 179 + 180 + for _, match := range matches { 181 + if len(match) >= 3 { 182 + name := strings.TrimSpace(match[1]) 183 + email := strings.TrimSpace(match[2]) 184 + 185 + coAuthors = append(coAuthors, object.Signature{ 186 + Name: name, 187 + Email: email, 188 + When: commit.Committer.When, 189 + }) 190 + } 191 + } 192 + 193 + return coAuthors 194 + }