1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
package cache
import (
"fmt"
"strings"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/repository"
)
type Cache interface {
RegisterRepository(ref string, repo repository.Repo)
RegisterDefaultRepository(repo repository.Repo)
ResolveRepo(ref string) (CachedRepo, error)
DefaultRepo() (CachedRepo, error)
}
type CachedRepo interface {
ResolveBug(id string) (CachedBug, error)
ResolveBugPrefix(prefix string) (CachedBug, error)
ClearAllBugs()
}
type CachedBug interface {
Snapshot() bug.Snapshot
ClearSnapshot()
}
// Cache ------------------------
type DefaultCache struct {
repos map[string]CachedRepo
}
func NewDefaultCache() Cache {
return &DefaultCache{
repos: make(map[string]CachedRepo),
}
}
func (c *DefaultCache) RegisterRepository(ref string, repo repository.Repo) {
c.repos[ref] = NewCachedRepo(repo)
}
func (c *DefaultCache) RegisterDefaultRepository(repo repository.Repo) {
c.repos[""] = NewCachedRepo(repo)
}
func (c *DefaultCache) DefaultRepo() (CachedRepo, error) {
if len(c.repos) != 1 {
return nil, fmt.Errorf("repository is not unique")
}
for _, r := range c.repos {
return r, nil
}
panic("unreachable")
}
func (c *DefaultCache) ResolveRepo(ref string) (CachedRepo, error) {
r, ok := c.repos[ref]
if !ok {
return nil, fmt.Errorf("unknown repo")
}
return r, nil
}
// Repo ------------------------
type CachedRepoImpl struct {
repo repository.Repo
bugs map[string]CachedBug
}
func NewCachedRepo(r repository.Repo) CachedRepo {
return &CachedRepoImpl{
repo: r,
bugs: make(map[string]CachedBug),
}
}
func (c CachedRepoImpl) ResolveBug(id string) (CachedBug, error) {
cached, ok := c.bugs[id]
if ok {
return cached, nil
}
b, err := bug.ReadLocalBug(c.repo, id)
if err != nil {
return nil, err
}
cached = NewCachedBug(b)
c.bugs[id] = cached
return cached, nil
}
func (c CachedRepoImpl) ResolveBugPrefix(prefix string) (CachedBug, error) {
// preallocate but empty
matching := make([]string, 0, 5)
for id := range c.bugs {
if strings.HasPrefix(id, prefix) {
matching = append(matching, id)
}
}
// TODO: should check matching bug in the repo as well
if len(matching) > 1 {
return nil, fmt.Errorf("Multiple matching bug found:\n%s", strings.Join(matching, "\n"))
}
if len(matching) == 1 {
b := c.bugs[matching[0]]
return b, nil
}
b, err := bug.FindLocalBug(c.repo, prefix)
if err != nil {
return nil, err
}
cached := NewCachedBug(b)
c.bugs[b.Id()] = cached
return cached, nil
}
func (c CachedRepoImpl) ClearAllBugs() {
c.bugs = make(map[string]CachedBug)
}
// Bug ------------------------
type CachedBugImpl struct {
bug *bug.Bug
snap *bug.Snapshot
}
func NewCachedBug(b *bug.Bug) CachedBug {
return &CachedBugImpl{
bug: b,
}
}
func (c CachedBugImpl) Snapshot() bug.Snapshot {
if c.snap == nil {
snap := c.bug.Compile()
c.snap = &snap
}
return *c.snap
}
func (c CachedBugImpl) ClearSnapshot() {
c.snap = nil
}
|