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