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