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
|
diff --git a/internal/http/repo.go b/internal/http/repo.go
index bc0196e159d32ab5b4167c8a599711e997d2a691..2c4aa9358a63521f591ceb32318e27ff4b6ac94a 100644
--- a/internal/http/repo.go
+++ b/internal/http/repo.go
@@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"go.jolheiser.com/ugit/internal/html/markup"
- "io/fs"
"mime"
"net/http"
"path/filepath"
@@ -19,19 +18,9 @@ )
func (rh repoHandler) repoTree(ref, path string) http.HandlerFunc {
return httperr.Handler(func(w http.ResponseWriter, r *http.Request) error {
- repoName := chi.URLParam(r, "repo")
- repo, err := git.NewRepo(rh.s.RepoDir, repoName)
- if err != nil {
- httpErr := http.StatusInternalServerError
- if errors.Is(err, fs.ErrNotExist) {
- httpErr = http.StatusNotFound
- }
- return httperr.Status(err, httpErr)
- }
- if repo.Meta.Private {
- return httperr.Status(errors.New("could not get git repo"), http.StatusNotFound)
- }
+ repo := r.Context().Value(repoCtxKey).(*git.Repo)
+ var err error
if ref == "" {
ref, err = repo.DefaultBranch()
if err != nil {
@@ -61,7 +50,7 @@ Description: repo.Meta.Description,
BaseContext: rh.baseContext(),
RepoHeaderComponentContext: rh.repoHeaderContext(repo, r),
RepoTreeComponentContext: html.RepoTreeComponentContext{
- Repo: repoName,
+ Repo: repo.Name(),
Ref: ref,
Tree: tree,
Back: back,
@@ -110,18 +99,7 @@ return nil
}
func (rh repoHandler) repoRefs(w http.ResponseWriter, r *http.Request) error {
- repoName := chi.URLParam(r, "repo")
- repo, err := git.NewRepo(rh.s.RepoDir, repoName)
- if err != nil {
- httpErr := http.StatusInternalServerError
- if errors.Is(err, fs.ErrNotExist) {
- httpErr = http.StatusNotFound
- }
- return httperr.Status(err, httpErr)
- }
- if repo.Meta.Private {
- return httperr.Status(errors.New("could not get git repo"), http.StatusNotFound)
- }
+ repo := r.Context().Value(repoCtxKey).(*git.Repo)
branches, err := repo.Branches()
if err != nil {
@@ -146,18 +124,7 @@ return nil
}
func (rh repoHandler) repoLog(w http.ResponseWriter, r *http.Request) error {
- repoName := chi.URLParam(r, "repo")
- repo, err := git.NewRepo(rh.s.RepoDir, repoName)
- if err != nil {
- httpErr := http.StatusInternalServerError
- if errors.Is(err, fs.ErrNotExist) {
- httpErr = http.StatusNotFound
- }
- return httperr.Status(err, httpErr)
- }
- if repo.Meta.Private {
- return httperr.Status(errors.New("could not get git repo"), http.StatusNotFound)
- }
+ repo := r.Context().Value(repoCtxKey).(*git.Repo)
commits, err := repo.Commits(chi.URLParam(r, "ref"))
if err != nil {
@@ -176,18 +143,7 @@ return nil
}
func (rh repoHandler) repoCommit(w http.ResponseWriter, r *http.Request) error {
- repoName := chi.URLParam(r, "repo")
- repo, err := git.NewRepo(rh.s.RepoDir, repoName)
- if err != nil {
- httpErr := http.StatusInternalServerError
- if errors.Is(err, fs.ErrNotExist) {
- httpErr = http.StatusNotFound
- }
- return httperr.Status(err, httpErr)
- }
- if repo.Meta.Private {
- return httperr.Status(errors.New("could not get git repo"), http.StatusNotFound)
- }
+ repo := r.Context().Value(repoCtxKey).(*git.Repo)
commit, err := repo.Commit(chi.URLParam(r, "commit"))
if err != nil {
@@ -214,18 +170,7 @@ return nil
}
func (rh repoHandler) repoPatch(w http.ResponseWriter, r *http.Request) error {
- repoName := chi.URLParam(r, "repo")
- repo, err := git.NewRepo(rh.s.RepoDir, repoName)
- if err != nil {
- httpErr := http.StatusInternalServerError
- if errors.Is(err, fs.ErrNotExist) {
- httpErr = http.StatusNotFound
- }
- return httperr.Status(err, httpErr)
- }
- if repo.Meta.Private {
- return httperr.Status(errors.New("could not get git repo"), http.StatusNotFound)
- }
+ repo := r.Context().Value(repoCtxKey).(*git.Repo)
commit, err := repo.Commit(chi.URLParam(r, "commit"))
if err != nil {
|