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.Repo
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.Repo) (*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// Repository return the underlying repository.
59// If you use this, make sure to never change the repo state.
60func (c *RepoCache) Repository() repository.Repo {
61 return c.repo
62}
63
64func (c *RepoCache) lock() error {
65 lockPath := repoLockFilePath(c.repo)
66
67 err := repoIsAvailable(c.repo)
68 if err != nil {
69 return err
70 }
71
72 f, err := os.Create(lockPath)
73 if err != nil {
74 return err
75 }
76
77 pid := fmt.Sprintf("%d", os.Getpid())
78 _, err = f.WriteString(pid)
79 if err != nil {
80 return err
81 }
82
83 return f.Close()
84}
85
86func (c *RepoCache) Close() error {
87 lockPath := repoLockFilePath(c.repo)
88 return os.Remove(lockPath)
89}
90
91// bugUpdated is a callback to trigger when the excerpt of a bug changed,
92// that is each time a bug is updated
93func (c *RepoCache) bugUpdated(id string) error {
94 b, ok := c.bugs[id]
95 if !ok {
96 panic("missing bug in the cache")
97 }
98
99 c.excerpts[id] = NewBugExcerpt(b.bug, b.Snapshot())
100
101 return c.write()
102}
103
104// load will try to read from the disk the bug cache file
105func (c *RepoCache) load() error {
106 f, err := os.Open(cacheFilePath(c.repo))
107 if err != nil {
108 return err
109 }
110
111 decoder := gob.NewDecoder(f)
112
113 aux := struct {
114 Version uint
115 Excerpts map[string]*BugExcerpt
116 }{}
117
118 err = decoder.Decode(&aux)
119 if err != nil {
120 return err
121 }
122
123 if aux.Version != 1 {
124 return fmt.Errorf("unknown cache format version %v", aux.Version)
125 }
126
127 c.excerpts = aux.Excerpts
128 return nil
129}
130
131// write will serialize on disk the bug cache file
132func (c *RepoCache) write() error {
133 var data bytes.Buffer
134
135 aux := struct {
136 Version uint
137 Excerpts map[string]*BugExcerpt
138 }{
139 Version: formatVersion,
140 Excerpts: c.excerpts,
141 }
142
143 encoder := gob.NewEncoder(&data)
144
145 err := encoder.Encode(aux)
146 if err != nil {
147 return err
148 }
149
150 f, err := os.Create(cacheFilePath(c.repo))
151 if err != nil {
152 return err
153 }
154
155 _, err = f.Write(data.Bytes())
156 if err != nil {
157 return err
158 }
159
160 return f.Close()
161}
162
163func cacheFilePath(repo repository.Repo) string {
164 return path.Join(repo.GetPath(), ".git", "git-bug", cacheFile)
165}
166
167func (c *RepoCache) buildCache() error {
168 fmt.Printf("Building bug cache... ")
169
170 c.excerpts = make(map[string]*BugExcerpt)
171
172 allBugs := bug.ReadAllLocalBugs(c.repo)
173
174 for b := range allBugs {
175 if b.Err != nil {
176 return b.Err
177 }
178
179 snap := b.Bug.Compile()
180 c.excerpts[b.Bug.Id()] = NewBugExcerpt(b.Bug, &snap)
181 }
182
183 fmt.Println("Done.")
184 return nil
185}
186
187func (c *RepoCache) ResolveBug(id string) (*BugCache, error) {
188 cached, ok := c.bugs[id]
189 if ok {
190 return cached, nil
191 }
192
193 b, err := bug.ReadLocalBug(c.repo, id)
194 if err != nil {
195 return nil, err
196 }
197
198 cached = NewBugCache(c, b)
199 c.bugs[id] = cached
200
201 return cached, nil
202}
203
204func (c *RepoCache) ResolveBugPrefix(prefix string) (*BugCache, error) {
205 // preallocate but empty
206 matching := make([]string, 0, 5)
207
208 for id := range c.excerpts {
209 if strings.HasPrefix(id, prefix) {
210 matching = append(matching, id)
211 }
212 }
213
214 if len(matching) > 1 {
215 return nil, fmt.Errorf("Multiple matching bug found:\n%s", strings.Join(matching, "\n"))
216 }
217
218 if len(matching) == 0 {
219 return nil, bug.ErrBugNotExist
220 }
221
222 return c.ResolveBug(matching[0])
223}
224
225func (c *RepoCache) QueryBugs(query *Query) []string {
226 if query == nil {
227 return c.AllBugsIds()
228 }
229
230 var filtered []*BugExcerpt
231
232 for _, excerpt := range c.excerpts {
233 if query.Match(excerpt) {
234 filtered = append(filtered, excerpt)
235 }
236 }
237
238 var sorter sort.Interface
239
240 switch query.OrderBy {
241 case OrderById:
242 sorter = BugsById(filtered)
243 case OrderByCreation:
244 sorter = BugsByCreationTime(filtered)
245 case OrderByEdit:
246 sorter = BugsByEditTime(filtered)
247 default:
248 panic("missing sort type")
249 }
250
251 if query.OrderDirection == OrderDescending {
252 sorter = sort.Reverse(sorter)
253 }
254
255 sort.Sort(sorter)
256
257 result := make([]string, len(filtered))
258
259 for i, val := range filtered {
260 result[i] = val.Id
261 }
262
263 return result
264}
265
266// AllBugsIds return all known bug ids
267func (c *RepoCache) AllBugsIds() []string {
268 result := make([]string, len(c.excerpts))
269
270 i := 0
271 for _, excerpt := range c.excerpts {
272 result[i] = excerpt.Id
273 i++
274 }
275
276 return result
277}
278
279// ClearAllBugs clear all bugs kept in memory
280func (c *RepoCache) ClearAllBugs() {
281 c.bugs = make(map[string]*BugCache)
282}
283
284// NewBug create a new bug
285// The new bug is written in the repository (commit)
286func (c *RepoCache) NewBug(title string, message string) (*BugCache, error) {
287 return c.NewBugWithFiles(title, message, nil)
288}
289
290// NewBugWithFiles create a new bug with attached files for the message
291// The new bug is written in the repository (commit)
292func (c *RepoCache) NewBugWithFiles(title string, message string, files []git.Hash) (*BugCache, error) {
293 author, err := bug.GetUser(c.repo)
294 if err != nil {
295 return nil, err
296 }
297
298 b, err := operations.CreateWithFiles(author, title, message, files)
299 if err != nil {
300 return nil, err
301 }
302
303 err = b.Commit(c.repo)
304 if err != nil {
305 return nil, err
306 }
307
308 cached := NewBugCache(c, b)
309 c.bugs[b.Id()] = cached
310
311 err = c.bugUpdated(b.Id())
312 if err != nil {
313 return nil, err
314 }
315
316 return cached, nil
317}
318
319// Fetch retrieve update from a remote
320// This does not change the local bugs state
321func (c *RepoCache) Fetch(remote string) (string, error) {
322 return bug.Fetch(c.repo, remote)
323}
324
325// MergeAll will merge all the available remote bug
326func (c *RepoCache) MergeAll(remote string) <-chan bug.MergeResult {
327 out := make(chan bug.MergeResult)
328
329 // Intercept merge results to update the cache properly
330 go func() {
331 defer close(out)
332
333 results := bug.MergeAll(c.repo, remote)
334 for result := range results {
335 out <- result
336
337 if result.Err != nil {
338 continue
339 }
340
341 id := result.Id
342
343 switch result.Status {
344 case bug.MergeStatusNew, bug.MergeStatusUpdated:
345 b := result.Bug
346 snap := b.Compile()
347 c.excerpts[id] = NewBugExcerpt(b, &snap)
348 }
349 }
350
351 err := c.write()
352
353 // No easy way out here ..
354 if err != nil {
355 panic(err)
356 }
357 }()
358
359 return out
360}
361
362// Push update a remote with the local changes
363func (c *RepoCache) Push(remote string) (string, error) {
364 return bug.Push(c.repo, remote)
365}
366
367func repoLockFilePath(repo repository.Repo) string {
368 return path.Join(repo.GetPath(), ".git", "git-bug", lockfile)
369}
370
371// repoIsAvailable check is the given repository is locked by a Cache.
372// Note: this is a smart function that will cleanup the lock file if the
373// corresponding process is not there anymore.
374// If no error is returned, the repo is free to edit.
375func repoIsAvailable(repo repository.Repo) error {
376 lockPath := repoLockFilePath(repo)
377
378 // Todo: this leave way for a racey access to the repo between the test
379 // if the file exist and the actual write. It's probably not a problem in
380 // practice because using a repository will be done from user interaction
381 // or in a context where a single instance of git-bug is already guaranteed
382 // (say, a server with the web UI running). But still, that might be nice to
383 // have a mutex or something to guard that.
384
385 // Todo: this will fail if somehow the filesystem is shared with another
386 // computer. Should add a configuration that prevent the cleaning of the
387 // lock file
388
389 f, err := os.Open(lockPath)
390
391 if err != nil && !os.IsNotExist(err) {
392 return err
393 }
394
395 if err == nil {
396 // lock file already exist
397 buf, err := ioutil.ReadAll(io.LimitReader(f, 10))
398 if err != nil {
399 return err
400 }
401 if len(buf) == 10 {
402 return fmt.Errorf("the lock file should be < 10 bytes")
403 }
404
405 pid, err := strconv.Atoi(string(buf))
406 if err != nil {
407 return err
408 }
409
410 if process.IsRunning(pid) {
411 return fmt.Errorf("the repository you want to access is already locked by the process pid %d", pid)
412 }
413
414 // The lock file is just laying there after a crash, clean it
415
416 fmt.Println("A lock file is present but the corresponding process is not, removing it.")
417 err = f.Close()
418 if err != nil {
419 return err
420 }
421
422 os.Remove(lockPath)
423 if err != nil {
424 return err
425 }
426 }
427
428 return nil
429}