Skip to content

Commit 103fc37

Browse files
committed
fix for initial branch missing prefix
1 parent 90eb999 commit 103fc37

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

cmd/init.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,12 @@ func runInit(cfg *config.Config, opts *initOptions) error {
145145
}
146146
}
147147
} else if len(opts.branches) > 0 {
148-
// Explicit branch names provided — create them
148+
// Explicit branch names provided — apply prefix and create them
149+
prefixed := make([]string, 0, len(opts.branches))
149150
for _, b := range opts.branches {
151+
if opts.prefix != "" {
152+
b = opts.prefix + "/" + b
153+
}
150154
if err := sf.ValidateNoDuplicateBranch(b); err != nil {
151155
cfg.Errorf("branch %q already exists in a stack", b)
152156
return ErrInvalidArgs
@@ -157,8 +161,9 @@ func runInit(cfg *config.Config, opts *initOptions) error {
157161
return ErrSilent
158162
}
159163
}
164+
prefixed = append(prefixed, b)
160165
}
161-
branches = opts.branches
166+
branches = prefixed
162167
} else {
163168
// Interactive mode
164169
if !cfg.IsInteractive() {

cmd/init_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,33 @@ func TestInit_PrefixStoredInStack(t *testing.T) {
110110
assert.Equal(t, "feat", sf.Stacks[0].Prefix)
111111
}
112112

113+
func TestInit_PrefixAppliedToExplicitBranches(t *testing.T) {
114+
gitDir := t.TempDir()
115+
var created []string
116+
restore := git.SetOps(&git.MockOps{
117+
GitDirFn: func() (string, error) { return gitDir, nil },
118+
DefaultBranchFn: func() (string, error) { return "main", nil },
119+
CurrentBranchFn: func() (string, error) { return "main", nil },
120+
CreateBranchFn: func(name, base string) error {
121+
created = append(created, name)
122+
return nil
123+
},
124+
})
125+
defer restore()
126+
127+
cfg, outR, errR := config.NewTestConfig()
128+
runInit(cfg, &initOptions{branches: []string{"b1", "b2"}, prefix: "feat"})
129+
output := collectOutput(cfg, outR, errR)
130+
131+
require.NotContains(t, output, "\u2717", "unexpected error")
132+
assert.Equal(t, []string{"feat/b1", "feat/b2"}, created, "branches should be created with prefix")
133+
134+
sf, err := stack.Load(gitDir)
135+
require.NoError(t, err, "loading stack")
136+
names := sf.Stacks[0].BranchNames()
137+
assert.Equal(t, []string{"feat/b1", "feat/b2"}, names, "stack should store prefixed branch names")
138+
}
139+
113140
func TestInit_RerereAlreadyEnabled(t *testing.T) {
114141
gitDir := t.TempDir()
115142
enableRerereCalled := false

0 commit comments

Comments
 (0)