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
|
package gitpb
// An app to submit patches for the 30 GO GUI repos
import (
"errors"
"fmt"
"os"
"path/filepath"
"time"
"go.wit.com/lib/config"
"go.wit.com/log"
"google.golang.org/protobuf/types/known/timestamppb"
)
func (repo *Repo) Mtime(fname string) *time.Time {
var fileTime *time.Time
tmp, err := repo.statMtime(fname)
fileTime = &tmp
if err != nil {
log.Info("MTime got err", err)
return nil
}
return fileTime
}
func (repo *Repo) statMtime(filename string) (time.Time, error) {
pathf := filepath.Join(repo.FullPath, filename)
statf, err := os.Stat(pathf)
if err == nil {
return statf.ModTime(), nil
}
log.Log(WARN, "Mtime() os.Stat() error", pathf, err)
return time.Now(), err
}
func (repo *Repo) changedDir() bool {
fname := ".git"
fileTime := repo.Mtime(fname)
if fileTime == nil {
// .git doesn't exist. something is wrong. rescan this repo
return true
}
mtime := timestamppb.New(*fileTime)
pbtime := repo.Times.MtimeDir
if pbtime == nil { // this can happen?
repo.Times.MtimeDir = mtime
return true
}
if (pbtime.Seconds == mtime.Seconds) && (pbtime.Nanos == mtime.Nanos) {
return false
}
dur := mtime.AsTime().Sub(pbtime.AsTime())
repo.StateChange = fmt.Sprintf("%s changed %s", fname, config.FormatDuration(dur))
repo.Times.MtimeDir = mtime
return true
}
func (repo *Repo) didFileChange(fname string, pbtime *timestamppb.Timestamp) bool {
fileTime := repo.Mtime(fname)
if fileTime == nil {
repo.StateChange = fmt.Sprintf("%s missing", fname)
return true
}
mtime := timestamppb.New(*fileTime)
if pbtime == nil {
repo.StateChange = fmt.Sprintf("%s mtime never recorded", fname)
return true
}
if (pbtime.Seconds == mtime.Seconds) && (pbtime.Nanos == mtime.Nanos) {
// it's the same!
return false
}
dur := mtime.AsTime().Sub(pbtime.AsTime())
repo.StateChange = fmt.Sprintf("%s mtime changed %s", fname, config.FormatDuration(dur))
// need to reload from the filesystem
return true
}
// boo. I'm not good at golang. this should use reflect. I'm bad. my code is bad. boo this man. you're cool, I'm outta here
// make this work right someday
func (repo *Repo) updateMtime(fname string, pbname string) bool {
fileTime := repo.Mtime(fname)
if fileTime == nil {
// .git/HEAD doesn't exist. something is wrong. rescan this repo
return true
}
mtime := timestamppb.New(*fileTime)
pbtime := repo.Times.MtimeHead
if pbtime == nil { // this can happen?
repo.Times.MtimeHead = mtime
return true
}
switch pbname {
case "MtimeHead":
if pbtime == nil { // this can happen?
repo.Times.MtimeHead = mtime
return true
}
default:
}
if (pbtime.Seconds == mtime.Seconds) && (pbtime.Nanos == mtime.Nanos) {
return false
}
dur := mtime.AsTime().Sub(pbtime.AsTime())
repo.StateChange = fmt.Sprintf("%s changed %s", fname, config.FormatDuration(dur))
repo.Times.MtimeHead = mtime
return true
}
func (repo *Repo) changedHead() bool {
fname := ".git/HEAD"
fileTime := repo.Mtime(fname)
if fileTime == nil {
// .git/HEAD doesn't exist. something is wrong. rescan this repo
return true
}
mtime := timestamppb.New(*fileTime)
pbtime := repo.Times.MtimeHead
if pbtime == nil { // this can happen?
repo.Times.MtimeHead = mtime
return true
}
if (pbtime.Seconds == mtime.Seconds) && (pbtime.Nanos == mtime.Nanos) {
return false
}
dur := mtime.AsTime().Sub(pbtime.AsTime())
repo.StateChange = fmt.Sprintf("%s changed %s", fname, config.FormatDuration(dur))
repo.Times.MtimeHead = mtime
return true
}
// check the mtime of the .git/config file
func (repo *Repo) changedConfig() bool {
fname := ".git/config"
fileTime := repo.Mtime(fname)
if fileTime == nil {
// .git/config doesn't exist. something is wrong!
log.Info("gitpb .git/config is missing", repo.GetGoPath())
return false
}
mtime := timestamppb.New(*fileTime)
pbtime := repo.Times.MtimeConfig
if pbtime == nil { // this can happen?
repo.Times.MtimeConfig = mtime
return true
}
if (pbtime.Seconds == mtime.Seconds) && (pbtime.Nanos == mtime.Nanos) {
return false
}
dur := mtime.AsTime().Sub(pbtime.AsTime())
repo.StateChange = fmt.Sprintf("%s changed %s", fname, config.FormatDuration(dur))
repo.Times.MtimeConfig = mtime
return true
}
func (repo *Repo) changedIndex() bool {
fname := ".git/index"
fileTime := repo.Mtime(fname)
if fileTime == nil {
// .git/index doesn't exist. something is wrong. rescan this repo
return true
}
mtime := timestamppb.New(*fileTime)
pbtime := repo.Times.MtimeIndex
if pbtime == nil { // this can happen?
repo.Times.MtimeIndex = mtime
return true
}
if (pbtime.Seconds == mtime.Seconds) && (pbtime.Nanos == mtime.Nanos) {
return false
}
dur := mtime.AsTime().Sub(pbtime.AsTime())
repo.StateChange = fmt.Sprintf("%s changed %s", fname, config.FormatDuration(dur))
repo.Times.MtimeIndex = mtime
return true
}
func (repo *Repo) reloadMtimes() bool {
var changed bool
if repo.Times == nil {
repo.Times = new(GitTimes)
log.Info(repo.FullPath, "repo.Times were nil")
}
if repo.changedHead() {
changed = true
}
if repo.changedIndex() {
changed = true
}
if repo.changedConfig() {
changed = true
}
if repo.changedDir() {
// changed = true
}
return changed
}
// also checks .git which seems to change *a lot*
// this might work immediately after git checkout
func (repo *Repo) DidRepoChangeDir() error {
if repo.Times == nil {
repo.Times = new(GitTimes)
}
if repo.didFileChange(".git/HEAD", repo.Times.MtimeHead) {
return log.Errorf(".git/HEAD changed")
}
if repo.didFileChange(".git/index", repo.Times.MtimeIndex) {
repo.StateChange += "jwc"
return log.Errorf(".git/index changed")
}
if repo.didFileChange(".git/config", repo.Times.MtimeConfig) {
return log.Errorf(".git/config changed")
}
if repo.didFileChange(".git", repo.Times.MtimeDir) {
// todo: do something with CheckDirty()
return log.Errorf(".git changed")
}
if repo.Times.LastUpdate == nil {
log.Info("repo.Reload() was never run")
return log.Errorf("Reload() ran for the first time")
} else {
if repo.Times.LastUpdate.Seconds < repo.Times.MtimeHead.Seconds {
log.Info("SHOULD RUN Reload() here", repo.Times.MtimeHead.Seconds-repo.Times.LastUpdate.Seconds, "secs diff")
return log.Errorf("Reload() time skew on .git/HEAD")
}
}
// log.Info("DidRepoChange() is false", repo.FullPath)
return nil
}
func (repo *Repo) DidRepoChange() error {
if repo.Times == nil {
repo.Times = new(GitTimes)
}
if repo.didFileChange(".git/HEAD", repo.Times.MtimeHead) {
return log.Errorf(".git/HEAD changed")
}
if repo.didFileChange(".git/index", repo.Times.MtimeIndex) {
repo.StateChange += "jwc2"
return log.Errorf(".git/index changed")
}
if repo.didFileChange(".git/config", repo.Times.MtimeConfig) {
return log.Errorf(".git/config changed")
}
if repo.didFileChange(".git", repo.Times.MtimeDir) {
// todo: do something with CheckDirty()
// return true
}
if repo.Times.LastUpdate == nil {
log.Info("repo.Reload() was never run")
return log.Errorf("Reload() ran for the first time")
} else {
if repo.Times.LastUpdate.Seconds < repo.Times.MtimeHead.Seconds {
log.Info("SHOULD RUN Reload() here", repo.Times.MtimeHead.Seconds-repo.Times.LastUpdate.Seconds, "secs diff")
return log.Errorf("Reload() time skew on .git/HEAD")
}
}
// log.Info("DidRepoChange() is false", repo.FullPath)
return nil
}
func (all *Repos) ScanAllMtimesVerbose() (string, error) {
var count int
var anyerr error
for r := range all.IterAll() {
var err error
var ok bool
if count < 10 {
// only show output for the first 10 repos
ok, err = r.didRepoChangeVerbose()
} else {
err = r.DidRepoChange()
}
if err != nil {
log.Info(r.Namespace, err)
}
if (err != nil) || ok {
// means there was output
anyerr = err
count += 1
}
}
if anyerr != nil {
all.Save()
}
return "looked into mtime changes", anyerr
}
func (r *Repo) didRepoChangeVerbose() (bool, error) {
var output bool
if r.Times == nil {
r.Times = new(GitTimes)
log.Info("repo.Reload() was never run")
output = true
// means this is going to populate the mtimes for the first time
// todo: make sure the whole thing gets saved
} else {
if r.Times.LastUpdate != nil {
lastupdate := r.Times.LastUpdate.AsTime()
dur := time.Since(lastupdate)
if dur < 5*time.Minute {
log.Info("repo.ReloadMtime() was was last run", config.FormatDuration(dur), r.Namespace)
output = true
}
} else {
log.Info("repo.ReloadMtime() LastUpdate == nil", r.Namespace)
output = true
}
}
if r.didFileChange(".git/HEAD", r.Times.MtimeHead) {
r.RunVerbose([]string{"ls", "-l", ".git/HEAD"})
output = true
return output, errors.New(".git/HEAD changed")
}
if r.didFileChange(".git/index", r.Times.MtimeIndex) {
r.StateChange += "jwc2"
r.RunVerbose([]string{"ls", "-l", ".git/index"})
output = true
return output, errors.New(".git/index changed")
}
if r.didFileChange(".git/config", r.Times.MtimeConfig) {
r.RunVerbose([]string{"ls", "-l", ".git/config"})
output = true
return output, errors.New(".git/config changed")
}
if r.didFileChange(".git", r.Times.MtimeDir) {
if r.Times.MtimeDir != nil {
lastupdate := r.Times.LastUpdate.AsTime()
dur := time.Since(lastupdate)
if dur < 5*time.Minute {
log.Info("repo.ReloadMtime() MtimeDir last run", config.FormatDuration(dur), r.Namespace)
r.RunVerbose([]string{"ls", "-l", ".git", "-d"})
output = true
}
} else {
}
// todo: do something with CheckDirty()
// return true
}
if r.Times.LastUpdate == nil {
log.Info("r.Reload() was never run")
output = true
return output, errors.New("Reload() ran for the first time")
} else {
if r.Times.LastUpdate.Seconds < r.Times.MtimeHead.Seconds {
log.Info("SHOULD RUN Reload() here", r.Times.MtimeHead.Seconds-r.Times.LastUpdate.Seconds, "secs diff")
output = true
return output, errors.New("Reload() time skew on .git/HEAD")
}
}
// nothing changed. All mtimes being monitored are old
return output, nil
}
|