-
Notifications
You must be signed in to change notification settings - Fork 18
/
branch_track.go
150 lines (127 loc) · 3.71 KB
/
branch_track.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
package main
import (
"context"
"errors"
"fmt"
"github.com/charmbracelet/log"
"go.abhg.dev/gs/internal/git"
"go.abhg.dev/gs/internal/spice"
"go.abhg.dev/gs/internal/spice/state"
"go.abhg.dev/gs/internal/text"
)
type branchTrackCmd struct {
Base string `short:"b" placeholder:"BRANCH" help:"Base branch this merges into" predictor:"trackedBranches"`
Branch string `arg:"" optional:"" help:"Name of the branch to track" predictor:"branches"`
}
func (*branchTrackCmd) Help() string {
return text.Dedent(`
A branch must be tracked to be able to run gs operations on it.
Use 'gs branch create' to automatically track new branches.
The base is guessed by comparing against other tracked branches.
Use --base to specify a base explicitly.
`)
}
func (cmd *branchTrackCmd) Run(
ctx context.Context,
log *log.Logger,
repo *git.Repository,
store *state.Store,
svc *spice.Service,
) error {
if cmd.Branch == "" {
var err error
cmd.Branch, err = repo.CurrentBranch(ctx)
if err != nil {
return fmt.Errorf("get current branch: %w", err)
}
}
if cmd.Branch == store.Trunk() {
return fmt.Errorf("cannot track trunk branch")
}
if cmd.Base == "" {
branchHash, err := repo.PeelToCommit(ctx, cmd.Branch)
if err != nil {
return fmt.Errorf("peel to commit: %w", err)
}
trunkHash, err := repo.PeelToCommit(ctx, store.Trunk())
if err != nil {
return fmt.Errorf("peel to commit: %w", err)
}
// Find all revisions between the current branch and the trunk branch
// and check if we know any branches at those revisions.
// If not, we'll use the trunk branch as the base.
revs, err := repo.ListCommits(ctx,
git.CommitRangeFrom(branchHash).ExcludeFrom(trunkHash))
if err != nil {
return fmt.Errorf("list commits: %w", err)
}
trackedBranches, err := store.ListBranches(ctx)
if err != nil {
return fmt.Errorf("list tracked branches: %w", err)
}
// Branch hashes will be filled in as needed.
// A branch hash of ZeroHash means the branch doesn't exist.
branchHashes := make([]git.Hash, len(trackedBranches))
hashFor := func(branchIdx int) (git.Hash, error) {
if hash := branchHashes[branchIdx]; hash != "" {
return hash, nil
}
name := trackedBranches[branchIdx]
hash, err := repo.PeelToCommit(ctx, name)
if err != nil {
if !errors.Is(err, git.ErrNotExist) {
return "", fmt.Errorf("resolve branch %q: %w", name, err)
}
hash = git.ZeroHash
}
branchHashes[branchIdx] = hash
return hash, nil
}
revLoop:
for _, rev := range revs {
for branchIdx, branchName := range trackedBranches {
if branchName == cmd.Branch {
continue
}
hash, err := hashFor(branchIdx)
if err != nil {
return err
}
if hash == rev {
cmd.Base = branchName
break revLoop
}
}
}
if cmd.Base == "" {
cmd.Base = store.Trunk()
}
log.Debugf("Detected base branch: %v", cmd.Base)
}
baseHash, err := repo.PeelToCommit(ctx, cmd.Base)
if err != nil {
return fmt.Errorf("peel to commit: %w", err)
}
msg := fmt.Sprintf("track %v with base %v", cmd.Branch, cmd.Base)
tx := store.BeginBranchTx()
if err := tx.Upsert(ctx, state.UpsertRequest{
Name: cmd.Branch,
Base: cmd.Base,
BaseHash: baseHash,
},
); err != nil {
return fmt.Errorf("%s: %w", msg, err)
}
if err := tx.Commit(ctx, msg); err != nil {
return fmt.Errorf("update state: %w", err)
}
log.Infof("%v: tracking with base %v", cmd.Branch, cmd.Base)
if err := svc.VerifyRestacked(ctx, cmd.Branch); err != nil {
var restackErr *spice.BranchNeedsRestackError
if errors.As(err, &restackErr) {
log.Warnf("%v: needs to be restacked: run 'gs branch restack --branch=%v'", cmd.Branch, cmd.Branch)
}
log.Warnf("error checking branch: %v", err)
}
return nil
}