-
Notifications
You must be signed in to change notification settings - Fork 14
/
bottom.go
48 lines (39 loc) · 1012 Bytes
/
bottom.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
package main
import (
"context"
"errors"
"fmt"
"github.com/charmbracelet/log"
"go.abhg.dev/gs/internal/text"
)
type bottomCmd struct {
checkoutOptions
}
func (*bottomCmd) Help() string {
return text.Dedent(`
Checks out the bottom-most branch in the current branch's stack.
Use the -n flag to print the branch without checking it out.
`)
}
func (cmd *bottomCmd) Run(ctx context.Context, log *log.Logger, opts *globalOptions) error {
repo, store, svc, err := openRepo(ctx, log, opts)
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)
}
if current == store.Trunk() {
return errors.New("no branches below current: already on trunk")
}
bottom, err := svc.FindBottom(ctx, current)
if err != nil {
return fmt.Errorf("find bottom: %w", err)
}
return (&branchCheckoutCmd{
checkoutOptions: cmd.checkoutOptions,
Branch: bottom,
}).Run(ctx, log, opts)
}