-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathreceive_pack.go
46 lines (37 loc) · 983 Bytes
/
receive_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
46
package main
import (
"io"
"io/ioutil"
"log"
"net/http"
"os/exec"
)
func receivePackHandler(w http.ResponseWriter, r *http.Request) {
userName, repoName, _ := GetParamValues(r)
execPath := RepoPath(userName, repoName)
cmd := exec.Command(config.GitPath, "receive-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-receive-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
}
}