Skip to content

Commit f8c0100

Browse files
skarimCopilot
andauthored
remove silent prefix detection in init (#126)
* Remove silent prefix detection from args path in init When explicit branch names containing slashes were passed to `gh stack init` (e.g. `gh stack init myprefix/branch`), detectPrefix would silently extract the prefix and store it in the stack config. This caused `gh stack add otherbranch` to unexpectedly produce `myprefix/otherbranch` without the user ever opting in. Remove the automatic prefix detection from the args path so that explicit branch names are taken literally. Users who want a prefix should use `--prefix`. The interactive path (no args) continues to prompt for confirmation before setting a prefix. * Remove dead detectPrefix function and its tests After removing the silent prefix detection from the args path, detectPrefix has no production callers. Remove the function and its table-driven unit test to avoid maintaining unused code. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8c2d9e3 commit f8c0100

2 files changed

Lines changed: 7 additions & 57 deletions

File tree

cmd/init.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,6 @@ func runInit(cfg *config.Config, opts *initOptions) error {
149149
return err
150150
}
151151

152-
// Prefix detection (only when --prefix not explicitly set)
153-
if opts.prefix == "" {
154-
if detected := detectPrefix(branches); detected != "" {
155-
opts.prefix = detected
156-
}
157-
}
158-
159152
} else if opts.numbered {
160153
// === NUMBERED PATH (unchanged) ===
161154
if opts.prefix == "" && cfg.IsInteractive() {
@@ -455,29 +448,6 @@ func promptBranchName(cfg *config.Config, prefix string) (string, error) {
455448
return branchName, nil
456449
}
457450

458-
// detectPrefix finds a common prefix across branches by splitting each
459-
// at its last slash. Returns the prefix (without trailing slash) if all
460-
// branches share the same one, or "" otherwise.
461-
func detectPrefix(branches []string) string {
462-
if len(branches) == 0 {
463-
return ""
464-
}
465-
var common string
466-
for i, b := range branches {
467-
lastSlash := strings.LastIndex(b, "/")
468-
if lastSlash <= 0 {
469-
return "" // no slash or leading slash — no prefix
470-
}
471-
prefix := b[:lastSlash]
472-
if i == 0 {
473-
common = prefix
474-
} else if prefix != common {
475-
return "" // different prefixes
476-
}
477-
}
478-
return common
479-
}
480-
481451
// printWhatsNext prints the scenario-aware "What's next" block after init.
482452
func printWhatsNext(cfg *config.Config, s *stack.Stack, branches []string, hasAdopted bool, prCount int) {
483453
lastBranch := branches[len(branches)-1]

cmd/init_test.go

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,9 @@ func TestInit_ImplicitAdopt_Mixed(t *testing.T) {
465465
}
466466

467467
func TestInit_PrefixDetection_ArgsCommonPrefix(t *testing.T) {
468-
// Scenario 9: args all share prefix → set silently
468+
// Explicit branch names with a common prefix should NOT auto-detect
469+
// a prefix — the slash is part of the branch name, not a convention.
470+
// Users who want a prefix should use --prefix.
469471
gitDir := t.TempDir()
470472
restore := git.SetOps(&git.MockOps{
471473
GitDirFn: func() (string, error) { return gitDir, nil },
@@ -481,7 +483,7 @@ func TestInit_PrefixDetection_ArgsCommonPrefix(t *testing.T) {
481483

482484
require.NoError(t, err)
483485
sf, _ := stack.Load(gitDir)
484-
assert.Equal(t, "feat", sf.Stacks[0].Prefix)
486+
assert.Equal(t, "", sf.Stacks[0].Prefix)
485487
}
486488

487489
func TestInit_PrefixDetection_ArgsMixedPrefix(t *testing.T) {
@@ -525,7 +527,8 @@ func TestInit_PrefixDetection_ArgsNoSlash(t *testing.T) {
525527
}
526528

527529
func TestInit_PrefixDetection_NestedPrefix(t *testing.T) {
528-
// Scenario 6: sameen/feat/x → prefix "sameen/feat"
530+
// Explicit branch names with nested slashes should NOT auto-detect
531+
// a prefix — the user typed the full branch name deliberately.
529532
gitDir := t.TempDir()
530533
restore := git.SetOps(&git.MockOps{
531534
GitDirFn: func() (string, error) { return gitDir, nil },
@@ -541,7 +544,7 @@ func TestInit_PrefixDetection_NestedPrefix(t *testing.T) {
541544

542545
require.NoError(t, err)
543546
sf, _ := stack.Load(gitDir)
544-
assert.Equal(t, "sameen/feat", sf.Stacks[0].Prefix)
547+
assert.Equal(t, "", sf.Stacks[0].Prefix)
545548
}
546549

547550
func TestInit_ExplicitPrefixSkipsDetection(t *testing.T) {
@@ -795,26 +798,3 @@ func TestInit_TwoPassValidation_InvalidRefName(t *testing.T) {
795798
assert.Contains(t, output, "invalid branch name")
796799
assert.Empty(t, created, "no branches should be created when an arg has an invalid ref name")
797800
}
798-
799-
func TestDetectPrefix(t *testing.T) {
800-
tests := []struct {
801-
name string
802-
branches []string
803-
want string
804-
}{
805-
{"common prefix", []string{"feat/a", "feat/b", "feat/c"}, "feat"},
806-
{"nested prefix", []string{"sameen/feat/a", "sameen/feat/b"}, "sameen/feat"},
807-
{"mixed prefixes", []string{"feat/a", "bug/b"}, ""},
808-
{"no slashes", []string{"auth", "api", "ui"}, ""},
809-
{"empty list", []string{}, ""},
810-
{"single branch with slash", []string{"feat/x"}, "feat"},
811-
{"single branch no slash", []string{"auth"}, ""},
812-
{"leading slash only", []string{"/x"}, ""},
813-
}
814-
for _, tt := range tests {
815-
t.Run(tt.name, func(t *testing.T) {
816-
got := detectPrefix(tt.branches)
817-
assert.Equal(t, tt.want, got)
818-
})
819-
}
820-
}

0 commit comments

Comments
 (0)