2024-01-15 22:26:51 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
2024-03-01 17:58:05 +00:00
|
|
|
"strings"
|
2024-01-15 22:26:51 +00:00
|
|
|
|
2024-02-22 19:14:05 +00:00
|
|
|
"go.jolheiser.com/ugit/internal/html/markup"
|
|
|
|
|
2024-01-15 22:26:51 +00:00
|
|
|
"go.jolheiser.com/ugit/internal/git"
|
|
|
|
"go.jolheiser.com/ugit/internal/html"
|
|
|
|
"go.jolheiser.com/ugit/internal/http/httperr"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (rh repoHandler) repoTree(ref, path string) http.HandlerFunc {
|
|
|
|
return httperr.Handler(func(w http.ResponseWriter, r *http.Request) error {
|
2024-01-19 05:00:13 +00:00
|
|
|
repo := r.Context().Value(repoCtxKey).(*git.Repo)
|
2024-01-15 22:26:51 +00:00
|
|
|
|
2024-01-19 05:00:13 +00:00
|
|
|
var err error
|
2024-01-15 22:26:51 +00:00
|
|
|
if ref == "" {
|
|
|
|
ref, err = repo.DefaultBranch()
|
|
|
|
if err != nil {
|
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tree, err := repo.Dir(ref, path)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, object.ErrDirectoryNotFound) {
|
|
|
|
return rh.repoFile(w, r, repo, ref, path)
|
|
|
|
}
|
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
|
2024-01-19 04:41:16 +00:00
|
|
|
readmeContent, err := markup.Readme(repo, ref, path)
|
2024-01-15 22:26:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var back string
|
|
|
|
if path != "" {
|
|
|
|
back = filepath.Dir(path)
|
|
|
|
}
|
|
|
|
if err := html.RepoTree(html.RepoTreeContext{
|
2024-06-27 04:23:41 +00:00
|
|
|
Description: repo.Meta.Description,
|
|
|
|
BaseContext: rh.baseContext(),
|
|
|
|
RepoHeaderComponentContext: rh.repoHeaderContext(repo, r),
|
|
|
|
RepoBreadcrumbComponentContext: rh.repoBreadcrumbContext(repo, r, path),
|
2024-01-15 22:26:51 +00:00
|
|
|
RepoTreeComponentContext: html.RepoTreeComponentContext{
|
2024-01-19 05:00:13 +00:00
|
|
|
Repo: repo.Name(),
|
2024-01-15 22:26:51 +00:00
|
|
|
Ref: ref,
|
|
|
|
Tree: tree,
|
|
|
|
Back: back,
|
|
|
|
},
|
|
|
|
ReadmeComponentContext: html.ReadmeComponentContext{
|
|
|
|
Markdown: readmeContent,
|
|
|
|
},
|
|
|
|
}).Render(r.Context(), w); err != nil {
|
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rh repoHandler) repoFile(w http.ResponseWriter, r *http.Request, repo *git.Repo, ref, path string) error {
|
|
|
|
content, err := repo.FileContent(ref, path)
|
|
|
|
if err != nil {
|
2024-05-25 02:31:35 +00:00
|
|
|
if errors.Is(err, object.ErrFileNotFound) {
|
|
|
|
return httperr.Status(err, http.StatusNotFound)
|
|
|
|
}
|
2024-01-15 22:26:51 +00:00
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.URL.Query().Has("raw") {
|
|
|
|
if r.URL.Query().Has("pretty") {
|
|
|
|
ext := filepath.Ext(path)
|
|
|
|
w.Header().Set("Content-Type", mime.TypeByExtension(ext))
|
|
|
|
}
|
|
|
|
w.Write([]byte(content))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
2024-01-19 04:41:16 +00:00
|
|
|
if err := markup.Code.Convert([]byte(content), filepath.Base(path), &buf); err != nil {
|
2024-01-15 22:26:51 +00:00
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := html.RepoFile(html.RepoFileContext{
|
2024-06-27 04:23:41 +00:00
|
|
|
BaseContext: rh.baseContext(),
|
|
|
|
RepoHeaderComponentContext: rh.repoHeaderContext(repo, r),
|
|
|
|
RepoBreadcrumbComponentContext: rh.repoBreadcrumbContext(repo, r, path),
|
|
|
|
Code: buf.String(),
|
2024-01-15 22:26:51 +00:00
|
|
|
}).Render(r.Context(), w); err != nil {
|
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-16 03:11:52 +00:00
|
|
|
|
|
|
|
func (rh repoHandler) repoRefs(w http.ResponseWriter, r *http.Request) error {
|
2024-01-19 05:00:13 +00:00
|
|
|
repo := r.Context().Value(repoCtxKey).(*git.Repo)
|
2024-01-16 03:11:52 +00:00
|
|
|
|
|
|
|
branches, err := repo.Branches()
|
|
|
|
if err != nil {
|
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tags, err := repo.Tags()
|
|
|
|
if err != nil {
|
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := html.RepoRefs(html.RepoRefsContext{
|
|
|
|
BaseContext: rh.baseContext(),
|
|
|
|
RepoHeaderComponentContext: rh.repoHeaderContext(repo, r),
|
|
|
|
Branches: branches,
|
|
|
|
Tags: tags,
|
|
|
|
}).Render(r.Context(), w); err != nil {
|
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-16 04:54:43 +00:00
|
|
|
|
|
|
|
func (rh repoHandler) repoLog(w http.ResponseWriter, r *http.Request) error {
|
2024-01-19 05:00:13 +00:00
|
|
|
repo := r.Context().Value(repoCtxKey).(*git.Repo)
|
2024-01-16 04:54:43 +00:00
|
|
|
|
|
|
|
commits, err := repo.Commits(chi.URLParam(r, "ref"))
|
|
|
|
if err != nil {
|
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := html.RepoLog(html.RepoLogContext{
|
|
|
|
BaseContext: rh.baseContext(),
|
|
|
|
RepoHeaderComponentContext: rh.repoHeaderContext(repo, r),
|
|
|
|
Commits: commits,
|
|
|
|
}).Render(r.Context(), w); err != nil {
|
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-17 03:37:25 +00:00
|
|
|
|
|
|
|
func (rh repoHandler) repoCommit(w http.ResponseWriter, r *http.Request) error {
|
2024-01-19 05:00:13 +00:00
|
|
|
repo := r.Context().Value(repoCtxKey).(*git.Repo)
|
2024-01-17 03:37:25 +00:00
|
|
|
|
|
|
|
commit, err := repo.Commit(chi.URLParam(r, "commit"))
|
|
|
|
if err != nil {
|
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, p := range commit.Files {
|
|
|
|
var patch bytes.Buffer
|
2024-01-19 04:41:16 +00:00
|
|
|
if err := markup.Code.Basic([]byte(p.Patch), "commit.patch", &patch); err != nil {
|
2024-01-17 03:37:25 +00:00
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
commit.Files[idx].Patch = patch.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := html.RepoCommit(html.RepoCommitContext{
|
|
|
|
BaseContext: rh.baseContext(),
|
|
|
|
RepoHeaderComponentContext: rh.repoHeaderContext(repo, r),
|
|
|
|
Commit: commit,
|
|
|
|
}).Render(r.Context(), w); err != nil {
|
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rh repoHandler) repoPatch(w http.ResponseWriter, r *http.Request) error {
|
2024-01-19 05:00:13 +00:00
|
|
|
repo := r.Context().Value(repoCtxKey).(*git.Repo)
|
2024-01-17 03:37:25 +00:00
|
|
|
|
|
|
|
commit, err := repo.Commit(chi.URLParam(r, "commit"))
|
|
|
|
if err != nil {
|
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write([]byte(commit.Patch))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-03-01 17:58:05 +00:00
|
|
|
|
|
|
|
func (rh repoHandler) repoSearch(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
repo := r.Context().Value(repoCtxKey).(*git.Repo)
|
|
|
|
|
|
|
|
var results []git.GrepResult
|
|
|
|
search := r.URL.Query().Get("q")
|
|
|
|
if q := strings.TrimSpace(search); q != "" {
|
|
|
|
var err error
|
|
|
|
results, err = repo.Grep(q)
|
|
|
|
if err != nil {
|
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
for idx, result := range results {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := markup.Snippet([]byte(result.Content), filepath.Base(result.File), result.StartLine, &buf); err != nil {
|
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
results[idx].Content = buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := html.RepoSearch(html.SearchContext{
|
|
|
|
BaseContext: rh.baseContext(),
|
|
|
|
RepoHeaderComponentContext: rh.repoHeaderContext(repo, r),
|
|
|
|
Results: results,
|
|
|
|
}).Render(r.Context(), w); err != nil {
|
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|