2022-12-11 05:52:47 +00:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2023-01-31 23:27:22 +00:00
|
|
|
"bytes"
|
2022-12-26 06:20:08 +00:00
|
|
|
"fmt"
|
2022-12-11 15:47:04 +00:00
|
|
|
"html/template"
|
2022-12-11 05:52:47 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2022-12-12 16:03:09 +00:00
|
|
|
"os"
|
2022-12-11 05:52:47 +00:00
|
|
|
"path/filepath"
|
2022-12-18 05:49:14 +00:00
|
|
|
"sort"
|
|
|
|
"time"
|
2022-12-11 05:52:47 +00:00
|
|
|
|
2022-12-19 03:32:23 +00:00
|
|
|
"git.icyphox.sh/legit/config"
|
|
|
|
"git.icyphox.sh/legit/git"
|
2022-12-11 05:52:47 +00:00
|
|
|
"github.com/alexedwards/flow"
|
2022-12-14 12:08:48 +00:00
|
|
|
"github.com/dustin/go-humanize"
|
2022-12-26 06:20:08 +00:00
|
|
|
"github.com/microcosm-cc/bluemonday"
|
|
|
|
"github.com/russross/blackfriday/v2"
|
2022-12-11 05:52:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type deps struct {
|
|
|
|
c *config.Config
|
2023-02-03 05:05:17 +00:00
|
|
|
t *template.Template
|
2022-12-11 05:52:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 15:23:58 +00:00
|
|
|
func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
|
2022-12-13 04:28:40 +00:00
|
|
|
dirs, err := os.ReadDir(d.c.Repo.ScanPath)
|
2022-12-12 16:03:09 +00:00
|
|
|
if err != nil {
|
|
|
|
d.Write500(w)
|
|
|
|
log.Printf("reading scan path: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-18 05:49:14 +00:00
|
|
|
type info struct {
|
|
|
|
Name, Desc, Idle string
|
|
|
|
d time.Time
|
|
|
|
}
|
2022-12-14 12:08:48 +00:00
|
|
|
|
|
|
|
infos := []info{}
|
2022-12-12 16:03:09 +00:00
|
|
|
|
|
|
|
for _, dir := range dirs {
|
2022-12-22 15:43:49 +00:00
|
|
|
if d.isIgnored(dir.Name()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-12-13 04:28:40 +00:00
|
|
|
path := filepath.Join(d.c.Repo.ScanPath, dir.Name())
|
2022-12-12 16:03:09 +00:00
|
|
|
gr, err := git.Open(path, "")
|
|
|
|
if err != nil {
|
2022-12-18 06:13:50 +00:00
|
|
|
continue
|
2022-12-12 16:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c, err := gr.LastCommit()
|
|
|
|
if err != nil {
|
|
|
|
d.Write500(w)
|
|
|
|
log.Println(err)
|
2022-12-17 07:15:21 +00:00
|
|
|
return
|
2022-12-12 16:03:09 +00:00
|
|
|
}
|
|
|
|
|
2022-12-17 16:03:04 +00:00
|
|
|
desc := getDescription(path)
|
2022-12-14 12:08:48 +00:00
|
|
|
|
|
|
|
infos = append(infos, info{
|
|
|
|
Name: dir.Name(),
|
|
|
|
Desc: desc,
|
|
|
|
Idle: humanize.Time(c.Author.When),
|
2022-12-18 05:49:14 +00:00
|
|
|
d: c.Author.When,
|
2022-12-14 12:08:48 +00:00
|
|
|
})
|
2022-12-12 16:03:09 +00:00
|
|
|
}
|
|
|
|
|
2022-12-18 05:49:14 +00:00
|
|
|
sort.Slice(infos, func(i, j int) bool {
|
|
|
|
return infos[j].d.Before(infos[i].d)
|
|
|
|
})
|
|
|
|
|
2022-12-12 16:03:09 +00:00
|
|
|
data := make(map[string]interface{})
|
|
|
|
data["meta"] = d.c.Meta
|
2022-12-14 12:08:48 +00:00
|
|
|
data["info"] = infos
|
2022-12-12 15:23:58 +00:00
|
|
|
|
2023-02-03 05:05:17 +00:00
|
|
|
if err := d.t.ExecuteTemplate(w, "index", data); err != nil {
|
2022-12-12 16:03:09 +00:00
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2022-12-12 15:23:58 +00:00
|
|
|
}
|
|
|
|
|
2022-12-11 06:59:50 +00:00
|
|
|
func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
|
|
|
|
name := flow.Param(r.Context(), "name")
|
2022-12-22 15:43:49 +00:00
|
|
|
if d.isIgnored(name) {
|
|
|
|
d.Write404(w)
|
|
|
|
return
|
|
|
|
}
|
2022-12-11 06:59:50 +00:00
|
|
|
name = filepath.Clean(name)
|
2022-12-13 04:28:40 +00:00
|
|
|
path := filepath.Join(d.c.Repo.ScanPath, name)
|
2022-12-22 15:43:49 +00:00
|
|
|
|
2022-12-11 15:47:04 +00:00
|
|
|
gr, err := git.Open(path, "")
|
2022-12-11 06:59:50 +00:00
|
|
|
if err != nil {
|
2022-12-12 15:23:58 +00:00
|
|
|
d.Write404(w)
|
2022-12-11 06:59:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-31 23:27:22 +00:00
|
|
|
mainBranch, err := gr.FindMainBranch(d.c.Repo.MainBranch)
|
|
|
|
if err != nil {
|
|
|
|
d.Write500(w)
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-30 04:08:08 +00:00
|
|
|
mailmap := gr.GetMailMap(d.c.Mailmap)
|
|
|
|
|
|
|
|
commits, err := gr.Commits(mailmap)
|
2022-12-11 06:59:50 +00:00
|
|
|
if err != nil {
|
2022-12-12 15:23:58 +00:00
|
|
|
d.Write500(w)
|
2022-12-11 06:59:50 +00:00
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-26 06:20:08 +00:00
|
|
|
var readmeContent template.HTML
|
2022-12-13 04:28:40 +00:00
|
|
|
for _, readme := range d.c.Repo.Readme {
|
2022-12-26 06:20:08 +00:00
|
|
|
ext := filepath.Ext(readme)
|
2023-01-31 23:27:22 +00:00
|
|
|
content, _ := gr.FileContent(readme, false)
|
2022-12-26 06:20:08 +00:00
|
|
|
if len(content) > 0 {
|
|
|
|
switch ext {
|
2022-12-26 06:34:09 +00:00
|
|
|
case ".md", ".mkd", ".markdown":
|
|
|
|
unsafe := blackfriday.Run(
|
|
|
|
[]byte(content),
|
|
|
|
blackfriday.WithExtensions(blackfriday.CommonExtensions),
|
|
|
|
)
|
2022-12-26 06:20:08 +00:00
|
|
|
html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
|
2023-01-31 23:27:22 +00:00
|
|
|
readmeContent = template.HTML(transformRelativeURLs(string(html), name, mainBranch))
|
2022-12-26 06:20:08 +00:00
|
|
|
default:
|
|
|
|
readmeContent = template.HTML(
|
|
|
|
fmt.Sprintf(`<pre>%s</pre>`, content),
|
|
|
|
)
|
|
|
|
}
|
2022-12-12 15:23:58 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if readmeContent == "" {
|
|
|
|
log.Printf("no readme found for %s", name)
|
|
|
|
}
|
|
|
|
|
2022-12-18 05:12:29 +00:00
|
|
|
if len(commits) >= 3 {
|
|
|
|
commits = commits[:3]
|
2022-12-17 16:03:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-11 15:47:04 +00:00
|
|
|
data := make(map[string]any)
|
|
|
|
data["name"] = name
|
2022-12-13 04:28:40 +00:00
|
|
|
data["ref"] = mainBranch
|
2022-12-12 15:23:58 +00:00
|
|
|
data["readme"] = readmeContent
|
2022-12-17 16:03:04 +00:00
|
|
|
data["commits"] = commits
|
|
|
|
data["desc"] = getDescription(path)
|
2022-12-19 03:32:23 +00:00
|
|
|
data["servername"] = d.c.Server.Name
|
2023-01-31 21:27:48 +00:00
|
|
|
data["gomod"] = isGoModule(gr)
|
2023-02-04 16:34:21 +00:00
|
|
|
data["meta"] = d.c.Meta
|
2022-12-17 16:03:04 +00:00
|
|
|
|
2023-02-03 05:05:17 +00:00
|
|
|
if err := d.t.ExecuteTemplate(w, "repo", data); err != nil {
|
2022-12-17 16:03:04 +00:00
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2022-12-11 06:59:50 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-11 08:48:39 +00:00
|
|
|
func (d *deps) RepoTree(w http.ResponseWriter, r *http.Request) {
|
2022-12-11 05:52:47 +00:00
|
|
|
name := flow.Param(r.Context(), "name")
|
2022-12-22 15:43:49 +00:00
|
|
|
if d.isIgnored(name) {
|
|
|
|
d.Write404(w)
|
|
|
|
return
|
|
|
|
}
|
2022-12-11 05:52:47 +00:00
|
|
|
treePath := flow.Param(r.Context(), "...")
|
2022-12-11 06:59:50 +00:00
|
|
|
ref := flow.Param(r.Context(), "ref")
|
2022-12-11 05:52:47 +00:00
|
|
|
|
|
|
|
name = filepath.Clean(name)
|
2022-12-13 04:28:40 +00:00
|
|
|
path := filepath.Join(d.c.Repo.ScanPath, name)
|
2022-12-11 15:47:04 +00:00
|
|
|
gr, err := git.Open(path, ref)
|
2022-12-11 05:52:47 +00:00
|
|
|
if err != nil {
|
2022-12-12 15:23:58 +00:00
|
|
|
d.Write404(w)
|
2022-12-11 05:52:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-11 15:47:04 +00:00
|
|
|
files, err := gr.FileTree(treePath)
|
2022-12-11 06:59:50 +00:00
|
|
|
if err != nil {
|
2022-12-12 15:23:58 +00:00
|
|
|
d.Write500(w)
|
2022-12-11 06:59:50 +00:00
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-11 15:47:04 +00:00
|
|
|
data := make(map[string]any)
|
|
|
|
data["name"] = name
|
|
|
|
data["ref"] = ref
|
|
|
|
data["parent"] = treePath
|
2022-12-18 05:12:29 +00:00
|
|
|
data["desc"] = getDescription(path)
|
2023-02-10 06:30:07 +00:00
|
|
|
data["dotdot"] = filepath.Dir(treePath)
|
2022-12-11 05:52:47 +00:00
|
|
|
|
2022-12-11 15:47:04 +00:00
|
|
|
d.listFiles(files, data, w)
|
2022-12-11 06:59:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-11 08:48:39 +00:00
|
|
|
func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) {
|
|
|
|
name := flow.Param(r.Context(), "name")
|
2022-12-22 15:43:49 +00:00
|
|
|
if d.isIgnored(name) {
|
|
|
|
d.Write404(w)
|
|
|
|
return
|
|
|
|
}
|
2022-12-11 08:48:39 +00:00
|
|
|
treePath := flow.Param(r.Context(), "...")
|
|
|
|
ref := flow.Param(r.Context(), "ref")
|
2022-12-11 05:52:47 +00:00
|
|
|
|
2022-12-11 08:48:39 +00:00
|
|
|
name = filepath.Clean(name)
|
2022-12-13 04:28:40 +00:00
|
|
|
path := filepath.Join(d.c.Repo.ScanPath, name)
|
2022-12-11 15:47:04 +00:00
|
|
|
gr, err := git.Open(path, ref)
|
2022-12-11 08:48:39 +00:00
|
|
|
if err != nil {
|
2022-12-12 15:23:58 +00:00
|
|
|
d.Write404(w)
|
2022-12-11 08:48:39 +00:00
|
|
|
return
|
|
|
|
}
|
2022-12-11 05:52:47 +00:00
|
|
|
|
2023-01-31 23:27:22 +00:00
|
|
|
contents, err := gr.FileContent(treePath, false)
|
2022-12-11 15:47:04 +00:00
|
|
|
data := make(map[string]any)
|
|
|
|
data["name"] = name
|
|
|
|
data["ref"] = ref
|
2022-12-18 05:12:29 +00:00
|
|
|
data["desc"] = getDescription(path)
|
2022-12-18 05:44:38 +00:00
|
|
|
data["path"] = treePath
|
2023-02-22 04:33:11 +00:00
|
|
|
data["raw"] = fmt.Sprintf("/%s/raw/%s/%s", name, ref, treePath)
|
2022-12-11 15:47:04 +00:00
|
|
|
|
|
|
|
d.showFile(contents, data, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *deps) Log(w http.ResponseWriter, r *http.Request) {
|
|
|
|
name := flow.Param(r.Context(), "name")
|
2022-12-22 15:43:49 +00:00
|
|
|
if d.isIgnored(name) {
|
|
|
|
d.Write404(w)
|
|
|
|
return
|
|
|
|
}
|
2022-12-11 15:47:04 +00:00
|
|
|
ref := flow.Param(r.Context(), "ref")
|
|
|
|
|
2022-12-13 04:28:40 +00:00
|
|
|
path := filepath.Join(d.c.Repo.ScanPath, name)
|
2022-12-11 15:47:04 +00:00
|
|
|
gr, err := git.Open(path, ref)
|
|
|
|
if err != nil {
|
2022-12-12 15:23:58 +00:00
|
|
|
d.Write404(w)
|
2022-12-11 15:47:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-30 04:08:08 +00:00
|
|
|
mailmap := gr.GetMailMap(d.c.Mailmap)
|
|
|
|
|
|
|
|
commits, err := gr.Commits(mailmap)
|
2022-12-11 08:48:39 +00:00
|
|
|
if err != nil {
|
2022-12-12 15:23:58 +00:00
|
|
|
d.Write500(w)
|
2022-12-11 05:52:47 +00:00
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2022-12-11 08:48:39 +00:00
|
|
|
|
2022-12-11 15:47:04 +00:00
|
|
|
data := make(map[string]interface{})
|
|
|
|
data["commits"] = commits
|
|
|
|
data["meta"] = d.c.Meta
|
|
|
|
data["name"] = name
|
|
|
|
data["ref"] = ref
|
2022-12-18 05:12:29 +00:00
|
|
|
data["desc"] = getDescription(path)
|
2023-02-10 06:30:07 +00:00
|
|
|
data["log"] = true
|
2022-12-11 15:47:04 +00:00
|
|
|
|
2023-02-03 05:05:17 +00:00
|
|
|
if err := d.t.ExecuteTemplate(w, "log", data); err != nil {
|
2022-12-11 15:47:04 +00:00
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2022-12-11 05:52:47 +00:00
|
|
|
}
|
2022-12-12 11:47:49 +00:00
|
|
|
|
|
|
|
func (d *deps) Diff(w http.ResponseWriter, r *http.Request) {
|
|
|
|
name := flow.Param(r.Context(), "name")
|
2022-12-22 15:43:49 +00:00
|
|
|
if d.isIgnored(name) {
|
|
|
|
d.Write404(w)
|
|
|
|
return
|
|
|
|
}
|
2022-12-12 11:47:49 +00:00
|
|
|
ref := flow.Param(r.Context(), "ref")
|
|
|
|
|
2022-12-13 04:28:40 +00:00
|
|
|
path := filepath.Join(d.c.Repo.ScanPath, name)
|
2022-12-12 11:47:49 +00:00
|
|
|
gr, err := git.Open(path, ref)
|
|
|
|
if err != nil {
|
2022-12-12 15:23:58 +00:00
|
|
|
d.Write404(w)
|
2022-12-12 11:47:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-30 04:08:08 +00:00
|
|
|
mailmap := gr.GetMailMap(d.c.Mailmap)
|
|
|
|
|
2022-12-12 11:47:49 +00:00
|
|
|
diff, err := gr.Diff()
|
2024-01-30 04:08:08 +00:00
|
|
|
|
2022-12-12 11:47:49 +00:00
|
|
|
if err != nil {
|
2022-12-12 15:23:58 +00:00
|
|
|
d.Write500(w)
|
2022-12-12 11:47:49 +00:00
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-30 04:30:56 +00:00
|
|
|
authorname, ok := mailmap[diff.Commit.Author.Email]
|
2024-01-30 04:08:08 +00:00
|
|
|
if ok {
|
2024-01-30 04:30:56 +00:00
|
|
|
diff.Commit.Author.Name = authorname
|
2024-01-30 04:08:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 11:47:49 +00:00
|
|
|
data := make(map[string]interface{})
|
|
|
|
|
|
|
|
data["commit"] = diff.Commit
|
|
|
|
data["stat"] = diff.Stat
|
|
|
|
data["diff"] = diff.Diff
|
|
|
|
data["meta"] = d.c.Meta
|
|
|
|
data["name"] = name
|
|
|
|
data["ref"] = ref
|
2022-12-18 05:12:29 +00:00
|
|
|
data["desc"] = getDescription(path)
|
2022-12-12 11:47:49 +00:00
|
|
|
|
2023-02-03 05:05:17 +00:00
|
|
|
if err := d.t.ExecuteTemplate(w, "commit", data); err != nil {
|
2022-12-12 11:47:49 +00:00
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-12-12 16:28:47 +00:00
|
|
|
|
|
|
|
func (d *deps) Refs(w http.ResponseWriter, r *http.Request) {
|
|
|
|
name := flow.Param(r.Context(), "name")
|
2022-12-22 15:43:49 +00:00
|
|
|
if d.isIgnored(name) {
|
|
|
|
d.Write404(w)
|
|
|
|
return
|
|
|
|
}
|
2022-12-12 16:28:47 +00:00
|
|
|
|
2022-12-13 04:28:40 +00:00
|
|
|
path := filepath.Join(d.c.Repo.ScanPath, name)
|
2022-12-12 16:28:47 +00:00
|
|
|
gr, err := git.Open(path, "")
|
|
|
|
if err != nil {
|
|
|
|
d.Write404(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tags, err := gr.Tags()
|
|
|
|
if err != nil {
|
|
|
|
// Non-fatal, we *should* have at least one branch to show.
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
branches, err := gr.Branches()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
d.Write500(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
|
|
|
|
data["meta"] = d.c.Meta
|
|
|
|
data["name"] = name
|
|
|
|
data["branches"] = branches
|
|
|
|
data["tags"] = tags
|
2022-12-18 05:12:29 +00:00
|
|
|
data["desc"] = getDescription(path)
|
2022-12-12 16:28:47 +00:00
|
|
|
|
2023-02-03 05:05:17 +00:00
|
|
|
if err := d.t.ExecuteTemplate(w, "refs", data); err != nil {
|
2022-12-12 16:28:47 +00:00
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-12-17 07:15:21 +00:00
|
|
|
|
2023-01-31 23:27:22 +00:00
|
|
|
func (d *deps) ServeStaticInRepo(w http.ResponseWriter, r *http.Request) {
|
|
|
|
f := flow.Param(r.Context(), "...")
|
|
|
|
p := flow.Param(r.Context(), "name")
|
|
|
|
ref := flow.Param(r.Context(), "ref")
|
|
|
|
|
|
|
|
repoPath := filepath.Clean(filepath.Join(d.c.Repo.ScanPath, p))
|
|
|
|
gr, err := git.Open(repoPath, ref)
|
|
|
|
if err != nil {
|
|
|
|
d.Write500(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
contents, err := gr.FileContent(f, true)
|
|
|
|
if err != nil {
|
|
|
|
d.Write500(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.ServeContent(w, r, filepath.Base(f), time.Unix(0, 0), bytes.NewReader([]byte(contents)))
|
|
|
|
}
|
|
|
|
|
2022-12-17 07:15:21 +00:00
|
|
|
func (d *deps) ServeStatic(w http.ResponseWriter, r *http.Request) {
|
|
|
|
f := flow.Param(r.Context(), "file")
|
|
|
|
f = filepath.Clean(filepath.Join(d.c.Dirs.Static, f))
|
|
|
|
|
|
|
|
http.ServeFile(w, r, f)
|
|
|
|
}
|