Repository navigation
Expand file tree
/
Copy pathstack.go
More file actions
293 lines (261 loc) · 7.86 KB
/
Copy pathstack.go
File metadata and controls
293 lines (261 loc) · 7.86 KB
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
package stack
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
const (
schemaVersion = 1
stackFileName = "gh-stack"
)
// PullRequestRef holds relatively immutable metadata about an associated PR.
type PullRequestRef struct {
Number int `json:"number"`
ID string `json:"id,omitempty"`
URL string `json:"url,omitempty"`
Merged bool `json:"merged,omitempty"`
}
// BranchRef represents a branch and its associated commit hash.
// For the trunk, Head stores the HEAD commit SHA.
// For stacked branches, Base stores the parent branch's HEAD SHA
// at the time of last sync/rebase, used to identify unique commits.
type BranchRef struct {
Branch string `json:"branch"`
Head string `json:"head,omitempty"`
Base string `json:"base,omitempty"`
PullRequest *PullRequestRef `json:"pullRequest,omitempty"`
}
// Stack represents a single stack of branches.
type Stack struct {
ID string `json:"id,omitempty"`
Prefix string `json:"prefix,omitempty"`
Numbered bool `json:"numbered,omitempty"`
Trunk BranchRef `json:"trunk"`
Branches []BranchRef `json:"branches"`
}
// DisplayChain returns a human-readable chain representation of the stack.
// Format: (trunk) <- branch1 <- branch2 <- branch3
func (s *Stack) DisplayChain() string {
parts := []string{"(" + s.Trunk.Branch + ")"}
for _, b := range s.Branches {
parts = append(parts, b.Branch)
}
return strings.Join(parts, " <- ")
}
// BranchNames returns the list of branch names in order.
func (s *Stack) BranchNames() []string {
names := make([]string, len(s.Branches))
for i, b := range s.Branches {
names[i] = b.Branch
}
return names
}
// IndexOf returns the index of the given branch in the stack, or -1 if not found.
func (s *Stack) IndexOf(branch string) int {
for i, b := range s.Branches {
if b.Branch == branch {
return i
}
}
return -1
}
// Contains returns true if the branch is part of this stack (including trunk).
func (s *Stack) Contains(branch string) bool {
if s.Trunk.Branch == branch {
return true
}
return s.IndexOf(branch) >= 0
}
// BaseBranch returns the base branch for the given branch in the stack.
// For the first branch, this is the trunk. For others, it's the previous branch.
func (s *Stack) BaseBranch(branch string) string {
idx := s.IndexOf(branch)
if idx <= 0 {
return s.Trunk.Branch
}
return s.Branches[idx-1].Branch
}
// IsMerged returns whether a branch's PR has been merged.
func (b *BranchRef) IsMerged() bool {
return b.PullRequest != nil && b.PullRequest.Merged
}
// ActiveBranches returns only non-merged branches, preserving order.
func (s *Stack) ActiveBranches() []BranchRef {
var active []BranchRef
for _, b := range s.Branches {
if !b.IsMerged() {
active = append(active, b)
}
}
return active
}
// MergedBranches returns only merged branches, preserving order.
func (s *Stack) MergedBranches() []BranchRef {
var merged []BranchRef
for _, b := range s.Branches {
if b.IsMerged() {
merged = append(merged, b)
}
}
return merged
}
// FirstActiveBranchIndex returns the index of the first non-merged branch, or -1.
func (s *Stack) FirstActiveBranchIndex() int {
for i, b := range s.Branches {
if !b.IsMerged() {
return i
}
}
return -1
}
// ActiveBranchIndices returns the indices of all non-merged branches.
func (s *Stack) ActiveBranchIndices() []int {
var indices []int
for i, b := range s.Branches {
if !b.IsMerged() {
indices = append(indices, i)
}
}
return indices
}
// ActiveBaseBranch returns the effective parent for a branch, skipping merged
// ancestors. For the first active branch (or any branch whose downstack is all
// merged), this returns the trunk.
func (s *Stack) ActiveBaseBranch(branch string) string {
idx := s.IndexOf(branch)
if idx <= 0 {
return s.Trunk.Branch
}
for j := idx - 1; j >= 0; j-- {
if !s.Branches[j].IsMerged() {
return s.Branches[j].Branch
}
}
return s.Trunk.Branch
}
// IsFullyMerged returns true if all branches in the stack have been merged.
func (s *Stack) IsFullyMerged() bool {
for _, b := range s.Branches {
if !b.IsMerged() {
return false
}
}
return len(s.Branches) > 0
}
// StackFile represents the JSON file stored in .git/gh-stack.
type StackFile struct {
SchemaVersion int `json:"schemaVersion"`
Repository string `json:"repository"`
Stacks []Stack `json:"stacks"`
}
// FindAllStacksForBranch returns all stacks that contain the given branch.
func (sf *StackFile) FindAllStacksForBranch(branch string) []*Stack {
var stacks []*Stack
for i := range sf.Stacks {
if sf.Stacks[i].Contains(branch) {
stacks = append(stacks, &sf.Stacks[i])
}
}
return stacks
}
// FindStackByPRNumber returns the first stack and branch whose PR number matches.
// Returns nil, nil if no match is found.
func (sf *StackFile) FindStackByPRNumber(prNumber int) (*Stack, *BranchRef) {
for i := range sf.Stacks {
for j := range sf.Stacks[i].Branches {
b := &sf.Stacks[i].Branches[j]
if b.PullRequest != nil && b.PullRequest.Number == prNumber {
return &sf.Stacks[i], b
}
}
}
return nil, nil
}
// ValidateNoDuplicateBranch checks that the branch is not already in any stack.
func (sf *StackFile) ValidateNoDuplicateBranch(branch string) error {
for _, s := range sf.Stacks {
if s.Contains(branch) {
return fmt.Errorf("branch %q is already part of a stack", branch)
}
}
return nil
}
// AddStack adds a new stack to the file.
func (sf *StackFile) AddStack(s Stack) {
sf.Stacks = append(sf.Stacks, s)
}
// RemoveStack removes the stack at the given index.
func (sf *StackFile) RemoveStack(idx int) {
sf.Stacks = append(sf.Stacks[:idx], sf.Stacks[idx+1:]...)
}
// RemoveStackForBranch removes the stack containing the given branch.
func (sf *StackFile) RemoveStackForBranch(branch string) bool {
for i := range sf.Stacks {
if sf.Stacks[i].Contains(branch) {
sf.RemoveStack(i)
return true
}
}
return false
}
// stackFilePath returns the path to the gh-stack file.
func stackFilePath(gitDir string) string {
return filepath.Join(gitDir, stackFileName)
}
// Load reads the stack file from the given git directory.
// Returns an empty StackFile if the file does not exist.
func Load(gitDir string) (*StackFile, error) {
path := stackFilePath(gitDir)
data, err := os.ReadFile(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return &StackFile{
SchemaVersion: schemaVersion,
Stacks: []Stack{},
}, nil
}
return nil, fmt.Errorf("reading stack file: %w", err)
}
var sf StackFile
if err := json.Unmarshal(data, &sf); err != nil {
return nil, fmt.Errorf("parsing stack file: %w", err)
}
if sf.SchemaVersion > schemaVersion {
return nil, fmt.Errorf("stack file has schema version %d, but this version of gh-stack only supports up to version %d — please upgrade gh-stack", sf.SchemaVersion, schemaVersion)
}
return &sf, nil
}
// Save acquires an exclusive lock on the stack file, writes sf as JSON, and
// releases the lock. The lock is held only for the duration of the write.
func Save(gitDir string, sf *StackFile) error {
lock, err := Lock(gitDir)
if err != nil {
return &LockError{Err: err}
}
defer lock.Unlock()
return writeStackFile(gitDir, sf)
}
// SaveLocked writes the stack file without acquiring the lock. The caller
// must already hold the lock (via Lock) to protect the write. Use this when
// you need an atomic Load-Modify-Save sequence.
func SaveLocked(gitDir string, sf *StackFile) error {
return writeStackFile(gitDir, sf)
}
func writeStackFile(gitDir string, sf *StackFile) error {
sf.SchemaVersion = schemaVersion
if sf.Stacks == nil {
sf.Stacks = []Stack{}
}
data, err := json.MarshalIndent(sf, "", " ")
if err != nil {
return fmt.Errorf("marshaling stack file: %w", err)
}
path := stackFilePath(gitDir)
if err := os.WriteFile(path, data, 0644); err != nil {
return fmt.Errorf("writing stack file: %w", err)
}
return nil
}