all repos — elgit @ 4824077ca3d2cd326880229ba3985bcf70a1fafa

fork of legit: web frontend for git, written in go

prefetch main repo metadata

Alan Pearce
commit

4824077ca3d2cd326880229ba3985bcf70a1fafa

parent

ef83d4e55a4b8a65c982bfb80c00b2b00950cf06

4 files changed, 134 insertions(+), 200 deletions(-)

changed files
M data/entries.godata/entries.go
@@ -3,15 +3,22 @@
import ( "sort" "time" + + "alin.ovh/elgit/git" ) type Repository struct { - Name string - Category string - Path string - Slug string - Description string - LastCommit time.Time + Name string + Category string + Path string + Slug string + Description string + LastCommit time.Time + LastCommits []*git.CommitReference + ReadmeContent string + MainBranch string + Gomod bool + *git.Repo } type Entry struct {
M routes/routes.goroutes/routes.go
@@ -13,14 +13,9 @@ "alin.ovh/elgit/data"
"alin.ovh/elgit/git" "alin.ovh/elgit/templates" "github.com/microcosm-cc/bluemonday" - "github.com/russross/blackfriday/v2" + blackfriday "github.com/russross/blackfriday/v2" "github.com/savsgio/atreugo/v11" "github.com/valyala/fasthttp" -) - -const ( - RepoIndexCommitCountReadme = 3 - RepoIndexCommitCountNoReadme = 10 ) type deps struct {
@@ -41,81 +36,36 @@ }
func (d *deps) RepoIndex(rc *atreugo.RequestCtx) error { repoName, _ := rc.UserValue("repoName").(string) - repoPath, _ := rc.UserValue("repoPath").(string) - gr, err := git.Open(repoPath, "") - if err != nil { + gr, found := d.repos.BySlug[repoName] + if !found { return d.NotFound(rc) } - commits, err := gr.Commits() - if err != nil { - return err - } - - var readmeContent string - for _, readme := range d.c.Repo.Readme { - ext := filepath.Ext(readme) - content, _ := gr.FileContent(readme) - if len(content) > 0 { - switch ext { - case ".md", ".mkd", ".markdown": - unsafe := blackfriday.Run( - []byte(content), - blackfriday.WithExtensions(blackfriday.CommonExtensions), - ) - html := bluemonday.UGCPolicy().SanitizeBytes(unsafe) - readmeContent = string(html) - default: - safe := bluemonday.UGCPolicy().SanitizeBytes([]byte(content)) - readmeContent = fmt.Sprintf(`<pre>%s</pre>`, safe) - } - - break - } - } - - mainBranch, err := gr.FindMainBranch(d.c.Repo.MainBranch) - if err != nil { - return err - } - - var baseComparison int - if readmeContent == "" { - baseComparison = RepoIndexCommitCountNoReadme - } else { - baseComparison = RepoIndexCommitCountReadme - } - - if len(commits) >= baseComparison { - commits = commits[:baseComparison] - } - pageData := templates.PageData{ Meta: d.c.Meta, Name: repoName, - Ref: mainBranch, - Description: getDescription(repoPath), + Ref: gr.MainBranch, + Description: gr.Description, Servername: d.c.Server.Name, - Gomod: isGoModule(gr), + Gomod: gr.Gomod, } rc.SetContentType("text/html; charset=utf-8") - return templates.RepoPage(pageData, commits, readmeContent).Render(rc) + return templates.RepoPage(pageData, gr.LastCommits, gr.ReadmeContent).Render(rc) } func (d *deps) RepoTree(rc *atreugo.RequestCtx) error { repoName, _ := rc.UserValue("repoName").(string) - repoPath, _ := rc.UserValue("repoPath").(string) rest, _ := rc.UserValue("rest").(string) treePath := strings.TrimSuffix(rest, "/") ref := rc.UserValue("ref").(string) - gr, err := git.Open(repoPath, ref) - if err != nil { - return err + gr, found := d.repos.BySlug[repoName] + if !found { + return d.NotFound(rc) } files, err := gr.FileTree(treePath)
@@ -123,16 +73,17 @@ if err != nil {
return err } - data := make(map[string]any) - data["name"] = repoName - data["ref"] = ref - data["parent"] = treePath - data["desc"] = getDescription(repoPath) - data["dotdot"] = filepath.Dir(treePath) + rc.SetContentType("text/html; charset=utf-8") - rc.SetContentType("text/html; charset=utf-8") + pageData := templates.PageData{ + Meta: d.c.Meta, + Name: repoName, + Ref: ref, + Description: gr.Description, + Parent: treePath, + } - return d.listFiles(files, data, rc) + return templates.TreePage(pageData, files, gr.ReadmeContent, filepath.Dir(treePath)).Render(rc) } func (d *deps) FileContent(rc *atreugo.RequestCtx) error {
@@ -142,13 +93,12 @@ raw = rawParam
} repoName, _ := rc.UserValue("repoName").(string) - repoPath, _ := rc.UserValue("repoPath").(string) treePath := rc.UserValue("rest").(string) ref := rc.UserValue("ref").(string) - gr, err := git.Open(repoPath, ref) - if err != nil { + gr, found := d.repos.BySlug[repoName] + if !found { return d.NotFound(rc) }
@@ -156,11 +106,6 @@ contents, err := gr.FileContent(treePath)
if err != nil { return err } - data := make(map[string]any) - data["name"] = repoName - data["ref"] = ref - data["desc"] = getDescription(repoPath) - data["path"] = treePath if raw { return rc.TextResponse(contents)
@@ -168,7 +113,45 @@ }
rc.SetContentType("text/html; charset=utf-8") - return d.showFile(contents, data, rc) + pageData := templates.PageData{ + Meta: d.c.Meta, + Name: repoName, + Ref: ref, + Description: gr.Description, + Path: treePath, + Content: contents, + } + + if len(pageData.Content) > 0 { + switch filepath.Ext(pageData.Path) { + case ".md": + unsafe := blackfriday.Run( + []byte(pageData.Content), + blackfriday.WithExtensions(blackfriday.CommonExtensions), + ) + html := bluemonday.UGCPolicy().SanitizeBytes(unsafe) + pageData.RenderedContent = string(html) + default: + safe := bluemonday.UGCPolicy().SanitizeBytes([]byte(pageData.Content)) + pageData.RenderedContent = fmt.Sprintf(`<pre>%s</pre>`, safe) + } + } + + lc, err := countLines(strings.NewReader(pageData.Content)) + if err != nil { + log.Printf("counting lines: %s", err) + } + + lines := make([]int, lc) + if lc > 0 { + for i := range lines { + lines[i] = i + 1 + } + } + + pageData.LineCount = lines + + return templates.FilePage(pageData).Render(rc) } func (d *deps) Archive(rc *atreugo.RequestCtx) error {
@@ -223,11 +206,10 @@ }
func (d *deps) Log(rc *atreugo.RequestCtx) error { repoName, _ := rc.UserValue("repoName").(string) - repoPath, _ := rc.UserValue("repoPath").(string) ref := rc.UserValue("ref").(string) - gr, err := git.Open(repoPath, ref) - if err != nil { + gr, found := d.repos.BySlug[repoName] + if !found { return d.NotFound(rc) }
@@ -240,7 +222,7 @@ pageData := templates.PageData{
Meta: d.c.Meta, Name: repoName, Ref: ref, - Description: getDescription(repoPath), + Description: gr.Description, Log: true, }
@@ -251,11 +233,10 @@ }
func (d *deps) Diff(rc *atreugo.RequestCtx) error { repoName, _ := rc.UserValue("repoName").(string) - repoPath, _ := rc.UserValue("repoPath").(string) ref := rc.UserValue("ref").(string) - gr, err := git.Open(repoPath, ref) - if err != nil { + gr, found := d.repos.BySlug[repoName] + if !found { return d.NotFound(rc) }
@@ -270,7 +251,7 @@ Name: repoName,
Stat: diff.Stat, Diff: diff.Diff, Ref: ref, - Description: getDescription(repoPath), + Description: gr.Description, } rc.SetContentType("text/html; charset=utf-8")
@@ -281,14 +262,13 @@
// FileDiff shows the changes to a specific file in a commit func (d *deps) FileDiff(rc *atreugo.RequestCtx) error { repoName, _ := rc.UserValue("repoName").(string) - repoPath, _ := rc.UserValue("repoPath").(string) ref := rc.UserValue("ref").(string) filePath := strings.TrimSuffix(rc.UserValue("file").(string), "/") - g, err := git.Open(repoPath, ref) - if err != nil { - return err + g, found := d.repos.BySlug[repoName] + if !found { + return d.NotFound(rc) } diff, err := g.DiffFile(filePath)
@@ -300,7 +280,7 @@ pageData := templates.PageData{
Meta: d.c.Meta, Name: repoName, Ref: ref, - Description: getDescription(repoPath), + Description: g.Description, Path: filePath, Diff: diff.Diff, }
@@ -312,10 +292,9 @@ }
func (d *deps) Refs(rc *atreugo.RequestCtx) error { repoName, _ := rc.UserValue("repoName").(string) - repoPath, _ := rc.UserValue("repoPath").(string) - gr, err := git.Open(repoPath, "") - if err != nil { + gr, found := d.repos.BySlug[repoName] + if !found { return d.NotFound(rc) }
@@ -333,7 +312,7 @@
pageData := templates.PageData{ Meta: d.c.Meta, Name: repoName, - Description: getDescription(repoPath), + Description: gr.Description, } rc.SetContentType("text/html; charset=utf-8")
M routes/template.goroutes/template.go
@@ -2,63 +2,9 @@ package routes
import ( "bytes" - "fmt" "io" - "log" - "path/filepath" - "strings" - - "alin.ovh/elgit/git" - "alin.ovh/elgit/templates" - "github.com/microcosm-cc/bluemonday" - "github.com/russross/blackfriday/v2" - "github.com/savsgio/atreugo/v11" ) -func (d *deps) WriteError(rc *atreugo.RequestCtx, err error, i int) error { - rc.SetStatusCode(i) - data := templates.PageData{ - Meta: d.c.Meta, - Error: &templates.Error{ - Code: i, - Message: err.Error(), - }, - } - if err := templates.ErrorPage(data).Render(rc); err != nil { - log.Printf("error template: %s", err) - - return err - } - - return nil -} - -func (d *deps) listFiles(files []git.NiceTree, data map[string]any, rc *atreugo.RequestCtx) error { - pageData := templates.PageData{ - Meta: d.c.Meta, - Name: data["name"].(string), - Ref: data["ref"].(string), - Description: data["desc"].(string), - Parent: "", - } - - if parent, ok := data["parent"]; ok && parent != nil { - pageData.Parent = parent.(string) - } - - readme := "" - if readmeContent, ok := data["readme"]; ok && readmeContent != nil { - readme = readmeContent.(string) - } - - dotdot := "" - if dotdotPath, ok := data["dotdot"]; ok && dotdotPath != nil { - dotdot = dotdotPath.(string) - } - - return templates.TreePage(pageData, files, readme, dotdot).Render(rc) -} - func countLines(r io.Reader) (int, error) { buf := make([]byte, 32*1024) bufLen := 0
@@ -85,47 +31,3 @@ return 0, err
} } } - -func (d *deps) showFile(content string, data map[string]any, rc *atreugo.RequestCtx) error { - var renderedContent string - if len(content) > 0 { - switch filepath.Ext(data["path"].(string)) { - case ".md": - unsafe := blackfriday.Run( - []byte(content), - blackfriday.WithExtensions(blackfriday.CommonExtensions), - ) - html := bluemonday.UGCPolicy().SanitizeBytes(unsafe) - renderedContent = string(html) - default: - safe := bluemonday.UGCPolicy().SanitizeBytes([]byte(content)) - renderedContent = fmt.Sprintf(`<pre>%s</pre>`, safe) - } - } - - lc, err := countLines(strings.NewReader(content)) - if err != nil { - // Non-fatal, we'll just skip showing line numbers in the template. - log.Printf("counting lines: %s", err) - } - - lines := make([]int, lc) - if lc > 0 { - for i := range lines { - lines[i] = i + 1 - } - } - - pageData := templates.PageData{ - Meta: d.c.Meta, - LineCount: lines, - Content: content, - RenderedContent: renderedContent, - Name: data["name"].(string), - Ref: data["ref"].(string), - Description: data["desc"].(string), - Path: data["path"].(string), - } - - return templates.FilePage(pageData).Render(rc) -}
M routes/util.goroutes/util.go
@@ -2,6 +2,7 @@ package routes
import ( "bufio" + "fmt" "log" "os" "path"
@@ -12,7 +13,14 @@
"alin.ovh/elgit/data" "alin.ovh/elgit/git" securejoin "github.com/cyphar/filepath-securejoin" + "github.com/microcosm-cc/bluemonday" + "github.com/russross/blackfriday/v2" "github.com/savsgio/atreugo/v11" +) + +const ( + RepoIndexCommitCountReadme = 3 + RepoIndexCommitCountNoReadme = 10 ) func isGoModule(gr *git.Repo) bool {
@@ -93,10 +101,48 @@ Category: category,
Path: fullPath, Slug: project, Description: getDescription(fullPath), + Repo: repo, + Gomod: isGoModule(repo), } - if cc, err := repo.LastCommit(); err == nil { - r.LastCommit = cc.Author.When + + r.MainBranch, err = r.FindMainBranch(d.c.Repo.MainBranch) + if err != nil { + log.Println(err) } + + for _, readme := range d.c.Repo.Readme { + ext := filepath.Ext(readme) + content, _ := r.FileContent(readme) + if len(content) > 0 { + switch ext { + case ".md", ".mkd", ".markdown": + unsafe := blackfriday.Run( + []byte(content), + blackfriday.WithExtensions(blackfriday.CommonExtensions), + ) + html := bluemonday.UGCPolicy().SanitizeBytes(unsafe) + r.ReadmeContent = string(html) + default: + safe := bluemonday.UGCPolicy().SanitizeBytes([]byte(content)) + r.ReadmeContent = fmt.Sprintf(`<pre>%s</pre>`, safe) + } + + break + } + } + + var baseComparison int + if r.ReadmeContent == "" { + baseComparison = RepoIndexCommitCountNoReadme + } else { + baseComparison = RepoIndexCommitCountReadme + } + + if commits, err := repo.Commits(); err == nil { + r.LastCommits = commits[:min(len(commits), baseComparison)] + r.LastCommit = commits[0].Author.When + } + entries.Add(&r) } }