all repos — legit @ acac8d47d0dd4bab02274f750d22937044bee988

web frontend for git, written in go

routes/util.go (view raw)

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