Skip to content

Commit 1a1f49f

Browse files
committed
exit with nonzero status on error
1 parent 50fdc33 commit 1a1f49f

14 files changed

Lines changed: 90 additions & 83 deletions

cmd/add.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ func runAdd(cfg *config.Config, opts *addOptions, args []string) error {
4646
// Validate flag combinations
4747
if opts.stageAll && opts.stageTracked {
4848
cfg.Errorf("flags -A and -u are mutually exclusive")
49-
return nil
49+
return ErrSilent
5050
}
5151

5252
result, err := loadStack(cfg, "")
5353
if err != nil {
54-
return nil
54+
return ErrSilent
5555
}
5656
gitDir := result.GitDir
5757
sf := result.StackFile
@@ -69,7 +69,7 @@ func runAdd(cfg *config.Config, opts *addOptions, args []string) error {
6969
// a new branch from it). Only block if we're in the middle of the stack.
7070
if idx >= 0 && idx < len(s.Branches)-1 {
7171
cfg.Errorf("can only add branches on top of the stack; run `%s` to switch to %q", cfg.ColorCyan("gh stack top"), s.Branches[len(s.Branches)-1].Branch)
72-
return nil
72+
return ErrSilent
7373
}
7474

7575
// Check if the current branch is a stack branch with no unique commits
@@ -88,12 +88,12 @@ func runAdd(cfg *config.Config, opts *addOptions, args []string) error {
8888
// Empty branch path: stage and commit here, don't create a new branch.
8989
if branchIsEmpty {
9090
if err := stageAndValidate(cfg, opts); err != nil {
91-
return nil
91+
return ErrSilent
9292
}
9393
sha, err := doCommit(opts.message)
9494
if err != nil {
9595
cfg.Errorf("failed to commit: %s", err)
96-
return nil
96+
return ErrSilent
9797
}
9898
cfg.Successf("Created commit %s on %s", cfg.ColorBold(sha), currentBranch)
9999
cfg.Warningf("Branch %s has no prior commits — adding your commit here instead of creating a new branch", currentBranch)
@@ -115,7 +115,7 @@ func runAdd(cfg *config.Config, opts *addOptions, args []string) error {
115115
name, info := branch.ResolveBranchName(s.Prefix, opts.message, explicitName, existingBranches, isFirstBranch)
116116
if name == "" {
117117
cfg.Errorf("could not generate branch name")
118-
return nil
118+
return ErrSilent
119119
}
120120
branchName = name
121121
if info != "" {
@@ -136,7 +136,7 @@ func runAdd(cfg *config.Config, opts *addOptions, args []string) error {
136136
if err != nil {
137137
if isInterruptError(err) {
138138
printInterrupt(cfg)
139-
return nil
139+
return ErrSilent
140140
}
141141
return fmt.Errorf("could not read branch name: %w", err)
142142
}
@@ -152,36 +152,36 @@ func runAdd(cfg *config.Config, opts *addOptions, args []string) error {
152152

153153
if branchName == "" {
154154
cfg.Errorf("branch name cannot be empty")
155-
return nil
155+
return ErrSilent
156156
}
157157

158158
if err := sf.ValidateNoDuplicateBranch(branchName); err != nil {
159159
cfg.Errorf("branch %q already exists in the stack", branchName)
160-
return nil
160+
return ErrSilent
161161
}
162162

163163
if git.BranchExists(branchName) {
164164
cfg.Errorf("branch %q already exists", branchName)
165-
return nil
165+
return ErrSilent
166166
}
167167

168168
// Stage changes before creating the branch so we can fail early if
169169
// there's nothing to commit (avoids leaving an empty orphan branch).
170170
if wantsCommit {
171171
if err := stageAndValidate(cfg, opts); err != nil {
172-
return nil
172+
return ErrSilent
173173
}
174174
}
175175

176176
// Create the new branch from the current HEAD and check it out
177177
if err := git.CreateBranch(branchName, currentBranch); err != nil {
178178
cfg.Errorf("failed to create branch: %s", err)
179-
return nil
179+
return ErrSilent
180180
}
181181

182182
if err := git.CheckoutBranch(branchName); err != nil {
183183
cfg.Errorf("failed to checkout branch: %s", err)
184-
return nil
184+
return ErrSilent
185185
}
186186

187187
base, err := git.RevParse(currentBranch)
@@ -196,14 +196,14 @@ func runAdd(cfg *config.Config, opts *addOptions, args []string) error {
196196
sha, err := doCommit(opts.message)
197197
if err != nil {
198198
cfg.Errorf("failed to commit: %s", err)
199-
return nil
199+
return ErrSilent
200200
}
201201
commitSHA = sha
202202
}
203203

204204
if err := stack.Save(gitDir, sf); err != nil {
205205
cfg.Errorf("failed to save stack state: %s", err)
206-
return nil
206+
return ErrSilent
207207
}
208208

209209
// Print summary

cmd/checkout.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ func runCheckout(cfg *config.Config, opts *checkoutOptions) error {
5353
gitDir, err := git.GitDir()
5454
if err != nil {
5555
cfg.Errorf("not a git repository")
56-
return nil
56+
return ErrSilent
5757
}
5858

5959
sf, err := stack.Load(gitDir)
6060
if err != nil {
6161
cfg.Errorf("failed to load stack state: %s", err)
62-
return nil
62+
return ErrSilent
6363
}
6464

6565
var s *stack.Stack
@@ -72,7 +72,7 @@ func runCheckout(cfg *config.Config, opts *checkoutOptions) error {
7272
if !errors.Is(err, errInterrupt) {
7373
cfg.Errorf("%s", err)
7474
}
75-
return nil
75+
return ErrSilent
7676
}
7777
if s == nil {
7878
return nil
@@ -85,7 +85,7 @@ func runCheckout(cfg *config.Config, opts *checkoutOptions) error {
8585
s, br, err = resolvePR(sf, opts.target)
8686
if err != nil {
8787
cfg.Errorf("%s", err)
88-
return nil
88+
return ErrSilent
8989
}
9090
targetBranch = br.Branch
9191
}
@@ -99,7 +99,7 @@ func runCheckout(cfg *config.Config, opts *checkoutOptions) error {
9999

100100
if err := git.CheckoutBranch(targetBranch); err != nil {
101101
cfg.Errorf("failed to checkout %s: %v", targetBranch, err)
102-
return nil
102+
return ErrSilent
103103
}
104104

105105
cfg.Successf("Switched to %s", targetBranch)

cmd/init.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,30 @@ func runInit(cfg *config.Config, opts *initOptions) error {
5252
gitDir, err := git.GitDir()
5353
if err != nil {
5454
cfg.Errorf("not a git repository")
55-
return nil
55+
return ErrSilent
5656
}
5757

5858
// Determine trunk branch
5959
trunk := opts.base
6060

6161
// Enable git rerere so conflict resolutions are remembered.
6262
if err := ensureRerere(cfg); errors.Is(err, errInterrupt) {
63-
return nil
63+
return ErrSilent
6464
}
6565

6666
if trunk == "" {
6767
trunk, err = git.DefaultBranch()
6868
if err != nil {
6969
cfg.Errorf("unable to determine default branch\nUse -b to specify the trunk branch")
70-
return nil
70+
return ErrSilent
7171
}
7272
}
7373

7474
// Load existing stack file
7575
sf, err := stack.Load(gitDir)
7676
if err != nil {
7777
cfg.Errorf("failed to load stack state: %s", err)
78-
return nil
78+
return ErrSilent
7979
}
8080

8181
// Set repository context
@@ -93,7 +93,7 @@ func runInit(cfg *config.Config, opts *initOptions) error {
9393
for _, s := range sf.FindAllStacksForBranch(currentBranch) {
9494
if s.IndexOf(currentBranch) >= 0 {
9595
cfg.Errorf("current branch %q is already part of a stack", currentBranch)
96-
return nil
96+
return ErrSilent
9797
}
9898
}
9999
}
@@ -104,16 +104,16 @@ func runInit(cfg *config.Config, opts *initOptions) error {
104104
// Adopt mode: validate all specified branches exist
105105
if len(opts.branches) == 0 {
106106
cfg.Errorf("--adopt requires at least one branch name")
107-
return nil
107+
return ErrSilent
108108
}
109109
for _, b := range opts.branches {
110110
if !git.BranchExists(b) {
111111
cfg.Errorf("branch %q does not exist", b)
112-
return nil
112+
return ErrSilent
113113
}
114114
if err := sf.ValidateNoDuplicateBranch(b); err != nil {
115115
cfg.Errorf("branch %q already exists in a stack", b)
116-
return nil
116+
return ErrSilent
117117
}
118118
}
119119
branches = opts.branches
@@ -132,7 +132,7 @@ func runInit(cfg *config.Config, opts *initOptions) error {
132132
state = "merged"
133133
}
134134
cfg.Errorf("branch %q already has a %s PR (#%d: %s)", b, state, pr.Number, pr.URL)
135-
return nil
135+
return ErrSilent
136136
}
137137
}
138138
}
@@ -141,12 +141,12 @@ func runInit(cfg *config.Config, opts *initOptions) error {
141141
for _, b := range opts.branches {
142142
if err := sf.ValidateNoDuplicateBranch(b); err != nil {
143143
cfg.Errorf("branch %q already exists in a stack", b)
144-
return nil
144+
return ErrSilent
145145
}
146146
if !git.BranchExists(b) {
147147
if err := git.CreateBranch(b, trunk); err != nil {
148148
cfg.Errorf("creating branch %s: %s", b, err)
149-
return nil
149+
return ErrSilent
150150
}
151151
}
152152
}
@@ -155,7 +155,7 @@ func runInit(cfg *config.Config, opts *initOptions) error {
155155
// Interactive mode
156156
if !cfg.IsInteractive() {
157157
cfg.Errorf("interactive input required; provide branch names or use --adopt")
158-
return nil
158+
return ErrSilent
159159
}
160160
p := prompter.New(cfg.In, cfg.Out, cfg.Err)
161161

@@ -165,10 +165,10 @@ func runInit(cfg *config.Config, opts *initOptions) error {
165165
if err != nil {
166166
if isInterruptError(err) {
167167
printInterrupt(cfg)
168-
return nil
168+
return ErrSilent
169169
}
170170
cfg.Errorf("failed to read prefix: %s", err)
171-
return nil
171+
return ErrSilent
172172
}
173173
opts.prefix = strings.TrimSpace(prefixInput)
174174
}
@@ -183,15 +183,15 @@ func runInit(cfg *config.Config, opts *initOptions) error {
183183
if err != nil {
184184
if isInterruptError(err) {
185185
printInterrupt(cfg)
186-
return nil
186+
return ErrSilent
187187
}
188188
cfg.Errorf("failed to confirm branch selection: %s", err)
189-
return nil
189+
return ErrSilent
190190
}
191191
if useCurrentBranch {
192192
if err := sf.ValidateNoDuplicateBranch(currentBranch); err != nil {
193193
cfg.Errorf("branch %q already exists in the stack", currentBranch)
194-
return nil
194+
return ErrSilent
195195
}
196196
branches = []string{currentBranch}
197197
}
@@ -206,10 +206,10 @@ func runInit(cfg *config.Config, opts *initOptions) error {
206206
if err != nil {
207207
if isInterruptError(err) {
208208
printInterrupt(cfg)
209-
return nil
209+
return ErrSilent
210210
}
211211
cfg.Errorf("failed to read branch name: %s", err)
212-
return nil
212+
return ErrSilent
213213
}
214214
branchName = strings.TrimSpace(branchName)
215215

@@ -218,20 +218,20 @@ func runInit(cfg *config.Config, opts *initOptions) error {
218218
branchName = branch.NextNumberedName(opts.prefix, nil)
219219
} else if branchName == "" {
220220
cfg.Errorf("branch name cannot be empty")
221-
return nil
221+
return ErrSilent
222222
} else if opts.prefix != "" {
223223
// Prepend prefix to the user-provided name
224224
branchName = opts.prefix + "/" + branchName
225225
}
226226

227227
if err := sf.ValidateNoDuplicateBranch(branchName); err != nil {
228228
cfg.Errorf("branch %q already exists in a stack", branchName)
229-
return nil
229+
return ErrSilent
230230
}
231231
if !git.BranchExists(branchName) {
232232
if err := git.CreateBranch(branchName, trunk); err != nil {
233233
cfg.Errorf("creating branch %s: %s", branchName, err)
234-
return nil
234+
return ErrSilent
235235
}
236236
}
237237
branches = []string{branchName}
@@ -242,7 +242,7 @@ func runInit(cfg *config.Config, opts *initOptions) error {
242242
if opts.prefix != "" {
243243
if err := git.ValidateRefName(opts.prefix); err != nil {
244244
cfg.Errorf("invalid prefix %q: must be a valid git ref component", opts.prefix)
245-
return nil
245+
return ErrSilent
246246
}
247247
}
248248

@@ -288,7 +288,7 @@ func runInit(cfg *config.Config, opts *initOptions) error {
288288
if currentBranch != lastBranch {
289289
if err := git.CheckoutBranch(lastBranch); err != nil {
290290
cfg.Errorf("switching to branch %s: %s", lastBranch, err)
291-
return nil
291+
return ErrSilent
292292
}
293293
cfg.Printf("Switched to branch %s", lastBranch)
294294
} else {

cmd/merge.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func runMerge(cfg *config.Config, target string) error {
3535
// Standard stack loading and validation.
3636
result, err := loadStack(cfg, "")
3737
if err != nil {
38-
return nil
38+
return ErrSilent
3939
}
4040
s := result.Stack
4141
currentBranch := result.CurrentBranch
@@ -52,7 +52,7 @@ func runMerge(cfg *config.Config, target string) error {
5252
_, br, err = resolvePR(result.StackFile, target)
5353
if err != nil {
5454
cfg.Errorf("%s", err)
55-
return nil
55+
return ErrSilent
5656
}
5757
} else {
5858
idx := s.IndexOf(currentBranch)
@@ -62,15 +62,15 @@ func runMerge(cfg *config.Config, target string) error {
6262
return nil
6363
}
6464
cfg.Errorf("current branch %q is not a stack branch (it may be the trunk)", currentBranch)
65-
return nil
65+
return ErrSilent
6666
}
6767
br = &s.Branches[idx]
6868
}
6969

7070
if br.PullRequest == nil {
7171
cfg.Errorf("no pull request found for branch %q", currentBranch)
7272
cfg.Printf(" Run %s to create PRs for this stack.", cfg.ColorCyan("gh stack push"))
73-
return nil
73+
return ErrSilent
7474
}
7575

7676
if br.IsMerged() {

cmd/merge_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestMerge_NoPullRequest(t *testing.T) {
4141
errOut, _ := io.ReadAll(errR)
4242
output := string(errOut)
4343

44-
assert.NoError(t, err)
44+
assert.ErrorIs(t, err, ErrSilent)
4545
assert.Contains(t, output, "no pull request found")
4646
assert.Contains(t, output, "gh stack push")
4747
}
@@ -145,7 +145,7 @@ func TestMerge_OnTrunk(t *testing.T) {
145145
errOut, _ := io.ReadAll(errR)
146146
output := string(errOut)
147147

148-
assert.NoError(t, err)
148+
assert.ErrorIs(t, err, ErrSilent)
149149
assert.Contains(t, output, "not a stack branch")
150150
}
151151

0 commit comments

Comments
 (0)