gitshow

Log | Files | Refs

log.go (1433B)


      1 package main
      2 import (
      3 	"github.com/gorilla/mux"
      4 	// "fmt"
      5 	"log"
      6 	"net/http"
      7 	"path/filepath"
      8 	"github.com/go-git/go-git/v5"
      9 	"github.com/go-git/go-git/v5/plumbing/object"
     10 	"github.com/go-git/go-git/v5/plumbing"
     11 	"html/template"
     12 )
     13 
     14 type LogPageData struct {
     15 	Name string
     16 	Commits []*object.Commit
     17 }
     18 
     19 func getLog(w http.ResponseWriter, r *http.Request) {
     20 	logger := log.Default()
     21 
     22 	vars := mux.Vars(r)
     23 
     24 	repo, err := checkRepo(vars["repository"])
     25 	if err != nil {
     26 		w.WriteHeader(http.StatusNotFound)
     27 		return
     28 	}
     29 
     30 	head, err := repo.Head()
     31 	if err != nil {
     32 		// TODO: handle no head
     33 		w.WriteHeader(http.StatusInternalServerError)
     34 		return
     35 	}
     36 
     37 	hash, err := repo.ResolveRevision(plumbing.Revision(head.Strings()[0]))
     38 	if err != nil {
     39 		logger.Fatal(err)
     40 	}
     41 
     42 	// commit, err := repo.LookupCommit(head.Target())
     43 	// if err != nil {
     44 	// 	logger.Fatal(err)
     45 	// }
     46 	//
     47 	var commits []*object.Commit = make([]*object.Commit, 0)
     48 	cIter, err := repo.Log(&git.LogOptions {
     49 		From: *hash,
     50 	})
     51 
     52 	cIter.ForEach(func(commit *object.Commit) error {
     53 		commits = append(commits, commit)
     54 		return nil
     55 	})
     56 
     57 	tmpl := template.Must(template.ParseFiles(
     58 		filepath.Join("templates", "log.html"),
     59 		filepath.Join("templates", "wrap.html"),
     60 	))
     61 
     62 	_, ishtmx := r.Header["Hx-Request"]
     63 
     64 	data := LogPageData {
     65 		Name: vars["repository"],
     66 		Commits: commits,
     67 	}
     68 
     69 	if ishtmx {
     70 		tmpl.ExecuteTemplate(w, "log_content", data)
     71 	} else {
     72 		tmpl.Execute(w, data)
     73 	}
     74 }