-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.go
More file actions
83 lines (77 loc) · 2.09 KB
/
main.go
File metadata and controls
83 lines (77 loc) · 2.09 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
package main
import (
"bufio"
"fmt"
"github.com/helm/helm/log"
"io"
"os"
)
// #!/bin/bash
// strip_remote_prefix() {
// stdbuf -i0 -o0 -e0 sed "s/^/"$'\e[1G'"/"
// }
//
// while read oldrev newrev refname
// do
// LOCKFILE="/tmp/$RECEIVE_REPO.lock"
// if ( set -o noclobber; echo "$$" > "$LOCKFILE" ) 2> /dev/null; then
// trap 'rm -f "$LOCKFILE"; exit 1' INT TERM EXIT
//
// # check for authorization on this repo
// {{.GitHome}}/receiver "$RECEIVE_REPO" "$newrev" "$RECEIVE_USER" "$RECEIVE_FINGERPRINT"
// rc=$?
// if [[ $rc != 0 ]] ; then
// echo " ERROR: failed on rev $newrev - push denied"
// exit $rc
// fi
// # builder assumes that we are running this script from $GITHOME
// cd {{.GitHome}}
// # if we're processing a receive-pack on an existing repo, run a build
// if [[ $SSH_ORIGINAL_COMMAND == git-receive-pack* ]]; then
// {{.GitHome}}/builder "$RECEIVE_USER" "$RECEIVE_REPO" "$newrev" 2>&1 | strip_remote_prefix
// fi
//
// rm -f "$LOCKFILE"
// trap - INT TERM EXIT
// else
// echo "Another git push is ongoing. Aborting..."
// exit 1
// fi
// done
const newline = "\n"
// readLine reads from bio until it reaches a "\n". returns the line, not including the "\n"
func getLine(bio *bufio.Reader) (string, error) {
line, err := bio.ReadString(newline)
if err != nil {
return "", err
}
if strings.HasSuffix(line, newline) {
return line[0 : len(line)-len(newline)]
}
}
func readLine(line string) (string, string, string, error) {
spl := strings.Split(line, " ")
if len(spl) != 3 {
return "", "", "", fmt.Errorf("malformed line [%s]", line)
}
return spl[0], spl[1], spl[2], nil
}
func main() {
conf, err := getConfig("gitreceive")
if err != nil {
log.Msg("config error [%s]", err)
os.Exit(1)
}
bio := bufio.NewReader(os.Stdin)
for line, err := getLine(bio); err != nil; {
oldRev, newRev, refName, err := readLine(line)
if err := receive(conf, newRev); err != nil {
log.Die("failed on rev %s - push denied", newRev)
os.Exit(1)
}
if err := build(conf, newRev); err != nil {
log.Die("error building %s [%s]", newRev, err)
os.Exit(1)
}
}
}