forked from git-hooks/git-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir.go
210 lines (188 loc) · 5.02 KB
/
dir.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"encoding/json"
"github.com/cattail/go-exclude"
"github.com/mitchellh/go-homedir"
"io/ioutil"
"os"
"os/user"
"path/filepath"
)
// list directories for project, user and global scopes
func hookDirs() map[string]string {
dirs := make(map[string]string)
// project scope
root, err := getGitRepoRoot()
if err == nil {
path := filepath.Join(root, "githooks")
isExist, _ := exists(path)
if isExist {
dirs["project"] = path
}
}
// user scope
home, err := homedir.Dir()
if err == nil {
path := filepath.Join(home, ".githooks")
isExist, _ := exists(path)
if isExist {
dirs["user"] = path
}
}
// global scope
// NOTE: git-hooks global hook actually configured via git --system
// configuration file
global, err := gitExec("config --get hooks.global")
if err == nil {
path := global
isExist, _ := exists(path)
if isExist {
dirs["global"] = path
}
}
return dirs
}
// list configurations for project, user and global scopes
func hookConfigs() map[string]string {
configs := make(map[string]string)
root, err := getGitRepoRoot()
if err == nil {
path := filepath.Join(root, "githooks.json")
isExist, _ := exists(path)
if isExist {
configs["project"] = path
}
}
home, err := homedir.Dir()
if err == nil {
path := filepath.Join(home, ".githooks.json")
isExist, _ := exists(path)
if isExist {
configs["user"] = path
}
}
global, err := gitExec("config --get hooks.globalconfig")
if err == nil {
path := global
isExist, _ := exists(path)
if isExist {
configs["global"] = path
}
}
return configs
}
// List available hooks inside directory
// Under trigger directory,
// Treate file as a hook if it's executable,
// Treate directory as a hook if it contain an executable file with the name of `trigger`
// Example:
// githooks
// ├── _pre-commit
// │ ├── test
// │ └── whitespace
// └── pre-commit
// ├── dir
// │ └── pre-commit
// └── whitespace
func listHooksInDir(scope, dirname string) (hooks map[string][]string, err error) {
hooks = make(map[string][]string)
dirs, err := ioutil.ReadDir(dirname)
if err != nil {
return
}
for _, dir := range dirs {
files, err := ioutil.ReadDir(filepath.Join(dirname, dir.Name()))
if err == nil {
hooks[dir.Name()] = make([]string, 0)
for _, file := range files {
// filter files or directories
file, err := os.Stat(filepath.Join(dirname, dir.Name(), file.Name()))
if err == nil {
if file.IsDir() {
libs, err := ioutil.ReadDir(filepath.Join(dirname, dir.Name(), file.Name()))
if err == nil {
for _, lib := range libs {
libname := lib.Name()
extension := filepath.Ext(libname)
if isExecutable(lib) && libname[0:len(libname)-len(extension)] == dir.Name() {
hooks[dir.Name()] = append(hooks[dir.Name()], filepath.Join(file.Name(), libname))
}
}
}
} else {
if isExecutable(file) {
hooks[dir.Name()] = append(hooks[dir.Name()], file.Name())
}
}
}
}
}
}
//
// exclude
//
// exclude only works for user and global scope
if scope == "user" || scope == "global" {
file, err := ioutil.ReadFile(filepath.Join(dirname, "excludes.json"))
if err == nil {
var excludes interface{}
json.Unmarshal(file, &excludes)
wrapper := make(map[string]interface{})
// repoid will be empty string if not in a git repo or don't have any commit yet
repoid, _ := gitExec(GIT["FirstCommit"])
if scope == "user" {
wrapper[repoid] = hooks
exclude.Exclude(wrapper, excludes)
if wrapper[repoid] == nil {
wrapper[repoid] = make(map[string][]string)
}
hooks = wrapper[repoid].(map[string][]string)
} else {
// global scope exclude
user, err := user.Current()
username := ""
if err == nil {
username = user.Username
}
wrapper[username] = make(map[string]interface{})
wrapper[username].(map[string]interface{})[repoid] = hooks
exclude.Exclude(wrapper, excludes)
if wrapper[username] == nil {
wrapper[username] = make(map[string][]string)
}
if wrapper[repoid] == nil {
wrapper[repoid] = make(map[string][]string)
}
hooks = wrapper[username].(map[string]interface{})[repoid].(map[string][]string)
}
}
}
return hooks, nil
}
// List available hooks configured by config file
func listHooksInConfig(config string) (hooks map[string]map[string][]string, err error) {
hooks = make(map[string]map[string][]string)
file, err := ioutil.ReadFile(config)
if err != nil {
return
}
json.Unmarshal(file, &hooks)
return
}
// Find contrib directory
func getContribDir() (contrib string) {
contrib, err := gitExec("config --get hooks.contrib")
isExist, _ := exists(contrib)
if err != nil || !isExist {
// default to use ~/.githooks-contrib
home, err := homedir.Dir()
if err != nil {
// fallback
home = "~"
}
contrib = filepath.Join(home, "."+CONTRIB_DIRNAME)
} else {
contrib = filepath.Join(contrib, CONTRIB_DIRNAME)
}
return
}