diff options
author | Anirudh Oppiliappan <x@icyphox.sh> | 2022-12-13 09:58:40 +0530 |
---|---|---|
committer | Anirudh Oppiliappan <x@icyphox.sh> | 2022-12-13 09:58:40 +0530 |
commit | eda8b58d9f2e85f1bae92ce3b70f279b89031046 (patch) | |
tree | 8b365320501fbbcac08f7abb9087eaef9c9b2711 | |
parent | 551c6637250172c5a2ca05070c011b1d72bb7d02 (diff) | |
download | legit-eda8b58d9f2e85f1bae92ce3b70f279b89031046.tar.gz legit-eda8b58d9f2e85f1bae92ce3b70f279b89031046.tar.bz2 legit-eda8b58d9f2e85f1bae92ce3b70f279b89031046.zip |
git: find main branch from config
-rw-r--r-- | config.yaml | 5 | ||||
-rw-r--r-- | config/config.go | 11 | ||||
-rw-r--r-- | git/git.go | 10 | ||||
-rw-r--r-- | routes/routes.go | 28 |
4 files changed, 37 insertions, 17 deletions
diff --git a/config.yaml b/config.yaml index f684779..59e903a 100644 --- a/config.yaml +++ b/config.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: diff --git a/config/config.go b/config/config.go index 1a29e4f..278e17e 100644 --- a/config/config.go +++ b/config/config.go @@ -8,10 +8,11 @@ import ( ) 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 @@ type Config struct { } `yaml:"meta"` Server struct { Host string `yaml:"host"` - Port int `yaml:"port"` + Port int `yaml:"port"` } `yaml:"server"` } @@ -110,3 +110,13 @@ func (g *GitRepo) Branches() ([]*plumbing.Reference, error) { 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") +} diff --git a/routes/routes.go b/routes/routes.go index 1683404..76983ed 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -18,7 +18,7 @@ type deps struct { } 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 @@ func (d *deps) Index(w http.ResponseWriter, r *http.Request) { 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) Index(w http.ResponseWriter, r *http.Request) { 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 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) { } 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 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) { 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 @@ func (d *deps) RepoTree(w http.ResponseWriter, r *http.Request) { 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 @@ func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) { 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) Diff(w http.ResponseWriter, r *http.Request) { 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) |