forked from sosedoff/gitkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
82 lines (65 loc) · 1.51 KB
/
utils.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
package gitkit
import (
"fmt"
"io"
"log"
"net/http"
"os/exec"
"regexp"
"strings"
"syscall"
)
var reSlashDedup = regexp.MustCompile(`\/{2,}`)
func fail500(w http.ResponseWriter, context string, err error) {
http.Error(w, "Internal server error", 500)
logError(context, err)
}
func logError(context string, err error) {
log.Printf("%s: %v\n", context, err)
}
func logInfo(context string, message string) {
log.Printf("%s: %s\n", context, message)
}
func cleanUpProcessGroup(cmd *exec.Cmd) {
if cmd == nil {
return
}
process := cmd.Process
if process != nil && process.Pid > 0 {
syscall.Kill(-process.Pid, syscall.SIGTERM)
}
go cmd.Wait()
}
func packLine(w io.Writer, s string) error {
_, err := fmt.Fprintf(w, "%04x%s", len(s)+4, s)
return err
}
func packFlush(w io.Writer) error {
_, err := fmt.Fprint(w, "0000")
return err
}
func subCommand(rpc string) string {
return strings.TrimPrefix(rpc, "git-")
}
// Parse out namespace and repository name from the path.
// Examples:
// repo -> "", "repo"
// org/repo -> "org", "repo"
// org/suborg/rpeo -> "org/suborg", "repo"
func getNamespaceAndRepo(input string) (string, string) {
if input == "" || input == "/" {
return "", ""
}
// Remove duplicate slashes
input = reSlashDedup.ReplaceAllString(input, "/")
// Remove leading slash
if input[0] == '/' && input != "/" {
input = input[1:]
}
blocks := strings.Split(input, "/")
num := len(blocks)
if num < 2 {
return "", blocks[0]
}
return strings.Join(blocks[0:num-1], "/"), blocks[num-1]
}