-
Notifications
You must be signed in to change notification settings - Fork 10
/
upload_pack.go
45 lines (37 loc) · 979 Bytes
/
upload_pack.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
package main
import (
"io"
"io/ioutil"
"log"
"net/http"
"os/exec"
)
func uploadPackHandler(w http.ResponseWriter, r *http.Request) {
userName, repoName, _ := GetParamValues(r)
execPath := RepoPath(userName, repoName)
cmd := exec.Command(config.GitPath, "upload-pack", "--stateless-rpc", execPath)
stdin, stdout, stderr, ok := GetChildPipes(cmd, w)
if !ok {
return
}
if err := cmd.Start(); err != nil {
log.Println(err)
http.Error(w, "Error while spawning", http.StatusInternalServerError)
return
}
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println("Error while reading request body:", err)
http.Error(w, "Error while reading request body", http.StatusInternalServerError)
return
}
stdin.Write(reqBody)
contentType := "application/x-git-upload-pack-result"
SetHeader(w, contentType)
go io.Copy(w, stdout)
go io.Copy(w, stderr)
if err := cmd.Wait(); err != nil {
log.Println("Error while waiting:", err)
return
}
}