-
Notifications
You must be signed in to change notification settings - Fork 18
/
submit.go
450 lines (392 loc) · 10.4 KB
/
submit.go
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package main
import (
"context"
"encoding"
"fmt"
"regexp"
"runtime"
"strings"
"sync"
"github.com/charmbracelet/log"
"go.abhg.dev/gs/internal/forge"
"go.abhg.dev/gs/internal/git"
"go.abhg.dev/gs/internal/secret"
"go.abhg.dev/gs/internal/spice"
"go.abhg.dev/gs/internal/spice/state"
"go.abhg.dev/gs/internal/ui"
)
// submitSession is a single session of submitting branches.
// This provides the ability to share state between
// the multiple 'branch submit' invocations made by
// 'stack submit', 'upstack submit', and 'downstack submit'.
type submitSession struct {
// Branches that have been submitted (created or updated)
// in this session.
branches []string
// Values that are memoized across multiple branch submits.
Remote memoizedValue[string]
RemoteRepo memoizedValue[forge.Repository]
}
func newSubmitSession(
repo *git.Repository,
store *state.Store,
stash secret.Stash,
view ui.View,
log *log.Logger,
) *submitSession {
var s submitSession
s.Remote.get = func(ctx context.Context) (string, error) {
return ensureRemote(ctx, repo, store, log, view)
}
s.RemoteRepo.get = func(ctx context.Context) (forge.Repository, error) {
remote, err := s.Remote.Get(ctx)
if err != nil {
return nil, err
}
return openRemoteRepository(ctx, log, stash, repo, remote)
}
return &s
}
// This whole type is a bit of a hack.
// We should have better plumbing and retention of information
// between the submits.
// Maybe newSubmitSession should handle opening remote repo.
type memoizedValue[A any] struct {
once sync.Once
val A
err error
get func(context.Context) (A, error)
}
func (m *memoizedValue[A]) Get(ctx context.Context) (_ A, err error) {
m.once.Do(func() { m.val, m.err = m.get(ctx) })
return m.val, m.err
}
// navigationCommentWhen specifies when a navigation comment should be posted
// (or updated if it already exists).
type navigationCommentWhen int
const (
// navigationCommentAlways always posts a navigation comment.
// This is the default.
navigationCommentAlways navigationCommentWhen = iota
// navigationCommentNever disables posting navigation comments.
// If an existing comment is found, it is left as is.
navigationCommentNever
// navigationCommentOnMultiple posts a navigation comment
// only if there are multiple branches in the stack
// that the current branch is part of.
navigationCommentOnMultiple
)
var _ encoding.TextUnmarshaler = (*navigationCommentWhen)(nil)
func (f *navigationCommentWhen) UnmarshalText(bs []byte) error {
switch string(bs) {
case "true", "1", "yes":
*f = navigationCommentAlways
case "false", "0", "no":
*f = navigationCommentNever
case "multiple":
*f = navigationCommentOnMultiple
default:
return fmt.Errorf("invalid value %q: expected true, false, or multiple", bs)
}
return nil
}
func (f navigationCommentWhen) String() string {
switch f {
case navigationCommentAlways:
return "true"
case navigationCommentNever:
return "false"
case navigationCommentOnMultiple:
return "multiple"
default:
return "unknown"
}
}
// For each branch in the list of submitted branches,
// we'll add or update a comment in the form:
//
// This change is part of the following stack:
//
// - #123
// - #124 ◀
// - #125
//
// <sub>Change managed by [git-spice](https://abhinav.github.io/git-spice/).</sub>
//
// Where the arrow indicates the current branch.
// For cases where this is the first time we're posting the comment,
// we'll need to also update the store to record the comment ID for later.
func updateNavigationComments(
ctx context.Context,
store *state.Store,
svc *spice.Service,
log *log.Logger,
navComment navigationCommentWhen,
session *submitSession,
) error {
submittedBranches := session.branches
if len(submittedBranches) == 0 {
return nil
}
remoteRepo, err := session.RemoteRepo.Get(ctx)
if err != nil {
return fmt.Errorf("resolve remote repository: %w", err)
}
if navComment == navigationCommentNever {
return nil // nothing to do
}
// Look up branch graph once, and share between all syncs.
trackedBranches, err := svc.LoadBranches(ctx)
if err != nil {
return fmt.Errorf("list tracked branches: %w", err)
}
type branchInfo struct {
Branch string
Meta forge.ChangeMetadata
}
var (
nodes []*stackedChange
infos []branchInfo // info for nodes[i]
)
idxByBranch := make(map[string]int) // branch -> index in nodes
// First pass: add nodes but don't connect.
for _, b := range trackedBranches {
if b.Change == nil {
continue
}
idxByBranch[b.Name] = len(nodes)
nodes = append(nodes, &stackedChange{
Change: b.Change.ChangeID(),
Base: -1,
})
infos = append(infos, branchInfo{
Branch: b.Name,
Meta: b.Change,
})
}
// Second pass: connect Aboves.
for _, b := range trackedBranches {
nodeIdx, ok := idxByBranch[b.Name]
if !ok {
continue
}
baseIdx, ok := idxByBranch[b.Base]
if !ok {
continue
}
node := nodes[nodeIdx]
node.Base = baseIdx
base := nodes[baseIdx]
base.Aboves = append(base.Aboves, nodeIdx)
}
type (
postComment struct {
Branch string
Meta forge.ChangeMetadata
Change forge.ChangeID
Body string
}
updateComment struct {
Change forge.ChangeID
Comment forge.ChangeCommentID
Body string
}
)
postc := make(chan *postComment)
updatec := make(chan *updateComment)
branchTx := store.BeginBranchTx()
var (
wg sync.WaitGroup
mu sync.Mutex // guards branchTx
upserted []string
)
for range min(runtime.GOMAXPROCS(0), len(submittedBranches)) {
wg.Add(1)
go func() {
defer wg.Done()
postc := postc
updatec := updatec
for postc != nil || updatec != nil {
select {
case post, ok := <-postc:
if !ok {
postc = nil
continue
}
commentID, err := remoteRepo.PostChangeComment(ctx, post.Change, post.Body)
if err != nil {
log.Warn("Error posting comment",
"change", post.Change.String(),
"error", err,
)
continue
}
meta := post.Meta
meta.SetNavigationCommentID(commentID)
bs, err := remoteRepo.Forge().MarshalChangeMetadata(meta)
if err != nil {
log.Warn("Error marshaling change metadata",
"change", post.Change.String(),
"error", err,
)
continue
}
mu.Lock()
if err := branchTx.Upsert(ctx, state.UpsertRequest{
Name: post.Branch,
ChangeMetadata: bs,
ChangeForge: remoteRepo.Forge().ID(),
}); err != nil {
log.Error("Unable to update branch metadata",
"branch", post.Branch,
"error", err,
)
} else {
upserted = append(upserted, post.Branch)
}
mu.Unlock()
case update, ok := <-updatec:
if !ok {
updatec = nil
continue
}
err := remoteRepo.UpdateChangeComment(ctx, update.Comment, update.Body)
if err != nil {
log.Warn("Error updating comment",
"change", update.Change.String(),
"error", err,
)
continue
}
}
}
}()
}
// Concurrently post and update comments.
for _, branch := range submittedBranches {
idx, ok := idxByBranch[branch]
if !ok {
// This should never happen.
log.Warnf("branch %q not found in tracked branches", branch)
continue
}
// If we're only posting on multiple,
// we'll need to check if the branch is part of a stack
// that has at least one other branch.
if navComment == navigationCommentOnMultiple {
if len(nodes[idx].Aboves) == 0 && nodes[idx].Base == -1 {
continue
}
}
info := infos[idx]
commentBody := generateStackNavigationComment(nodes, idx)
if info.Meta.NavigationCommentID() == nil {
postc <- &postComment{
Branch: branch,
Meta: info.Meta,
Change: info.Meta.ChangeID(),
Body: commentBody,
}
} else {
updatec <- &updateComment{
Change: info.Meta.ChangeID(),
Comment: info.Meta.NavigationCommentID(),
Body: commentBody,
}
}
}
close(postc)
close(updatec)
wg.Wait()
var msg strings.Builder
msg.WriteString("Post stack navigation comments\n\n")
for _, name := range upserted {
fmt.Fprintf(&msg, "- %s\n", name)
}
if err := branchTx.Commit(ctx, msg.String()); err != nil {
return fmt.Errorf("update state: %w", err)
}
return nil
}
type stackedChange struct {
Change forge.ChangeID
Base int // -1 = no base CR
Aboves []int
}
const (
_commentHeader = "This change is part of the following stack:"
_commentFooter = "<sub>Change managed by [git-spice](https://abhinav.github.io/git-spice/).</sub>"
_commentMarker = "<!-- gs:navigation comment -->"
)
// Regular expressions that must ALL match a comment
// for it to be considered a navigation comment
// when detecting existing comments.
var _navCommentRegexes = []*regexp.Regexp{
regexp.MustCompile(`(?m)^\Q` + _commentHeader + `\E$`),
regexp.MustCompile(`(?m)^\Q` + _commentMarker + `\E$`),
}
func generateStackNavigationComment(
nodes []*stackedChange,
current int,
) string {
var sb strings.Builder
sb.WriteString(_commentHeader)
sb.WriteString("\n\n")
write := func(nodeIdx, indent int) {
node := nodes[nodeIdx]
for range indent {
sb.WriteString(" ")
}
fmt.Fprintf(&sb, "- %v", node.Change)
if nodeIdx == current {
sb.WriteString(" ◀")
}
sb.WriteString("\n")
}
// The graph is a DAG, so we don't expect cycles.
// Guard against it anyway.
visited := make([]bool, len(nodes))
ok := func(i int) bool {
if i < 0 || i >= len(nodes) || visited[i] {
return false
}
visited[i] = true
return true
}
// Write the downstacks, not including the current node.
// This will change the indent level.
// The downstacks leading up to the current branch are always linear.
var indent int
{
var downstacks []int
for base := nodes[current].Base; ok(base); base = nodes[base].Base {
downstacks = append(downstacks, base)
}
// Reverse order to print from base to current.
for i := len(downstacks) - 1; i >= 0; i-- {
write(downstacks[i], indent)
indent++
}
}
// For the upstacks, we'll need to traverse the graph
// and recursively write the upstacks.
// Indentation will increase for each subtree.
var visit func(int, int)
visit = func(nodeIdx, indent int) {
if !ok(nodeIdx) {
return
}
write(nodeIdx, indent)
for _, aboveIdx := range nodes[nodeIdx].Aboves {
visit(aboveIdx, indent+1)
}
}
// Current branch and its upstacks.
visit(current, indent)
sb.WriteString("\n")
sb.WriteString(_commentFooter)
sb.WriteString("\n")
sb.WriteString(_commentMarker)
sb.WriteString("\n")
return sb.String()
}