repo_cache.go

  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// ValidLabels list valid labels
285//
286// Note: in the future, a proper label policy could be implemented where valid
287// labels are defined in a configuration file. Until that, the default behavior
288// is to return the list of labels already used.
289func (c *RepoCache) ValidLabels() []bug.Label {
290	set := map[bug.Label]interface{}{}
291
292	for _, excerpt := range c.excerpts {
293		for _, l := range excerpt.Labels {
294			set[l] = nil
295		}
296	}
297
298	result := make([]bug.Label, len(set))
299
300	i := 0
301	for l := range set {
302		result[i] = l
303		i++
304	}
305
306	// Sort
307	sort.Slice(result, func(i, j int) bool {
308		return string(result[i]) < string(result[j])
309	})
310
311	return result
312}
313
314// NewBug create a new bug
315// The new bug is written in the repository (commit)
316func (c *RepoCache) NewBug(title string, message string) (*BugCache, error) {
317	return c.NewBugWithFiles(title, message, nil)
318}
319
320// NewBugWithFiles create a new bug with attached files for the message
321// The new bug is written in the repository (commit)
322func (c *RepoCache) NewBugWithFiles(title string, message string, files []git.Hash) (*BugCache, error) {
323	author, err := bug.GetUser(c.repo)
324	if err != nil {
325		return nil, err
326	}
327
328	b, err := operations.CreateWithFiles(author, title, message, files)
329	if err != nil {
330		return nil, err
331	}
332
333	err = b.Commit(c.repo)
334	if err != nil {
335		return nil, err
336	}
337
338	cached := NewBugCache(c, b)
339	c.bugs[b.Id()] = cached
340
341	err = c.bugUpdated(b.Id())
342	if err != nil {
343		return nil, err
344	}
345
346	return cached, nil
347}
348
349// Fetch retrieve update from a remote
350// This does not change the local bugs state
351func (c *RepoCache) Fetch(remote string) (string, error) {
352	return bug.Fetch(c.repo, remote)
353}
354
355// MergeAll will merge all the available remote bug
356func (c *RepoCache) MergeAll(remote string) <-chan bug.MergeResult {
357	out := make(chan bug.MergeResult)
358
359	// Intercept merge results to update the cache properly
360	go func() {
361		defer close(out)
362
363		results := bug.MergeAll(c.repo, remote)
364		for result := range results {
365			out <- result
366
367			if result.Err != nil {
368				continue
369			}
370
371			id := result.Id
372
373			switch result.Status {
374			case bug.MergeStatusNew, bug.MergeStatusUpdated:
375				b := result.Bug
376				snap := b.Compile()
377				c.excerpts[id] = NewBugExcerpt(b, &snap)
378			}
379		}
380
381		err := c.write()
382
383		// No easy way out here ..
384		if err != nil {
385			panic(err)
386		}
387	}()
388
389	return out
390}
391
392// Push update a remote with the local changes
393func (c *RepoCache) Push(remote string) (string, error) {
394	return bug.Push(c.repo, remote)
395}
396
397func repoLockFilePath(repo repository.Repo) string {
398	return path.Join(repo.GetPath(), ".git", "git-bug", lockfile)
399}
400
401// repoIsAvailable check is the given repository is locked by a Cache.
402// Note: this is a smart function that will cleanup the lock file if the
403// corresponding process is not there anymore.
404// If no error is returned, the repo is free to edit.
405func repoIsAvailable(repo repository.Repo) error {
406	lockPath := repoLockFilePath(repo)
407
408	// Todo: this leave way for a racey access to the repo between the test
409	// if the file exist and the actual write. It's probably not a problem in
410	// practice because using a repository will be done from user interaction
411	// or in a context where a single instance of git-bug is already guaranteed
412	// (say, a server with the web UI running). But still, that might be nice to
413	// have a mutex or something to guard that.
414
415	// Todo: this will fail if somehow the filesystem is shared with another
416	// computer. Should add a configuration that prevent the cleaning of the
417	// lock file
418
419	f, err := os.Open(lockPath)
420
421	if err != nil && !os.IsNotExist(err) {
422		return err
423	}
424
425	if err == nil {
426		// lock file already exist
427		buf, err := ioutil.ReadAll(io.LimitReader(f, 10))
428		if err != nil {
429			return err
430		}
431		if len(buf) == 10 {
432			return fmt.Errorf("the lock file should be < 10 bytes")
433		}
434
435		pid, err := strconv.Atoi(string(buf))
436		if err != nil {
437			return err
438		}
439
440		if process.IsRunning(pid) {
441			return fmt.Errorf("the repository you want to access is already locked by the process pid %d", pid)
442		}
443
444		// The lock file is just laying there after a crash, clean it
445
446		fmt.Println("A lock file is present but the corresponding process is not, removing it.")
447		err = f.Close()
448		if err != nil {
449			return err
450		}
451
452		os.Remove(lockPath)
453		if err != nil {
454			return err
455		}
456	}
457
458	return nil
459}