Skip to content

Commit 9be7bfe

Browse files
committed
deprecate adopt flag
1 parent 70d2fe7 commit 9be7bfe

2 files changed

Lines changed: 26 additions & 55 deletions

File tree

cmd/init.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import (
1616
type initOptions struct {
1717
branches []string
1818
base string
19-
adopt bool
2019
prefix string
2120
numbered bool
21+
adopt bool // deprecated, kept for backward compat
2222
}
2323

2424
func InitCmd(cfg *config.Config) *cobra.Command {
@@ -44,7 +44,7 @@ Use --base to specify a different trunk branch.`,
4444
# Adopt existing branches into a stack (bottom to top)
4545
$ gh stack init feat/auth feat/api feat/ui
4646
47-
# Use auto-numbered branches with a prefix
47+
# Create a stack with auto-numbered branches (feat/01, feat/02, etc.)
4848
$ gh stack init --prefix feat --numbered
4949
5050
# Specify a different trunk branch
@@ -56,9 +56,10 @@ Use --base to specify a different trunk branch.`,
5656
}
5757

5858
cmd.Flags().StringVarP(&opts.base, "base", "b", "", "Trunk branch for stack (defaults to default branch)")
59-
cmd.Flags().BoolVarP(&opts.adopt, "adopt", "a", false, "Track existing branches as part of a stack")
6059
cmd.Flags().StringVarP(&opts.prefix, "prefix", "p", "", "Branch name prefix for the stack")
6160
cmd.Flags().BoolVarP(&opts.numbered, "numbered", "n", false, "Use auto-incrementing numbered branch names (requires --prefix)")
61+
cmd.Flags().BoolVarP(&opts.adopt, "adopt", "a", false, "Deprecated: existing branches are now adopted automatically")
62+
_ = cmd.Flags().MarkHidden("adopt")
6263

6364
return cmd
6465
}
@@ -115,10 +116,11 @@ func runInit(cfg *config.Config, opts *initOptions) error {
115116

116117
// --- Flag validation ---
117118

118-
// --adopt takes existing branches as-is; --prefix and --numbered don't apply.
119-
if opts.adopt && (opts.prefix != "" || opts.numbered) {
120-
cfg.Errorf("--adopt cannot be combined with --prefix or --numbered")
121-
return ErrInvalidArgs
119+
// --adopt is deprecated; print a notice and continue normally.
120+
if opts.adopt {
121+
cfg.Warningf("The --adopt flag is deprecated. Existing branches are now adopted automatically.")
122+
cfg.Printf("You can simply run: %s",
123+
cfg.ColorCyan("gh stack init <branch1> <branch2> ..."))
122124
}
123125

124126
// --numbered requires a prefix (either from flag or interactive input).
@@ -189,7 +191,7 @@ func runInit(cfg *config.Config, opts *initOptions) error {
189191
} else {
190192
// === INTERACTIVE PATH ===
191193
if !cfg.IsInteractive() {
192-
cfg.Errorf("interactive input required; provide branch names or use --adopt")
194+
cfg.Errorf("interactive input required; provide branch names as arguments")
193195
return ErrInvalidArgs
194196
}
195197

@@ -285,10 +287,6 @@ func resolveArgBranches(cfg *config.Config, opts *initOptions, sf *stack.StackFi
285287
}
286288
exists := git.BranchExists(b)
287289

288-
if opts.adopt && !exists {
289-
cfg.Errorf("branch %q does not exist", b)
290-
return nil, nil, ErrInvalidArgs
291-
}
292290
if err := sf.ValidateNoDuplicateBranch(b); err != nil {
293291
cfg.Errorf("branch %q already exists in a stack", b)
294292
return nil, nil, ErrInvalidArgs

cmd/init_test.go

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -166,38 +166,22 @@ func TestInit_InvalidPrefixRejectedBeforeBranchCreation(t *testing.T) {
166166
assert.Empty(t, created, "no branches should be created when prefix is invalid")
167167
}
168168

169-
func TestInit_AdoptRejectsPrefix(t *testing.T) {
170-
gitDir := t.TempDir()
171-
restore := git.SetOps(&git.MockOps{
172-
GitDirFn: func() (string, error) { return gitDir, nil },
173-
DefaultBranchFn: func() (string, error) { return "main", nil },
174-
CurrentBranchFn: func() (string, error) { return "main", nil },
175-
})
176-
defer restore()
177-
178-
cfg, outR, errR := config.NewTestConfig()
179-
err := runInit(cfg, &initOptions{adopt: true, branches: []string{"b1"}, prefix: "feat"})
180-
output := collectOutput(cfg, outR, errR)
181-
182-
assert.ErrorIs(t, err, ErrInvalidArgs)
183-
assert.Contains(t, output, "--adopt cannot be combined with --prefix or --numbered")
184-
}
185-
186-
func TestInit_AdoptRejectsNumbered(t *testing.T) {
169+
func TestInit_AdoptFlagShowsDeprecationWarning(t *testing.T) {
187170
gitDir := t.TempDir()
188171
restore := git.SetOps(&git.MockOps{
189172
GitDirFn: func() (string, error) { return gitDir, nil },
190173
DefaultBranchFn: func() (string, error) { return "main", nil },
191174
CurrentBranchFn: func() (string, error) { return "main", nil },
175+
BranchExistsFn: func(string) bool { return true },
192176
})
193177
defer restore()
194178

195179
cfg, outR, errR := config.NewTestConfig()
196-
err := runInit(cfg, &initOptions{adopt: true, branches: []string{"b1"}, numbered: true})
180+
err := runInit(cfg, &initOptions{adopt: true, branches: []string{"b1"}})
197181
output := collectOutput(cfg, outR, errR)
198182

199-
assert.ErrorIs(t, err, ErrInvalidArgs)
200-
assert.Contains(t, output, "--adopt cannot be combined with --prefix or --numbered")
183+
require.NoError(t, err)
184+
assert.Contains(t, output, "--adopt flag is deprecated")
201185
}
202186

203187
func TestInit_RerereAlreadyEnabled(t *testing.T) {
@@ -249,21 +233,29 @@ func TestInit_RefuseIfBranchAlreadyInStack(t *testing.T) {
249233
assert.Contains(t, output, "already part of a stack")
250234
}
251235

252-
func TestInit_AdoptNonexistentBranch(t *testing.T) {
236+
func TestInit_AdoptNonexistentBranch_CreatesIt(t *testing.T) {
237+
// --adopt with missing branch now creates it (no error, just a deprecation warning)
253238
gitDir := t.TempDir()
239+
var created []string
254240
restore := git.SetOps(&git.MockOps{
255241
GitDirFn: func() (string, error) { return gitDir, nil },
256242
DefaultBranchFn: func() (string, error) { return "main", nil },
257243
CurrentBranchFn: func() (string, error) { return "main", nil },
258244
BranchExistsFn: func(string) bool { return false },
245+
CreateBranchFn: func(name, base string) error {
246+
created = append(created, name)
247+
return nil
248+
},
259249
})
260250
defer restore()
261251

262252
cfg, outR, errR := config.NewTestConfig()
263-
runInit(cfg, &initOptions{branches: []string{"nonexistent"}, adopt: true})
253+
err := runInit(cfg, &initOptions{branches: []string{"nonexistent"}, adopt: true})
264254
output := collectOutput(cfg, outR, errR)
265255

266-
assert.Contains(t, output, "does not exist")
256+
require.NoError(t, err)
257+
assert.Contains(t, output, "--adopt flag is deprecated")
258+
assert.Equal(t, []string{"nonexistent"}, created)
267259
}
268260

269261
func TestInit_MultipleBranches_CreatesAll(t *testing.T) {
@@ -472,25 +464,6 @@ func TestInit_ImplicitAdopt_Mixed(t *testing.T) {
472464
assert.Equal(t, []string{"existing1", "new1", "existing2"}, sf.Stacks[0].BranchNames())
473465
}
474466

475-
func TestInit_StrictAdopt_MissingBranch(t *testing.T) {
476-
// Scenario 13: --adopt with missing branch → error
477-
gitDir := t.TempDir()
478-
restore := git.SetOps(&git.MockOps{
479-
GitDirFn: func() (string, error) { return gitDir, nil },
480-
DefaultBranchFn: func() (string, error) { return "main", nil },
481-
CurrentBranchFn: func() (string, error) { return "main", nil },
482-
BranchExistsFn: func(name string) bool { return name == "b1" },
483-
})
484-
defer restore()
485-
486-
cfg, outR, errR := config.NewTestConfig()
487-
err := runInit(cfg, &initOptions{branches: []string{"b1", "b2-missing"}, adopt: true})
488-
output := collectOutput(cfg, outR, errR)
489-
490-
assert.ErrorIs(t, err, ErrInvalidArgs)
491-
assert.Contains(t, output, "does not exist")
492-
}
493-
494467
func TestInit_PrefixDetection_ArgsCommonPrefix(t *testing.T) {
495468
// Scenario 9: args all share prefix → set silently
496469
gitDir := t.TempDir()

0 commit comments

Comments
 (0)