forked from dustin/gitmirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitmirror.go
More file actions
280 lines (232 loc) · 5.59 KB
/
gitmirror.go
File metadata and controls
280 lines (232 loc) · 5.59 KB
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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"time"
)
var thePath = flag.String("dir", "/tmp", "working directory")
var git = flag.String("git", "/usr/bin/git", "path to git")
var port = flag.Int("port", 8124, "port to listen on")
type commandRequest struct {
w http.ResponseWriter
abspath string
bg bool
after time.Time
cmds []*exec.Cmd
ch chan bool
}
var reqch = make(chan commandRequest, 100)
var updates = map[string]time.Time{}
func exists(path string) (rv bool) {
rv = true
if _, err := os.Stat(path); os.IsNotExist(err) {
rv = false
}
return
}
func maybePanic(err error) {
if err != nil {
panic(err)
}
}
func runCommands(w http.ResponseWriter, bg bool,
abspath string, cmds []*exec.Cmd) {
stderr := io.Writer(ioutil.Discard)
stdout := io.Writer(ioutil.Discard)
if !bg {
stderr = &bytes.Buffer{}
stdout = &bytes.Buffer{}
}
for _, cmd := range cmds {
if exists(cmd.Path) {
log.Printf("Running %v in %v", cmd.Args, abspath)
fmt.Fprintf(stdout, "# Running %v\n", cmd.Args)
fmt.Fprintf(stderr, "# Running %v\n", cmd.Args)
cmd.Stdout = stdout
cmd.Stderr = stderr
cmd.Dir = abspath
err := cmd.Run()
if err != nil {
log.Printf("Error running %v in %v: %v",
cmd.Args, abspath, err)
if !bg {
fmt.Fprintf(stderr,
"\n[gitmirror internal error: %v]\n", err)
}
}
}
}
if !bg {
fmt.Fprintf(w, "---- stdout ----\n")
_, err := stdout.(*bytes.Buffer).WriteTo(w)
maybePanic(err)
fmt.Fprintf(w, "\n----\n\n\n---- stderr ----\n")
_, err = stderr.(*bytes.Buffer).WriteTo(w)
maybePanic(err)
fmt.Fprintf(w, "\n----\n")
}
}
func shouldRun(path string, after time.Time) bool {
if path == "/tmp" {
return true
}
lastRun := updates[path]
return lastRun.Before(after)
}
func didRun(path string, t time.Time) {
updates[path] = t
}
func pathRunner(ch chan commandRequest) {
for r := range ch {
if shouldRun(r.abspath, r.after) {
t := time.Now()
runCommands(r.w, r.bg, r.abspath, r.cmds)
didRun(r.abspath, t)
} else {
log.Printf("Skipping redundant update: %v", r.abspath)
if !r.bg {
fmt.Fprintf(r.w, "Redundant request.")
}
}
select {
case r.ch <- true:
default:
}
}
}
func commandRunner() {
m := map[string]chan commandRequest{}
for r := range reqch {
ch, running := m[r.abspath]
if !running {
ch = make(chan commandRequest, 10)
m[r.abspath] = ch
go pathRunner(ch)
}
ch <- r
}
}
func queueCommand(w http.ResponseWriter, bg bool,
abspath string, cmds []*exec.Cmd) chan bool {
req := commandRequest{w, abspath, bg, time.Now(),
cmds, make(chan bool)}
reqch <- req
return req.ch
}
func updateGit(w http.ResponseWriter, section string,
bg bool, payload []byte) bool {
time.Sleep(5 * time.Second)
abspath := filepath.Join(*thePath, section)
if !exists(abspath) {
if !bg {
http.Error(w, "Not found", http.StatusNotFound)
}
return false
}
cmds := []*exec.Cmd{
exec.Command(*git, "remote", "update", "-p"),
exec.Command(*git, "gc", "--auto"),
exec.Command(filepath.Join(abspath, "hooks/post-fetch")),
exec.Command(filepath.Join(*thePath, "bin/post-fetch")),
}
cmds[2].Stdin = bytes.NewBuffer(payload)
cmds[3].Stdin = bytes.NewBuffer(payload)
return <-queueCommand(w, bg, abspath, cmds)
}
func getPath(req *http.Request) string {
return filepath.Clean(filepath.FromSlash(req.URL.Path))[1:]
}
func createRepo(w http.ResponseWriter, section string,
bg bool, payload []byte) {
p := struct {
Repository struct {
Owner interface{}
Private bool
Name string
}
}{}
err := json.Unmarshal(payload, &p)
if err != nil {
log.Printf("Error unmarshalling data: %v", err)
http.Error(w, "Error parsing JSON", http.StatusInternalServerError)
return
}
var ownerName string
switch i := p.Repository.Owner.(type) {
case string:
ownerName = i
case map[string]interface{}:
ownerName = fmt.Sprintf("%v", i["name"])
}
repo := fmt.Sprintf("git://github.com/%v/%v.git",
ownerName, p.Repository.Name)
if p.Repository.Private {
repo = fmt.Sprintf("git@github.com:%v/%v.git",
ownerName, p.Repository.Name)
}
cmds := []*exec.Cmd{
exec.Command(*git, "clone", "--mirror", "--bare", repo,
filepath.Join(*thePath, section)),
}
if bg {
w.WriteHeader(201)
}
queueCommand(w, true, "/tmp", cmds)
}
func doUpdate(w http.ResponseWriter, path string,
bg bool, payload []byte) {
if bg {
go updateGit(w, path, bg, payload)
w.WriteHeader(201)
} else {
updateGit(w, path, bg, payload)
}
}
func handleGet(w http.ResponseWriter, req *http.Request, bg bool) {
path := getPath(req)
doUpdate(w, path, bg, nil)
}
func handlePost(w http.ResponseWriter, req *http.Request, bg bool) {
b := []byte(req.FormValue("payload"))
path := getPath(req)
abspath := filepath.Join(*thePath, path)
if exists(abspath) {
doUpdate(w, path, bg, b)
} else {
createRepo(w, path, bg, b)
}
}
func handleReq(w http.ResponseWriter, req *http.Request) {
backgrounded := req.FormValue("bg") != "false"
log.Printf("Handling %v %v", req.Method, req.URL.Path)
switch req.Method {
case "GET":
handleGet(w, req, backgrounded)
case "POST":
handlePost(w, req, backgrounded)
default:
http.Error(w, "Method not allowed",
http.StatusMethodNotAllowed)
}
}
func main() {
flag.Parse()
log.SetFlags(log.Lmicroseconds)
go commandRunner()
http.HandleFunc("/", handleReq)
http.HandleFunc("/favicon.ico",
func(w http.ResponseWriter, req *http.Request) {
http.Error(w, "No favicon", http.StatusGone)
})
addr := fmt.Sprintf(":%d", *port)
log.Fatal(http.ListenAndServe(addr, nil))
}