all repos — legit @ eda8b58d9f2e85f1bae92ce3b70f279b89031046

web frontend for git, written in go

git: find main branch from config
Anirudh Oppiliappan x@icyphox.sh
Tue, 13 Dec 2022 09:58:40 +0530
commit

eda8b58d9f2e85f1bae92ce3b70f279b89031046

parent

551c6637250172c5a2ca05070c011b1d72bb7d02

4 files changed, 37 insertions(+), 17 deletions(-)

jump to
M config.yamlconfig.yaml

@@ -1,10 +1,13 @@

-git: +repo: scanPath: /home/icy/code/tmp/testrepos readme: - readme - README - readme.md - README.md + mainBranch: + - master + - main template: dir: ./templates meta:
M config/config.goconfig/config.go

@@ -8,10 +8,11 @@ "gopkg.in/yaml.v3"

) type Config struct { - Git struct { - ScanPath string `yaml:"scanPath"` - Readme []string `yaml:"readme"` - } `yaml:"git"` + Repo struct { + ScanPath string `yaml:"scanPath"` + Readme []string `yaml:"readme"` + MainBranch []string `yaml:"mainBranch"` + } `yaml:"repo"` Template struct { Dir string `yaml:"dir"` } `yaml:"template"`

@@ -21,7 +22,7 @@ Description string `yaml:"description"`

} `yaml:"meta"` Server struct { Host string `yaml:"host"` - Port int `yaml:"port"` + Port int `yaml:"port"` } `yaml:"server"` }
M git/git.gogit/git.go

@@ -110,3 +110,13 @@ })

return branches, nil } + +func (g *GitRepo) FindMainBranch(branches []string) (string, error) { + for _, b := range branches { + _, err := g.r.ResolveRevision(plumbing.Revision(b)) + if err == nil { + return b, nil + } + } + return "", fmt.Errorf("unable to find main branch") +}
M routes/routes.goroutes/routes.go

@@ -18,7 +18,7 @@ c *config.Config

} func (d *deps) Index(w http.ResponseWriter, r *http.Request) { - dirs, err := os.ReadDir(d.c.Git.ScanPath) + dirs, err := os.ReadDir(d.c.Repo.ScanPath) if err != nil { d.Write500(w) log.Printf("reading scan path: %s", err)

@@ -28,7 +28,7 @@

repoInfo := make(map[string]time.Time) for _, dir := range dirs { - path := filepath.Join(d.c.Git.ScanPath, dir.Name()) + path := filepath.Join(d.c.Repo.ScanPath, dir.Name()) gr, err := git.Open(path, "") if err != nil { d.Write500(w)

@@ -61,7 +61,7 @@

func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) { name := flow.Param(r.Context(), "name") name = filepath.Clean(name) - path := filepath.Join(d.c.Git.ScanPath, name) + path := filepath.Join(d.c.Repo.ScanPath, name) gr, err := git.Open(path, "") if err != nil { d.Write404(w)

@@ -76,7 +76,7 @@ return

} var readmeContent string - for _, readme := range d.c.Git.Readme { + for _, readme := range d.c.Repo.Readme { readmeContent, _ = gr.FileContent(readme) if readmeContent != "" { break

@@ -87,10 +87,16 @@ if readmeContent == "" {

log.Printf("no readme found for %s", name) } + mainBranch, err := gr.FindMainBranch(d.c.Repo.MainBranch) + if err != nil { + d.Write500(w) + log.Println(err) + return + } + data := make(map[string]any) data["name"] = name - // TODO: make this configurable - data["ref"] = "master" + data["ref"] = mainBranch data["readme"] = readmeContent d.listFiles(files, data, w)

@@ -103,7 +109,7 @@ treePath := flow.Param(r.Context(), "...")

ref := flow.Param(r.Context(), "ref") name = filepath.Clean(name) - path := filepath.Join(d.c.Git.ScanPath, name) + path := filepath.Join(d.c.Repo.ScanPath, name) gr, err := git.Open(path, ref) if err != nil { d.Write404(w)

@@ -132,7 +138,7 @@ treePath := flow.Param(r.Context(), "...")

ref := flow.Param(r.Context(), "ref") name = filepath.Clean(name) - path := filepath.Join(d.c.Git.ScanPath, name) + path := filepath.Join(d.c.Repo.ScanPath, name) gr, err := git.Open(path, ref) if err != nil { d.Write404(w)

@@ -152,7 +158,7 @@ func (d *deps) Log(w http.ResponseWriter, r *http.Request) {

name := flow.Param(r.Context(), "name") ref := flow.Param(r.Context(), "ref") - path := filepath.Join(d.c.Git.ScanPath, name) + path := filepath.Join(d.c.Repo.ScanPath, name) gr, err := git.Open(path, ref) if err != nil { d.Write404(w)

@@ -185,7 +191,7 @@ func (d *deps) Diff(w http.ResponseWriter, r *http.Request) {

name := flow.Param(r.Context(), "name") ref := flow.Param(r.Context(), "ref") - path := filepath.Join(d.c.Git.ScanPath, name) + path := filepath.Join(d.c.Repo.ScanPath, name) gr, err := git.Open(path, ref) if err != nil { d.Write404(w)

@@ -220,7 +226,7 @@

func (d *deps) Refs(w http.ResponseWriter, r *http.Request) { name := flow.Param(r.Context(), "name") - path := filepath.Join(d.c.Git.ScanPath, name) + path := filepath.Join(d.c.Repo.ScanPath, name) gr, err := git.Open(path, "") if err != nil { d.Write404(w)