2018-08-21 19:46:53 +03:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2018-08-23 22:24:57 +03:00
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
2018-08-21 19:46:53 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-08-31 14:18:03 +03:00
|
|
|
"io/ioutil"
|
2018-08-23 22:24:57 +03:00
|
|
|
"os"
|
|
|
|
"path"
|
2018-09-09 21:21:49 +03:00
|
|
|
"sort"
|
2018-08-31 14:18:03 +03:00
|
|
|
"strconv"
|
2018-08-21 19:46:53 +03:00
|
|
|
"strings"
|
2018-09-25 18:56:58 +03:00
|
|
|
"time"
|
2018-08-21 19:46:53 +03:00
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/bug"
|
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2018-09-11 23:04:16 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/git"
|
|
|
|
"github.com/MichaelMure/git-bug/util/process"
|
2018-08-21 19:46:53 +03:00
|
|
|
)
|
|
|
|
|
2018-09-13 18:59:02 +03:00
|
|
|
const cacheFile = "cache"
|
|
|
|
const formatVersion = 1
|
|
|
|
|
2018-08-21 19:46:53 +03:00
|
|
|
type RepoCache struct {
|
2018-09-11 20:28:32 +03:00
|
|
|
// the underlying repo
|
2018-09-21 19:18:51 +03:00
|
|
|
repo repository.ClockedRepo
|
2018-09-11 20:28:32 +03:00
|
|
|
// excerpt of bugs data for all bugs
|
2018-09-09 21:21:49 +03:00
|
|
|
excerpts map[string]*BugExcerpt
|
2018-09-11 20:28:32 +03:00
|
|
|
// bug loaded in memory
|
|
|
|
bugs map[string]*BugCache
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-09-21 19:18:51 +03:00
|
|
|
func NewRepoCache(r repository.ClockedRepo) (*RepoCache, error) {
|
2018-08-23 22:24:57 +03:00
|
|
|
c := &RepoCache{
|
2018-08-21 19:46:53 +03:00
|
|
|
repo: r,
|
2018-08-23 22:24:57 +03:00
|
|
|
bugs: make(map[string]*BugCache),
|
|
|
|
}
|
|
|
|
|
2018-08-31 14:18:03 +03:00
|
|
|
err := c.lock()
|
|
|
|
if err != nil {
|
|
|
|
return &RepoCache{}, err
|
|
|
|
}
|
2018-08-23 22:24:57 +03:00
|
|
|
|
2018-09-13 18:59:02 +03:00
|
|
|
err = c.load()
|
2018-08-23 22:24:57 +03:00
|
|
|
if err == nil {
|
|
|
|
return c, nil
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
2018-08-23 22:24:57 +03:00
|
|
|
|
2018-09-19 00:36:45 +03:00
|
|
|
err = c.buildCache()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-23 22:24:57 +03:00
|
|
|
|
2018-09-13 18:59:02 +03:00
|
|
|
return c, c.write()
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-09-21 19:18:51 +03:00
|
|
|
// GetPath returns the path to the repo.
|
|
|
|
func (c *RepoCache) GetPath() string {
|
|
|
|
return c.repo.GetPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPath returns the path to the repo.
|
|
|
|
func (c *RepoCache) GetCoreEditor() (string, error) {
|
|
|
|
return c.repo.GetCoreEditor()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserName returns the name the the user has used to configure git
|
|
|
|
func (c *RepoCache) GetUserName() (string, error) {
|
|
|
|
return c.repo.GetUserName()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserEmail returns the email address that the user has used to configure git.
|
|
|
|
func (c *RepoCache) GetUserEmail() (string, error) {
|
|
|
|
return c.repo.GetUserEmail()
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-09-24 17:24:38 +03:00
|
|
|
// StoreConfig store a single key/value pair in the config of the repo
|
2018-09-24 16:21:34 +03:00
|
|
|
func (c *RepoCache) StoreConfig(key string, value string) error {
|
|
|
|
return c.repo.StoreConfig(key, value)
|
|
|
|
}
|
|
|
|
|
2018-09-24 17:24:38 +03:00
|
|
|
// ReadConfigs read all key/value pair matching the key prefix
|
2018-09-24 16:21:34 +03:00
|
|
|
func (c *RepoCache) ReadConfigs(keyPrefix string) (map[string]string, error) {
|
|
|
|
return c.repo.ReadConfigs(keyPrefix)
|
|
|
|
}
|
|
|
|
|
2018-09-24 17:24:38 +03:00
|
|
|
// RmConfigs remove all key/value pair matching the key prefix
|
|
|
|
func (c *RepoCache) RmConfigs(keyPrefix string) error {
|
|
|
|
return c.repo.RmConfigs(keyPrefix)
|
|
|
|
}
|
|
|
|
|
2018-08-31 14:18:03 +03:00
|
|
|
func (c *RepoCache) lock() error {
|
|
|
|
lockPath := repoLockFilePath(c.repo)
|
|
|
|
|
|
|
|
err := repoIsAvailable(c.repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(lockPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pid := fmt.Sprintf("%d", os.Getpid())
|
|
|
|
_, err = f.WriteString(pid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RepoCache) Close() error {
|
|
|
|
lockPath := repoLockFilePath(c.repo)
|
|
|
|
return os.Remove(lockPath)
|
|
|
|
}
|
|
|
|
|
2018-08-23 22:24:57 +03:00
|
|
|
// bugUpdated is a callback to trigger when the excerpt of a bug changed,
|
|
|
|
// that is each time a bug is updated
|
|
|
|
func (c *RepoCache) bugUpdated(id string) error {
|
|
|
|
b, ok := c.bugs[id]
|
|
|
|
if !ok {
|
|
|
|
panic("missing bug in the cache")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.excerpts[id] = NewBugExcerpt(b.bug, b.Snapshot())
|
|
|
|
|
2018-09-13 18:59:02 +03:00
|
|
|
return c.write()
|
2018-08-23 22:24:57 +03:00
|
|
|
}
|
|
|
|
|
2018-09-13 18:59:02 +03:00
|
|
|
// load will try to read from the disk the bug cache file
|
|
|
|
func (c *RepoCache) load() error {
|
|
|
|
f, err := os.Open(cacheFilePath(c.repo))
|
2018-08-23 22:24:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder := gob.NewDecoder(f)
|
|
|
|
|
2018-09-13 18:59:02 +03:00
|
|
|
aux := struct {
|
|
|
|
Version uint
|
|
|
|
Excerpts map[string]*BugExcerpt
|
|
|
|
}{}
|
2018-08-23 22:24:57 +03:00
|
|
|
|
2018-09-13 18:59:02 +03:00
|
|
|
err = decoder.Decode(&aux)
|
2018-08-23 22:24:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-13 18:59:02 +03:00
|
|
|
if aux.Version != 1 {
|
|
|
|
return fmt.Errorf("unknown cache format version %v", aux.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.excerpts = aux.Excerpts
|
2018-08-23 22:24:57 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-13 18:59:02 +03:00
|
|
|
// write will serialize on disk the bug cache file
|
|
|
|
func (c *RepoCache) write() error {
|
2018-08-23 22:24:57 +03:00
|
|
|
var data bytes.Buffer
|
|
|
|
|
2018-09-13 18:59:02 +03:00
|
|
|
aux := struct {
|
|
|
|
Version uint
|
|
|
|
Excerpts map[string]*BugExcerpt
|
|
|
|
}{
|
|
|
|
Version: formatVersion,
|
|
|
|
Excerpts: c.excerpts,
|
|
|
|
}
|
|
|
|
|
2018-08-23 22:24:57 +03:00
|
|
|
encoder := gob.NewEncoder(&data)
|
|
|
|
|
2018-09-13 18:59:02 +03:00
|
|
|
err := encoder.Encode(aux)
|
2018-08-23 22:24:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-13 18:59:02 +03:00
|
|
|
f, err := os.Create(cacheFilePath(c.repo))
|
2018-08-23 22:24:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = f.Write(data.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.Close()
|
|
|
|
}
|
|
|
|
|
2018-09-13 18:59:02 +03:00
|
|
|
func cacheFilePath(repo repository.Repo) string {
|
|
|
|
return path.Join(repo.GetPath(), ".git", "git-bug", cacheFile)
|
2018-08-23 22:24:57 +03:00
|
|
|
}
|
|
|
|
|
2018-09-19 00:36:45 +03:00
|
|
|
func (c *RepoCache) buildCache() error {
|
2018-09-09 21:21:49 +03:00
|
|
|
fmt.Printf("Building bug cache... ")
|
|
|
|
|
|
|
|
c.excerpts = make(map[string]*BugExcerpt)
|
2018-08-23 22:24:57 +03:00
|
|
|
|
|
|
|
allBugs := bug.ReadAllLocalBugs(c.repo)
|
|
|
|
|
|
|
|
for b := range allBugs {
|
2018-09-19 00:36:45 +03:00
|
|
|
if b.Err != nil {
|
|
|
|
return b.Err
|
|
|
|
}
|
|
|
|
|
2018-08-23 22:24:57 +03:00
|
|
|
snap := b.Bug.Compile()
|
|
|
|
c.excerpts[b.Bug.Id()] = NewBugExcerpt(b.Bug, &snap)
|
|
|
|
}
|
2018-09-02 17:36:48 +03:00
|
|
|
|
2018-09-09 21:21:49 +03:00
|
|
|
fmt.Println("Done.")
|
2018-09-19 00:36:45 +03:00
|
|
|
return nil
|
2018-09-02 17:36:48 +03:00
|
|
|
}
|
|
|
|
|
2018-10-01 22:47:12 +03:00
|
|
|
// ResolveBug retrieve a bug matching the exact given id
|
2018-08-23 22:24:57 +03:00
|
|
|
func (c *RepoCache) ResolveBug(id string) (*BugCache, error) {
|
2018-08-21 19:46:53 +03:00
|
|
|
cached, ok := c.bugs[id]
|
|
|
|
if ok {
|
|
|
|
return cached, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := bug.ReadLocalBug(c.repo, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-08-23 22:24:57 +03:00
|
|
|
cached = NewBugCache(c, b)
|
2018-08-21 19:46:53 +03:00
|
|
|
c.bugs[id] = cached
|
|
|
|
|
|
|
|
return cached, nil
|
|
|
|
}
|
|
|
|
|
2018-10-01 22:47:12 +03:00
|
|
|
// ResolveBugPrefix retrieve a bug matching an id prefix. It fails if multiple
|
|
|
|
// bugs match.
|
2018-08-23 22:24:57 +03:00
|
|
|
func (c *RepoCache) ResolveBugPrefix(prefix string) (*BugCache, error) {
|
2018-08-21 19:46:53 +03:00
|
|
|
// preallocate but empty
|
|
|
|
matching := make([]string, 0, 5)
|
|
|
|
|
2018-09-11 18:11:16 +03:00
|
|
|
for id := range c.excerpts {
|
2018-08-21 19:46:53 +03:00
|
|
|
if strings.HasPrefix(id, prefix) {
|
|
|
|
matching = append(matching, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matching) > 1 {
|
2018-10-02 00:34:45 +03:00
|
|
|
return nil, bug.ErrMultipleMatch{Matching: matching}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matching) == 0 {
|
|
|
|
return nil, bug.ErrBugNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.ResolveBug(matching[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveBugCreateMetadata retrieve a bug that has the exact given metadata on
|
|
|
|
// its Create operation, that is, the first operation. It fails if multiple bugs
|
|
|
|
// match.
|
|
|
|
func (c *RepoCache) ResolveBugCreateMetadata(key string, value string) (*BugCache, error) {
|
|
|
|
// preallocate but empty
|
|
|
|
matching := make([]string, 0, 5)
|
|
|
|
|
|
|
|
for id, excerpt := range c.excerpts {
|
|
|
|
if excerpt.CreateMetadata[key] == value {
|
|
|
|
matching = append(matching, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matching) > 1 {
|
|
|
|
return nil, bug.ErrMultipleMatch{Matching: matching}
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-09-18 13:49:16 +03:00
|
|
|
if len(matching) == 0 {
|
|
|
|
return nil, bug.ErrBugNotExist
|
|
|
|
}
|
|
|
|
|
2018-09-11 18:11:16 +03:00
|
|
|
return c.ResolveBug(matching[0])
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-09-09 21:21:49 +03:00
|
|
|
func (c *RepoCache) QueryBugs(query *Query) []string {
|
|
|
|
if query == nil {
|
|
|
|
return c.AllBugsIds()
|
|
|
|
}
|
|
|
|
|
|
|
|
var filtered []*BugExcerpt
|
|
|
|
|
|
|
|
for _, excerpt := range c.excerpts {
|
|
|
|
if query.Match(excerpt) {
|
|
|
|
filtered = append(filtered, excerpt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var sorter sort.Interface
|
|
|
|
|
|
|
|
switch query.OrderBy {
|
|
|
|
case OrderById:
|
|
|
|
sorter = BugsById(filtered)
|
|
|
|
case OrderByCreation:
|
|
|
|
sorter = BugsByCreationTime(filtered)
|
|
|
|
case OrderByEdit:
|
|
|
|
sorter = BugsByEditTime(filtered)
|
|
|
|
default:
|
|
|
|
panic("missing sort type")
|
|
|
|
}
|
|
|
|
|
|
|
|
if query.OrderDirection == OrderDescending {
|
|
|
|
sorter = sort.Reverse(sorter)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(sorter)
|
|
|
|
|
|
|
|
result := make([]string, len(filtered))
|
|
|
|
|
|
|
|
for i, val := range filtered {
|
|
|
|
result[i] = val.Id
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllBugsIds return all known bug ids
|
|
|
|
func (c *RepoCache) AllBugsIds() []string {
|
|
|
|
result := make([]string, len(c.excerpts))
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for _, excerpt := range c.excerpts {
|
|
|
|
result[i] = excerpt.Id
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2018-09-02 16:37:28 +03:00
|
|
|
// ClearAllBugs clear all bugs kept in memory
|
2018-08-21 19:46:53 +03:00
|
|
|
func (c *RepoCache) ClearAllBugs() {
|
2018-08-23 22:24:57 +03:00
|
|
|
c.bugs = make(map[string]*BugCache)
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-09-21 15:02:05 +03:00
|
|
|
// ValidLabels list valid labels
|
|
|
|
//
|
|
|
|
// Note: in the future, a proper label policy could be implemented where valid
|
|
|
|
// labels are defined in a configuration file. Until that, the default behavior
|
|
|
|
// is to return the list of labels already used.
|
|
|
|
func (c *RepoCache) ValidLabels() []bug.Label {
|
|
|
|
set := map[bug.Label]interface{}{}
|
|
|
|
|
|
|
|
for _, excerpt := range c.excerpts {
|
|
|
|
for _, l := range excerpt.Labels {
|
|
|
|
set[l] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]bug.Label, len(set))
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for l := range set {
|
|
|
|
result[i] = l
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort
|
|
|
|
sort.Slice(result, func(i, j int) bool {
|
|
|
|
return string(result[i]) < string(result[j])
|
|
|
|
})
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2018-09-02 16:37:28 +03:00
|
|
|
// NewBug create a new bug
|
|
|
|
// The new bug is written in the repository (commit)
|
2018-08-23 22:24:57 +03:00
|
|
|
func (c *RepoCache) NewBug(title string, message string) (*BugCache, error) {
|
2018-08-21 19:46:53 +03:00
|
|
|
return c.NewBugWithFiles(title, message, nil)
|
|
|
|
}
|
|
|
|
|
2018-09-02 16:37:28 +03:00
|
|
|
// NewBugWithFiles create a new bug with attached files for the message
|
|
|
|
// The new bug is written in the repository (commit)
|
2018-09-11 23:04:16 +03:00
|
|
|
func (c *RepoCache) NewBugWithFiles(title string, message string, files []git.Hash) (*BugCache, error) {
|
2018-08-21 19:46:53 +03:00
|
|
|
author, err := bug.GetUser(c.repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-25 18:56:58 +03:00
|
|
|
return c.NewBugRaw(author, time.Now().Unix(), title, message, files, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBugWithFilesMeta create a new bug with attached files for the message, as
|
|
|
|
// well as metadata for the Create operation.
|
|
|
|
// The new bug is written in the repository (commit)
|
|
|
|
func (c *RepoCache) NewBugRaw(author bug.Person, unixTime int64, title string, message string, files []git.Hash, metadata map[string]string) (*BugCache, error) {
|
2018-10-02 00:31:16 +03:00
|
|
|
b, op, err := bug.CreateWithFiles(author, unixTime, title, message, files)
|
2018-08-21 19:46:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-25 18:56:58 +03:00
|
|
|
for key, value := range metadata {
|
2018-10-02 00:31:16 +03:00
|
|
|
op.SetMetadata(key, value)
|
2018-09-25 18:56:58 +03:00
|
|
|
}
|
|
|
|
|
2018-08-21 19:46:53 +03:00
|
|
|
err = b.Commit(c.repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-08-23 22:24:57 +03:00
|
|
|
cached := NewBugCache(c, b)
|
2018-08-21 19:46:53 +03:00
|
|
|
c.bugs[b.Id()] = cached
|
|
|
|
|
2018-09-04 19:18:24 +03:00
|
|
|
err = c.bugUpdated(b.Id())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-08-21 19:46:53 +03:00
|
|
|
return cached, nil
|
|
|
|
}
|
|
|
|
|
2018-09-02 16:37:28 +03:00
|
|
|
// Fetch retrieve update from a remote
|
|
|
|
// This does not change the local bugs state
|
2018-08-21 19:46:53 +03:00
|
|
|
func (c *RepoCache) Fetch(remote string) (string, error) {
|
|
|
|
return bug.Fetch(c.repo, remote)
|
|
|
|
}
|
|
|
|
|
2018-09-08 15:17:08 +03:00
|
|
|
// MergeAll will merge all the available remote bug
|
2018-08-21 19:46:53 +03:00
|
|
|
func (c *RepoCache) MergeAll(remote string) <-chan bug.MergeResult {
|
2018-09-08 15:17:08 +03:00
|
|
|
out := make(chan bug.MergeResult)
|
|
|
|
|
|
|
|
// Intercept merge results to update the cache properly
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
|
|
|
|
results := bug.MergeAll(c.repo, remote)
|
|
|
|
for result := range results {
|
2018-09-19 22:16:16 +03:00
|
|
|
out <- result
|
|
|
|
|
2018-09-08 15:17:08 +03:00
|
|
|
if result.Err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
id := result.Id
|
|
|
|
|
|
|
|
switch result.Status {
|
2018-09-13 12:13:51 +03:00
|
|
|
case bug.MergeStatusNew, bug.MergeStatusUpdated:
|
2018-09-08 15:17:08 +03:00
|
|
|
b := result.Bug
|
|
|
|
snap := b.Compile()
|
|
|
|
c.excerpts[id] = NewBugExcerpt(b, &snap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 18:59:02 +03:00
|
|
|
err := c.write()
|
2018-09-08 15:17:08 +03:00
|
|
|
|
|
|
|
// No easy way out here ..
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return out
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-09-02 16:37:28 +03:00
|
|
|
// Push update a remote with the local changes
|
2018-08-21 19:46:53 +03:00
|
|
|
func (c *RepoCache) Push(remote string) (string, error) {
|
|
|
|
return bug.Push(c.repo, remote)
|
|
|
|
}
|
2018-08-31 14:18:03 +03:00
|
|
|
|
|
|
|
func repoLockFilePath(repo repository.Repo) string {
|
|
|
|
return path.Join(repo.GetPath(), ".git", "git-bug", lockfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
// repoIsAvailable check is the given repository is locked by a Cache.
|
|
|
|
// Note: this is a smart function that will cleanup the lock file if the
|
|
|
|
// corresponding process is not there anymore.
|
|
|
|
// If no error is returned, the repo is free to edit.
|
|
|
|
func repoIsAvailable(repo repository.Repo) error {
|
|
|
|
lockPath := repoLockFilePath(repo)
|
|
|
|
|
|
|
|
// Todo: this leave way for a racey access to the repo between the test
|
|
|
|
// if the file exist and the actual write. It's probably not a problem in
|
|
|
|
// practice because using a repository will be done from user interaction
|
|
|
|
// or in a context where a single instance of git-bug is already guaranteed
|
|
|
|
// (say, a server with the web UI running). But still, that might be nice to
|
|
|
|
// have a mutex or something to guard that.
|
|
|
|
|
|
|
|
// Todo: this will fail if somehow the filesystem is shared with another
|
|
|
|
// computer. Should add a configuration that prevent the cleaning of the
|
|
|
|
// lock file
|
|
|
|
|
|
|
|
f, err := os.Open(lockPath)
|
|
|
|
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
// lock file already exist
|
|
|
|
buf, err := ioutil.ReadAll(io.LimitReader(f, 10))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(buf) == 10 {
|
2018-09-10 13:47:05 +03:00
|
|
|
return fmt.Errorf("the lock file should be < 10 bytes")
|
2018-08-31 14:18:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pid, err := strconv.Atoi(string(buf))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-11 23:04:16 +03:00
|
|
|
if process.IsRunning(pid) {
|
2018-09-10 13:47:05 +03:00
|
|
|
return fmt.Errorf("the repository you want to access is already locked by the process pid %d", pid)
|
2018-08-31 14:18:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The lock file is just laying there after a crash, clean it
|
|
|
|
|
|
|
|
fmt.Println("A lock file is present but the corresponding process is not, removing it.")
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Remove(lockPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|