diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2023-03-08 16:55:44 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2023-03-19 16:30:39 +1100 |
commit | 73c7dc9c5d00408e156724ff5b9bd792b4d17273 (patch) | |
tree | 634d633fec7c788672e407f8f63b08efef1776dd /pkg/gui/controllers/staging_controller.go | |
parent | b542579db31f160a8d13d255b447d654d253db17 (diff) |
refactor patch code
Diffstat (limited to 'pkg/gui/controllers/staging_controller.go')
-rw-r--r-- | pkg/gui/controllers/staging_controller.go | 44 |
1 files changed, 29 insertions, 15 deletions
diff --git a/pkg/gui/controllers/staging_controller.go b/pkg/gui/controllers/staging_controller.go index 78c271640..0738ae1dc 100644 --- a/pkg/gui/controllers/staging_controller.go +++ b/pkg/gui/controllers/staging_controller.go @@ -181,10 +181,16 @@ 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}) - - if patch == "" { + patchToApply := patch. + Parse(state.GetDiff()). + Transform(patch.TransformOpts{ + Reverse: reverse, + IncludedLineIndices: patch.ExpandRange(firstLineIdx, lastLineIdx), + FileNameOverride: path, + }). + FormatPlain() + + if patchToApply == "" { return nil } @@ -198,7 +204,7 @@ func (self *StagingController) applySelection(reverse bool) error { applyFlags = append(applyFlags, "cached") } self.c.LogAction(self.c.Tr.Actions.ApplyPatch) - err := self.git.WorkingTree.ApplyPatch(patch, applyFlags...) + err := self.git.WorkingTree.ApplyPatch(patchToApply, applyFlags...) if err != nil { return self.c.Error(err) } @@ -229,18 +235,23 @@ func (self *StagingController) editHunk() error { return nil } - hunk := state.CurrentHunk() - patchText := patch.ModifiedPatchForRange( - self.c.Log, path, state.GetDiff(), hunk.FirstLineIdx, hunk.LastLineIdx(), - patch.PatchOptions{Reverse: self.staged, KeepOriginalHeader: false}, - ) + hunkStartIdx, hunkEndIdx := state.CurrentHunkBounds() + patchText := patch. + Parse(state.GetDiff()). + Transform(patch.TransformOpts{ + Reverse: self.staged, + IncludedLineIndices: patch.ExpandRange(hunkStartIdx, hunkEndIdx), + FileNameOverride: path, + }). + FormatPlain() + patchFilepath, err := self.git.WorkingTree.SaveTemporaryPatch(patchText) if err != nil { return err } lineOffset := 3 - lineIdxInHunk := state.GetSelectedLineIdx() - hunk.FirstLineIdx + lineIdxInHunk := state.GetSelectedLineIdx() - hunkStartIdx if err := self.helpers.Files.EditFileAtLine(patchFilepath, lineIdxInHunk+lineOffset); err != nil { return err } @@ -253,10 +264,13 @@ func (self *StagingController) editHunk() error { self.c.LogAction(self.c.Tr.Actions.ApplyPatch) lineCount := strings.Count(editedPatchText, "\n") + 1 - newPatchText := patch.ModifiedPatchForRange( - self.c.Log, path, editedPatchText, 0, lineCount, - patch.PatchOptions{KeepOriginalHeader: false}, - ) + newPatchText := patch. + Parse(editedPatchText). + Transform(patch.TransformOpts{ + IncludedLineIndices: patch.ExpandRange(0, lineCount), + FileNameOverride: path, + }). + FormatPlain() applyFlags := []string{"cached"} if self.staged { |