all repos — legit @ 68184129028fc75defdcb7b4b4b55b93e2aa7cca

web frontend for git

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