better interface for ApplyPatch function

This commit is contained in:
Jesse Duffield 2019-11-05 18:44:46 +11:00
parent db8c398fa3
commit 72fe770974
4 changed files with 16 additions and 17 deletions

View file

@ -613,24 +613,19 @@ func (c *GitCommand) Diff(file *File, plain bool, cached bool) string {
return s 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) c.Log.Warn(patch)
filepath := filepath.Join(c.Config.GetUserConfigDir(), utils.GetCurrentRepoName(), time.Now().Format(time.StampNano)+".patch") filepath := filepath.Join(c.Config.GetUserConfigDir(), utils.GetCurrentRepoName(), time.Now().Format(time.StampNano)+".patch")
if err := c.OSCommand.CreateFileWithContent(filepath, patch); err != nil { if err := c.OSCommand.CreateFileWithContent(filepath, patch); err != nil {
return err return err
} }
reverseFlag := "" flagStr := ""
if reverse { for _, flag := range flags {
reverseFlag = "--reverse" flagStr += " --" + flag
} }
cachedFlag := "" return c.OSCommand.RunCommand(fmt.Sprintf("git apply %s %s", flagStr, c.OSCommand.Quote(filepath)))
if cached {
cachedFlag = "--cached"
}
return c.OSCommand.RunCommand(fmt.Sprintf("git apply %s %s %s %s", cachedFlag, reverseFlag, extraFlags, c.OSCommand.Quote(filepath)))
} }
func (c *GitCommand) FastForward(branchName string) error { func (c *GitCommand) FastForward(branchName string) error {

View file

@ -1733,7 +1733,7 @@ func TestGitCommandApplyPatch(t *testing.T) {
t.Run(s.testName, func(t *testing.T) { t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand() gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.command = s.command gitCmd.OSCommand.command = s.command
s.test(gitCmd.ApplyPatch("test", false, true, "")) s.test(gitCmd.ApplyPatch("test", "cached"))
}) })
} }
} }

View file

@ -13,7 +13,7 @@ type fileInfo struct {
diff string 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) // 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 { type PatchManager struct {
@ -177,11 +177,11 @@ func (p *PatchManager) ApplyPatches(reverse bool) error {
continue continue
} }
applyFlags := []string{"index", "3way"}
reverseOnGenerate := false reverseOnGenerate := false
reverseOnApply := false
if reverse { if reverse {
if info.mode == WHOLE { if info.mode == WHOLE {
reverseOnApply = true applyFlags = append(applyFlags, "reverse")
} else { } else {
reverseOnGenerate = true reverseOnGenerate = true
} }
@ -194,7 +194,7 @@ func (p *PatchManager) ApplyPatches(reverse bool) error {
if patch == "" { if patch == "" {
continue continue
} }
if err = p.ApplyPatch(patch, reverseOnApply, false, "--index --3way"); err != nil { if err = p.ApplyPatch(patch, applyFlags...); err != nil {
continue continue
} }
break break

View file

@ -100,9 +100,13 @@ func (gui *Gui) applySelection(reverse bool) error {
// apply the patch then refresh this panel // apply the patch then refresh this panel
// create a new temp file with the patch, then call git apply with that patch // create a new temp file with the patch, then call git apply with that patch
err = gui.GitCommand.ApplyPatch(patch, false, !reverse || state.SecondaryFocused, "") applyFlags := []string{}
if !reverse || state.SecondaryFocused {
applyFlags = append(applyFlags, "cached")
}
err = gui.GitCommand.ApplyPatch(patch, applyFlags...)
if err != nil { if err != nil {
return err return gui.createErrorPanel(gui.g, err.Error())
} }
if state.SelectMode == RANGE { if state.SelectMode == RANGE {