forked from sosedoff/gitkit
/
hook_info.go
65 lines (54 loc) · 1.08 KB
/
hook_info.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
package gitkit
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
type HookInfo struct {
RepoName string
RepoPath string
OldRev string
NewRev string
Ref string
RefType string
RefName string
}
func ReadHookInput(input io.Reader) (*HookInfo, error) {
reader := bufio.NewReader(input)
line, _, err := reader.ReadLine()
if err != nil {
return nil, err
}
chunks := strings.Split(string(line), " ")
if len(chunks) != 3 {
return nil, fmt.Errorf("Invalid hook input")
}
refchunks := strings.Split(chunks[2], "/")
dir, _ := os.Getwd()
info := HookInfo{
RepoName: filepath.Base(dir),
RepoPath: dir,
OldRev: chunks[0],
NewRev: chunks[1],
Ref: chunks[2],
RefType: refchunks[1],
RefName: refchunks[2],
}
return &info, nil
}
func (h *HookInfo) Action() string {
action := "push"
context := "branch"
if h.RefType == "tags" {
context = "tag"
}
if h.OldRev == ZeroSHA && h.NewRev != ZeroSHA {
action = "create"
} else if h.OldRev != ZeroSHA && h.NewRev == ZeroSHA {
action = "delete"
}
return context + "." + action
}