git: find main branch from config

This commit is contained in:
Anirudh Oppiliappan 2022-12-13 09:58:40 +05:30
parent 551c663725
commit eda8b58d9f
No known key found for this signature in database
GPG Key ID: 8A93F96F78C5D4C4
4 changed files with 37 additions and 17 deletions

View File

@ -1,10 +1,13 @@
git: repo:
scanPath: /home/icy/code/tmp/testrepos scanPath: /home/icy/code/tmp/testrepos
readme: readme:
- readme - readme
- README - README
- readme.md - readme.md
- README.md - README.md
mainBranch:
- master
- main
template: template:
dir: ./templates dir: ./templates
meta: meta:

View File

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

View File

@ -110,3 +110,13 @@ func (g *GitRepo) Branches() ([]*plumbing.Reference, error) {
return branches, nil 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")
}

View File

@ -18,7 +18,7 @@ type deps struct {
} }
func (d *deps) Index(w http.ResponseWriter, r *http.Request) { 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 { if err != nil {
d.Write500(w) d.Write500(w)
log.Printf("reading scan path: %s", err) 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) repoInfo := make(map[string]time.Time)
for _, dir := range dirs { 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, "") gr, err := git.Open(path, "")
if err != nil { if err != nil {
d.Write500(w) 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) { func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
name := flow.Param(r.Context(), "name") name := flow.Param(r.Context(), "name")
name = filepath.Clean(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, "") gr, err := git.Open(path, "")
if err != nil { if err != nil {
d.Write404(w) d.Write404(w)
@ -76,7 +76,7 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
} }
var readmeContent string var readmeContent string
for _, readme := range d.c.Git.Readme { for _, readme := range d.c.Repo.Readme {
readmeContent, _ = gr.FileContent(readme) readmeContent, _ = gr.FileContent(readme)
if readmeContent != "" { if readmeContent != "" {
break break
@ -87,10 +87,16 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
log.Printf("no readme found for %s", name) 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 := make(map[string]any)
data["name"] = name data["name"] = name
// TODO: make this configurable data["ref"] = mainBranch
data["ref"] = "master"
data["readme"] = readmeContent data["readme"] = readmeContent
d.listFiles(files, data, w) 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") ref := flow.Param(r.Context(), "ref")
name = filepath.Clean(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, ref) gr, err := git.Open(path, ref)
if err != nil { if err != nil {
d.Write404(w) d.Write404(w)
@ -132,7 +138,7 @@ func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) {
ref := flow.Param(r.Context(), "ref") ref := flow.Param(r.Context(), "ref")
name = filepath.Clean(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, ref) gr, err := git.Open(path, ref)
if err != nil { if err != nil {
d.Write404(w) d.Write404(w)
@ -152,7 +158,7 @@ func (d *deps) Log(w http.ResponseWriter, r *http.Request) {
name := flow.Param(r.Context(), "name") name := flow.Param(r.Context(), "name")
ref := flow.Param(r.Context(), "ref") 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) gr, err := git.Open(path, ref)
if err != nil { if err != nil {
d.Write404(w) d.Write404(w)
@ -185,7 +191,7 @@ func (d *deps) Diff(w http.ResponseWriter, r *http.Request) {
name := flow.Param(r.Context(), "name") name := flow.Param(r.Context(), "name")
ref := flow.Param(r.Context(), "ref") 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) gr, err := git.Open(path, ref)
if err != nil { if err != nil {
d.Write404(w) 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) { func (d *deps) Refs(w http.ResponseWriter, r *http.Request) {
name := flow.Param(r.Context(), "name") 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, "") gr, err := git.Open(path, "")
if err != nil { if err != nil {
d.Write404(w) d.Write404(w)