From e4659145e89f8b84e4c12078eca5361ce0715611 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 7 Mar 2023 08:55:48 +0100 Subject: [PATCH] Use WillBeAppliedReverse (and git apply --reverse) in the staging panel too It's simpler to have only one way of reversing a patch. --- pkg/gui/controllers/staging_controller.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pkg/gui/controllers/staging_controller.go b/pkg/gui/controllers/staging_controller.go index 840c18a18..b29361aca 100644 --- a/pkg/gui/controllers/staging_controller.go +++ b/pkg/gui/controllers/staging_controller.go @@ -182,7 +182,7 @@ func (self *StagingController) applySelection(reverse bool) error { firstLineIdx, lastLineIdx := state.SelectedRange() patch := patch.ModifiedPatchForRange(self.c.Log, path, state.GetDiff(), firstLineIdx, lastLineIdx, - patch.PatchOptions{Reverse: reverse, KeepOriginalHeader: false}) + patch.PatchOptions{Reverse: false, WillBeAppliedReverse: reverse, KeepOriginalHeader: false}) if patch == "" { return nil @@ -191,6 +191,9 @@ func (self *StagingController) applySelection(reverse bool) error { // apply the patch then refresh this panel // create a new temp file with the patch, then call git apply with that patch applyFlags := []string{} + if reverse { + applyFlags = append(applyFlags, "reverse") + } if !reverse || self.staged { applyFlags = append(applyFlags, "cached") } @@ -229,7 +232,7 @@ func (self *StagingController) editHunk() error { hunk := state.CurrentHunk() patchText := patch.ModifiedPatchForRange( self.c.Log, path, state.GetDiff(), hunk.FirstLineIdx, hunk.LastLineIdx(), - patch.PatchOptions{Reverse: self.staged, KeepOriginalHeader: false}, + patch.PatchOptions{Reverse: false, WillBeAppliedReverse: self.staged, KeepOriginalHeader: false}, ) patchFilepath, err := self.git.WorkingTree.SaveTemporaryPatch(patchText) if err != nil { @@ -254,7 +257,12 @@ func (self *StagingController) editHunk() error { self.c.Log, path, editedPatchText, 0, lineCount, patch.PatchOptions{Reverse: false, KeepOriginalHeader: false}, ) - if err := self.git.WorkingTree.ApplyPatch(newPatchText, "cached"); err != nil { + + applyFlags := []string{"cached"} + if self.staged { + applyFlags = append(applyFlags, "reverse") + } + if err := self.git.WorkingTree.ApplyPatch(newPatchText, applyFlags...); err != nil { return self.c.Error(err) }