2022-12-11 05:52:47 +00:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
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-12 16:03:09 +00:00
|
|
|
"time"
|
2022-12-11 05:52:47 +00:00
|
|
|
|
|
|
|
"github.com/alexedwards/flow"
|
|
|
|
"icyphox.sh/legit/config"
|
|
|
|
"icyphox.sh/legit/git"
|
|
|
|
)
|
|
|
|
|
|
|
|
type deps struct {
|
|
|
|
c *config.Config
|
|
|
|
}
|
|
|
|
|
2022-12-12 15:23:58 +00:00
|
|
|
func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
|
2022-12-12 16:03:09 +00:00
|
|
|
dirs, err := os.ReadDir(d.c.Git.ScanPath)
|
|
|
|
if err != nil {
|
|
|
|
d.Write500(w)
|
|
|
|
log.Printf("reading scan path: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
repoInfo := make(map[string]time.Time)
|
|
|
|
|
|
|
|
for _, dir := range dirs {
|
|
|
|
path := filepath.Join(d.c.Git.ScanPath, dir.Name())
|
|
|
|
gr, err := git.Open(path, "")
|
|
|
|
if err != nil {
|
|
|
|
d.Write500(w)
|
|
|
|
log.Printf("opening dir %s: %s", path, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := gr.LastCommit()
|
|
|
|
if err != nil {
|
|
|
|
d.Write500(w)
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
repoInfo[dir.Name()] = c.Author.When
|
|
|
|
}
|
|
|
|
|
|
|
|
tpath := filepath.Join(d.c.Template.Dir, "*")
|
|
|
|
t := template.Must(template.ParseGlob(tpath))
|
|
|
|
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
data["meta"] = d.c.Meta
|
|
|
|
data["info"] = repoInfo
|
2022-12-12 15:23:58 +00:00
|
|
|
|
2022-12-12 16:03:09 +00:00
|
|
|
if err := t.ExecuteTemplate(w, "index", data); err != nil {
|
|
|
|
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")
|
|
|
|
name = filepath.Clean(name)
|
2022-12-12 16:03:09 +00:00
|
|
|
path := filepath.Join(d.c.Git.ScanPath, name)
|
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
|
|
|
|
}
|
|
|
|
|
2022-12-11 15:47:04 +00:00
|
|
|
files, err := gr.FileTree("")
|
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-12 15:23:58 +00:00
|
|
|
var readmeContent string
|
|
|
|
for _, readme := range d.c.Git.Readme {
|
|
|
|
readmeContent, _ = gr.FileContent(readme)
|
|
|
|
if readmeContent != "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if readmeContent == "" {
|
|
|
|
log.Printf("no readme found for %s", name)
|
|
|
|
}
|
|
|
|
|
2022-12-11 15:47:04 +00:00
|
|
|
data := make(map[string]any)
|
|
|
|
data["name"] = name
|
|
|
|
// TODO: make this configurable
|
|
|
|
data["ref"] = "master"
|
2022-12-12 15:23:58 +00:00
|
|
|
data["readme"] = readmeContent
|
2022-12-11 06:59:50 +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) RepoTree(w http.ResponseWriter, r *http.Request) {
|
2022-12-11 05:52:47 +00:00
|
|
|
name := flow.Param(r.Context(), "name")
|
|
|
|
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-12 16:03:09 +00:00
|
|
|
path := filepath.Join(d.c.Git.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-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")
|
|
|
|
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-12 16:03:09 +00:00
|
|
|
path := filepath.Join(d.c.Git.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
|
|
|
|
2022-12-11 15:47:04 +00:00
|
|
|
contents, err := gr.FileContent(treePath)
|
|
|
|
data := make(map[string]any)
|
|
|
|
data["name"] = name
|
|
|
|
data["ref"] = ref
|
|
|
|
|
|
|
|
d.showFile(contents, data, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *deps) Log(w http.ResponseWriter, r *http.Request) {
|
|
|
|
name := flow.Param(r.Context(), "name")
|
|
|
|
ref := flow.Param(r.Context(), "ref")
|
|
|
|
|
2022-12-12 16:03:09 +00:00
|
|
|
path := filepath.Join(d.c.Git.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
|
|
|
|
}
|
|
|
|
|
|
|
|
commits, err := gr.Commits()
|
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
|
|
|
tpath := filepath.Join(d.c.Template.Dir, "*")
|
|
|
|
t := template.Must(template.ParseGlob(tpath))
|
|
|
|
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
data["commits"] = commits
|
|
|
|
data["meta"] = d.c.Meta
|
|
|
|
data["name"] = name
|
|
|
|
data["ref"] = ref
|
|
|
|
|
|
|
|
if err := t.ExecuteTemplate(w, "log", data); err != nil {
|
|
|
|
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")
|
|
|
|
ref := flow.Param(r.Context(), "ref")
|
|
|
|
|
2022-12-12 16:03:09 +00:00
|
|
|
path := filepath.Join(d.c.Git.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
|
|
|
|
}
|
|
|
|
|
|
|
|
diff, err := gr.Diff()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
tpath := filepath.Join(d.c.Template.Dir, "*")
|
|
|
|
t := template.Must(template.ParseGlob(tpath))
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if err := t.ExecuteTemplate(w, "commit", data); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|