Skip to content

Commit ce918bf

Browse files
committed
addressing review comments
1 parent 9c974f8 commit ce918bf

3 files changed

Lines changed: 28 additions & 14 deletions

File tree

cmd/submit.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ func runSubmit(cfg *config.Config, opts *submitOptions) error {
136136
if errors.Is(err, errInterrupt) {
137137
return ErrSilent
138138
}
139+
// DeleteStack or other failure — don't continue with stale state
140+
return ErrSilent
139141
}
140142
}
141143

internal/modify/apply.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ func ApplyPlan(
193193
originalRefs[newName] = sha
194194
delete(originalRefs, oldName)
195195
}
196+
// Update originalParentTips key
197+
if sha, ok := originalParentTips[oldName]; ok {
198+
originalParentTips[newName] = sha
199+
delete(originalParentTips, oldName)
200+
}
196201
s.Branches[idx].Branch = newName
197202
}
198203
// Update the node's ref for later steps
@@ -360,25 +365,29 @@ func ApplyPlan(
360365
needsReorder = true
361366
}
362367

363-
// Rebuild s.Branches in the desired order
364-
if needsReorder || len(s.Branches) != len(desiredOrder) {
365-
newBranches := make([]stack.BranchRef, 0, len(desiredOrder))
366-
367-
// Add merged branches first (they stay in place)
368-
for _, b := range s.Branches {
369-
if b.IsMerged() {
370-
newBranches = append(newBranches, b)
371-
}
372-
}
373-
374-
// Add active branches in desired order
368+
// Rebuild s.Branches in the desired order, preserving merged branches
369+
// at their original positions.
370+
if needsReorder {
371+
// Build a queue of active branches in the desired order
372+
desiredIdx := 0
375373
branchMap := make(map[string]stack.BranchRef)
376374
for _, b := range s.Branches {
377375
branchMap[b.Branch] = b
378376
}
379-
for _, name := range desiredOrder {
380-
if b, ok := branchMap[name]; ok {
377+
378+
newBranches := make([]stack.BranchRef, 0, len(s.Branches))
379+
for _, b := range s.Branches {
380+
if b.IsMerged() {
381+
// Merged branches stay at their original position
381382
newBranches = append(newBranches, b)
383+
} else {
384+
// Substitute the next active branch from the desired order
385+
if desiredIdx < len(desiredOrder) {
386+
if sub, ok := branchMap[desiredOrder[desiredIdx]]; ok {
387+
newBranches = append(newBranches, sub)
388+
}
389+
desiredIdx++
390+
}
382391
}
383392
}
384393

internal/modify/state.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ func SaveState(gitDir string, state *StateFile) error {
8585
if err := os.WriteFile(tmp, data, 0644); err != nil {
8686
return fmt.Errorf("writing modify state: %w", err)
8787
}
88+
// Remove existing target before rename for Windows compatibility
89+
// (os.Rename fails on Windows if the target already exists).
90+
_ = os.Remove(target)
8891
if err := os.Rename(tmp, target); err != nil {
8992
_ = os.Remove(tmp)
9093
return fmt.Errorf("committing modify state: %w", err)

0 commit comments

Comments
 (0)