This repository has been archived by the owner on Aug 14, 2020. It is now read-only.
/
main.go
488 lines (449 loc) · 11.7 KB
/
main.go
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// Copyright 2015 Marc-Antoine Ruel. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strings"
"sync"
"text/template"
"time"
"github.com/facebookgo/grace/gracehttp"
"github.com/maruel/circular"
"github.com/maruel/interrupt"
)
var footerTmpl = template.Must(template.New("name").Parse(`<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '{{.UA}}', 'auto');
ga('send', 'pageview');
</script>`))
// TODO(maruel): Split the model-view.
type repositories struct {
root string
delay time.Duration
footer string // HTML safe pre-generated footer.
wg sync.WaitGroup
readLock sync.Mutex
addLock sync.Mutex
repos map[string]*git
}
func (r *repositories) init() error {
// Find all the repositories, sync them all.
var wg sync.WaitGroup
err := filepath.Walk(r.root, func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, "/config") {
wg.Add(1)
go func() {
defer wg.Done()
path = path[:len(path)-7]
name := path[len(r.root)+1:]
g := &git{
root: path,
name: name,
delay: r.delay,
footer: r.footer,
revList: map[string][]string{},
}
r.repos[name] = g
// TODO(maruel): Do this asynchronously.
if err := g.updateBranches(false); err != nil {
log.Fatal(err)
}
r.wg.Add(1)
go func() {
defer r.wg.Done()
g.scanLoop()
}()
}()
return filepath.SkipDir
}
return nil
})
wg.Wait()
return err
}
func (r *repositories) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/" {
r.rootHandler(w, req)
} else {
r.flowHandler(w, req)
}
}
var repoRootTmpl = template.Must(template.New("name").Parse(`<html><body>
<h1>Repositories</h1><ul>
{{range .Items}}<li><a href="{{.}}.git/">{{.}}</a></li>
{{end}}</ul>
{{.Footer}}</body></html>`))
func (r *repositories) rootHandler(w http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
http.Error(w, "Invalid Method", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
r.readLock.Lock()
defer r.readLock.Unlock()
list := make([]string, 0, len(r.repos))
for k := range r.repos {
list = append(list, k)
}
sort.Strings(list)
t := &struct {
Items []string
Footer string
}{
list,
r.footer,
}
if err := repoRootTmpl.Execute(w, t); err != nil {
log.Fatal(err)
}
}
var (
repoReStr = "^[a-z0-9./\\-]+$"
repoRe = regexp.MustCompile(repoReStr)
)
func (r *repositories) flowHandler(w http.ResponseWriter, req *http.Request) {
// TODO(maruel): Rewrite this code.
// The first part is the git URL.
i := strings.Index(req.URL.Path, ".git")
if i < 5 || i == -1 || (len(req.URL.Path) > i+4 && req.URL.Path[i+4] != '/') {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
http.Error(w, "Use .git to specify the git repository", 400)
return
}
repo := req.URL.Path[1:i]
req.URL.Path = req.URL.Path[i+4:]
// Whitelist the characters allowed in repo URL section.
if !repoRe.MatchString(repo) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
http.Error(w, fmt.Sprintf("Repository URL %s must match \"%s\".", repo, repoReStr), 400)
return
}
r.readLock.Lock()
g, ok := r.repos[repo]
r.readLock.Unlock()
if !ok {
// init is the special case to create one.
if req.URL.Path != "/init" {
u := "http://" + req.Host + "/" + repo + ".git/init"
http.Error(w, fmt.Sprintf("Visit %s to initialize the repository.", u), 400)
} else {
// TODO(maruel): Enforce POST.
r.addLock.Lock()
defer r.addLock.Unlock()
g := &git{
root: filepath.Join(r.root, repo),
name: repo,
delay: r.delay,
footer: r.footer,
revList: map[string][]string{},
}
if err := g.initialClone(w); err != nil {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
http.Error(w, "Use .git to specify the git repository", 400)
return
}
r.repos[repo] = g
r.wg.Add(1)
go func() {
defer r.wg.Done()
g.scanLoop()
}()
}
} else {
if req.URL.Path == "/init" {
// Discard redondant init.
req.URL.Path = "/"
}
if req.URL.Path == "" {
req.URL.Path = "/"
}
g.ServeHTTP(w, req)
}
}
// TODO(maruel): Split the model-view.
type git struct {
root string
name string
delay time.Duration
footer string // HTML safe pre-generated footer.
lock sync.RWMutex
branches map[string]string
revList map[string][]string
}
func (g *git) initialClone(w http.ResponseWriter) error {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
start := time.Now()
repo := "https://" + g.name
log.Printf("%s: git cloning %s", g.root, repo)
cmd := exec.Command("git", "clone", "--mirror", "--progress", "--verbose", repo, g.root)
w.WriteHeader(http.StatusOK)
f := circular.AutoFlush(w, 500*time.Millisecond)
cmd.Stdout = f
cmd.Stderr = f
if err := cmd.Run(); err != nil {
log.Printf("%s: git clone failed: %s in %s", g.root, err, time.Since(start))
return fmt.Errorf("git clone %s failed", repo)
}
log.Printf("%s: git clone in %s", g.root, time.Since(start))
return g.updateBranches(true)
}
func (g *git) scanLoop() {
for {
select {
case <-interrupt.Channel:
return
case <-time.After(g.delay):
if err := g.fetch(); err != nil {
log.Fatal(err)
}
}
}
}
func (g *git) fetch() error {
log.Printf("%s: Fetching", g.root)
cmd := exec.Command("git", "fetch", "origin", "--prune", "--quiet")
cmd.Dir = g.root
start := time.Now()
if err := cmd.Run(); err != nil {
log.Printf("%s: fetch() failed: %s", g.root, err)
return fmt.Errorf("git fetch %s failed", g.name)
}
log.Printf("%s: fetch() in %s", g.root, time.Since(start))
return g.updateBranches(false)
}
func (g *git) updateBranches(locked bool) error {
newRefs, err := getRefs(g.root)
if err != nil {
log.Printf("%s: getRefs() failed: %s", g.root, err)
return fmt.Errorf("getting branches for %s failed", g.name)
}
// Rescan only if fetched something.
if !eqMap(newRefs, g.branches) {
log.Printf("%s: %d branches found", g.root, len(newRefs))
start := time.Now()
revList, err := scanParentHood(g.root, newRefs)
if err != nil {
log.Printf("%s: scanParentHood() failed: %s", g.root, err)
return fmt.Errorf("scanning rev-list for %s failed", g.name)
}
log.Printf("%s: scanParentHood() found %d commits in %s", g.root, len(revList), time.Since(start))
if !locked {
g.lock.Lock()
defer g.lock.Unlock()
}
g.branches = newRefs
g.revList = revList
}
return nil
}
func (g *git) isChild(parent, child string) bool {
for _, c := range g.revList[parent] {
if c == child {
return true
}
if g.isChild(c, child) {
return true
}
}
return false
}
func (g *git) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
http.Error(w, "Invalid Method", http.StatusMethodNotAllowed)
return
}
if r.URL.Path == "/" {
g.rootHandler(w, r)
return
}
if strings.HasPrefix(r.URL.Path, "/compare/") {
g.compareHandler(w, r)
return
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
http.Error(w, "Unknown URL", 404)
return
}
func (g *git) compareHandler(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path[9:], "/")
if len(parts) != 2 {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
http.Error(w, "Use /compare/ref1/ref2", 400)
return
}
p, _ := g.compare(parts[0], parts[1])
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if err := json.NewEncoder(w).Encode(&p); err != nil {
log.Fatal(err)
}
}
type parentHood struct {
ParentValid bool
ChildValid bool
Ancestor bool
}
func (g *git) compare(parent, child string) (*parentHood, error) {
p := &parentHood{}
parent = strings.ToLower(parent)
child = strings.ToLower(child)
if !isHashValid(parent) || !isHashValid(child) {
return p, errors.New("use valid commit digests")
}
g.lock.RLock()
defer g.lock.RUnlock()
_, p.ParentValid = g.revList[parent]
_, p.ParentValid = g.revList[child]
p.Ancestor = g.isChild(parent, child)
return p, nil
}
var gitRootTmpl = template.Must(template.New("name").Parse(`<html><body>
<h1>{{.Name}}</h1><ul>
{{range $index, $key := .Keys}}<li>{{$key}}: {{index $.Map $key}}</li>
{{end}}</ul>
<form method="GET" action="">
<table>
<tr>
<td>Parent:</td>
<td><input type="text" name="parent" maxlen=40 size=42 value="{{.Parent}}"></input></td>
</tr>
<tr>
<td>Child:</td>
<td><input type="text" name="child" maxlen=40 size=42 value="{{.Child}}"></input></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Check"></input></td>
</tr>
</table>
</form>
{{.Extra}}<p>
<em>ps: there's a JSON API</em>
{{.Footer}}</body></html>`))
func (g *git) rootHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
values := r.URL.Query()
parent := values.Get("parent")
child := values.Get("child")
p, err := g.compare(parent, child)
extra := ""
if err == nil {
extra = fmt.Sprintf("Ancestor: %t", p.Ancestor)
}
g.lock.RLock()
defer g.lock.RUnlock()
list := make([]string, 0, len(g.branches))
for k := range g.branches {
list = append(list, k)
}
sort.Strings(list)
t := &struct {
Name string
Keys []string
Map map[string]string
Parent string
Child string
Extra string
Footer string
}{
g.name,
list,
g.branches,
parent,
child,
extra,
g.footer,
}
if err := gitRootTmpl.Execute(w, t); err != nil {
log.Fatal(err)
}
}
// Preload the footer.
type config struct {
UA string
}
func loadConfig() *config {
// TODO(maruel): Very barebone.
f, err := os.Open("git-scan.json")
if err != nil {
return nil
}
defer f.Close()
c := &config{}
if err := json.NewDecoder(f).Decode(c); err != nil {
return nil
}
return c
}
func mainImpl() error {
port := flag.Int("port", 8010, "port number")
notLocal := flag.Bool("all", false, "listen on all IPs, not just localhost")
cwd, _ := os.Getwd()
root := flag.String("root", filepath.Join(cwd, "repos"), "checkout root dir")
flag.Parse()
log.SetFlags(log.Ldate | log.Lmicroseconds | log.LUTC)
if err := os.MkdirAll(*root, 0700); err != nil {
return err
}
footer := ""
if c := loadConfig(); c != nil {
b := &bytes.Buffer{}
if err := footerTmpl.Execute(b, c); err != nil {
return err
}
footer = b.String()
}
repos := &repositories{
root: *root,
delay: 60 * time.Second,
repos: map[string]*git{},
footer: footer,
}
// TODO(maruel): This is a race condition with the parent process.
if err := repos.init(); err != nil {
return err
}
var addr string
if *notLocal {
addr = fmt.Sprintf(":%d", *port)
} else {
addr = fmt.Sprintf("localhost:%d", *port)
}
mux := http.NewServeMux()
mux.Handle("/favicon.ico", restrictFunc(func(w http.ResponseWriter, r *http.Request) {
// TODO(maruel): Eh.
http.Redirect(w, r, "https://git-scm.com/images/logos/downloads/Git-Icon-1788C.png", http.StatusFound)
}, "GET"))
mux.Handle("/", repos)
s := &http.Server{
Addr: addr,
Handler: exitOnPanic{&loggingHandler{mux, nil}},
}
// TODO(maruel): Handle Ctrl-C to quick shutdown but wait for git operations.
err := gracehttp.Serve(s)
interrupt.Set()
repos.wg.Wait()
return err
}
func main() {
if err := mainImpl(); err != nil {
fmt.Fprintf(os.Stderr, "Failed: %s\n", err)
os.Exit(1)
}
}