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