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
15 "github.com/MichaelMure/git-bug/bug"
16 "github.com/MichaelMure/git-bug/operations"
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
216func (c *RepoCache) ResolveBug(id string) (*BugCache, error) {
217 cached, ok := c.bugs[id]
218 if ok {
219 return cached, nil
220 }
221
222 b, err := bug.ReadLocalBug(c.repo, id)
223 if err != nil {
224 return nil, err
225 }
226
227 cached = NewBugCache(c, b)
228 c.bugs[id] = cached
229
230 return cached, nil
231}
232
233func (c *RepoCache) ResolveBugPrefix(prefix string) (*BugCache, error) {
234 // preallocate but empty
235 matching := make([]string, 0, 5)
236
237 for id := range c.excerpts {
238 if strings.HasPrefix(id, prefix) {
239 matching = append(matching, id)
240 }
241 }
242
243 if len(matching) > 1 {
244 return nil, fmt.Errorf("Multiple matching bug found:\n%s", strings.Join(matching, "\n"))
245 }
246
247 if len(matching) == 0 {
248 return nil, bug.ErrBugNotExist
249 }
250
251 return c.ResolveBug(matching[0])
252}
253
254func (c *RepoCache) QueryBugs(query *Query) []string {
255 if query == nil {
256 return c.AllBugsIds()
257 }
258
259 var filtered []*BugExcerpt
260
261 for _, excerpt := range c.excerpts {
262 if query.Match(excerpt) {
263 filtered = append(filtered, excerpt)
264 }
265 }
266
267 var sorter sort.Interface
268
269 switch query.OrderBy {
270 case OrderById:
271 sorter = BugsById(filtered)
272 case OrderByCreation:
273 sorter = BugsByCreationTime(filtered)
274 case OrderByEdit:
275 sorter = BugsByEditTime(filtered)
276 default:
277 panic("missing sort type")
278 }
279
280 if query.OrderDirection == OrderDescending {
281 sorter = sort.Reverse(sorter)
282 }
283
284 sort.Sort(sorter)
285
286 result := make([]string, len(filtered))
287
288 for i, val := range filtered {
289 result[i] = val.Id
290 }
291
292 return result
293}
294
295// AllBugsIds return all known bug ids
296func (c *RepoCache) AllBugsIds() []string {
297 result := make([]string, len(c.excerpts))
298
299 i := 0
300 for _, excerpt := range c.excerpts {
301 result[i] = excerpt.Id
302 i++
303 }
304
305 return result
306}
307
308// ClearAllBugs clear all bugs kept in memory
309func (c *RepoCache) ClearAllBugs() {
310 c.bugs = make(map[string]*BugCache)
311}
312
313// ValidLabels list valid labels
314//
315// Note: in the future, a proper label policy could be implemented where valid
316// labels are defined in a configuration file. Until that, the default behavior
317// is to return the list of labels already used.
318func (c *RepoCache) ValidLabels() []bug.Label {
319 set := map[bug.Label]interface{}{}
320
321 for _, excerpt := range c.excerpts {
322 for _, l := range excerpt.Labels {
323 set[l] = nil
324 }
325 }
326
327 result := make([]bug.Label, len(set))
328
329 i := 0
330 for l := range set {
331 result[i] = l
332 i++
333 }
334
335 // Sort
336 sort.Slice(result, func(i, j int) bool {
337 return string(result[i]) < string(result[j])
338 })
339
340 return result
341}
342
343// NewBug create a new bug
344// The new bug is written in the repository (commit)
345func (c *RepoCache) NewBug(title string, message string) (*BugCache, error) {
346 return c.NewBugWithFiles(title, message, nil)
347}
348
349// NewBugWithFiles create a new bug with attached files for the message
350// The new bug is written in the repository (commit)
351func (c *RepoCache) NewBugWithFiles(title string, message string, files []git.Hash) (*BugCache, error) {
352 author, err := bug.GetUser(c.repo)
353 if err != nil {
354 return nil, err
355 }
356
357 b, err := operations.CreateWithFiles(author, title, message, files)
358 if err != nil {
359 return nil, err
360 }
361
362 err = b.Commit(c.repo)
363 if err != nil {
364 return nil, err
365 }
366
367 cached := NewBugCache(c, b)
368 c.bugs[b.Id()] = cached
369
370 err = c.bugUpdated(b.Id())
371 if err != nil {
372 return nil, err
373 }
374
375 return cached, nil
376}
377
378// Fetch retrieve update from a remote
379// This does not change the local bugs state
380func (c *RepoCache) Fetch(remote string) (string, error) {
381 return bug.Fetch(c.repo, remote)
382}
383
384// MergeAll will merge all the available remote bug
385func (c *RepoCache) MergeAll(remote string) <-chan bug.MergeResult {
386 out := make(chan bug.MergeResult)
387
388 // Intercept merge results to update the cache properly
389 go func() {
390 defer close(out)
391
392 results := bug.MergeAll(c.repo, remote)
393 for result := range results {
394 out <- result
395
396 if result.Err != nil {
397 continue
398 }
399
400 id := result.Id
401
402 switch result.Status {
403 case bug.MergeStatusNew, bug.MergeStatusUpdated:
404 b := result.Bug
405 snap := b.Compile()
406 c.excerpts[id] = NewBugExcerpt(b, &snap)
407 }
408 }
409
410 err := c.write()
411
412 // No easy way out here ..
413 if err != nil {
414 panic(err)
415 }
416 }()
417
418 return out
419}
420
421// Push update a remote with the local changes
422func (c *RepoCache) Push(remote string) (string, error) {
423 return bug.Push(c.repo, remote)
424}
425
426func repoLockFilePath(repo repository.Repo) string {
427 return path.Join(repo.GetPath(), ".git", "git-bug", lockfile)
428}
429
430// repoIsAvailable check is the given repository is locked by a Cache.
431// Note: this is a smart function that will cleanup the lock file if the
432// corresponding process is not there anymore.
433// If no error is returned, the repo is free to edit.
434func repoIsAvailable(repo repository.Repo) error {
435 lockPath := repoLockFilePath(repo)
436
437 // Todo: this leave way for a racey access to the repo between the test
438 // if the file exist and the actual write. It's probably not a problem in
439 // practice because using a repository will be done from user interaction
440 // or in a context where a single instance of git-bug is already guaranteed
441 // (say, a server with the web UI running). But still, that might be nice to
442 // have a mutex or something to guard that.
443
444 // Todo: this will fail if somehow the filesystem is shared with another
445 // computer. Should add a configuration that prevent the cleaning of the
446 // lock file
447
448 f, err := os.Open(lockPath)
449
450 if err != nil && !os.IsNotExist(err) {
451 return err
452 }
453
454 if err == nil {
455 // lock file already exist
456 buf, err := ioutil.ReadAll(io.LimitReader(f, 10))
457 if err != nil {
458 return err
459 }
460 if len(buf) == 10 {
461 return fmt.Errorf("the lock file should be < 10 bytes")
462 }
463
464 pid, err := strconv.Atoi(string(buf))
465 if err != nil {
466 return err
467 }
468
469 if process.IsRunning(pid) {
470 return fmt.Errorf("the repository you want to access is already locked by the process pid %d", pid)
471 }
472
473 // The lock file is just laying there after a crash, clean it
474
475 fmt.Println("A lock file is present but the corresponding process is not, removing it.")
476 err = f.Close()
477 if err != nil {
478 return err
479 }
480
481 os.Remove(lockPath)
482 if err != nil {
483 return err
484 }
485 }
486
487 return nil
488}