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