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