add naive global mailmap support

This commit is contained in:
Iris Lightshard 2024-01-29 21:08:08 -07:00
parent f6c4dfbc41
commit 5fe7b8e470
Signed by: Iris Lightshard
GPG key ID: 688407174966CAF3
7 changed files with 94 additions and 32 deletions

View file

@ -2,8 +2,8 @@ package config
import (
"fmt"
"os"
"html/template"
"os"
"strings"
"gopkg.in/yaml.v3"
@ -21,33 +21,34 @@ type Config struct {
Static string `yaml:"static"`
} `yaml:"dirs"`
Meta struct {
Title string `yaml:"title"`
Description string `yaml:"description"`
Footer string `yaml:"footer,omitempty"`
MaintainerEmail string `yaml:"maintainerEmail,omitempty"`
CompiledFooter template.HTML `yaml:"thisIsNotSupposedToBeHere,omitempty"`
Title string `yaml:"title"`
Description string `yaml:"description"`
Footer string `yaml:"footer,omitempty"`
MaintainerEmail string `yaml:"maintainerEmail,omitempty"`
CompiledFooter template.HTML `yaml:"thisIsNotSupposedToBeHere,omitempty"`
} `yaml:"meta"`
Server struct {
Name string `yaml:"name,omitempty"`
Host string `yaml:"host"`
Port int `yaml:"port"`
} `yaml:"server"`
Mailmap string `yaml:"mailmap,omitempty"`
}
func compileFooter(c *Config, version string) {
if c.Meta.Footer != "" {
c.Meta.CompiledFooter = template.HTML(
strings.ReplaceAll(
strings.ReplaceAll(
c.Meta.Footer,
"VERSION",
version),
"MAINTAINER",
fmt.Sprintf(
"<a href='mailto:%s'>%s</a>",
c.Meta.MaintainerEmail,
c.Meta.MaintainerEmail)))
}
if c.Meta.Footer != "" {
c.Meta.CompiledFooter = template.HTML(
strings.ReplaceAll(
strings.ReplaceAll(
c.Meta.Footer,
"VERSION",
version),
"MAINTAINER",
fmt.Sprintf(
"<a href='mailto:%s'>%s</a>",
c.Meta.MaintainerEmail,
c.Meta.MaintainerEmail)))
}
}
func Read(f, v string) (*Config, error) {

View file

@ -2,11 +2,12 @@ package git
import (
"fmt"
"sort"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"os"
"sort"
"strings"
)
type GitRepo struct {
@ -53,7 +54,8 @@ func Open(path string, ref string) (*GitRepo, error) {
return &g, nil
}
func (g *GitRepo) Commits() ([]*object.Commit, error) {
func (g *GitRepo) Commits(mailmap map[string]string) ([]*object.Commit, error) {
ci, err := g.r.Log(&git.LogOptions{From: g.h})
if err != nil {
return nil, fmt.Errorf("commits from ref: %w", err)
@ -61,6 +63,18 @@ func (g *GitRepo) Commits() ([]*object.Commit, error) {
commits := []*object.Commit{}
ci.ForEach(func(c *object.Commit) error {
if mailmap != nil {
name, ok := mailmap[c.Author.Email]
if ok {
c.Author.Name = name
}
name, ok = mailmap[c.Committer.Email]
if ok {
c.Committer.Name = name
}
}
commits = append(commits, c)
return nil
})
@ -158,3 +172,28 @@ func (g *GitRepo) FindMainBranch(branches []string) (string, error) {
}
return "", fmt.Errorf("unable to find main branch")
}
func (g *GitRepo) GetMailMap(path string) map[string]string {
f, err := os.ReadFile(path)
if err != nil {
return nil
}
self := make(map[string]string)
fileData := string(f[:])
lines := strings.Split(fileData, "\n")
for _, l := range lines {
parts := strings.Split(l, "<")
if len(parts) != 2 {
continue
}
if parts[1][len(parts[1])-1] != '>' {
continue
}
parts[1] = parts[1][0 : len(parts[1])-1]
self[parts[1]] = strings.Trim(parts[0], " ")
}
return self
}

8
go.mod
View file

@ -8,9 +8,9 @@ require (
github.com/dustin/go-humanize v1.0.0
github.com/go-git/go-billy/v5 v5.3.1
github.com/go-git/go-git/v5 v5.5.1
github.com/microcosm-cc/bluemonday v1.0.21
github.com/microcosm-cc/bluemonday v1.0.26
github.com/russross/blackfriday/v2 v2.1.0
golang.org/x/sys v0.3.0
golang.org/x/sys v0.13.0
gopkg.in/yaml.v3 v3.0.0
)
@ -30,9 +30,9 @@ require (
github.com/sergi/go-diff v1.1.0 // indirect
github.com/skeema/knownhosts v1.1.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.4.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/tools v0.4.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)

10
go.sum
View file

@ -59,6 +59,8 @@ github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A=
github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
github.com/microcosm-cc/bluemonday v1.0.21 h1:dNH3e4PSyE4vNX+KlRGHT5KrSvjeUkoNPwEORjffHJg=
github.com/microcosm-cc/bluemonday v1.0.21/go.mod h1:ytNkv4RrDrLJ2pqlsSI46O6IVXmZOBBD4SaJyDwwTkM=
github.com/microcosm-cc/bluemonday v1.0.26 h1:xbqSvqzQMeEHCqMi64VAs4d8uy6Mequs3rQ0k/Khz58=
github.com/microcosm-cc/bluemonday v1.0.26/go.mod h1:JyzOCs9gkyQyjs+6h10UEVSe02CGwkhd72Xdqh78TWs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pjbgf/sha1cd v0.2.3 h1:uKQP/7QOzNtKYH7UTohZLcjF5/55EnTw0jO/Ru4jZwI=
github.com/pjbgf/sha1cd v0.2.3/go.mod h1:HOK9QrgzdHpbc2Kzip0Q1yi3M2MFGPADtR6HjG65m5M=
@ -89,6 +91,8 @@ golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d/go.mod h1:IxCIyHEi3zRg3s0
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.4.0 h1:UVQgzMY87xqpKNgb+kDsll2Igd33HszWHFLmpaRMq/8=
golang.org/x/crypto v0.4.0/go.mod h1:3quD/ATkf6oY+rnes5c3ExXTbLc8mueNue5/DoinL80=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA=
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
@ -100,6 +104,8 @@ golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b/go.mod h1:YDH+HFinaLZZlnHAfS
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
@ -120,17 +126,21 @@ golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
golang.org/x/term v0.3.0 h1:qoo4akIqOcDME5bhc/NgxUdovd6BSS2uMsVjB56q1xI=
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=

View file

@ -13,7 +13,7 @@ import (
)
func main() {
const version string = "0.2.99999-nilix"
const version string = "0.3.0-nilix"
var cfg string
flag.StringVar(&cfg, "config", "./config.yaml", "path to config file")
flag.Parse()

View file

@ -1,8 +1,8 @@
package routes
import (
"net/http"
"html/template"
"net/http"
"git.icyphox.sh/legit/config"
"github.com/alexedwards/flow"

View file

@ -103,7 +103,9 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
return
}
commits, err := gr.Commits()
mailmap := gr.GetMailMap(d.c.Mailmap)
commits, err := gr.Commits(mailmap)
if err != nil {
d.Write500(w)
log.Println(err)
@ -237,7 +239,9 @@ func (d *deps) Log(w http.ResponseWriter, r *http.Request) {
return
}
commits, err := gr.Commits()
mailmap := gr.GetMailMap(d.c.Mailmap)
commits, err := gr.Commits(mailmap)
if err != nil {
d.Write500(w)
log.Println(err)
@ -273,13 +277,21 @@ func (d *deps) Diff(w http.ResponseWriter, r *http.Request) {
return
}
mailmap := gr.GetMailMap(d.c.Mailmap)
diff, err := gr.Diff()
if err != nil {
d.Write500(w)
log.Println(err)
return
}
name, ok := mailmap[diff.Commit.Author.Email]
if ok {
diff.Commit.Author.Name = name
}
data := make(map[string]interface{})
data["commit"] = diff.Commit