Home

ugit @9aac5f719e337be2c75a5a532a8106f5ca59c240 - refs - log -
-
https://git.jolheiser.com/ugit.git
The code powering this h*ckin' site
ugit / internal / http / repo.go
- raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package http

import (
	"bytes"
	"errors"
	"mime"
	"net/http"
	"path/filepath"
	"strings"

	"go.jolheiser.com/ugit/internal/html/markup"

	"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 {
		repo := r.Context().Value(repoCtxKey).(*git.Repo)

		var err error
		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)
		}

		readmeContent, err := markup.Readme(repo, ref, path)
		if err != nil {
			return httperr.Error(err)
		}

		var back string
		if path != "" {
			back = filepath.Dir(path)
		}
		if err := html.RepoTree(html.RepoTreeContext{
			Description:                repo.Meta.Description,
			BaseContext:                rh.baseContext(),
			RepoHeaderComponentContext: rh.repoHeaderContext(repo, r),
			RepoTreeComponentContext: html.RepoTreeComponentContext{
				Repo: repo.Name(),
				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 {
		if errors.Is(err, object.ErrFileNotFound) {
			return httperr.Status(err, http.StatusNotFound)
		}
		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
	if err := markup.Code.Convert([]byte(content), filepath.Base(path), &buf); err != nil {
		return httperr.Error(err)
	}

	if err := html.RepoFile(html.RepoFileContext{
		BaseContext:                rh.baseContext(),
		RepoHeaderComponentContext: rh.repoHeaderContext(repo, r),
		Code:                       buf.String(),
		Path:                       path,
	}).Render(r.Context(), w); err != nil {
		return httperr.Error(err)
	}

	return nil
}

func (rh repoHandler) repoRefs(w http.ResponseWriter, r *http.Request) error {
	repo := r.Context().Value(repoCtxKey).(*git.Repo)

	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
}

func (rh repoHandler) repoLog(w http.ResponseWriter, r *http.Request) error {
	repo := r.Context().Value(repoCtxKey).(*git.Repo)

	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
}

func (rh repoHandler) repoCommit(w http.ResponseWriter, r *http.Request) error {
	repo := r.Context().Value(repoCtxKey).(*git.Repo)

	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
		if err := markup.Code.Basic([]byte(p.Patch), "commit.patch", &patch); err != nil {
			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 {
	repo := r.Context().Value(repoCtxKey).(*git.Repo)

	commit, err := repo.Commit(chi.URLParam(r, "commit"))
	if err != nil {
		return httperr.Error(err)
	}

	w.Write([]byte(commit.Patch))

	return nil
}

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
}