-
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
package routes
import (
"compress/gzip"
"fmt"
"log"
"net/http"
"path/filepath"
"strings"
)
func (d *deps) serveRepoTree(w http.ResponseWriter, r *http.Request) {
ref := r.PathValue("ref")
gr, name, err := d.openRepository(r.PathValue("name"), ref)
if err != nil {
d.write404(w)
return
}
treePath := r.PathValue("rest")
files, err := gr.FileTree(treePath)
if err != nil {
d.write500(w)
log.Println(err)
return
}
relpath := []string{}
if len(treePath) > 0 {
relpath = strings.Split(treePath, "/")
}
data := repoTreeRefData{
Config: d.c,
Meta: repositoryMeta{
DisplayName: getDisplayName(name),
DirName: name,
Description: gr.GitwebDescription(),
Ref: ref,
},
Path: relpath,
Files: files,
}
if err := d.template().ExecuteTemplate(w, "repo-tree-ref", data); err != nil {
log.Println(err)
return
}
}
func (d *deps) serveArchive(w http.ResponseWriter, r *http.Request) {
name := r.PathValue("name")
file := r.PathValue("file")
// TODO: extend this to add more files compression (e.g.: xz)
if !strings.HasSuffix(file, ".tar.gz") {
d.write404(w)
return
}
ref := strings.TrimSuffix(file, ".tar.gz")
// This allows the browser to use a proper name for the file when
// downloading
filename := fmt.Sprintf("%s-%s.tar.gz", name, ref)
setContentDisposition(w, filename)
setGZipMIME(w)
gr, name, err := d.openRepository(name, ref)
if err != nil {
d.write404(w)
return
}
gw := gzip.NewWriter(w)
defer gw.Close()
prefix := fmt.Sprintf("%s-%s", name, ref)
err = gr.WriteTar(gw, prefix)
if err != nil {
// once we start writing to the body we can't report error anymore
// so we are only left with printing the error.
log.Println(err)
return
}
err = gw.Flush()
if err != nil {
// once we start writing to the body we can't report error anymore
// so we are only left with printing the error.
log.Println(err)
return
}
}
func (d *deps) serveDiff(w http.ResponseWriter, r *http.Request) {
ref := r.PathValue("ref")
gr, name, err := d.openRepository(r.PathValue("name"), ref)
if err != nil {
d.write404(w)
return
}
diff, err := gr.Diff()
if err != nil {
d.write500(w)
log.Println(err)
return
}
data := repoCommitData{
Config: d.c,
Meta: repositoryMeta{
DisplayName: getDisplayName(name),
DirName: name,
Description: gr.GitwebDescription(),
Ref: diff.Commit.Hash.String(),
},
Commit: diff.Commit,
Parent: diff.Parent,
Diff: diff,
}
if err := d.template().ExecuteTemplate(w, "repo-commit", data); err != nil {
log.Println(err)
return
}
}
func (d *deps) serveRefs(w http.ResponseWriter, r *http.Request) {
gr, name, err := d.openRepository(r.PathValue("name"), "")
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
}
mainBranch, err := gr.FindMainBranch(d.c.Repo.MainBranch)
if err != nil {
d.write500(w)
log.Println(err)
return
}
data := repoRefsData{
Config: d.c,
Meta: repositoryMeta{
DisplayName: getDisplayName(name),
DirName: name,
Description: gr.GitwebDescription(),
Ref: mainBranch,
},
Tags: tags,
Branches: branches,
}
if err := d.template().ExecuteTemplate(w, "repo-refs", data); err != nil {
log.Println(err)
return
}
}
func (d *deps) serveStatic(w http.ResponseWriter, r *http.Request) {
name := r.PathValue("file")
name = filepath.Clean(name)
if !filepath.IsLocal(name) {
d.write404(w)
return
}
http.ServeFileFS(w, r, d.staticDir, name)
}