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
readme:
- readme
- README
- readme.md
- README.md
mainBranch:
- master
- main
template:
dir: ./templates
meta:

View file

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

View file

@ -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")
}

View file

@ -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)