routes: refs view
This commit is contained in:
		
							parent
							
								
									01f27147ba
								
							
						
					
					
						commit
						7a6ff3565e
					
				
							
								
								
									
										32
									
								
								git/git.go
								
								
								
								
							
							
						
						
									
										32
									
								
								git/git.go
								
								
								
								
							| 
						 | 
				
			
			@ -78,3 +78,35 @@ func (g *GitRepo) FileContent(path string) (string, error) {
 | 
			
		|||
 | 
			
		||||
	return file.Contents()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (g *GitRepo) Tags() ([]*object.Tag, error) {
 | 
			
		||||
	ti, err := g.r.TagObjects()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("tag objects: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	tags := []*object.Tag{}
 | 
			
		||||
 | 
			
		||||
	_ = ti.ForEach(func(t *object.Tag) error {
 | 
			
		||||
		tags = append(tags, t)
 | 
			
		||||
		return nil
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	return tags, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (g *GitRepo) Branches() ([]*plumbing.Reference, error) {
 | 
			
		||||
	bi, err := g.r.Branches()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("branchs: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	branches := []*plumbing.Reference{}
 | 
			
		||||
 | 
			
		||||
	_ = bi.ForEach(func(ref *plumbing.Reference) error {
 | 
			
		||||
		branches = append(branches, ref)
 | 
			
		||||
		return nil
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	return branches, nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -21,5 +21,6 @@ func Handlers(c *config.Config) *flow.Mux {
 | 
			
		|||
	mux.HandleFunc("/:name/blob/:ref/...", d.FileContent, "GET")
 | 
			
		||||
	mux.HandleFunc("/:name/log/:ref", d.Log, "GET")
 | 
			
		||||
	mux.HandleFunc("/:name/commit/:ref", d.Diff, "GET")
 | 
			
		||||
	mux.HandleFunc("/:name/refs", d.Refs, "GET")
 | 
			
		||||
	return mux
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -216,3 +216,42 @@ func (d *deps) Diff(w http.ResponseWriter, r *http.Request) {
 | 
			
		|||
		return
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (d *deps) Refs(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	name := flow.Param(r.Context(), "name")
 | 
			
		||||
 | 
			
		||||
	path := filepath.Join(d.c.Git.ScanPath, name)
 | 
			
		||||
	gr, err := git.Open(path, "")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		d.Write404(w)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	tags, err := gr.Tags()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		// Non-fatal, we *should* have at least one branch to show.
 | 
			
		||||
		log.Println(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	branches, err := gr.Branches()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Println(err)
 | 
			
		||||
		d.Write500(w)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	tpath := filepath.Join(d.c.Template.Dir, "*")
 | 
			
		||||
	t := template.Must(template.ParseGlob(tpath))
 | 
			
		||||
 | 
			
		||||
	data := make(map[string]interface{})
 | 
			
		||||
 | 
			
		||||
	data["meta"] = d.c.Meta
 | 
			
		||||
	data["name"] = name
 | 
			
		||||
	data["branches"] = branches
 | 
			
		||||
	data["tags"] = tags
 | 
			
		||||
 | 
			
		||||
	if err := t.ExecuteTemplate(w, "refs", data); err != nil {
 | 
			
		||||
		log.Println(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,32 @@
 | 
			
		|||
{{ define "refs" }}
 | 
			
		||||
<html>
 | 
			
		||||
{{ template "head" . }}
 | 
			
		||||
 | 
			
		||||
  <header>
 | 
			
		||||
    <h1>{{ .meta.Title }}</h1>
 | 
			
		||||
    <h2>{{ .meta.Description }}</h2>
 | 
			
		||||
  </header>
 | 
			
		||||
  <body>
 | 
			
		||||
    {{ template "nav" . }}
 | 
			
		||||
    <main>
 | 
			
		||||
      <h3>branches</h3>
 | 
			
		||||
      {{ $name := .name }}
 | 
			
		||||
      {{ range .branches }}
 | 
			
		||||
      <p>
 | 
			
		||||
      <strong>{{ .Name.Short }}</strong>
 | 
			
		||||
      <a href="/{{ $name }}/tree/{{ .Name.Short }}/">browse</a>
 | 
			
		||||
      <a href="/{{ $name }}/log/{{ .Name.Short }}">log</a>
 | 
			
		||||
      </p>
 | 
			
		||||
      {{ end }}
 | 
			
		||||
      {{ if .tags }}
 | 
			
		||||
      {{ range .tags }}
 | 
			
		||||
      <p>{{ .Name }}</p>
 | 
			
		||||
      {{ if .Message }}
 | 
			
		||||
      <p>{{ .Message }}</p>
 | 
			
		||||
      {{ end }}
 | 
			
		||||
      {{ end }}
 | 
			
		||||
      {{ end }}
 | 
			
		||||
    </main>
 | 
			
		||||
  </body>
 | 
			
		||||
</html>
 | 
			
		||||
{{ end }}
 | 
			
		||||
		Loading…
	
		Reference in New Issue