all repos — elgit @ 4824077ca3d2cd326880229ba3985bcf70a1fafa

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

prefetch main repo metadata

Alan Pearce
commit

4824077ca3d2cd326880229ba3985bcf70a1fafa

parent

ef83d4e55a4b8a65c982bfb80c00b2b00950cf06

1 file changed, 73 insertions(+), 94 deletions(-)

changed files
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")