this repo has no description
1package routes 2 3import ( 4 "fmt" 5 "io/fs" 6 "log" 7 "net/http" 8 "os" 9 "path/filepath" 10 "strings" 11 12 "github.com/go-chi/chi/v5" 13 "github.com/icyphox/bild/legit/git" 14 "github.com/microcosm-cc/bluemonday" 15) 16 17func sanitize(content []byte) []byte { 18 return bluemonday.UGCPolicy().SanitizeBytes([]byte(content)) 19} 20 21func isGoModule(gr *git.GitRepo) bool { 22 _, err := gr.FileContent("go.mod") 23 return err == nil 24} 25 26func uniqueName(r *http.Request) string { 27 user := chi.URLParam(r, "user") 28 name := chi.URLParam(r, "name") 29 return fmt.Sprintf("%s/%s", user, name) 30} 31 32func getDisplayName(name string) string { 33 return strings.TrimSuffix(name, ".git") 34} 35 36func getDescription(path string) (desc string) { 37 db, err := os.ReadFile(filepath.Join(path, "description")) 38 if err == nil { 39 desc = string(db) 40 } else { 41 desc = "" 42 } 43 return 44} 45 46func (h *Handle) isUnlisted(name string) bool { 47 for _, i := range h.c.Repo.Unlisted { 48 if name == i { 49 return true 50 } 51 } 52 53 return false 54} 55 56func (h *Handle) isIgnored(name string) bool { 57 for _, i := range h.c.Repo.Ignore { 58 if name == i { 59 return true 60 } 61 } 62 63 return false 64} 65 66type repoInfo struct { 67 Git *git.GitRepo 68 Path string 69 Category string 70} 71 72func (d *Handle) getAllRepos() ([]repoInfo, error) { 73 repos := []repoInfo{} 74 max := strings.Count(d.c.Repo.ScanPath, string(os.PathSeparator)) + 2 75 76 err := filepath.WalkDir(d.c.Repo.ScanPath, func(path string, de fs.DirEntry, err error) error { 77 if err != nil { 78 return err 79 } 80 81 if de.IsDir() { 82 // Check if we've exceeded our recursion depth 83 if strings.Count(path, string(os.PathSeparator)) > max { 84 return fs.SkipDir 85 } 86 87 if d.isIgnored(path) { 88 return fs.SkipDir 89 } 90 91 // A bare repo should always have at least a HEAD file, if it 92 // doesn't we can continue recursing 93 if _, err := os.Lstat(filepath.Join(path, "HEAD")); err == nil { 94 repo, err := git.Open(path, "") 95 if err != nil { 96 log.Println(err) 97 } else { 98 relpath, _ := filepath.Rel(d.c.Repo.ScanPath, path) 99 repos = append(repos, repoInfo{ 100 Git: repo, 101 Path: relpath, 102 Category: d.category(path), 103 }) 104 // Since we found a Git repo, we don't want to recurse 105 // further 106 return fs.SkipDir 107 } 108 } 109 } 110 return nil 111 }) 112 113 return repos, err 114} 115 116func (d *Handle) category(path string) string { 117 return strings.TrimPrefix(filepath.Dir(strings.TrimPrefix(path, d.c.Repo.ScanPath)), string(os.PathSeparator)) 118} 119 120func setContentDisposition(w http.ResponseWriter, name string) { 121 h := "inline; filename=\"" + name + "\"" 122 w.Header().Add("Content-Disposition", h) 123} 124 125func setGZipMIME(w http.ResponseWriter) { 126 setMIME(w, "application/gzip") 127} 128 129func setMIME(w http.ResponseWriter, mime string) { 130 w.Header().Add("Content-Type", mime) 131}