web frontend for git (tangled's grandpa)
7
fork

Configure Feed

Select the types of activity you want to include in your feed.

utils: Add helper to get all repos

This does a filepath.WalkDir of the config.ScanPath and tries to find
all valid git repos. It returns a list of repoInfo structs holding basic
information of each repository.

authored by

Daniele Sluijters and committed by
Anirudh Oppiliappan
ee5ab32a ee459fce

+57
+57
routes/util.go
··· 1 1 package routes 2 2 3 3 import ( 4 + "io/fs" 5 + "log" 4 6 "os" 5 7 "path/filepath" 8 + "strings" 6 9 7 10 "git.icyphox.sh/legit/git" 8 11 ) ··· 31 34 32 35 return false 33 36 } 37 + 38 + type repoInfo struct { 39 + Git *git.GitRepo 40 + Path string 41 + Category string 42 + } 43 + 44 + func (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 + 88 + func (d *deps) category(path string) string { 89 + return strings.TrimPrefix(filepath.Dir(strings.TrimPrefix(path, d.c.Repo.ScanPath)), string(os.PathSeparator)) 90 + }