-
Notifications
You must be signed in to change notification settings - Fork 52
/
hook_info.go
77 lines (65 loc) · 1.41 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
66
67
68
69
70
71
72
73
74
75
76
77
package gitkit
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
const (
BranchPushAction = "branch.push"
BranchCreateAction = "branch.create"
BranchDeleteAction = "branch.delete"
TagCreateAction = "tag.create"
TagDeleteAction = "tag.delete"
)
// HookInfo holds git hook context
type HookInfo struct {
Action string
RepoName string
RepoPath string
OldRev string
NewRev string
Ref string
RefType string
RefName string
}
// ReadHookInput reads the hook context
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],
}
info.Action = parseHookAction(info)
return &info, nil
}
func parseHookAction(h HookInfo) 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 fmt.Sprintf("%s.%s", context, action)
}