Skip to content

Commit 0b9293a

Browse files
committed
disallow prefix or numbered flags with adopt
1 parent 569c635 commit 0b9293a

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

cmd/init.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ func runInit(cfg *config.Config, opts *initOptions) error {
101101

102102
var branches []string
103103

104+
// --adopt takes existing branches as-is; --prefix and --numbered don't apply.
105+
if opts.adopt && (opts.prefix != "" || opts.numbered) {
106+
cfg.Errorf("--adopt cannot be combined with --prefix or --numbered")
107+
return ErrInvalidArgs
108+
}
109+
104110
// Validate --numbered requires a prefix (either from flag or interactive input,
105111
// but for non-interactive paths we can check early).
106112
if opts.numbered && opts.prefix == "" && !cfg.IsInteractive() {

cmd/init_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,40 @@ func TestInit_InvalidPrefixRejectedBeforeBranchCreation(t *testing.T) {
165165
assert.Empty(t, created, "no branches should be created when prefix is invalid")
166166
}
167167

168+
func TestInit_AdoptRejectsPrefix(t *testing.T) {
169+
gitDir := t.TempDir()
170+
restore := git.SetOps(&git.MockOps{
171+
GitDirFn: func() (string, error) { return gitDir, nil },
172+
DefaultBranchFn: func() (string, error) { return "main", nil },
173+
CurrentBranchFn: func() (string, error) { return "main", nil },
174+
})
175+
defer restore()
176+
177+
cfg, outR, errR := config.NewTestConfig()
178+
err := runInit(cfg, &initOptions{adopt: true, branches: []string{"b1"}, prefix: "feat"})
179+
output := collectOutput(cfg, outR, errR)
180+
181+
assert.ErrorIs(t, err, ErrInvalidArgs)
182+
assert.Contains(t, output, "--adopt cannot be combined with --prefix or --numbered")
183+
}
184+
185+
func TestInit_AdoptRejectsNumbered(t *testing.T) {
186+
gitDir := t.TempDir()
187+
restore := git.SetOps(&git.MockOps{
188+
GitDirFn: func() (string, error) { return gitDir, nil },
189+
DefaultBranchFn: func() (string, error) { return "main", nil },
190+
CurrentBranchFn: func() (string, error) { return "main", nil },
191+
})
192+
defer restore()
193+
194+
cfg, outR, errR := config.NewTestConfig()
195+
err := runInit(cfg, &initOptions{adopt: true, branches: []string{"b1"}, numbered: true})
196+
output := collectOutput(cfg, outR, errR)
197+
198+
assert.ErrorIs(t, err, ErrInvalidArgs)
199+
assert.Contains(t, output, "--adopt cannot be combined with --prefix or --numbered")
200+
}
201+
168202
func TestInit_RerereAlreadyEnabled(t *testing.T) {
169203
gitDir := t.TempDir()
170204
enableRerereCalled := false

0 commit comments

Comments
 (0)