forked from thoughtworks/talisman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre_push_hook.go
76 lines (61 loc) · 2.22 KB
/
pre_push_hook.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
package main
import (
"os"
log "github.com/Sirupsen/logrus"
"talisman/gitrepo"
)
const (
//EmptySha represents the state of a brand new ref
EmptySha string = "0000000000000000000000000000000000000000"
//ShaId of the empty tree in Git
EmptyTreeSha string = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
)
type PrePushHook struct {
localRef, localCommit, remoteRef, remoteCommit string
}
func NewPrePushHook(localRef, localCommit, remoteRef, remoteCommit string) *PrePushHook {
return &PrePushHook{localRef, localCommit, remoteRef, remoteCommit}
}
//If the outgoing ref does not exist on the remote, all commits on the local ref will be checked
//If the outgoing ref already exists, all additions in the range between "localSha" and "remoteSha" will be validated
func (p *PrePushHook) GetRepoAdditions() []gitrepo.Addition {
if p.runningOnDeletedRef() {
log.WithFields(log.Fields{
"localRef": p.localRef,
"localCommit": p.localCommit,
"remoteRef": p.remoteRef,
"remoteCommit": p.remoteCommit,
}).Info("Running on a deleted ref. Nothing to verify as outgoing changes are all deletions.")
return []gitrepo.Addition{}
}
if p.runningOnNewRef() {
log.WithFields(log.Fields{
"localRef": p.localRef,
"localCommit": p.localCommit,
"remoteRef": p.remoteRef,
"remoteCommit": p.remoteCommit,
}).Info("Running on a new ref. All changes in the ref will be verified.")
return p.getRepoAdditionsFrom(EmptyTreeSha, p.localCommit)
}
log.WithFields(log.Fields{
"localRef": p.localRef,
"localCommit": p.localCommit,
"remoteRef": p.remoteRef,
"remoteCommit": p.remoteCommit,
}).Info("Running on an existing ref. All changes in the commit range will be verified.")
return p.getRepoAdditions()
}
func (p *PrePushHook) runningOnDeletedRef() bool {
return p.localCommit == EmptySha
}
func (p *PrePushHook) runningOnNewRef() bool {
return p.remoteCommit == EmptySha
}
func (p *PrePushHook) getRepoAdditions() []gitrepo.Addition {
return p.getRepoAdditionsFrom(p.remoteCommit, p.localCommit)
}
func (p *PrePushHook) getRepoAdditionsFrom(oldCommit, newCommit string) []gitrepo.Addition {
wd, _ := os.Getwd()
repo := gitrepo.RepoLocatedAt(wd)
return repo.AdditionsWithinRange(oldCommit, newCommit)
}