-
Notifications
You must be signed in to change notification settings - Fork 0
/
merger.go
87 lines (80 loc) · 1.96 KB
/
merger.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
package notification
import (
"context"
"fmt"
"os"
"path/filepath"
"sort"
"sync"
"golang.org/x/sync/errgroup"
)
type Merger struct {
NotificationLoader *Loader
}
func NewMerger(notificationLoader *Loader) *Merger {
return &Merger{
NotificationLoader: notificationLoader,
}
}
func containsStopFile(path string) bool {
// Check if the path contains a .notify-stop file
// If it does, return true
// Otherwise, return false
_, err := os.Stat(filepath.Join(path, ".git"))
return err == nil
}
func (n *Merger) Merge(ctx context.Context, path string) (*File, error) {
// Walk up the path, looking for notification files
// Merge them together
// Return the merged notification file
path = filepath.Clean(path)
rootPath := path
type loadRetVal struct {
idx int
notification *File
}
var i int
eg, egCtx := errgroup.WithContext(ctx)
allRetValues := make([]loadRetVal, 0, 10)
var allRetValuesMu sync.Mutex
for i = 0; ; i++ {
idx := i
loadPath := path
eg.Go(func() error {
notification, err := n.NotificationLoader.LoadForPath(egCtx, loadPath)
if err != nil {
return fmt.Errorf("failed to load notification for path %s: %w", loadPath, err)
}
if notification != nil {
notification.ChangedFile = rootPath
}
allRetValuesMu.Lock()
defer allRetValuesMu.Unlock()
allRetValues = append(allRetValues, loadRetVal{
idx: idx,
notification: notification,
})
return nil
})
if containsStopFile(path) {
break
}
newPath := filepath.Dir(path)
if newPath == path {
// We've reached the root
break
}
path = newPath
}
if err := eg.Wait(); err != nil {
return nil, fmt.Errorf("failed to load notifications: %w", err)
}
sort.Slice(allRetValues, func(i, j int) bool {
return allRetValues[i].idx < allRetValues[j].idx
})
ret := allRetValues[0].notification
for idx := 1; idx < len(allRetValues); idx++ {
allRetValues[idx-1].notification.Parent = allRetValues[idx].notification
}
return ret, nil
}