-
Notifications
You must be signed in to change notification settings - Fork 18
/
log.go
227 lines (192 loc) · 5.37 KB
/
log.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
package main
import (
"context"
"errors"
"fmt"
"os"
"strings"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"go.abhg.dev/gs/internal/forge"
"go.abhg.dev/gs/internal/git"
"go.abhg.dev/gs/internal/spice"
"go.abhg.dev/gs/internal/spice/state"
"go.abhg.dev/gs/internal/ui"
"go.abhg.dev/gs/internal/ui/fliptree"
"go.abhg.dev/gs/internal/ui/widget"
)
type logCmd struct {
Short logShortCmd `cmd:"" aliases:"s" help:"List branches"`
Long logLongCmd `cmd:"" aliases:"l" help:"List branches and commits"`
}
var (
_branchStyle = ui.NewStyle().Bold(true)
_currentBranchStyle = ui.NewStyle().
Foreground(ui.Cyan).
Bold(true)
_logCommitStyle = widget.CommitSummaryStyle{
Hash: ui.NewStyle().Foreground(ui.Yellow),
Subject: ui.NewStyle().Foreground(ui.Plain),
Time: ui.NewStyle().Foreground(ui.Gray),
}
_logCommitFaintStyle = _logCommitStyle.Faint(true)
_needsRestackStyle = ui.NewStyle().
Foreground(ui.Gray).
SetString(" (needs restack)")
_markerStyle = ui.NewStyle().
Foreground(ui.Yellow).
Bold(true).
SetString("◀")
)
// branchLogCmd is the shared implementation of logShortCmd and logLongCmd.
type branchLogCmd struct {
All bool `short:"a" long:"all" config:"log.all" help:"Show all tracked branches, not just the current stack."`
}
type branchLogOptions struct {
Commits bool
Log *log.Logger
}
func (cmd *branchLogCmd) run(
ctx context.Context,
opts *branchLogOptions,
repo *git.Repository,
store *state.Store,
svc *spice.Service,
) (err error) {
log := opts.Log
currentBranch, err := repo.CurrentBranch(ctx)
if err != nil {
currentBranch = "" // may be detached
}
allBranches, err := svc.LoadBranches(ctx)
if err != nil {
return fmt.Errorf("load branches: %w", err)
}
type branchInfo struct {
Index int
Name string
Base string
ChangeID forge.ChangeID
Commits []git.CommitDetail
Aboves []int
}
infos := make([]*branchInfo, 0, len(allBranches)+1) // +1 for trunk
infoIdxByName := make(map[string]int, len(allBranches))
for _, branch := range allBranches {
info := &branchInfo{
Name: branch.Name,
Base: branch.Base,
}
if branch.Change != nil {
info.ChangeID = branch.Change.ChangeID()
}
if opts.Commits {
commits, err := repo.ListCommitsDetails(ctx,
git.CommitRangeFrom(branch.Head).
ExcludeFrom(branch.BaseHash).
FirstParent())
if err != nil {
log.Warn("Could not list commits for branch. Skipping.", "branch", branch.Name, "err", err)
continue
}
info.Commits = commits
}
idx := len(infos)
info.Index = idx
infos = append(infos, info)
infoIdxByName[branch.Name] = idx
}
trunkIdx := len(infos)
infos = append(infos, &branchInfo{
Index: trunkIdx,
Name: store.Trunk(),
})
infoIdxByName[store.Trunk()] = trunkIdx
// Second pass: Connect the "aboves".
for idx, branch := range infos {
if branch.Base == "" {
continue
}
baseIdx, ok := infoIdxByName[branch.Base]
if !ok {
continue
}
infos[baseIdx].Aboves = append(infos[baseIdx].Aboves, idx)
}
isVisible := func(*branchInfo) bool { return true }
if !cmd.All && currentBranch != "" {
visible := make(map[int]struct{})
currentBranchIdx := infoIdxByName[currentBranch]
// Add the upstacks of the current branch to the visible set.
for unseen := []int{currentBranchIdx}; len(unseen) > 0; {
idx := unseen[len(unseen)-1]
unseen = unseen[:len(unseen)-1]
visible[idx] = struct{}{}
unseen = append(unseen, infos[idx].Aboves...)
}
// Add the downstack of the current branch to the visible set.
for idx, ok := currentBranchIdx, true; ok; idx, ok = infoIdxByName[infos[idx].Base] {
visible[idx] = struct{}{}
}
isVisible = func(info *branchInfo) bool {
_, ok := visible[info.Index]
return ok
}
}
treeStyle := fliptree.DefaultStyle[*branchInfo]()
treeStyle.NodeMarker = func(b *branchInfo) lipgloss.Style {
if b.Name == currentBranch {
return fliptree.DefaultNodeMarker.SetString("■")
}
return fliptree.DefaultNodeMarker
}
var s strings.Builder
err = fliptree.Write(&s, fliptree.Graph[*branchInfo]{
Roots: []int{trunkIdx},
Values: infos,
View: func(b *branchInfo) string {
var o strings.Builder
if b.Name == currentBranch {
o.WriteString(_currentBranchStyle.Render(b.Name))
} else {
o.WriteString(_branchStyle.Render(b.Name))
}
if b.ChangeID != nil {
_, _ = fmt.Fprintf(&o, " (%v)", b.ChangeID)
}
if restackErr := new(spice.BranchNeedsRestackError); errors.As(svc.VerifyRestacked(ctx, b.Name), &restackErr) {
o.WriteString(_needsRestackStyle.String())
}
if b.Name == currentBranch {
o.WriteString(" " + _markerStyle.String())
}
commitStyle := _logCommitStyle
if b.Name != currentBranch {
commitStyle = _logCommitFaintStyle
}
for _, commit := range b.Commits {
o.WriteString("\n")
(&widget.CommitSummary{
ShortHash: commit.ShortHash,
Subject: commit.Subject,
AuthorDate: commit.AuthorDate,
}).Render(&o, commitStyle)
}
return o.String()
},
Edges: func(bi *branchInfo) []int {
aboves := make([]int, 0, len(bi.Aboves))
for _, above := range bi.Aboves {
if isVisible(infos[above]) {
aboves = append(aboves, above)
}
}
return aboves
},
}, fliptree.Options[*branchInfo]{Style: treeStyle})
if err != nil {
return fmt.Errorf("write tree: %w", err)
}
_, err = fmt.Fprint(os.Stderr, s.String())
return err
}