forked from Southclaws/gitwatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitwatch.go
More file actions
232 lines (206 loc) · 5.92 KB
/
gitwatch.go
File metadata and controls
232 lines (206 loc) · 5.92 KB
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
// Package gitwatch provides a simple tool to first clone a set of git
// repositories to a local directory and then periodically check them all for
// any updates.
package gitwatch
import (
"context"
"net/url"
"path/filepath"
"strings"
"time"
"github.com/pkg/errors"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
)
// Session represents a git watch session configuration
type Session struct {
Repositories []string // list of local or remote repository URLs to watch
Interval time.Duration // the interval between remote checks
Directory string // the directory to store repositories
Auth transport.AuthMethod // authentication method for git operations
InitialEvent bool // if true, an event for each repo will be emitted upon construction
InitialDone chan struct{} // if InitialEvent true, this is pushed to after initial setup done
Events chan Event // when a change is detected, events are pushed here
Errors chan error // when an error occurs, errors come here instead of halting the loop
ctx context.Context
cf context.CancelFunc
}
// Event represents an update detected on one of the watched repositories
type Event struct {
URL string
Path string
Timestamp time.Time
}
// New constructs a new git watch session on the given repositories
func New(
ctx context.Context,
repos []string,
interval time.Duration,
dir string,
auth transport.AuthMethod,
initialEvent bool,
) (session *Session, err error) {
ctx2, cf := context.WithCancel(ctx)
session = &Session{
Repositories: repos,
Interval: interval,
Directory: dir,
Events: make(chan Event),
Errors: make(chan error, 16),
InitialEvent: initialEvent,
InitialDone: make(chan struct{}, 1),
ctx: ctx2,
cf: cf,
}
return
}
// Run begins the watcher and blocks until an error occurs
func (s *Session) Run() (err error) {
return s.daemon()
}
// Close gracefully shuts down the git watcher
func (s *Session) Close() {
s.cf()
}
func (s *Session) daemon() (err error) {
t := time.NewTicker(s.Interval)
// a function to select over the session's context and the ticker to check
// repositories.
f := func() (err error) {
select {
case <-s.ctx.Done():
err = context.Canceled
case <-t.C:
err = s.checkRepos()
if err != nil {
s.Errors <- err
return nil
}
}
return
}
// before starting the daemon process loop, perform an initial check against
// all targets. If the targets do not exist, they will be cloned and events
// will be emitted for them.
if s.InitialEvent {
err = s.checkRepos()
if err != nil {
return
}
s.InitialDone <- struct{}{}
}
for {
err = f()
if err != nil {
return
}
}
}
// checkRepos simply iterates all repositories and collects events from them, if
// there are any, they will be emitted to the Events channel concurrently.
func (s *Session) checkRepos() (err error) {
for _, repoPath := range s.Repositories {
var event *Event
event, err = s.checkRepo(repoPath)
if err != nil {
return
}
if event != nil {
go func() { s.Events <- *event }()
}
}
return
}
// checkRepo checks a specific git repository that may or may not exist locally
// and if there are changes or the repository had to be cloned fresh (and
// InitialEvents is true) then an event is returned.
func (s *Session) checkRepo(repoPath string) (event *Event, err error) {
localPath, err := GetRepoPath(s.Directory, repoPath)
if err != nil {
err = errors.Wrap(err, "failed to get path from repo url")
return
}
repo, err := git.PlainOpen(localPath)
if err != nil {
if err != git.ErrRepositoryNotExists {
err = errors.Wrap(err, "failed to open local repo")
return
}
return s.cloneRepo(repoPath, localPath)
}
return s.GetEventFromRepoChanges(repo)
}
// cloneRepo clones the specified repository to the session's cache and, if
// InitialEvent is true, emits an event for the newly cloned repo.
func (s *Session) cloneRepo(repoPath, localPath string) (event *Event, err error) {
repo, err := git.PlainCloneContext(s.ctx, localPath, false, &git.CloneOptions{
Auth: s.Auth,
URL: repoPath,
})
if err != nil {
err = errors.Wrap(err, "failed to clone initial copy of repository")
return
}
if s.InitialEvent {
event, err = GetEventFromRepo(repo)
}
return
}
// GetEventFromRepoChanges reads a locally cloned git repository an returns an
// event only if an attempted fetch resulted in new changes in the working tree.
func (s *Session) GetEventFromRepoChanges(repo *git.Repository) (event *Event, err error) {
wt, err := repo.Worktree()
if err != nil {
return nil, errors.Wrap(err, "failed to get worktree")
}
err = wt.Pull(&git.PullOptions{
Auth: s.Auth,
})
if err != nil {
if err == git.NoErrAlreadyUpToDate {
return nil, nil
}
return nil, errors.Wrap(err, "failed to pull local repo")
}
return GetEventFromRepo(repo)
}
// GetEventFromRepo reads a locally cloned git repository and returns an event
// based on the most recent commit.
func GetEventFromRepo(repo *git.Repository) (event *Event, err error) {
wt, err := repo.Worktree()
if err != nil {
return nil, errors.Wrap(err, "failed to get worktree")
}
remote, err := repo.Remote("origin")
if err != nil {
return
}
ref, err := repo.Head()
if err != nil {
return
}
c, err := repo.CommitObject(ref.Hash())
if err != nil {
return
}
return &Event{
URL: remote.Config().URLs[0],
Path: wt.Filesystem.Root(),
Timestamp: c.Author.When,
}, nil
}
// GetRepoPath returns the local path of a cached repo from the given cache, the
// base component of the repo path is used as a directory name for the target
// repository.
func GetRepoPath(cache, repo string) (result string, err error) {
path := strings.Split(repo, ":")
i := 0
if len(path) == 2 {
i = 1
}
u, err := url.Parse(path[i])
if err != nil {
return
}
return filepath.Join(cache, filepath.Base(u.Path)), nil
}