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