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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
package cache
import (
"fmt"
"github.com/go-git/go-billy/v5"
"github.com/pkg/errors"
"github.com/MichaelMure/git-bug/entities/bug"
"github.com/MichaelMure/git-bug/entities/identity"
"github.com/MichaelMure/git-bug/entity"
"github.com/MichaelMure/git-bug/repository"
)
func (c *RepoCache) Name() string {
return c.name
}
// LocalConfig give access to the repository scoped configuration
func (c *RepoCache) LocalConfig() repository.Config {
return c.repo.LocalConfig()
}
// GlobalConfig give access to the global scoped configuration
func (c *RepoCache) GlobalConfig() repository.Config {
return c.repo.GlobalConfig()
}
// AnyConfig give access to a merged local/global configuration
func (c *RepoCache) AnyConfig() repository.ConfigRead {
return c.repo.AnyConfig()
}
// Keyring give access to a user-wide storage for secrets
func (c *RepoCache) Keyring() repository.Keyring {
return c.repo.Keyring()
}
// GetUserName returns the name 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()
}
// GetCoreEditor returns the name of the editor that the user has used to configure git.
func (c *RepoCache) GetCoreEditor() (string, error) {
return c.repo.GetCoreEditor()
}
// GetRemotes returns the configured remotes repositories.
func (c *RepoCache) GetRemotes() (map[string]string, error) {
return c.repo.GetRemotes()
}
// LocalStorage return a billy.Filesystem giving access to $RepoPath/.git/git-bug
func (c *RepoCache) LocalStorage() billy.Filesystem {
return c.repo.LocalStorage()
}
// ReadData will attempt to read arbitrary data from the given hash
func (c *RepoCache) ReadData(hash repository.Hash) ([]byte, error) {
return c.repo.ReadData(hash)
}
// StoreData will store arbitrary data and return the corresponding hash
func (c *RepoCache) StoreData(data []byte) (repository.Hash, error) {
return c.repo.StoreData(data)
}
// Fetch retrieve updates from a remote
// This does not change the local bugs or identities state
func (c *RepoCache) Fetch(remote string) (string, error) {
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
}
return stdout1 + stdout2, nil
}
// MergeAll will merge all the available remote bug and identities
func (c *RepoCache) MergeAll(remote string) <-chan entity.MergeResult {
out := make(chan entity.MergeResult)
// Intercept merge results to update the cache properly
go func() {
defer close(out)
author, err := c.GetUserIdentity()
if err != nil {
out <- entity.NewMergeError(err, "")
return
}
results := identity.MergeAll(c.repo, remote)
for result := range results {
out <- result
if result.Err != nil {
continue
}
switch result.Status {
case entity.MergeStatusNew, entity.MergeStatusUpdated:
i := result.Entity.(*identity.Identity)
c.muIdentity.Lock()
c.identitiesExcerpts[result.Id] = NewIdentityExcerpt(i)
c.muIdentity.Unlock()
}
}
results = bug.MergeAll(c.repo, c.resolvers, remote, author)
for result := range results {
out <- result
if result.Err != nil {
continue
}
// TODO: have subcache do the merging?
switch result.Status {
case entity.MergeStatusNew:
b := result.Entity.(*bug.Bug)
_, err := c.bugs.add(b)
case entity.MergeStatusUpdated:
_, err := c.bugs.entityUpdated(b)
snap := b.Compile()
c.muBug.Lock()
c.bugExcerpts[result.Id] = NewBugExcerpt(b, snap)
c.muBug.Unlock()
}
}
err = c.write()
if err != nil {
out <- entity.NewMergeError(err, "")
return
}
}()
return out
}
// Push update a remote with the local changes
func (c *RepoCache) Push(remote string) (string, error) {
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
}
// 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
}
func (c *RepoCache) SetUserIdentity(i *IdentityCache) error {
err := identity.SetUserIdentity(c.repo, i.Identity)
if err != nil {
return err
}
c.muIdentity.RLock()
defer c.muIdentity.RUnlock()
// 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")
}
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
}
}
c.muIdentity.Lock()
defer c.muIdentity.Unlock()
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
}
func (c *RepoCache) GetUserIdentityExcerpt() (*IdentityExcerpt, error) {
if c.userIdentityId == "" {
id, err := identity.GetUserIdentityId(c.repo)
if err != nil {
return nil, err
}
c.userIdentityId = id
}
c.muIdentity.RLock()
defer c.muIdentity.RUnlock()
excerpt, ok := c.identitiesExcerpts[c.userIdentityId]
if !ok {
return nil, fmt.Errorf("cache: missing identity excerpt %v", c.userIdentityId)
}
return excerpt, nil
}
func (c *RepoCache) IsUserIdentitySet() (bool, error) {
return identity.IsUserIdentitySet(c.repo)
}
|