Skip to content

Commit 4e0194d

Browse files
committed
Replace MergeOpts struct with MergeVariant enum
- Squash and FastForwardOnly are mutually exclusive, and instead of asserting this at runtime, model the API so that they can't be passed together. - FastForwardOnly is unused, so remove it; however, we are going to need --ff and --no-ff in the next commit, so add those instead. - Instead of putting the enum into the MergeOpts struct, replace the struct by the enum. We can reintroduce the struct when we add more arguments, but for now it's an unnecessary indirection.
1 parent d334bae commit 4e0194d

File tree

3 files changed

+50
-21
lines changed

3 files changed

+50
-21
lines changed

pkg/commands/git_commands/branch.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,20 +250,35 @@ func (self *BranchCommands) Rename(oldName string, newName string) error {
250250
return self.cmd.New(cmdArgs).Run()
251251
}
252252

253-
type MergeOpts struct {
254-
FastForwardOnly bool
255-
Squash bool
256-
}
253+
type MergeVariant int
254+
255+
const (
256+
MERGE_VARIANT_REGULAR MergeVariant = iota
257+
MERGE_VARIANT_FAST_FORWARD
258+
MERGE_VARIANT_NON_FAST_FORWARD
259+
MERGE_VARIANT_SQUASH
260+
)
261+
262+
func (self *BranchCommands) Merge(branchName string, variant MergeVariant) error {
263+
extraArgs := func() []string {
264+
switch variant {
265+
case MERGE_VARIANT_REGULAR:
266+
return []string{}
267+
case MERGE_VARIANT_FAST_FORWARD:
268+
return []string{"--ff"}
269+
case MERGE_VARIANT_NON_FAST_FORWARD:
270+
return []string{"--no-ff"}
271+
case MERGE_VARIANT_SQUASH:
272+
return []string{"--squash", "--ff"}
273+
}
274+
275+
panic("shouldn't get here")
276+
}()
257277

258-
func (self *BranchCommands) Merge(branchName string, opts MergeOpts) error {
259-
if opts.Squash && opts.FastForwardOnly {
260-
panic("Squash and FastForwardOnly can't both be true")
261-
}
262278
cmdArgs := NewGitCmd("merge").
263279
Arg("--no-edit").
264280
Arg(strings.Fields(self.UserConfig().Git.Merging.Args)...).
265-
ArgIf(opts.FastForwardOnly, "--ff-only").
266-
ArgIf(opts.Squash, "--squash", "--ff").
281+
Arg(extraArgs...).
267282
Arg(branchName).
268283
ToArgv()
269284

pkg/commands/git_commands/branch_test.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ func TestBranchMerge(t *testing.T) {
122122
scenarios := []struct {
123123
testName string
124124
userConfig *config.UserConfig
125-
opts MergeOpts
125+
variant MergeVariant
126126
branchName string
127127
expected []string
128128
}{
129129
{
130130
testName: "basic",
131131
userConfig: &config.UserConfig{},
132-
opts: MergeOpts{},
132+
variant: MERGE_VARIANT_REGULAR,
133133
branchName: "mybranch",
134134
expected: []string{"merge", "--no-edit", "mybranch"},
135135
},
@@ -142,7 +142,7 @@ func TestBranchMerge(t *testing.T) {
142142
},
143143
},
144144
},
145-
opts: MergeOpts{},
145+
variant: MERGE_VARIANT_REGULAR,
146146
branchName: "mybranch",
147147
expected: []string{"merge", "--no-edit", "--merging-args", "mybranch"},
148148
},
@@ -155,16 +155,30 @@ func TestBranchMerge(t *testing.T) {
155155
},
156156
},
157157
},
158-
opts: MergeOpts{},
158+
variant: MERGE_VARIANT_REGULAR,
159159
branchName: "mybranch",
160160
expected: []string{"merge", "--no-edit", "--arg1", "--arg2", "mybranch"},
161161
},
162162
{
163-
testName: "fast forward only",
163+
testName: "fast-forward merge",
164164
userConfig: &config.UserConfig{},
165-
opts: MergeOpts{FastForwardOnly: true},
165+
variant: MERGE_VARIANT_FAST_FORWARD,
166166
branchName: "mybranch",
167-
expected: []string{"merge", "--no-edit", "--ff-only", "mybranch"},
167+
expected: []string{"merge", "--no-edit", "--ff", "mybranch"},
168+
},
169+
{
170+
testName: "non-fast-forward merge",
171+
userConfig: &config.UserConfig{},
172+
variant: MERGE_VARIANT_NON_FAST_FORWARD,
173+
branchName: "mybranch",
174+
expected: []string{"merge", "--no-edit", "--no-ff", "mybranch"},
175+
},
176+
{
177+
testName: "squash merge",
178+
userConfig: &config.UserConfig{},
179+
variant: MERGE_VARIANT_SQUASH,
180+
branchName: "mybranch",
181+
expected: []string{"merge", "--no-edit", "--squash", "--ff", "mybranch"},
168182
},
169183
}
170184

@@ -174,7 +188,7 @@ func TestBranchMerge(t *testing.T) {
174188
ExpectGitArgs(s.expected, "", nil)
175189
instance := buildBranchCommands(commonDeps{runner: runner, userConfig: s.userConfig})
176190

177-
assert.NoError(t, instance.Merge(s.branchName, s.opts))
191+
assert.NoError(t, instance.Merge(s.branchName, s.variant))
178192
runner.CheckForMissingCalls()
179193
})
180194
}

pkg/gui/controllers/helpers/merge_and_rebase_helper.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,23 +426,23 @@ func (self *MergeAndRebaseHelper) MergeRefIntoCheckedOutBranch(refName string) e
426426
func (self *MergeAndRebaseHelper) RegularMerge(refName string) func() error {
427427
return func() error {
428428
self.c.LogAction(self.c.Tr.Actions.Merge)
429-
err := self.c.Git().Branch.Merge(refName, git_commands.MergeOpts{})
429+
err := self.c.Git().Branch.Merge(refName, git_commands.MERGE_VARIANT_REGULAR)
430430
return self.CheckMergeOrRebase(err)
431431
}
432432
}
433433

434434
func (self *MergeAndRebaseHelper) SquashMergeUncommitted(refName string) func() error {
435435
return func() error {
436436
self.c.LogAction(self.c.Tr.Actions.SquashMerge)
437-
err := self.c.Git().Branch.Merge(refName, git_commands.MergeOpts{Squash: true})
437+
err := self.c.Git().Branch.Merge(refName, git_commands.MERGE_VARIANT_SQUASH)
438438
return self.CheckMergeOrRebase(err)
439439
}
440440
}
441441

442442
func (self *MergeAndRebaseHelper) SquashMergeCommitted(refName, checkedOutBranchName string) func() error {
443443
return func() error {
444444
self.c.LogAction(self.c.Tr.Actions.SquashMerge)
445-
err := self.c.Git().Branch.Merge(refName, git_commands.MergeOpts{Squash: true})
445+
err := self.c.Git().Branch.Merge(refName, git_commands.MERGE_VARIANT_SQUASH)
446446
if err = self.CheckMergeOrRebase(err); err != nil {
447447
return err
448448
}

0 commit comments

Comments
 (0)