routes/util.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | package routes
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"slices"
"strings"
"alin.ovh/elgit/data"
"alin.ovh/elgit/git"
"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 {
has, _ := gr.HasFile("go.mod")
return has
}
func getDescription(path string) (desc string) {
db, err := os.ReadFile(filepath.Join(path, "description"))
if err == nil {
desc = string(db)
} else {
desc = ""
}
return
}
func (d *deps) isUnlisted(name string) bool {
return slices.Contains(d.c.Repo.Unlisted, name)
}
func (d *deps) isNotAllowed(name string) bool {
return d.repos.BySlug[name] == nil
}
func ReadProjectsList(filename string) ([]string, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
projects := []string{}
for scanner.Scan() {
projects = append(projects, strings.TrimSuffix(scanner.Text(), ".git"))
}
if err := scanner.Err(); err != nil {
return nil, err
}
return projects, nil
}
func (d *deps) UpdateRepos() {
entries := data.NewEntries()
for _, project := range d.projects {
if project == "" || d.isUnlisted(project) {
continue
}
fullPath := filepath.Join(d.c.Repo.Root, "repositories", project+".git")
// A bare repo should always have at least a HEAD file
if _, err := os.Lstat(filepath.Join(fullPath, "HEAD")); err == nil {
repo, err := git.Open(fullPath, "")
if err != nil {
log.Println(err)
} else {
category, name, found := strings.Cut(project, string(os.PathSeparator))
if !found {
category = ""
name = project
}
r := data.Repository{
Name: name,
Category: category,
Path: fullPath,
Slug: project,
Description: getDescription(fullPath),
Repo: repo,
Gomod: isGoModule(repo),
}
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, r.MainBranch)
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)
}
}
}
entries.Sort()
d.repos = &entries
}
func setContentDisposition(rc *atreugo.RequestCtx, name string) {
h := "inline; filename=\"" + name + "\""
rc.Response.Header.Set("Content-Disposition", h)
}
func setGZipMIME(rc *atreugo.RequestCtx) {
rc.SetContentType("application/gzip")
}
|