-
Notifications
You must be signed in to change notification settings - Fork 18
/
down.go
74 lines (61 loc) · 1.55 KB
/
down.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
package main
import (
"context"
"fmt"
"github.com/charmbracelet/log"
"go.abhg.dev/gs/internal/text"
"go.abhg.dev/gs/internal/ui"
)
type downCmd struct {
checkoutOptions
N int `arg:"" optional:"" help:"Number of branches to move up." default:"1"`
}
func (*downCmd) Help() string {
return text.Dedent(`
Checks out the branch below the current branch.
If the current branch is at the bottom of the stack,
checks out the trunk branch.
Use the -n flag to print the branch without checking it out.
`)
}
func (cmd *downCmd) Run(ctx context.Context, log *log.Logger, view ui.View) error {
repo, store, svc, err := openRepo(ctx, log, view)
if err != nil {
return err
}
current, err := repo.CurrentBranch(ctx)
if err != nil {
// TODO: handle not a branch
return fmt.Errorf("get current branch: %w", err)
}
var below string
outer:
for range cmd.N {
downstack, err := svc.ListDownstack(ctx, current)
if err != nil {
return fmt.Errorf("list downstacks: %w", err)
}
switch len(downstack) {
case 0:
if below != "" {
// If we've already moved up once,
// and there are no branches above the current one,
// we're done.
break outer
}
return fmt.Errorf("%v: no branches found downstack", current)
case 1:
// Current branch is bottom of stack.
// Move to trunk.
log.Info("moving to trunk: end of stack")
below = store.Trunk()
default:
below = downstack[1]
}
current = below
}
return (&branchCheckoutCmd{
checkoutOptions: cmd.checkoutOptions,
Branch: below,
}).Run(ctx, log, view)
}