forked from abhinav/git-spice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo_init.go
237 lines (203 loc) · 6.18 KB
/
repo_init.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
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
package main
import (
"context"
"errors"
"fmt"
"github.com/charmbracelet/log"
"go.abhg.dev/gs/internal/git"
"go.abhg.dev/gs/internal/must"
"go.abhg.dev/gs/internal/spice"
"go.abhg.dev/gs/internal/spice/state"
"go.abhg.dev/gs/internal/spice/state/storage"
"go.abhg.dev/gs/internal/text"
"go.abhg.dev/gs/internal/ui"
)
type repoInitCmd struct {
Trunk string `placeholder:"BRANCH" predictor:"branches" help:"Name of the trunk branch"`
Remote string `placeholder:"NAME" predictor:"remotes" help:"Name of the remote to push changes to"`
Reset bool `help:"Forget all information about the repository"`
}
func (*repoInitCmd) Help() string {
return text.Dedent(`
A trunk branch is required.
This is the branch that changes will be merged into.
A prompt will ask for one if not provided with --trunk.
Most branch stacking operations are local
and do not require a network connection.
For operations that push or pull commits, a remote is required.
A prompt will ask for one during initialization
if not provided with --remote.
Re-run the command on an already initialized repository
to change the trunk or remote.
If the trunk branch is changed on re-initialization,
existing branches stacked on the old trunk
will be updated to point to the new trunk.
Re-run with --reset to discard all stored information
and untrack all branches.
`)
}
func (cmd *repoInitCmd) Run(ctx context.Context, log *log.Logger, globalOpts *globalOptions) error {
repo, err := git.Open(ctx, ".", git.OpenOptions{
Log: log,
})
if err != nil {
return fmt.Errorf("open repository: %w", err)
}
guesser := spice.Guesser{
Select: func(op spice.GuessOp, opts []string, selected string) (string, error) {
if !globalOpts.Prompt {
return "", errNoPrompt
}
var msg, desc string
switch op {
case spice.GuessRemote:
msg = "Please select a remote"
desc = "Merged changes will be pushed to this remote"
case spice.GuessTrunk:
msg = "Please select the trunk branch"
desc = "Changes will be merged into this branch"
default:
must.Failf("unknown guess operation: %v", op)
}
var result string
prompt := ui.NewSelect[string]().
WithValue(&result).
With(ui.ComparableOptions(selected, opts...)).
WithTitle(msg).
WithDescription(desc)
if err := ui.Run(prompt); err != nil {
return "", err
}
return result, nil
},
}
if cmd.Remote == "" {
cmd.Remote, err = guesser.GuessRemote(ctx, repo)
if err != nil {
return fmt.Errorf("guess remote: %w", err)
}
if cmd.Remote == "" {
log.Warn("No remotes found. Commands that require a remote will fail.")
} else {
log.Infof("Using remote: %v", cmd.Remote)
}
}
if cmd.Trunk == "" {
cmd.Trunk, err = guesser.GuessTrunk(ctx, repo, cmd.Remote)
if err != nil {
return fmt.Errorf("guess trunk: %w", err)
}
}
must.NotBeBlankf(cmd.Trunk, "trunk branch must have been set")
_, err = state.InitStore(ctx, state.InitStoreRequest{
DB: newRepoStorage(repo, log),
Trunk: cmd.Trunk,
Remote: cmd.Remote,
Reset: cmd.Reset,
})
if err != nil {
return fmt.Errorf("initialize storage: %w", err)
}
log.Info("Initialized repository", "trunk", cmd.Trunk)
return nil
}
const (
_dataRef = "refs/spice/data"
_authorName = "git-spice"
_authorEmail = "git-spice@localhost"
)
func newRepoStorage(repo storage.GitRepository, log *log.Logger) *storage.DB {
return storage.NewDB(storage.NewGitBackend(storage.GitConfig{
Repo: repo,
Ref: _dataRef,
AuthorName: _authorName,
AuthorEmail: _authorEmail,
Log: log,
}))
}
func openRepo(ctx context.Context, log *log.Logger, opts *globalOptions) (
*git.Repository, *state.Store, *spice.Service, error,
) {
repo, err := git.Open(ctx, ".", git.OpenOptions{
Log: log,
})
if err != nil {
return nil, nil, nil, fmt.Errorf("open repository: %w", err)
}
store, err := ensureStore(ctx, repo, log, opts)
if err != nil {
return nil, nil, nil, fmt.Errorf("open store: %w", err)
}
svc := spice.NewService(ctx, repo, store, log)
return repo, store, svc, nil
}
// ensureStore will open the spice data store in the provided Git repository,
// initializing it with `gs repo init` if it hasn't already been initialized.
//
// This allows nearly any other command to work without initialization
// by auto-initializing the repository at that time.
func ensureStore(
ctx context.Context,
repo storage.GitRepository,
log *log.Logger,
opts *globalOptions,
) (*state.Store, error) {
db := newRepoStorage(repo, log)
store, err := state.OpenStore(ctx, db, log)
if err == nil {
return store, nil
}
if errors.Is(err, state.ErrUninitialized) {
log.Info("Repository not initialized. Initializing.")
if err := (&repoInitCmd{}).Run(ctx, log, opts); err != nil {
return nil, fmt.Errorf("auto-initialize: %w", err)
}
// Assume initialization was a success.
return state.OpenStore(ctx, db, log)
}
return nil, fmt.Errorf("open store: %w", err)
}
func ensureRemote(
ctx context.Context,
repo spice.GitRepository,
store *state.Store,
log *log.Logger,
globals *globalOptions,
) (string, error) {
remote, err := store.Remote()
if err == nil {
return remote, nil
}
if !errors.Is(err, state.ErrNotExist) {
return "", fmt.Errorf("get remote: %w", err)
}
// No remote was specified at init time.
// Guess or prompt for one and update the store.
log.Warn("No remote was specified at init time")
remote, err = (&spice.Guesser{
Select: func(_ spice.GuessOp, opts []string, selected string) (string, error) {
if !globals.Prompt {
return "", errNoPrompt
}
result := selected
prompt := ui.NewSelect[string]().
WithValue(&result).
With(ui.ComparableOptions(selected, opts...)).
WithTitle("Please select a remote").
WithDescription("Changes will be pushed to this remote")
if err := ui.Run(prompt); err != nil {
return "", err
}
return result, nil
},
}).GuessRemote(ctx, repo)
if err != nil {
return "", fmt.Errorf("guess remote: %w", err)
}
if err := store.SetRemote(ctx, remote); err != nil {
return "", fmt.Errorf("set remote: %w", err)
}
// TODO: this should also update the Forge associated with the spice.Service.
log.Infof("Changed repository remote to %s", remote)
return remote, nil
}