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
···
package repo
import (
-
"regexp"
"slices"
"sort"
"strings"
···
"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) {
···
package repo
import (
"slices"
"sort"
"strings"
···
"tangled.org/core/appview/db"
"tangled.org/core/appview/models"
"tangled.org/core/types"
)
func sortFiles(files []types.NiceTree) {
+26
types/commit.go
···
"encoding/json"
"fmt"
"maps"
"strings"
"github.com/go-git/go-git/v5/plumbing"
···
return payload.String()
}
···
"encoding/json"
"fmt"
"maps"
+
"regexp"
"strings"
"github.com/go-git/go-git/v5/plumbing"
···
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
+
}