Skip to content

Commit f3db397

Browse files
committed
block init inside an existing stack
1 parent 157d87c commit f3db397

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

cmd/init.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func runInit(cfg *config.Config, opts *initOptions) error {
5656
if trunk == "" {
5757
trunk, err = git.DefaultBranch()
5858
if err != nil {
59-
cfg.Errorf("unable to determine default branch: %s\nUse -b to specify the trunk branch", err)
59+
cfg.Errorf("unable to determine default branch\nUse -b to specify the trunk branch")
6060
return nil
6161
}
6262
}
@@ -76,6 +76,14 @@ func runInit(cfg *config.Config, opts *initOptions) error {
7676

7777
currentBranch, _ := git.CurrentBranch()
7878

79+
// Don't allow initializing a stack if the current branch is already part of another stack
80+
if currentBranch != "" {
81+
if existing := sf.FindStackForBranch(currentBranch); existing != nil {
82+
cfg.Errorf("current branch %q is already part of a stack", currentBranch)
83+
return nil
84+
}
85+
}
86+
7987
var branches []string
8088

8189
if opts.adopt {
@@ -90,7 +98,7 @@ func runInit(cfg *config.Config, opts *initOptions) error {
9098
return nil
9199
}
92100
if err := sf.ValidateNoDuplicateBranch(b); err != nil {
93-
cfg.Errorf("branch %q already exists in the stack", b)
101+
cfg.Errorf("branch %q already exists in a stack", b)
94102
return nil
95103
}
96104
}
@@ -99,7 +107,7 @@ func runInit(cfg *config.Config, opts *initOptions) error {
99107
// Explicit branch names provided — create them
100108
for _, b := range opts.branches {
101109
if err := sf.ValidateNoDuplicateBranch(b); err != nil {
102-
cfg.Errorf("branch %q already exists in the stack", b)
110+
cfg.Errorf("branch %q already exists in a stack", b)
103111
return nil
104112
}
105113
if !git.BranchExists(b) {
@@ -144,7 +152,7 @@ func runInit(cfg *config.Config, opts *initOptions) error {
144152
return nil
145153
}
146154
if err := sf.ValidateNoDuplicateBranch(branchName); err != nil {
147-
cfg.Errorf("branch %q already exists in the stack", branchName)
155+
cfg.Errorf("branch %q already exists in a stack", branchName)
148156
return nil
149157
}
150158
if !git.BranchExists(branchName) {

internal/git/git.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package git
22

33
import (
44
"context"
5-
"fmt"
65
"os"
76
"os/exec"
87
"strconv"
@@ -73,16 +72,17 @@ func Fetch(remote string) error {
7372

7473
// --- Custom operations not available in cligit ---
7574

76-
// DefaultBranch returns the default branch of origin.
75+
// DefaultBranch returns the HEAD branch from origin.
7776
func DefaultBranch() (string, error) {
7877
ref, err := run("symbolic-ref", "refs/remotes/origin/HEAD")
78+
// fallback: if origin/HEAD doesn't exist, look for common default branch names
7979
if err != nil {
8080
for _, name := range []string{"main", "master"} {
8181
if BranchExists(name) {
8282
return name, nil
8383
}
8484
}
85-
return "", fmt.Errorf("unable to determine default branch: %w", err)
85+
return "", err
8686
}
8787
return strings.TrimPrefix(ref, "refs/remotes/origin/"), nil
8888
}

0 commit comments

Comments
 (0)