1package cache
2
3import (
4 "bytes"
5 "encoding/gob"
6 "fmt"
7 "io"
8 "io/ioutil"
9 "os"
10 "path"
11 "sort"
12 "strconv"
13 "strings"
14 "time"
15
16 "github.com/MichaelMure/git-bug/bug"
17 "github.com/MichaelMure/git-bug/repository"
18 "github.com/MichaelMure/git-bug/util/git"
19 "github.com/MichaelMure/git-bug/util/process"
20)
21
22const cacheFile = "cache"
23const formatVersion = 1
24
25type RepoCache struct {
26 // the underlying repo
27 repo repository.ClockedRepo
28 // excerpt of bugs data for all bugs
29 excerpts map[string]*BugExcerpt
30 // bug loaded in memory
31 bugs map[string]*BugCache
32}
33
34func NewRepoCache(r repository.ClockedRepo) (*RepoCache, error) {
35 c := &RepoCache{
36 repo: r,
37 bugs: make(map[string]*BugCache),
38 }
39
40 err := c.lock()
41 if err != nil {
42 return &RepoCache{}, err
43 }
44
45 err = c.load()
46 if err == nil {
47 return c, nil
48 }
49
50 err = c.buildCache()
51 if err != nil {
52 return nil, err
53 }
54
55 return c, c.write()
56}
57
58// GetPath returns the path to the repo.
59func (c *RepoCache) GetPath() string {
60 return c.repo.GetPath()
61}
62
63// GetPath returns the path to the repo.
64func (c *RepoCache) GetCoreEditor() (string, error) {
65 return c.repo.GetCoreEditor()
66}
67
68// GetUserName returns the name the the user has used to configure git
69func (c *RepoCache) GetUserName() (string, error) {
70 return c.repo.GetUserName()
71}
72
73// GetUserEmail returns the email address that the user has used to configure git.
74func (c *RepoCache) GetUserEmail() (string, error) {
75 return c.repo.GetUserEmail()
76}
77
78// StoreConfig store a single key/value pair in the config of the repo
79func (c *RepoCache) StoreConfig(key string, value string) error {
80 return c.repo.StoreConfig(key, value)
81}
82
83// ReadConfigs read all key/value pair matching the key prefix
84func (c *RepoCache) ReadConfigs(keyPrefix string) (map[string]string, error) {
85 return c.repo.ReadConfigs(keyPrefix)
86}
87
88// RmConfigs remove all key/value pair matching the key prefix
89func (c *RepoCache) RmConfigs(keyPrefix string) error {
90 return c.repo.RmConfigs(keyPrefix)
91}
92
93func (c *RepoCache) lock() error {
94 lockPath := repoLockFilePath(c.repo)
95
96 err := repoIsAvailable(c.repo)
97 if err != nil {
98 return err
99 }
100
101 f, err := os.Create(lockPath)
102 if err != nil {
103 return err
104 }
105
106 pid := fmt.Sprintf("%d", os.Getpid())
107 _, err = f.WriteString(pid)
108 if err != nil {
109 return err
110 }
111
112 return f.Close()
113}
114
115func (c *RepoCache) Close() error {
116 lockPath := repoLockFilePath(c.repo)
117 return os.Remove(lockPath)
118}
119
120// bugUpdated is a callback to trigger when the excerpt of a bug changed,
121// that is each time a bug is updated
122func (c *RepoCache) bugUpdated(id string) error {
123 b, ok := c.bugs[id]
124 if !ok {
125 panic("missing bug in the cache")
126 }
127
128 c.excerpts[id] = NewBugExcerpt(b.bug, b.Snapshot())
129
130 return c.write()
131}
132
133// load will try to read from the disk the bug cache file
134func (c *RepoCache) load() error {
135 f, err := os.Open(cacheFilePath(c.repo))
136 if err != nil {
137 return err
138 }
139
140 decoder := gob.NewDecoder(f)
141
142 aux := struct {
143 Version uint
144 Excerpts map[string]*BugExcerpt
145 }{}
146
147 err = decoder.Decode(&aux)
148 if err != nil {
149 return err
150 }
151
152 if aux.Version != 1 {
153 return fmt.Errorf("unknown cache format version %v", aux.Version)
154 }
155
156 c.excerpts = aux.Excerpts
157 return nil
158}
159
160// write will serialize on disk the bug cache file
161func (c *RepoCache) write() error {
162 var data bytes.Buffer
163
164 aux := struct {
165 Version uint
166 Excerpts map[string]*BugExcerpt
167 }{
168 Version: formatVersion,
169 Excerpts: c.excerpts,
170 }
171
172 encoder := gob.NewEncoder(&data)
173
174 err := encoder.Encode(aux)
175 if err != nil {
176 return err
177 }
178
179 f, err := os.Create(cacheFilePath(c.repo))
180 if err != nil {
181 return err
182 }
183
184 _, err = f.Write(data.Bytes())
185 if err != nil {
186 return err
187 }
188
189 return f.Close()
190}
191
192func cacheFilePath(repo repository.Repo) string {
193 return path.Join(repo.GetPath(), ".git", "git-bug", cacheFile)
194}
195
196func (c *RepoCache) buildCache() error {
197 fmt.Printf("Building bug cache... ")
198
199 c.excerpts = make(map[string]*BugExcerpt)
200
201 allBugs := bug.ReadAllLocalBugs(c.repo)
202
203 for b := range allBugs {
204 if b.Err != nil {
205 return b.Err
206 }
207
208 snap := b.Bug.Compile()
209 c.excerpts[b.Bug.Id()] = NewBugExcerpt(b.Bug, &snap)
210 }
211
212 fmt.Println("Done.")
213 return nil
214}
215
216// ResolveBug retrieve a bug matching the exact given id
217func (c *RepoCache) ResolveBug(id string) (*BugCache, error) {
218 cached, ok := c.bugs[id]
219 if ok {
220 return cached, nil
221 }
222
223 b, err := bug.ReadLocalBug(c.repo, id)
224 if err != nil {
225 return nil, err
226 }
227
228 cached = NewBugCache(c, b)
229 c.bugs[id] = cached
230
231 return cached, nil
232}
233
234// ResolveBugPrefix retrieve a bug matching an id prefix. It fails if multiple
235// bugs match.
236func (c *RepoCache) ResolveBugPrefix(prefix string) (*BugCache, error) {
237 // preallocate but empty
238 matching := make([]string, 0, 5)
239
240 for id := range c.excerpts {
241 if strings.HasPrefix(id, prefix) {
242 matching = append(matching, id)
243 }
244 }
245
246 if len(matching) > 1 {
247 return nil, fmt.Errorf("Multiple matching bug found:\n%s", strings.Join(matching, "\n"))
248 }
249
250 if len(matching) == 0 {
251 return nil, bug.ErrBugNotExist
252 }
253
254 return c.ResolveBug(matching[0])
255}
256
257func (c *RepoCache) QueryBugs(query *Query) []string {
258 if query == nil {
259 return c.AllBugsIds()
260 }
261
262 var filtered []*BugExcerpt
263
264 for _, excerpt := range c.excerpts {
265 if query.Match(excerpt) {
266 filtered = append(filtered, excerpt)
267 }
268 }
269
270 var sorter sort.Interface
271
272 switch query.OrderBy {
273 case OrderById:
274 sorter = BugsById(filtered)
275 case OrderByCreation:
276 sorter = BugsByCreationTime(filtered)
277 case OrderByEdit:
278 sorter = BugsByEditTime(filtered)
279 default:
280 panic("missing sort type")
281 }
282
283 if query.OrderDirection == OrderDescending {
284 sorter = sort.Reverse(sorter)
285 }
286
287 sort.Sort(sorter)
288
289 result := make([]string, len(filtered))
290
291 for i, val := range filtered {
292 result[i] = val.Id
293 }
294
295 return result
296}
297
298// AllBugsIds return all known bug ids
299func (c *RepoCache) AllBugsIds() []string {
300 result := make([]string, len(c.excerpts))
301
302 i := 0
303 for _, excerpt := range c.excerpts {
304 result[i] = excerpt.Id
305 i++
306 }
307
308 return result
309}
310
311// ClearAllBugs clear all bugs kept in memory
312func (c *RepoCache) ClearAllBugs() {
313 c.bugs = make(map[string]*BugCache)
314}
315
316// ValidLabels list valid labels
317//
318// Note: in the future, a proper label policy could be implemented where valid
319// labels are defined in a configuration file. Until that, the default behavior
320// is to return the list of labels already used.
321func (c *RepoCache) ValidLabels() []bug.Label {
322 set := map[bug.Label]interface{}{}
323
324 for _, excerpt := range c.excerpts {
325 for _, l := range excerpt.Labels {
326 set[l] = nil
327 }
328 }
329
330 result := make([]bug.Label, len(set))
331
332 i := 0
333 for l := range set {
334 result[i] = l
335 i++
336 }
337
338 // Sort
339 sort.Slice(result, func(i, j int) bool {
340 return string(result[i]) < string(result[j])
341 })
342
343 return result
344}
345
346// NewBug create a new bug
347// The new bug is written in the repository (commit)
348func (c *RepoCache) NewBug(title string, message string) (*BugCache, error) {
349 return c.NewBugWithFiles(title, message, nil)
350}
351
352// NewBugWithFiles create a new bug with attached files for the message
353// The new bug is written in the repository (commit)
354func (c *RepoCache) NewBugWithFiles(title string, message string, files []git.Hash) (*BugCache, error) {
355 author, err := bug.GetUser(c.repo)
356 if err != nil {
357 return nil, err
358 }
359
360 return c.NewBugRaw(author, time.Now().Unix(), title, message, files, nil)
361}
362
363// NewBugWithFilesMeta create a new bug with attached files for the message, as
364// well as metadata for the Create operation.
365// The new bug is written in the repository (commit)
366func (c *RepoCache) NewBugRaw(author bug.Person, unixTime int64, title string, message string, files []git.Hash, metadata map[string]string) (*BugCache, error) {
367 b, err := bug.CreateWithFiles(author, unixTime, title, message, files)
368 if err != nil {
369 return nil, err
370 }
371
372 for key, value := range metadata {
373 b.FirstOp().SetMetadata(key, value)
374 }
375
376 err = b.Commit(c.repo)
377 if err != nil {
378 return nil, err
379 }
380
381 cached := NewBugCache(c, b)
382 c.bugs[b.Id()] = cached
383
384 err = c.bugUpdated(b.Id())
385 if err != nil {
386 return nil, err
387 }
388
389 return cached, nil
390}
391
392// Fetch retrieve update from a remote
393// This does not change the local bugs state
394func (c *RepoCache) Fetch(remote string) (string, error) {
395 return bug.Fetch(c.repo, remote)
396}
397
398// MergeAll will merge all the available remote bug
399func (c *RepoCache) MergeAll(remote string) <-chan bug.MergeResult {
400 out := make(chan bug.MergeResult)
401
402 // Intercept merge results to update the cache properly
403 go func() {
404 defer close(out)
405
406 results := bug.MergeAll(c.repo, remote)
407 for result := range results {
408 out <- result
409
410 if result.Err != nil {
411 continue
412 }
413
414 id := result.Id
415
416 switch result.Status {
417 case bug.MergeStatusNew, bug.MergeStatusUpdated:
418 b := result.Bug
419 snap := b.Compile()
420 c.excerpts[id] = NewBugExcerpt(b, &snap)
421 }
422 }
423
424 err := c.write()
425
426 // No easy way out here ..
427 if err != nil {
428 panic(err)
429 }
430 }()
431
432 return out
433}
434
435// Push update a remote with the local changes
436func (c *RepoCache) Push(remote string) (string, error) {
437 return bug.Push(c.repo, remote)
438}
439
440func repoLockFilePath(repo repository.Repo) string {
441 return path.Join(repo.GetPath(), ".git", "git-bug", lockfile)
442}
443
444// repoIsAvailable check is the given repository is locked by a Cache.
445// Note: this is a smart function that will cleanup the lock file if the
446// corresponding process is not there anymore.
447// If no error is returned, the repo is free to edit.
448func repoIsAvailable(repo repository.Repo) error {
449 lockPath := repoLockFilePath(repo)
450
451 // Todo: this leave way for a racey access to the repo between the test
452 // if the file exist and the actual write. It's probably not a problem in
453 // practice because using a repository will be done from user interaction
454 // or in a context where a single instance of git-bug is already guaranteed
455 // (say, a server with the web UI running). But still, that might be nice to
456 // have a mutex or something to guard that.
457
458 // Todo: this will fail if somehow the filesystem is shared with another
459 // computer. Should add a configuration that prevent the cleaning of the
460 // lock file
461
462 f, err := os.Open(lockPath)
463
464 if err != nil && !os.IsNotExist(err) {
465 return err
466 }
467
468 if err == nil {
469 // lock file already exist
470 buf, err := ioutil.ReadAll(io.LimitReader(f, 10))
471 if err != nil {
472 return err
473 }
474 if len(buf) == 10 {
475 return fmt.Errorf("the lock file should be < 10 bytes")
476 }
477
478 pid, err := strconv.Atoi(string(buf))
479 if err != nil {
480 return err
481 }
482
483 if process.IsRunning(pid) {
484 return fmt.Errorf("the repository you want to access is already locked by the process pid %d", pid)
485 }
486
487 // The lock file is just laying there after a crash, clean it
488
489 fmt.Println("A lock file is present but the corresponding process is not, removing it.")
490 err = f.Close()
491 if err != nil {
492 return err
493 }
494
495 os.Remove(lockPath)
496 if err != nil {
497 return err
498 }
499 }
500
501 return nil
502}