all repos — legit @ 5dc6516d74885ce3f03db9dd997da2e11517b8f7

web frontend for git, written in go

routes/util.go (view raw)

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