diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2019-11-05 18:44:46 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2019-11-05 19:22:01 +1100 |
commit | 72fe77097406bf4299fe74720e459cc229dd2391 (patch) | |
tree | afc218582ed65c9815bb9ffbcad6d833bd5fca65 /pkg/commands | |
parent | db8c398fa33c6240bb4deca08aa6b5553497ef64 (diff) |
better interface for ApplyPatch function
Diffstat (limited to 'pkg/commands')
-rw-r--r-- | pkg/commands/git.go | 15 | ||||
-rw-r--r-- | pkg/commands/git_test.go | 2 | ||||
-rw-r--r-- | pkg/commands/patch_manager.go | 8 |
3 files changed, 10 insertions, 15 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 1448b37cb..e306efe13 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -613,24 +613,19 @@ func (c *GitCommand) Diff(file *File, plain bool, cached bool) string { return s } -func (c *GitCommand) ApplyPatch(patch string, reverse bool, cached bool, extraFlags string) error { +func (c *GitCommand) ApplyPatch(patch string, flags ...string) error { c.Log.Warn(patch) filepath := filepath.Join(c.Config.GetUserConfigDir(), utils.GetCurrentRepoName(), time.Now().Format(time.StampNano)+".patch") if err := c.OSCommand.CreateFileWithContent(filepath, patch); err != nil { return err } - reverseFlag := "" - if reverse { - reverseFlag = "--reverse" + flagStr := "" + for _, flag := range flags { + flagStr += " --" + flag } - cachedFlag := "" - if cached { - cachedFlag = "--cached" - } - - return c.OSCommand.RunCommand(fmt.Sprintf("git apply %s %s %s %s", cachedFlag, reverseFlag, extraFlags, c.OSCommand.Quote(filepath))) + return c.OSCommand.RunCommand(fmt.Sprintf("git apply %s %s", flagStr, c.OSCommand.Quote(filepath))) } func (c *GitCommand) FastForward(branchName string) error { diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 52db798ed..3ff5aa66d 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -1733,7 +1733,7 @@ func TestGitCommandApplyPatch(t *testing.T) { t.Run(s.testName, func(t *testing.T) { gitCmd := NewDummyGitCommand() gitCmd.OSCommand.command = s.command - s.test(gitCmd.ApplyPatch("test", false, true, "")) + s.test(gitCmd.ApplyPatch("test", "cached")) }) } } diff --git a/pkg/commands/patch_manager.go b/pkg/commands/patch_manager.go index 5191c2348..671b90f45 100644 --- a/pkg/commands/patch_manager.go +++ b/pkg/commands/patch_manager.go @@ -13,7 +13,7 @@ type fileInfo struct { diff string } -type applyPatchFunc func(patch string, reverse bool, cached bool, extraFlags string) error +type applyPatchFunc func(patch string, flags ...string) error // PatchManager manages the building of a patch for a commit to be applied to another commit (or the working tree, or removed from the current commit) type PatchManager struct { @@ -177,11 +177,11 @@ func (p *PatchManager) ApplyPatches(reverse bool) error { continue } + applyFlags := []string{"index", "3way"} reverseOnGenerate := false - reverseOnApply := false if reverse { if info.mode == WHOLE { - reverseOnApply = true + applyFlags = append(applyFlags, "reverse") } else { reverseOnGenerate = true } @@ -194,7 +194,7 @@ func (p *PatchManager) ApplyPatches(reverse bool) error { if patch == "" { continue } - if err = p.ApplyPatch(patch, reverseOnApply, false, "--index --3way"); err != nil { + if err = p.ApplyPatch(patch, applyFlags...); err != nil { continue } break |