-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit.go
541 lines (481 loc) · 12.4 KB
/
git.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
package cloudwatcher
import (
"encoding/json"
"errors"
"fmt"
"github.com/go-git/go-git/v5/plumbing/transport"
"io/ioutil"
"os"
"strings"
"sync/atomic"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
)
var errExitFromLoop = errors.New("exit")
// GitWatcher is the specialized watcher for Git service
type GitWatcher struct {
WatcherBase
syncing uint32
repository *git.Repository
auth transport.AuthMethod
ticker *time.Ticker
stop chan bool
config *gitConfiguration
fileCache map[string]*GitObject
branchCache map[string]string // Branch name -> last commit hash
tagCache map[string]string
}
// GitCommit is the object that contains the info about the commit
type GitCommit struct {
Hash string
Message string
Branch string
AuthorName string
AuthorEmail string
Time time.Time
}
// GitObject is the object that contains the info of the file
type GitObject struct {
Key string
Size int64
FileMode os.FileMode
Hash string
Commits []*GitCommit
}
type gitConfiguration struct {
Debug Bool `json:"debug"`
MonitorType string `json:"monitor_type"` // file, repo
AuthType string `json:"auth_type"` // ssh, http_token, http_user_pass
SSHPrivateKey string `json:"ssh_pkey"`
SSHPKeyPassword string `json:"ssh_pkey_password"`
HTTPToken string `json:"http_token"`
HTTPUsername string `json:"http_username"`
HTTPPassword string `json:"http_password"`
RepoURL string `json:"repo_url"`
RepoBranch string `json:"repo_branch"`
AssembleEvents Bool `json:"assemble_events"`
TempDir string `json:"temp_dir"`
}
func newGitWatcher(dir string, interval time.Duration) (Watcher, error) {
return &GitWatcher{
tagCache: make(map[string]string),
fileCache: make(map[string]*GitObject),
branchCache: make(map[string]string),
stop: make(chan bool, 1),
WatcherBase: WatcherBase{
Events: make(chan Event, 100),
Errors: make(chan error, 100),
watchDir: dir,
pollingTime: interval,
},
}, nil
}
// SetConfig is used to configure the GitWatcher
func (w *GitWatcher) SetConfig(m map[string]string) error {
j, err := json.Marshal(m)
if err != nil {
return err
}
config := gitConfiguration{}
if err := json.Unmarshal(j, &config); err != nil {
return err
}
if config.MonitorType == "" {
config.MonitorType = "repo" // setting default behaviour
} else if !inArray(config.MonitorType, []string{"repo", "file"}) {
return fmt.Errorf("unknown monitor_type '%s'", config.MonitorType)
}
if config.AuthType == "ssh" {
_, err := os.Stat(config.SSHPrivateKey)
if err != nil {
return fmt.Errorf("cannot read file '%s': %s", config.SSHPrivateKey, err)
}
}
if !inArray(config.AuthType, []string{"", "none", "ssh", "http_token", "http_user_pass"}) {
return fmt.Errorf("unknown auth_type '%s'", config.AuthType)
}
if config.RepoURL == "" {
return fmt.Errorf("url repository required")
}
if config.AuthType == "file" && config.RepoBranch == "" {
return fmt.Errorf("branch repository required")
}
if config.TempDir == "" {
dir, err := ioutil.TempDir("", "tmp_git")
if err != nil {
return fmt.Errorf("creating temp dir: %s", err)
}
config.TempDir = dir
}
w.config = &config
return nil
}
// Start launches the polling process
func (w *GitWatcher) Start() error {
if w.config == nil {
return fmt.Errorf("configuration for Git needed")
}
w.ticker = time.NewTicker(w.pollingTime)
go func() {
// launch synchronization also the first time
w.sync(true)
for {
select {
case <-w.ticker.C:
w.sync(false)
case <-w.stop:
close(w.Events)
close(w.Errors)
return
}
}
}()
return nil
}
// Close stop the polling process
func (w *GitWatcher) Close() {
if w.stop != nil {
w.stop <- true
}
}
func (w *GitWatcher) getCachedObject(o *GitObject) *GitObject {
if cachedObject, ok := w.fileCache[o.Key]; ok {
return cachedObject
}
return nil
}
func (w *GitWatcher) sync(firstSync bool) {
// allow only one sync at same time
if !atomic.CompareAndSwapUint32(&w.syncing, 0, 1) {
return
}
defer atomic.StoreUint32(&w.syncing, 0)
err := w.updateRepo()
if err != nil {
w.Errors <- err
return
}
// default behaviour is file
if w.config.MonitorType == "repo" {
w.checkCommits(firstSync)
w.checkTags(firstSync)
} else {
fileList := make(map[string]*GitObject, 0)
err := w.enumerateFiles(w.watchDir, func(obj *GitObject) bool {
// With the first sync we need to cache all the files
if firstSync {
w.fileCache[obj.Key] = obj
return true
}
// Store the files to check the deleted one
fileList[obj.Key] = obj
// Check if the object is cached by Key
cached := w.getCachedObject(obj)
// Object has been cached previously by Key
if cached != nil {
// Check if the Hash or the FileMode have been changed
if cached.Hash != obj.Hash {
event := Event{
Key: obj.Key,
Type: FileChanged,
Object: obj,
}
w.Events <- event
} else if cached.FileMode != obj.FileMode {
event := Event{
Key: obj.Key,
Type: TagsChanged,
Object: obj,
}
w.Events <- event
}
} else {
event := Event{
Key: obj.Key,
Type: FileCreated,
Object: obj,
}
w.Events <- event
}
w.fileCache[obj.Key] = obj
return true
})
if err != nil {
w.Errors <- err
return
}
for k, o := range w.fileCache {
if _, found := fileList[k]; !found {
// file not found in the list...deleting it
delete(w.fileCache, k)
event := Event{
Key: o.Key,
Type: FileDeleted,
Object: o,
}
w.Events <- event
}
}
}
}
func (w *GitWatcher) checkCommits(disableNotification bool) {
branches := make([]string, 0)
// if RepoBranch is empty we are collecting all the branches
if w.config.RepoBranch == "" {
rIter, err := w.repository.Branches()
if err != nil {
w.Errors <- fmt.Errorf("retrieving branches: %s", err)
return
}
err = rIter.ForEach(func(ref *plumbing.Reference) error {
branches = append(branches, ref.Name().Short())
return nil
})
} else {
branches = append(branches, w.config.RepoBranch)
}
for _, branch := range branches {
err := w.moveToBranch(branch)
if err != nil {
w.Errors <- err
continue
}
// retrieving commits for the current branch
cIter, err := w.repository.Log(&git.LogOptions{})
if err != nil {
w.Errors <- err
return
}
commits := make([]*GitCommit, 0)
err = cIter.ForEach(func(c *object.Commit) error {
if v, ok := w.branchCache[branch]; ok {
// Exit from the loop, we reached the last commit we saw previously
if c.Hash.String() == v {
return errExitFromLoop
}
commit := &GitCommit{
Hash: c.Hash.String(),
Message: c.Message,
AuthorName: c.Author.Name,
AuthorEmail: c.Author.Email,
Time: c.Author.When,
Branch: branch,
}
commits = append(commits, commit)
} else {
// this is the first time we see this branch.
// Storing only the last commit's hash because
// we can't know from what other branch it comes
commit := &GitCommit{
Hash: c.Hash.String(),
Message: c.Message,
AuthorName: c.Author.Name,
AuthorEmail: c.Author.Email,
Time: c.Author.When,
Branch: branch,
}
commits = append(commits, commit)
return errExitFromLoop
}
return nil
})
if err != nil && err != errExitFromLoop {
w.Errors <- err
return
}
if len(commits) != 0 {
// Caching last commit
w.branchCache[branch] = commits[0].Hash
if disableNotification == false {
if w.config.AssembleEvents {
event := Event{
Key: "commit",
Type: 0,
Object: &GitObject{
Commits: commits,
},
}
w.Events <- event
} else {
for _, commit := range commits {
event := Event{
Key: "commit",
Type: 0,
Object: &GitObject{
Commits: []*GitCommit{commit},
},
}
w.Events <- event
}
}
}
}
}
}
func (w *GitWatcher) checkTags(disableNotification bool) {
// event on Tags
tagrefs, err := w.repository.Tags()
if err != nil {
w.Errors <- err
return
}
tags := make([]*GitCommit, 0)
err = tagrefs.ForEach(func(t *plumbing.Reference) error {
if _, ok := w.tagCache[t.Name().Short()]; !ok {
tags = append(tags, &GitCommit{
Hash: t.Hash().String(),
Message: t.Name().Short(),
})
w.tagCache[t.Name().Short()] = t.Hash().String()
}
return nil
})
if err != nil {
w.Errors <- err
return
}
if disableNotification == false && len(tags) != 0 {
if w.config.AssembleEvents {
event := Event{
Key: "tag",
Type: 0,
Object: &GitObject{
Commits: tags,
},
}
w.Events <- event
} else {
for _, tag := range tags {
event := Event{
Key: "tag",
Type: 0,
Object: &GitObject{
Commits: []*GitCommit{tag},
},
}
w.Events <- event
}
}
}
}
func (w *GitWatcher) moveToBranch(branch string) error {
wt, err := w.repository.Worktree()
if err != nil {
return fmt.Errorf("getting worktree: %s", err)
}
// Move to a precise branch
err = wt.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName(branch),
})
if err != nil {
return fmt.Errorf("checkout of repo '%s': %s", branch, err)
}
return nil
}
func (w *GitWatcher) updateRepo() error {
// tmp dir has been deleted?!?!
if _, err := os.Stat(w.config.TempDir); os.IsNotExist(err) {
w.repository = nil
}
if w.repository == nil {
opts := &git.CloneOptions{
URL: w.config.RepoURL,
}
switch w.config.AuthType {
case "ssh":
_, err := os.Stat(w.config.SSHPrivateKey)
if err != nil {
return fmt.Errorf("cannot read file '%s': %s", w.config.SSHPrivateKey, err)
}
publicKeys, err := ssh.NewPublicKeysFromFile("git", w.config.SSHPrivateKey, w.config.SSHPKeyPassword)
if err != nil {
return fmt.Errorf("loading pkeys from '%s': %s", w.config.SSHPrivateKey, err)
}
opts.Auth = publicKeys
case "http_token":
// token auth ignores the username but it cannot be empty
if w.config.HTTPUsername == "" {
w.config.HTTPUsername = "token"
}
opts.Auth = &http.BasicAuth{
Username: w.config.HTTPUsername,
Password: w.config.HTTPToken,
}
case "http_user_pass":
opts.Auth = &http.BasicAuth{
Username: w.config.HTTPUsername,
Password: w.config.HTTPPassword,
}
default:
}
w.auth = opts.Auth
r, err := git.PlainClone(w.config.TempDir, false, opts)
if err != nil && err == git.ErrRepositoryAlreadyExists {
r, err = git.PlainOpen(w.config.TempDir)
}
if err != nil {
return fmt.Errorf("cloning repo: %s", err)
}
w.repository = r
}
wt, err := w.repository.Worktree()
if err != nil {
return fmt.Errorf("getting worktree: %s", err)
}
// Update the repository
err = wt.Pull(&git.PullOptions{
Auth: w.auth,
})
if err != nil && err != git.NoErrAlreadyUpToDate {
return fmt.Errorf("checkout of repo '%s': %s", w.config.RepoURL, err)
}
return nil
}
func (w *GitWatcher) enumerateFiles(prefix string, callback func(object *GitObject) bool) error {
err := w.moveToBranch(w.config.RepoBranch)
if err != nil {
return fmt.Errorf("switching branch to '%s': %s", w.config.RepoBranch, err)
}
// getting head reference
ref, err := w.repository.Head()
if err != nil {
return fmt.Errorf("getting head reference: %s", err)
}
// retrieve the commit pointed from head
commit, err := w.repository.CommitObject(ref.Hash())
if err != nil {
return fmt.Errorf("retrieving commit '%s': %s", ref.Hash().String(), err)
}
// retrieve the tree from the commit
tree, err := commit.Tree()
if err != nil {
return fmt.Errorf("retrieving tree of '%s': %s", ref.Hash().String(), err)
}
// iterate files in the commit
err = tree.Files().ForEach(func(f *object.File) error {
if strings.HasPrefix(f.Name, prefix) || prefix != "" {
o := &GitObject{
Key: f.Name,
Size: f.Size,
Hash: f.Hash.String(),
FileMode: os.FileMode(f.Mode),
Commits: nil,
}
if callback(o) == false {
return errExitFromLoop
}
}
return nil
})
if err != nil && err != errExitFromLoop {
return fmt.Errorf("looping files : %s", err)
}
return nil
}
func init() {
supportedServices["git"] = newGitWatcher
}