Small task automation and simplification tool for bare-bones git hosting. https://aphrodite.dev/~notebook/projects/gitmgr.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

87 lines
1.9 KiB

package main
import (
"bytes"
"embed"
"fmt"
html "html/template"
"path"
"strings"
text "text/template"
)
//go:embed templates
var templateFS embed.FS
//go:embed static/*
var staticFS embed.FS
var (
Views, _ = html.New("").Funcs(html.FuncMap{
"trim": Trim,
"empty": Empty,
"url": Url,
"reftype": RefTypeLabel,
"prettify": PrettifyRef,
"public_url": PublicGitUrl,
"title": GetTitle,
}).ParseFS(templateFS, "templates/*.html")
// Global configs
MaxDescriptionLength = 20
UrlPrefix = ""
PublicLinkTemplate *text.Template
Title = ""
)
func Trim(input string) html.HTML {
if len(input) > MaxDescriptionLength+1 {
return html.HTML(input[:MaxDescriptionLength] + "…")
}
return html.HTML(input)
}
func Empty(input string) bool {
return len(input) == 0
}
func Url(input string) string {
return path.Join(UrlPrefix, input)
}
func RefTypeLabel(input int) string {
return map[RefType]string{
RefTypeUnknown: "Unknown ref type",
RefTypeBranch: "Branches",
RefTypeTag: "Tags",
RefTypePR: "Pull/Merge requests (Github / Gitlab mirrored branch)",
}[input]
}
func PrettifyRef(input string, refType int) string {
return map[RefType]func(string) string{
RefTypeUnknown: func(a string) string { return a },
RefTypeBranch: func(a string) string { return strings.TrimPrefix(a, "heads/") },
RefTypeTag: func(a string) string { return strings.Replace(a, "tags/", "# ", 1) },
RefTypePR: func(a string) string {
tokens := strings.Split(a, "/")
return fmt.Sprintf(
"# %s (%s)",
tokens[1],
map[string]string{
"head": "Changes code",
"merge": "Temp. merge branch",
}[tokens[2]],
)
},
}[refType](input)
}
func PublicGitUrl(repo string) string {
var tpl bytes.Buffer
_ = PublicLinkTemplate.Execute(&tpl, repo)
return tpl.String()
}
func GetTitle() string {
return Title
}