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"
|
2020-02-08 19:12:49 +03:00
|
|
|
"sync"
|
2018-09-25 18:56:58 +03:00
|
|
|
"time"
|
2018-08-21 19:46:53 +03:00
|
|
|
|
2019-08-12 17:12:14 +03:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2018-08-21 19:46:53 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bug"
|
2019-03-27 23:44:11 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2018-11-21 20:56:12 +03:00
|
|
|
"github.com/MichaelMure/git-bug/identity"
|
2020-03-14 18:47:38 +03:00
|
|
|
"github.com/MichaelMure/git-bug/query/ast"
|
2018-08-21 19:46:53 +03:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2019-02-18 16:11:37 +03:00
|
|
|
const bugCacheFile = "bug-cache"
|
|
|
|
const identityCacheFile = "identity-cache"
|
|
|
|
|
|
|
|
// 1: original format
|
|
|
|
// 2: added cache for identities with a reference in the bug cache
|
|
|
|
const formatVersion = 2
|
2018-09-13 18:59:02 +03:00
|
|
|
|
2019-02-19 01:16:47 +03:00
|
|
|
type ErrInvalidCacheFormat struct {
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrInvalidCacheFormat) Error() string {
|
|
|
|
return e.message
|
|
|
|
}
|
|
|
|
|
2019-05-27 22:18:46 +03:00
|
|
|
var _ repository.RepoCommon = &RepoCache{}
|
|
|
|
|
2019-01-19 18:01:06 +03:00
|
|
|
// RepoCache is a cache for a Repository. This cache has multiple functions:
|
|
|
|
//
|
|
|
|
// 1. After being loaded, a Bug is kept in memory in the cache, allowing for fast
|
|
|
|
// access later.
|
2019-04-18 03:48:27 +03:00
|
|
|
// 2. The cache maintain in memory and on disk a pre-digested excerpt for each bug,
|
2019-01-19 18:01:06 +03:00
|
|
|
// allowing for fast querying the whole set of bugs without having to load
|
|
|
|
// them individually.
|
|
|
|
// 3. The cache guarantee that a single instance of a Bug is loaded at once, avoiding
|
|
|
|
// loss of data that we could have with multiple copies in the same process.
|
|
|
|
// 4. The same way, the cache maintain in memory a single copy of the loaded identities.
|
|
|
|
//
|
|
|
|
// The cache also protect the on-disk data by locking the git repository for its
|
|
|
|
// own usage, by writing a lock file. Of course, normal git operations are not
|
|
|
|
// affected, only git-bug related one.
|
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
|
2019-02-18 16:11:37 +03:00
|
|
|
|
2020-02-12 23:03:20 +03:00
|
|
|
// the name of the repository, as defined in the MultiRepoCache
|
|
|
|
name string
|
|
|
|
|
2020-02-08 19:12:49 +03:00
|
|
|
muBug sync.RWMutex
|
2018-09-11 20:28:32 +03:00
|
|
|
// excerpt of bugs data for all bugs
|
2019-08-12 17:12:14 +03:00
|
|
|
bugExcerpts map[entity.Id]*BugExcerpt
|
2018-09-11 20:28:32 +03:00
|
|
|
// bug loaded in memory
|
2019-08-12 17:12:14 +03:00
|
|
|
bugs map[entity.Id]*BugCache
|
2019-02-18 16:11:37 +03:00
|
|
|
|
2020-02-08 19:12:49 +03:00
|
|
|
muIdentity sync.RWMutex
|
2019-02-18 16:11:37 +03:00
|
|
|
// excerpt of identities data for all identities
|
2019-08-12 17:12:14 +03:00
|
|
|
identitiesExcerpts map[entity.Id]*IdentityExcerpt
|
2019-01-17 05:09:08 +03:00
|
|
|
// identities loaded in memory
|
2019-08-12 17:12:14 +03:00
|
|
|
identities map[entity.Id]*IdentityCache
|
2019-02-18 16:11:37 +03:00
|
|
|
|
2019-02-17 18:12:06 +03:00
|
|
|
// the user identity's id, if known
|
2019-08-12 17:12:14 +03:00
|
|
|
userIdentityId entity.Id
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-09-21 19:18:51 +03:00
|
|
|
func NewRepoCache(r repository.ClockedRepo) (*RepoCache, error) {
|
2020-02-12 23:03:20 +03:00
|
|
|
return NewNamedRepoCache(r, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNamedRepoCache(r repository.ClockedRepo, name string) (*RepoCache, error) {
|
2018-08-23 22:24:57 +03:00
|
|
|
c := &RepoCache{
|
2019-02-17 18:12:06 +03:00
|
|
|
repo: r,
|
2020-02-12 23:03:20 +03:00
|
|
|
name: name,
|
2019-08-12 17:12:14 +03:00
|
|
|
bugs: make(map[entity.Id]*BugCache),
|
|
|
|
identities: make(map[entity.Id]*IdentityCache),
|
2018-08-23 22:24:57 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2019-02-19 01:16:47 +03:00
|
|
|
if _, ok := err.(ErrInvalidCacheFormat); ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-02-12 23:03:20 +03:00
|
|
|
func (c *RepoCache) Name() string {
|
|
|
|
return c.name
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:01:21 +03:00
|
|
|
// LocalConfig give access to the repository scoped configuration
|
|
|
|
func (c *RepoCache) LocalConfig() repository.Config {
|
|
|
|
return c.repo.LocalConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GlobalConfig give access to the git global configuration
|
|
|
|
func (c *RepoCache) GlobalConfig() repository.Config {
|
|
|
|
return c.repo.GlobalConfig()
|
|
|
|
}
|
|
|
|
|
2018-09-21 19:18:51 +03:00
|
|
|
// GetPath returns the path to the repo.
|
|
|
|
func (c *RepoCache) GetPath() string {
|
|
|
|
return c.repo.GetPath()
|
|
|
|
}
|
|
|
|
|
2019-05-25 16:13:40 +03:00
|
|
|
// GetCoreEditor returns the name of the editor that the user has used to configure git.
|
2018-09-21 19:18:51 +03:00
|
|
|
func (c *RepoCache) GetCoreEditor() (string, error) {
|
|
|
|
return c.repo.GetCoreEditor()
|
|
|
|
}
|
|
|
|
|
2019-05-25 16:13:40 +03:00
|
|
|
// GetRemotes returns the configured remotes repositories.
|
|
|
|
func (c *RepoCache) GetRemotes() (map[string]string, error) {
|
|
|
|
return c.repo.GetRemotes()
|
|
|
|
}
|
|
|
|
|
2018-09-21 19:18:51 +03:00
|
|
|
// 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-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 {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.Lock()
|
|
|
|
defer c.muBug.Unlock()
|
|
|
|
c.muIdentity.Lock()
|
|
|
|
defer c.muIdentity.Unlock()
|
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
c.identities = make(map[entity.Id]*IdentityCache)
|
|
|
|
c.identitiesExcerpts = nil
|
|
|
|
c.bugs = make(map[entity.Id]*BugCache)
|
|
|
|
c.bugExcerpts = nil
|
2019-03-25 23:58:27 +03:00
|
|
|
|
2018-08-31 14:18:03 +03:00
|
|
|
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
|
2019-08-12 17:12:14 +03:00
|
|
|
func (c *RepoCache) bugUpdated(id entity.Id) error {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.Lock()
|
|
|
|
|
2018-08-23 22:24:57 +03:00
|
|
|
b, ok := c.bugs[id]
|
|
|
|
if !ok {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.Unlock()
|
2018-08-23 22:24:57 +03:00
|
|
|
panic("missing bug in the cache")
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:11:37 +03:00
|
|
|
c.bugExcerpts[id] = NewBugExcerpt(b.bug, b.Snapshot())
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.Unlock()
|
2019-02-18 16:11:37 +03:00
|
|
|
|
2019-02-19 01:16:47 +03:00
|
|
|
// we only need to write the bug cache
|
|
|
|
return c.writeBugCache()
|
2019-02-18 16:11:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// identityUpdated is a callback to trigger when the excerpt of an identity
|
|
|
|
// changed, that is each time an identity is updated
|
2019-08-12 17:12:14 +03:00
|
|
|
func (c *RepoCache) identityUpdated(id entity.Id) error {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muIdentity.Lock()
|
|
|
|
|
2019-02-18 16:11:37 +03:00
|
|
|
i, ok := c.identities[id]
|
|
|
|
if !ok {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muIdentity.Unlock()
|
2019-02-18 16:11:37 +03:00
|
|
|
panic("missing identity in the cache")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.identitiesExcerpts[id] = NewIdentityExcerpt(i.Identity)
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muIdentity.Unlock()
|
2018-08-23 22:24:57 +03:00
|
|
|
|
2019-02-19 01:16:47 +03:00
|
|
|
// we only need to write the identity cache
|
|
|
|
return c.writeIdentityCache()
|
2018-08-23 22:24:57 +03:00
|
|
|
}
|
|
|
|
|
2019-02-19 01:16:47 +03:00
|
|
|
// load will try to read from the disk all the cache files
|
2018-09-13 18:59:02 +03:00
|
|
|
func (c *RepoCache) load() error {
|
2019-02-19 01:16:47 +03:00
|
|
|
err := c.loadBugCache()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.loadIdentityCache()
|
|
|
|
}
|
|
|
|
|
|
|
|
// load will try to read from the disk the bug cache file
|
|
|
|
func (c *RepoCache) loadBugCache() error {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.Lock()
|
|
|
|
defer c.muBug.Unlock()
|
|
|
|
|
2019-02-18 16:11:37 +03:00
|
|
|
f, err := os.Open(bugCacheFilePath(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
|
2019-08-12 17:12:14 +03:00
|
|
|
Excerpts map[entity.Id]*BugExcerpt
|
2018-09-13 18:59:02 +03:00
|
|
|
}{}
|
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
|
|
|
|
}
|
|
|
|
|
2019-02-19 01:16:47 +03:00
|
|
|
if aux.Version != 2 {
|
|
|
|
return ErrInvalidCacheFormat{
|
|
|
|
message: fmt.Sprintf("unknown cache format version %v", aux.Version),
|
|
|
|
}
|
2018-09-13 18:59:02 +03:00
|
|
|
}
|
|
|
|
|
2019-02-18 16:11:37 +03:00
|
|
|
c.bugExcerpts = aux.Excerpts
|
2018-08-23 22:24:57 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-19 01:16:47 +03:00
|
|
|
// load will try to read from the disk the identity cache file
|
|
|
|
func (c *RepoCache) loadIdentityCache() error {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muIdentity.Lock()
|
|
|
|
defer c.muIdentity.Unlock()
|
|
|
|
|
2019-02-19 01:16:47 +03:00
|
|
|
f, err := os.Open(identityCacheFilePath(c.repo))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder := gob.NewDecoder(f)
|
|
|
|
|
|
|
|
aux := struct {
|
|
|
|
Version uint
|
2019-08-12 17:12:14 +03:00
|
|
|
Excerpts map[entity.Id]*IdentityExcerpt
|
2019-02-19 01:16:47 +03:00
|
|
|
}{}
|
|
|
|
|
|
|
|
err = decoder.Decode(&aux)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if aux.Version != 2 {
|
|
|
|
return ErrInvalidCacheFormat{
|
|
|
|
message: fmt.Sprintf("unknown cache format version %v", aux.Version),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.identitiesExcerpts = aux.Excerpts
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// write will serialize on disk all the cache files
|
2018-09-13 18:59:02 +03:00
|
|
|
func (c *RepoCache) write() error {
|
2019-02-19 01:16:47 +03:00
|
|
|
err := c.writeBugCache()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.writeIdentityCache()
|
|
|
|
}
|
|
|
|
|
|
|
|
// write will serialize on disk the bug cache file
|
|
|
|
func (c *RepoCache) writeBugCache() error {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.RLock()
|
|
|
|
defer c.muBug.RUnlock()
|
|
|
|
|
2018-08-23 22:24:57 +03:00
|
|
|
var data bytes.Buffer
|
|
|
|
|
2018-09-13 18:59:02 +03:00
|
|
|
aux := struct {
|
|
|
|
Version uint
|
2019-08-12 17:12:14 +03:00
|
|
|
Excerpts map[entity.Id]*BugExcerpt
|
2018-09-13 18:59:02 +03:00
|
|
|
}{
|
|
|
|
Version: formatVersion,
|
2019-02-18 16:11:37 +03:00
|
|
|
Excerpts: c.bugExcerpts,
|
2018-09-13 18:59:02 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:11:37 +03:00
|
|
|
f, err := os.Create(bugCacheFilePath(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()
|
|
|
|
}
|
|
|
|
|
2019-02-19 01:16:47 +03:00
|
|
|
// write will serialize on disk the identity cache file
|
|
|
|
func (c *RepoCache) writeIdentityCache() error {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muIdentity.RLock()
|
|
|
|
defer c.muIdentity.RUnlock()
|
|
|
|
|
2019-02-19 01:16:47 +03:00
|
|
|
var data bytes.Buffer
|
|
|
|
|
|
|
|
aux := struct {
|
|
|
|
Version uint
|
2019-08-12 17:12:14 +03:00
|
|
|
Excerpts map[entity.Id]*IdentityExcerpt
|
2019-02-19 01:16:47 +03:00
|
|
|
}{
|
|
|
|
Version: formatVersion,
|
|
|
|
Excerpts: c.identitiesExcerpts,
|
|
|
|
}
|
|
|
|
|
|
|
|
encoder := gob.NewEncoder(&data)
|
|
|
|
|
|
|
|
err := encoder.Encode(aux)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(identityCacheFilePath(c.repo))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = f.Write(data.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.Close()
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:11:37 +03:00
|
|
|
func bugCacheFilePath(repo repository.Repo) string {
|
2019-08-24 23:42:25 +03:00
|
|
|
return path.Join(repo.GetPath(), "git-bug", bugCacheFile)
|
2019-02-18 16:11:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func identityCacheFilePath(repo repository.Repo) string {
|
2019-08-24 23:42:25 +03:00
|
|
|
return path.Join(repo.GetPath(), "git-bug", identityCacheFile)
|
2018-08-23 22:24:57 +03:00
|
|
|
}
|
|
|
|
|
2018-09-19 00:36:45 +03:00
|
|
|
func (c *RepoCache) buildCache() error {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.Lock()
|
|
|
|
defer c.muBug.Unlock()
|
|
|
|
c.muIdentity.Lock()
|
|
|
|
defer c.muIdentity.Unlock()
|
|
|
|
|
2019-02-19 01:16:47 +03:00
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "Building identity cache... ")
|
|
|
|
|
2019-08-12 17:12:14 +03:00
|
|
|
c.identitiesExcerpts = make(map[entity.Id]*IdentityExcerpt)
|
2019-02-19 01:16:47 +03:00
|
|
|
|
|
|
|
allIdentities := identity.ReadAllLocalIdentities(c.repo)
|
|
|
|
|
|
|
|
for i := range allIdentities {
|
|
|
|
if i.Err != nil {
|
|
|
|
return i.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.identitiesExcerpts[i.Identity.Id()] = NewIdentityExcerpt(i.Identity)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = fmt.Fprintln(os.Stderr, "Done.")
|
|
|
|
|
2019-02-20 23:58:29 +03:00
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "Building bug cache... ")
|
2018-09-09 21:21:49 +03:00
|
|
|
|
2019-08-12 17:12:14 +03:00
|
|
|
c.bugExcerpts = make(map[entity.Id]*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()
|
2019-02-18 16:11:37 +03:00
|
|
|
c.bugExcerpts[b.Bug.Id()] = NewBugExcerpt(b.Bug, &snap)
|
2018-08-23 22:24:57 +03:00
|
|
|
}
|
2018-09-02 17:36:48 +03:00
|
|
|
|
2019-02-20 23:58:29 +03:00
|
|
|
_, _ = fmt.Fprintln(os.Stderr, "Done.")
|
2018-09-19 00:36:45 +03:00
|
|
|
return nil
|
2018-09-02 17:36:48 +03:00
|
|
|
}
|
|
|
|
|
2020-02-03 23:03:48 +03:00
|
|
|
// ResolveBugExcerpt retrieve a BugExcerpt matching the exact given id
|
|
|
|
func (c *RepoCache) ResolveBugExcerpt(id entity.Id) (*BugExcerpt, error) {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.RLock()
|
|
|
|
defer c.muBug.RUnlock()
|
|
|
|
|
2020-02-03 23:03:48 +03:00
|
|
|
e, ok := c.bugExcerpts[id]
|
|
|
|
if !ok {
|
|
|
|
return nil, bug.ErrBugNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2018-10-01 22:47:12 +03:00
|
|
|
// ResolveBug retrieve a bug matching the exact given id
|
2019-08-12 17:12:14 +03:00
|
|
|
func (c *RepoCache) ResolveBug(id entity.Id) (*BugCache, error) {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.RLock()
|
2018-08-21 19:46:53 +03:00
|
|
|
cached, ok := c.bugs[id]
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.RUnlock()
|
2018-08-21 19:46:53 +03:00
|
|
|
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)
|
2020-02-08 19:12:49 +03:00
|
|
|
|
|
|
|
c.muBug.Lock()
|
2018-08-21 19:46:53 +03:00
|
|
|
c.bugs[id] = cached
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.Unlock()
|
2018-08-21 19:46:53 +03:00
|
|
|
|
|
|
|
return cached, nil
|
|
|
|
}
|
|
|
|
|
2020-02-03 23:03:48 +03:00
|
|
|
// ResolveBugExcerptPrefix retrieve a BugExcerpt matching an id prefix. It fails if multiple
|
|
|
|
// bugs match.
|
|
|
|
func (c *RepoCache) ResolveBugExcerptPrefix(prefix string) (*BugExcerpt, error) {
|
|
|
|
return c.ResolveBugExcerptMatcher(func(excerpt *BugExcerpt) bool {
|
|
|
|
return excerpt.Id.HasPrefix(prefix)
|
|
|
|
})
|
2019-02-27 00:25:52 +03:00
|
|
|
}
|
|
|
|
|
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) {
|
2020-01-15 00:02:03 +03:00
|
|
|
return c.ResolveBugMatcher(func(excerpt *BugExcerpt) bool {
|
|
|
|
return excerpt.Id.HasPrefix(prefix)
|
|
|
|
})
|
2018-10-02 00:34:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2020-01-15 00:02:03 +03:00
|
|
|
return c.ResolveBugMatcher(func(excerpt *BugExcerpt) bool {
|
|
|
|
return excerpt.CreateMetadata[key] == value
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-03 23:03:48 +03:00
|
|
|
func (c *RepoCache) ResolveBugExcerptMatcher(f func(*BugExcerpt) bool) (*BugExcerpt, error) {
|
|
|
|
id, err := c.resolveBugMatcher(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c.ResolveBugExcerpt(id)
|
|
|
|
}
|
|
|
|
|
2020-01-15 00:02:03 +03:00
|
|
|
func (c *RepoCache) ResolveBugMatcher(f func(*BugExcerpt) bool) (*BugCache, error) {
|
2020-02-03 23:03:48 +03:00
|
|
|
id, err := c.resolveBugMatcher(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c.ResolveBug(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RepoCache) resolveBugMatcher(f func(*BugExcerpt) bool) (entity.Id, error) {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.RLock()
|
|
|
|
defer c.muBug.RUnlock()
|
|
|
|
|
2018-10-02 00:34:45 +03:00
|
|
|
// preallocate but empty
|
2019-08-12 17:12:14 +03:00
|
|
|
matching := make([]entity.Id, 0, 5)
|
2018-10-02 00:34:45 +03:00
|
|
|
|
2020-01-15 00:02:03 +03:00
|
|
|
for _, excerpt := range c.bugExcerpts {
|
|
|
|
if f(excerpt) {
|
|
|
|
matching = append(matching, excerpt.Id)
|
2018-10-02 00:34:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matching) > 1 {
|
2020-02-03 23:03:48 +03:00
|
|
|
return entity.UnsetId, bug.NewErrMultipleMatchBug(matching)
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-09-18 13:49:16 +03:00
|
|
|
if len(matching) == 0 {
|
2020-02-03 23:03:48 +03:00
|
|
|
return entity.UnsetId, bug.ErrBugNotExist
|
2018-09-18 13:49:16 +03:00
|
|
|
}
|
|
|
|
|
2020-02-03 23:03:48 +03:00
|
|
|
return matching[0], nil
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2019-01-17 05:09:08 +03:00
|
|
|
// QueryBugs return the id of all Bug matching the given Query
|
2020-03-14 18:47:38 +03:00
|
|
|
func (c *RepoCache) QueryBugs(query *ast.Query) []entity.Id {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.RLock()
|
|
|
|
defer c.muBug.RUnlock()
|
|
|
|
|
2018-09-09 21:21:49 +03:00
|
|
|
if query == nil {
|
|
|
|
return c.AllBugsIds()
|
|
|
|
}
|
|
|
|
|
2020-03-14 18:47:38 +03:00
|
|
|
matcher := compileMatcher(query.Filters)
|
|
|
|
|
2018-09-09 21:21:49 +03:00
|
|
|
var filtered []*BugExcerpt
|
|
|
|
|
2019-02-18 16:11:37 +03:00
|
|
|
for _, excerpt := range c.bugExcerpts {
|
2020-03-14 18:47:38 +03:00
|
|
|
if matcher.Match(excerpt, c) {
|
2018-09-09 21:21:49 +03:00
|
|
|
filtered = append(filtered, excerpt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var sorter sort.Interface
|
|
|
|
|
|
|
|
switch query.OrderBy {
|
2020-03-14 18:47:38 +03:00
|
|
|
case ast.OrderById:
|
2018-09-09 21:21:49 +03:00
|
|
|
sorter = BugsById(filtered)
|
2020-03-14 18:47:38 +03:00
|
|
|
case ast.OrderByCreation:
|
2018-09-09 21:21:49 +03:00
|
|
|
sorter = BugsByCreationTime(filtered)
|
2020-03-14 18:47:38 +03:00
|
|
|
case ast.OrderByEdit:
|
2018-09-09 21:21:49 +03:00
|
|
|
sorter = BugsByEditTime(filtered)
|
|
|
|
default:
|
|
|
|
panic("missing sort type")
|
|
|
|
}
|
|
|
|
|
2020-03-14 18:47:38 +03:00
|
|
|
switch query.OrderDirection {
|
|
|
|
case ast.OrderAscending:
|
|
|
|
// Nothing to do
|
|
|
|
case ast.OrderDescending:
|
2018-09-09 21:21:49 +03:00
|
|
|
sorter = sort.Reverse(sorter)
|
2020-03-14 18:47:38 +03:00
|
|
|
default:
|
|
|
|
panic("missing sort direction")
|
2018-09-09 21:21:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(sorter)
|
|
|
|
|
2019-08-12 17:12:14 +03:00
|
|
|
result := make([]entity.Id, len(filtered))
|
2018-09-09 21:21:49 +03:00
|
|
|
|
|
|
|
for i, val := range filtered {
|
|
|
|
result[i] = val.Id
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllBugsIds return all known bug ids
|
2019-08-12 17:12:14 +03:00
|
|
|
func (c *RepoCache) AllBugsIds() []entity.Id {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.RLock()
|
|
|
|
defer c.muBug.RUnlock()
|
|
|
|
|
2019-08-12 17:12:14 +03:00
|
|
|
result := make([]entity.Id, len(c.bugExcerpts))
|
2018-09-09 21:21:49 +03:00
|
|
|
|
|
|
|
i := 0
|
2019-02-18 16:11:37 +03:00
|
|
|
for _, excerpt := range c.bugExcerpts {
|
2018-09-09 21:21:49 +03:00
|
|
|
result[i] = excerpt.Id
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.RLock()
|
|
|
|
defer c.muBug.RUnlock()
|
|
|
|
|
2018-09-21 15:02:05 +03:00
|
|
|
set := map[bug.Label]interface{}{}
|
|
|
|
|
2019-02-18 16:11:37 +03:00
|
|
|
for _, excerpt := range c.bugExcerpts {
|
2018-09-21 15:02:05 +03:00
|
|
|
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)
|
2019-06-16 22:04:36 +03:00
|
|
|
func (c *RepoCache) NewBug(title string, message string) (*BugCache, *bug.CreateOperation, 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)
|
2019-06-16 22:04:36 +03:00
|
|
|
func (c *RepoCache) NewBugWithFiles(title string, message string, files []git.Hash) (*BugCache, *bug.CreateOperation, error) {
|
2019-02-17 18:12:06 +03:00
|
|
|
author, err := c.GetUserIdentity()
|
2018-08-21 19:46:53 +03:00
|
|
|
if err != nil {
|
2019-06-16 22:04:36 +03:00
|
|
|
return nil, nil, err
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
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)
|
2019-06-16 22:04:36 +03:00
|
|
|
func (c *RepoCache) NewBugRaw(author *IdentityCache, unixTime int64, title string, message string, files []git.Hash, metadata map[string]string) (*BugCache, *bug.CreateOperation, error) {
|
2019-02-17 18:12:06 +03:00
|
|
|
b, op, err := bug.CreateWithFiles(author.Identity, unixTime, title, message, files)
|
2018-08-21 19:46:53 +03:00
|
|
|
if err != nil {
|
2019-06-16 22:04:36 +03:00
|
|
|
return nil, nil, err
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-06-16 22:04:36 +03:00
|
|
|
return nil, nil, err
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.Lock()
|
2019-01-19 18:01:06 +03:00
|
|
|
if _, has := c.bugs[b.Id()]; has {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.Unlock()
|
2019-06-16 22:04:36 +03:00
|
|
|
return nil, nil, fmt.Errorf("bug %s already exist in the cache", b.Id())
|
2019-01-19 18:01:06 +03:00
|
|
|
}
|
|
|
|
|
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
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.Unlock()
|
2018-08-21 19:46:53 +03:00
|
|
|
|
2019-01-19 18:01:06 +03:00
|
|
|
// force the write of the excerpt
|
2018-09-04 19:18:24 +03:00
|
|
|
err = c.bugUpdated(b.Id())
|
|
|
|
if err != nil {
|
2019-06-16 22:04:36 +03:00
|
|
|
return nil, nil, err
|
2018-09-04 19:18:24 +03:00
|
|
|
}
|
|
|
|
|
2019-06-16 22:04:36 +03:00
|
|
|
return cached, op, nil
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2019-03-27 23:44:11 +03:00
|
|
|
// Fetch retrieve updates from a remote
|
|
|
|
// This does not change the local bugs or identities state
|
2018-08-21 19:46:53 +03:00
|
|
|
func (c *RepoCache) Fetch(remote string) (string, error) {
|
2019-03-27 23:44:11 +03:00
|
|
|
stdout1, err := identity.Fetch(c.repo, remote)
|
|
|
|
if err != nil {
|
|
|
|
return stdout1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stdout2, err := bug.Fetch(c.repo, remote)
|
|
|
|
if err != nil {
|
|
|
|
return stdout2, err
|
|
|
|
}
|
2018-08-21 19:46:53 +03:00
|
|
|
|
2019-03-27 23:44:11 +03:00
|
|
|
return stdout1 + stdout2, nil
|
|
|
|
}
|
2019-02-19 01:16:47 +03:00
|
|
|
|
2019-03-27 23:44:11 +03:00
|
|
|
// MergeAll will merge all the available remote bug and identities
|
|
|
|
func (c *RepoCache) MergeAll(remote string) <-chan entity.MergeResult {
|
|
|
|
out := make(chan entity.MergeResult)
|
2018-09-08 15:17:08 +03:00
|
|
|
|
|
|
|
// Intercept merge results to update the cache properly
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
|
2019-03-27 23:44:11 +03:00
|
|
|
results := identity.MergeAll(c.repo, remote)
|
2018-09-08 15:17:08 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-03-27 23:44:11 +03:00
|
|
|
switch result.Status {
|
|
|
|
case entity.MergeStatusNew, entity.MergeStatusUpdated:
|
|
|
|
i := result.Entity.(*identity.Identity)
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muIdentity.Lock()
|
2019-03-27 23:44:11 +03:00
|
|
|
c.identitiesExcerpts[result.Id] = NewIdentityExcerpt(i)
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muIdentity.Unlock()
|
2019-03-27 23:44:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
results = bug.MergeAll(c.repo, remote)
|
|
|
|
for result := range results {
|
|
|
|
out <- result
|
|
|
|
|
|
|
|
if result.Err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-09-08 15:17:08 +03:00
|
|
|
|
|
|
|
switch result.Status {
|
2019-03-27 23:44:11 +03:00
|
|
|
case entity.MergeStatusNew, entity.MergeStatusUpdated:
|
|
|
|
b := result.Entity.(*bug.Bug)
|
2018-09-08 15:17:08 +03:00
|
|
|
snap := b.Compile()
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.Lock()
|
2019-03-27 23:44:11 +03:00
|
|
|
c.bugExcerpts[result.Id] = NewBugExcerpt(b, &snap)
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.Unlock()
|
2018-09-08 15:17:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-03-27 23:44:11 +03:00
|
|
|
stdout1, err := identity.Push(c.repo, remote)
|
|
|
|
if err != nil {
|
|
|
|
return stdout1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stdout2, err := bug.Push(c.repo, remote)
|
|
|
|
if err != nil {
|
|
|
|
return stdout2, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdout1 + stdout2, nil
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
2018-08-31 14:18:03 +03:00
|
|
|
|
2019-04-02 23:01:42 +03:00
|
|
|
// Pull will do a Fetch + MergeAll
|
|
|
|
// This function will return an error if a merge fail
|
|
|
|
func (c *RepoCache) Pull(remote string) error {
|
|
|
|
_, err := c.Fetch(remote)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for merge := range c.MergeAll(remote) {
|
|
|
|
if merge.Err != nil {
|
|
|
|
return merge.Err
|
|
|
|
}
|
|
|
|
if merge.Status == entity.MergeStatusInvalid {
|
|
|
|
return errors.Errorf("merge failure: %s", merge.Reason)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-31 14:18:03 +03:00
|
|
|
func repoLockFilePath(repo repository.Repo) string {
|
2019-08-24 23:42:25 +03:00
|
|
|
return path.Join(repo.GetPath(), "git-bug", lockfile)
|
2018-08-31 14:18:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-02-24 16:57:58 +03:00
|
|
|
err = os.Remove(lockPath)
|
2018-08-31 14:18:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-17 05:09:08 +03:00
|
|
|
|
2020-02-03 23:03:48 +03:00
|
|
|
// ResolveIdentityExcerpt retrieve a IdentityExcerpt matching the exact given id
|
|
|
|
func (c *RepoCache) ResolveIdentityExcerpt(id entity.Id) (*IdentityExcerpt, error) {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muIdentity.RLock()
|
|
|
|
defer c.muIdentity.RUnlock()
|
|
|
|
|
2020-02-03 23:03:48 +03:00
|
|
|
e, ok := c.identitiesExcerpts[id]
|
|
|
|
if !ok {
|
|
|
|
return nil, identity.ErrIdentityNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2019-01-17 05:09:08 +03:00
|
|
|
// ResolveIdentity retrieve an identity matching the exact given id
|
2019-08-12 17:12:14 +03:00
|
|
|
func (c *RepoCache) ResolveIdentity(id entity.Id) (*IdentityCache, error) {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muIdentity.RLock()
|
2019-01-17 05:09:08 +03:00
|
|
|
cached, ok := c.identities[id]
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muIdentity.RUnlock()
|
2019-01-17 05:09:08 +03:00
|
|
|
if ok {
|
|
|
|
return cached, nil
|
|
|
|
}
|
|
|
|
|
2019-02-03 21:55:35 +03:00
|
|
|
i, err := identity.ReadLocal(c.repo, id)
|
2019-01-17 05:09:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-17 18:12:06 +03:00
|
|
|
cached = NewIdentityCache(c, i)
|
2020-02-08 19:12:49 +03:00
|
|
|
|
|
|
|
c.muIdentity.Lock()
|
2019-02-17 18:12:06 +03:00
|
|
|
c.identities[id] = cached
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muIdentity.Unlock()
|
2019-01-17 05:09:08 +03:00
|
|
|
|
2019-02-17 18:12:06 +03:00
|
|
|
return cached, nil
|
2019-01-17 05:09:08 +03:00
|
|
|
}
|
|
|
|
|
2020-02-03 23:03:48 +03:00
|
|
|
// ResolveIdentityExcerptPrefix retrieve a IdentityExcerpt matching an id prefix.
|
|
|
|
// It fails if multiple identities match.
|
|
|
|
func (c *RepoCache) ResolveIdentityExcerptPrefix(prefix string) (*IdentityExcerpt, error) {
|
|
|
|
return c.ResolveIdentityExcerptMatcher(func(excerpt *IdentityExcerpt) bool {
|
|
|
|
return excerpt.Id.HasPrefix(prefix)
|
|
|
|
})
|
2019-02-27 00:25:52 +03:00
|
|
|
}
|
|
|
|
|
2019-01-19 18:01:06 +03:00
|
|
|
// ResolveIdentityPrefix retrieve an Identity matching an id prefix.
|
|
|
|
// It fails if multiple identities match.
|
2019-02-17 18:12:06 +03:00
|
|
|
func (c *RepoCache) ResolveIdentityPrefix(prefix string) (*IdentityCache, error) {
|
2020-01-15 00:02:03 +03:00
|
|
|
return c.ResolveIdentityMatcher(func(excerpt *IdentityExcerpt) bool {
|
|
|
|
return excerpt.Id.HasPrefix(prefix)
|
|
|
|
})
|
2019-01-19 18:01:06 +03:00
|
|
|
}
|
2019-01-17 05:09:08 +03:00
|
|
|
|
|
|
|
// ResolveIdentityImmutableMetadata retrieve an Identity that has the exact given metadata on
|
|
|
|
// one of it's version. If multiple version have the same key, the first defined take precedence.
|
2019-02-17 18:12:06 +03:00
|
|
|
func (c *RepoCache) ResolveIdentityImmutableMetadata(key string, value string) (*IdentityCache, error) {
|
2020-01-15 00:02:03 +03:00
|
|
|
return c.ResolveIdentityMatcher(func(excerpt *IdentityExcerpt) bool {
|
|
|
|
return excerpt.ImmutableMetadata[key] == value
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-03 23:03:48 +03:00
|
|
|
func (c *RepoCache) ResolveIdentityExcerptMatcher(f func(*IdentityExcerpt) bool) (*IdentityExcerpt, error) {
|
|
|
|
id, err := c.resolveIdentityMatcher(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c.ResolveIdentityExcerpt(id)
|
|
|
|
}
|
|
|
|
|
2020-01-15 00:02:03 +03:00
|
|
|
func (c *RepoCache) ResolveIdentityMatcher(f func(*IdentityExcerpt) bool) (*IdentityCache, error) {
|
2020-02-03 23:03:48 +03:00
|
|
|
id, err := c.resolveIdentityMatcher(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c.ResolveIdentity(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RepoCache) resolveIdentityMatcher(f func(*IdentityExcerpt) bool) (entity.Id, error) {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muIdentity.RLock()
|
|
|
|
defer c.muIdentity.RUnlock()
|
|
|
|
|
2019-01-19 18:01:06 +03:00
|
|
|
// preallocate but empty
|
2019-08-12 17:12:14 +03:00
|
|
|
matching := make([]entity.Id, 0, 5)
|
2019-01-19 18:01:06 +03:00
|
|
|
|
2020-01-15 00:02:03 +03:00
|
|
|
for _, excerpt := range c.identitiesExcerpts {
|
|
|
|
if f(excerpt) {
|
|
|
|
matching = append(matching, excerpt.Id)
|
2019-01-19 18:01:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matching) > 1 {
|
2020-02-03 23:03:48 +03:00
|
|
|
return entity.UnsetId, identity.NewErrMultipleMatch(matching)
|
2019-01-19 18:01:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(matching) == 0 {
|
2020-02-03 23:03:48 +03:00
|
|
|
return entity.UnsetId, identity.ErrIdentityNotExist
|
2019-01-19 18:01:06 +03:00
|
|
|
}
|
|
|
|
|
2020-02-03 23:03:48 +03:00
|
|
|
return matching[0], nil
|
2019-01-19 18:01:06 +03:00
|
|
|
}
|
|
|
|
|
2019-02-18 16:11:37 +03:00
|
|
|
// AllIdentityIds return all known identity ids
|
2019-08-12 17:12:14 +03:00
|
|
|
func (c *RepoCache) AllIdentityIds() []entity.Id {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muIdentity.RLock()
|
|
|
|
defer c.muIdentity.RUnlock()
|
|
|
|
|
2019-08-12 17:12:14 +03:00
|
|
|
result := make([]entity.Id, len(c.identitiesExcerpts))
|
2019-02-18 16:11:37 +03:00
|
|
|
|
|
|
|
i := 0
|
|
|
|
for _, excerpt := range c.identitiesExcerpts {
|
|
|
|
result[i] = excerpt.Id
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-02-17 18:12:06 +03:00
|
|
|
func (c *RepoCache) SetUserIdentity(i *IdentityCache) error {
|
|
|
|
err := identity.SetUserIdentity(c.repo, i.Identity)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muIdentity.RLock()
|
|
|
|
defer c.muIdentity.RUnlock()
|
|
|
|
|
2019-02-19 01:16:47 +03:00
|
|
|
// Make sure that everything is fine
|
|
|
|
if _, ok := c.identities[i.Id()]; !ok {
|
|
|
|
panic("SetUserIdentity while the identity is not from the cache, something is wrong")
|
|
|
|
}
|
|
|
|
|
2019-02-17 18:12:06 +03:00
|
|
|
c.userIdentityId = i.Id()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RepoCache) GetUserIdentity() (*IdentityCache, error) {
|
|
|
|
if c.userIdentityId != "" {
|
|
|
|
i, ok := c.identities[c.userIdentityId]
|
|
|
|
if ok {
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muIdentity.Lock()
|
|
|
|
defer c.muIdentity.Unlock()
|
|
|
|
|
2019-02-17 18:12:06 +03:00
|
|
|
i, err := identity.GetUserIdentity(c.repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cached := NewIdentityCache(c, i)
|
|
|
|
c.identities[i.Id()] = cached
|
|
|
|
c.userIdentityId = i.Id()
|
|
|
|
|
|
|
|
return cached, nil
|
|
|
|
}
|
|
|
|
|
2020-02-03 23:03:48 +03:00
|
|
|
func (c *RepoCache) GetUserIdentityExcerpt() (*IdentityExcerpt, error) {
|
|
|
|
if c.userIdentityId == "" {
|
|
|
|
id, err := identity.GetUserIdentityId(c.repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c.userIdentityId = id
|
|
|
|
}
|
|
|
|
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muIdentity.RLock()
|
|
|
|
defer c.muIdentity.RUnlock()
|
|
|
|
|
2020-02-03 23:03:48 +03:00
|
|
|
excerpt, ok := c.identitiesExcerpts[c.userIdentityId]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("cache: missing identity excerpt %v", c.userIdentityId)
|
|
|
|
}
|
|
|
|
return excerpt, nil
|
|
|
|
}
|
|
|
|
|
2019-12-10 22:13:49 +03:00
|
|
|
func (c *RepoCache) IsUserIdentitySet() (bool, error) {
|
|
|
|
return identity.IsUserIdentitySet(c.repo)
|
|
|
|
}
|
|
|
|
|
2020-01-24 02:30:13 +03:00
|
|
|
func (c *RepoCache) NewIdentityFromGitUser() (*IdentityCache, error) {
|
|
|
|
return c.NewIdentityFromGitUserRaw(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RepoCache) NewIdentityFromGitUserRaw(metadata map[string]string) (*IdentityCache, error) {
|
|
|
|
i, err := identity.NewFromGitUser(c.repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c.finishIdentity(i, metadata)
|
|
|
|
}
|
|
|
|
|
2019-01-19 18:01:06 +03:00
|
|
|
// NewIdentity create a new identity
|
|
|
|
// The new identity is written in the repository (commit)
|
2019-02-17 18:12:06 +03:00
|
|
|
func (c *RepoCache) NewIdentity(name string, email string) (*IdentityCache, error) {
|
2020-02-25 23:35:57 +03:00
|
|
|
return c.NewIdentityRaw(name, email, "", "", nil)
|
2019-01-19 18:01:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewIdentityFull create a new identity
|
|
|
|
// The new identity is written in the repository (commit)
|
2020-02-25 23:35:57 +03:00
|
|
|
func (c *RepoCache) NewIdentityFull(name string, email string, login string, avatarUrl string) (*IdentityCache, error) {
|
|
|
|
return c.NewIdentityRaw(name, email, login, avatarUrl, nil)
|
2019-01-19 18:01:06 +03:00
|
|
|
}
|
|
|
|
|
2020-02-25 23:35:57 +03:00
|
|
|
func (c *RepoCache) NewIdentityRaw(name string, email string, login string, avatarUrl string, metadata map[string]string) (*IdentityCache, error) {
|
|
|
|
i := identity.NewIdentityFull(name, email, login, avatarUrl)
|
2020-01-24 02:30:13 +03:00
|
|
|
return c.finishIdentity(i, metadata)
|
|
|
|
}
|
2019-01-19 18:01:06 +03:00
|
|
|
|
2020-01-24 02:30:13 +03:00
|
|
|
func (c *RepoCache) finishIdentity(i *identity.Identity, metadata map[string]string) (*IdentityCache, error) {
|
2019-01-19 18:01:06 +03:00
|
|
|
for key, value := range metadata {
|
|
|
|
i.SetMetadata(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := i.Commit(c.repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muIdentity.Lock()
|
2019-01-19 18:01:06 +03:00
|
|
|
if _, has := c.identities[i.Id()]; has {
|
|
|
|
return nil, fmt.Errorf("identity %s already exist in the cache", i.Id())
|
|
|
|
}
|
|
|
|
|
2019-02-17 18:12:06 +03:00
|
|
|
cached := NewIdentityCache(c, i)
|
|
|
|
c.identities[i.Id()] = cached
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muIdentity.Unlock()
|
2019-01-19 18:01:06 +03:00
|
|
|
|
2019-02-18 16:11:37 +03:00
|
|
|
// force the write of the excerpt
|
|
|
|
err = c.identityUpdated(i.Id())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-17 18:12:06 +03:00
|
|
|
return cached, nil
|
2019-01-17 05:09:08 +03:00
|
|
|
}
|