diff --git a/pkg/commands/patch/hunk.go b/pkg/commands/patch/hunk.go index a2727f2c9..e0aeb4157 100644 --- a/pkg/commands/patch/hunk.go +++ b/pkg/commands/patch/hunk.go @@ -45,7 +45,7 @@ func headerInfo(header string) (int, int, string) { return oldStart, newStart, heading } -func (hunk *PatchHunk) updatedLines(lineIndices []int, reverse bool, willBeAppliedReverse bool) []string { +func (hunk *PatchHunk) updatedLines(lineIndices []int, willBeAppliedReverse bool) []string { skippedNewlineMessageIndex := -1 newLines := []string{} @@ -58,7 +58,7 @@ func (hunk *PatchHunk) updatedLines(lineIndices []int, reverse bool, willBeAppli isLineSelected := lo.Contains(lineIndices, lineIdx) firstChar, content := line[:1], line[1:] - transformedFirstChar := transformedFirstChar(firstChar, reverse, willBeAppliedReverse, isLineSelected) + transformedFirstChar := transformedFirstChar(firstChar, willBeAppliedReverse, isLineSelected) if isLineSelected || (transformedFirstChar == "\\" && skippedNewlineMessageIndex != lineIdx) || transformedFirstChar == " " { newLines = append(newLines, transformedFirstChar+content) @@ -74,19 +74,7 @@ func (hunk *PatchHunk) updatedLines(lineIndices []int, reverse bool, willBeAppli return newLines } -func transformedFirstChar(firstChar string, reverse bool, willBeAppliedReverse bool, isLineSelected bool) string { - if reverse { - if !isLineSelected && firstChar == "+" { - return " " - } else if firstChar == "-" { - return "+" - } else if firstChar == "+" { - return "-" - } else { - return firstChar - } - } - +func transformedFirstChar(firstChar string, willBeAppliedReverse bool, isLineSelected bool) string { linesToKeepInPatchContext := "-" if willBeAppliedReverse { linesToKeepInPatchContext = "+" @@ -102,16 +90,16 @@ func (hunk *PatchHunk) formatHeader(oldStart int, oldLength int, newStart int, n return fmt.Sprintf("@@ -%d,%d +%d,%d @@%s\n", oldStart, oldLength, newStart, newLength, heading) } -func (hunk *PatchHunk) formatWithChanges(lineIndices []int, reverse bool, willBeAppliedReverse bool, startOffset int) (int, string) { - bodyLines := hunk.updatedLines(lineIndices, reverse, willBeAppliedReverse) - startOffset, header, ok := hunk.updatedHeader(bodyLines, startOffset, reverse) +func (hunk *PatchHunk) formatWithChanges(lineIndices []int, willBeAppliedReverse bool, startOffset int) (int, string) { + bodyLines := hunk.updatedLines(lineIndices, willBeAppliedReverse) + startOffset, header, ok := hunk.updatedHeader(bodyLines, startOffset) if !ok { return startOffset, "" } return startOffset, header + strings.Join(bodyLines, "") } -func (hunk *PatchHunk) updatedHeader(newBodyLines []string, startOffset int, reverse bool) (int, string, bool) { +func (hunk *PatchHunk) updatedHeader(newBodyLines []string, startOffset int) (int, string, bool) { changeCount := nLinesWithPrefix(newBodyLines, []string{"+", "-"}) oldLength := nLinesWithPrefix(newBodyLines, []string{" ", "-"}) newLength := nLinesWithPrefix(newBodyLines, []string{"+", " "}) @@ -121,12 +109,7 @@ func (hunk *PatchHunk) updatedHeader(newBodyLines []string, startOffset int, rev return startOffset, "", false } - var oldStart int - if reverse { - oldStart = hunk.newStart - } else { - oldStart = hunk.oldStart - } + oldStart := hunk.oldStart var newStartOffset int // if the hunk went from zero to positive length, we need to increment the starting point by one diff --git a/pkg/commands/patch/patch_manager.go b/pkg/commands/patch/patch_manager.go index e50c0fd3a..7c7197583 100644 --- a/pkg/commands/patch/patch_manager.go +++ b/pkg/commands/patch/patch_manager.go @@ -178,7 +178,6 @@ func (p *PatchManager) renderPlainPatchForFile(filename string, willBeAppliedRev // generate a new diff with just the selected lines return ModifiedPatchForLines(p.Log, filename, info.diff, info.includedLineIndices, PatchOptions{ - Reverse: false, WillBeAppliedReverse: willBeAppliedReverse, KeepOriginalHeader: true, }) diff --git a/pkg/commands/patch/patch_modifier.go b/pkg/commands/patch/patch_modifier.go index 985743b76..c8e97e42d 100644 --- a/pkg/commands/patch/patch_modifier.go +++ b/pkg/commands/patch/patch_modifier.go @@ -14,17 +14,11 @@ var ( ) type PatchOptions struct { - // Create a reverse patch; in other words, flip all the '+' and '-' while - // generating the patch. - Reverse bool - // If true, we're building a patch that we are going to apply using // "git apply --reverse". In other words, we are not flipping the '+' and // '-' ourselves while creating the patch, but git is going to do that when // applying. This has consequences for which lines we need to keep or // discard when filtering lines from partial hunks. - // - // Currently incompatible with Reverse. WillBeAppliedReverse bool // Whether to keep or discard the original diff header including the @@ -96,10 +90,6 @@ func NewPatchModifier(log *logrus.Entry, filename string, diffText string) *Patc } func (d *PatchModifier) ModifiedPatchForLines(lineIndices []int, opts PatchOptions) string { - if opts.Reverse && opts.KeepOriginalHeader { - panic("reverse and keepOriginalHeader are not compatible") - } - // step one is getting only those hunks which we care about hunksInRange := []*PatchHunk{} outer: @@ -119,7 +109,7 @@ outer: var formattedHunk string for _, hunk := range hunksInRange { startOffset, formattedHunk = hunk.formatWithChanges( - lineIndices, opts.Reverse, opts.WillBeAppliedReverse, startOffset) + lineIndices, opts.WillBeAppliedReverse, startOffset) formattedHunks += formattedHunk } diff --git a/pkg/commands/patch/patch_modifier_test.go b/pkg/commands/patch/patch_modifier_test.go index c97fd89be..55fdfb547 100644 --- a/pkg/commands/patch/patch_modifier_test.go +++ b/pkg/commands/patch/patch_modifier_test.go @@ -120,7 +120,6 @@ func TestModifyPatchForRange(t *testing.T) { diffText string firstLineIndex int lastLineIndex int - reverse bool willBeAppliedReverse bool expected string } @@ -131,7 +130,6 @@ func TestModifyPatchForRange(t *testing.T) { filename: "filename", firstLineIndex: -1, lastLineIndex: -1, - reverse: false, diffText: simpleDiff, expected: "", }, @@ -140,7 +138,6 @@ func TestModifyPatchForRange(t *testing.T) { filename: "filename", firstLineIndex: 5, lastLineIndex: 5, - reverse: false, diffText: simpleDiff, expected: "", }, @@ -149,7 +146,6 @@ func TestModifyPatchForRange(t *testing.T) { filename: "filename", firstLineIndex: 0, lastLineIndex: 11, - reverse: false, diffText: simpleDiff, expected: `--- a/filename +++ b/filename @@ -167,7 +163,6 @@ func TestModifyPatchForRange(t *testing.T) { filename: "filename", firstLineIndex: 6, lastLineIndex: 6, - reverse: false, diffText: simpleDiff, expected: `--- a/filename +++ b/filename @@ -184,7 +179,6 @@ func TestModifyPatchForRange(t *testing.T) { filename: "filename", firstLineIndex: 7, lastLineIndex: 7, - reverse: false, diffText: simpleDiff, expected: `--- a/filename +++ b/filename @@ -202,7 +196,6 @@ func TestModifyPatchForRange(t *testing.T) { filename: "filename", firstLineIndex: -100, lastLineIndex: 100, - reverse: false, diffText: simpleDiff, expected: `--- a/filename +++ b/filename @@ -213,59 +206,6 @@ func TestModifyPatchForRange(t *testing.T) { ... ... ... -`, - }, - { - testName: "whole range reversed", - filename: "filename", - firstLineIndex: 0, - lastLineIndex: 11, - reverse: true, - diffText: simpleDiff, - expected: `--- a/filename -+++ b/filename -@@ -1,5 +1,5 @@ - apple -+orange --grape - ... - ... - ... -`, - }, - { - testName: "removal reversed", - filename: "filename", - firstLineIndex: 6, - lastLineIndex: 6, - reverse: true, - diffText: simpleDiff, - expected: `--- a/filename -+++ b/filename -@@ -1,5 +1,6 @@ - apple -+orange - grape - ... - ... - ... -`, - }, - { - testName: "removal reversed", - filename: "filename", - firstLineIndex: 7, - lastLineIndex: 7, - reverse: true, - diffText: simpleDiff, - expected: `--- a/filename -+++ b/filename -@@ -1,5 +1,4 @@ - apple --grape - ... - ... - ... `, }, { @@ -273,7 +213,6 @@ func TestModifyPatchForRange(t *testing.T) { filename: "filename", firstLineIndex: -100, lastLineIndex: 100, - reverse: false, diffText: addNewlineToEndOfFile, expected: `--- a/filename +++ b/filename @@ -284,40 +223,6 @@ func TestModifyPatchForRange(t *testing.T) { -last line \ No newline at end of file +last line -`, - }, - { - testName: "add newline to end of file, addition only", - filename: "filename", - firstLineIndex: 8, - lastLineIndex: 8, - reverse: true, - diffText: addNewlineToEndOfFile, - expected: `--- a/filename -+++ b/filename -@@ -60,4 +60,5 @@ grape - ... - ... - ... -+last line -\ No newline at end of file - last line -`, - }, - { - testName: "add newline to end of file, removal only", - filename: "filename", - firstLineIndex: 10, - lastLineIndex: 10, - reverse: true, - diffText: addNewlineToEndOfFile, - expected: `--- a/filename -+++ b/filename -@@ -60,4 +60,3 @@ grape - ... - ... - ... --last line `, }, { @@ -325,7 +230,6 @@ func TestModifyPatchForRange(t *testing.T) { filename: "filename", firstLineIndex: -100, lastLineIndex: 100, - reverse: false, diffText: removeNewlinefromEndOfFile, expected: `--- a/filename +++ b/filename @@ -343,7 +247,6 @@ func TestModifyPatchForRange(t *testing.T) { filename: "filename", firstLineIndex: 8, lastLineIndex: 8, - reverse: false, diffText: removeNewlinefromEndOfFile, expected: `--- a/filename +++ b/filename @@ -359,7 +262,6 @@ func TestModifyPatchForRange(t *testing.T) { filename: "filename", firstLineIndex: 9, lastLineIndex: 9, - reverse: false, diffText: removeNewlinefromEndOfFile, expected: `--- a/filename +++ b/filename @@ -377,7 +279,6 @@ func TestModifyPatchForRange(t *testing.T) { filename: "filename", firstLineIndex: -100, lastLineIndex: 100, - reverse: false, diffText: twoHunks, expected: `--- a/filename +++ b/filename @@ -404,7 +305,6 @@ func TestModifyPatchForRange(t *testing.T) { filename: "filename", firstLineIndex: 7, lastLineIndex: 15, - reverse: false, diffText: twoHunks, expected: `--- a/filename +++ b/filename @@ -423,32 +323,6 @@ func TestModifyPatchForRange(t *testing.T) { ... ... ... -`, - }, - { - testName: "staging part of both hunks, reversed", - filename: "filename", - firstLineIndex: 7, - lastLineIndex: 15, - reverse: true, - diffText: twoHunks, - expected: `--- a/filename -+++ b/filename -@@ -1,5 +1,4 @@ - apple --orange - ... - ... - ... -@@ -8,8 +7,7 @@ grape - ... - ... - ... --pear - lemon - ... - ... - ... `, }, { @@ -456,7 +330,6 @@ func TestModifyPatchForRange(t *testing.T) { filename: "newfile", firstLineIndex: -100, lastLineIndex: 100, - reverse: false, diffText: newFile, expected: `--- a/newfile +++ b/newfile @@ -471,28 +344,12 @@ func TestModifyPatchForRange(t *testing.T) { filename: "newfile", firstLineIndex: 6, lastLineIndex: 7, - reverse: false, diffText: newFile, expected: `--- a/newfile +++ b/newfile @@ -0,0 +1,2 @@ +apple +orange -`, - }, - { - testName: "adding a new file, reversed", - filename: "newfile", - firstLineIndex: -100, - lastLineIndex: 100, - reverse: true, - diffText: newFile, - expected: `--- a/newfile -+++ b/newfile -@@ -1,3 +0,0 @@ --apple --orange --grape `, }, { @@ -500,27 +357,12 @@ func TestModifyPatchForRange(t *testing.T) { filename: "newfile", firstLineIndex: -100, lastLineIndex: 100, - reverse: false, diffText: addNewlineToPreviouslyEmptyFile, expected: `--- a/newfile +++ b/newfile @@ -0,0 +1,1 @@ +new line \ No newline at end of file -`, - }, - { - testName: "adding a new line to a previously empty file, reversed", - filename: "newfile", - firstLineIndex: -100, - lastLineIndex: 100, - reverse: true, - diffText: addNewlineToPreviouslyEmptyFile, - expected: `--- a/newfile -+++ b/newfile -@@ -1,1 +0,0 @@ --new line -\ No newline at end of file `, }, { @@ -528,7 +370,6 @@ func TestModifyPatchForRange(t *testing.T) { filename: "filename", firstLineIndex: 6, lastLineIndex: 7, - reverse: false, willBeAppliedReverse: false, diffText: twoChangesInOneHunk, expected: `--- a/filename @@ -547,7 +388,6 @@ func TestModifyPatchForRange(t *testing.T) { filename: "filename", firstLineIndex: 6, lastLineIndex: 7, - reverse: false, willBeAppliedReverse: true, diffText: twoChangesInOneHunk, expected: `--- a/filename @@ -568,7 +408,6 @@ func TestModifyPatchForRange(t *testing.T) { t.Run(s.testName, func(t *testing.T) { result := ModifiedPatchForRange(nil, s.filename, s.diffText, s.firstLineIndex, s.lastLineIndex, PatchOptions{ - Reverse: s.reverse, WillBeAppliedReverse: s.willBeAppliedReverse, KeepOriginalHeader: false, }) diff --git a/pkg/gui/controllers/staging_controller.go b/pkg/gui/controllers/staging_controller.go index b29361aca..2fef137d0 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: false, WillBeAppliedReverse: reverse, KeepOriginalHeader: false}) + patch.PatchOptions{WillBeAppliedReverse: reverse, KeepOriginalHeader: false}) if patch == "" { return nil @@ -232,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: false, WillBeAppliedReverse: self.staged, KeepOriginalHeader: false}, + patch.PatchOptions{WillBeAppliedReverse: self.staged, KeepOriginalHeader: false}, ) patchFilepath, err := self.git.WorkingTree.SaveTemporaryPatch(patchText) if err != nil { @@ -255,7 +255,7 @@ func (self *StagingController) editHunk() error { lineCount := strings.Count(editedPatchText, "\n") + 1 newPatchText := patch.ModifiedPatchForRange( self.c.Log, path, editedPatchText, 0, lineCount, - patch.PatchOptions{Reverse: false, KeepOriginalHeader: false}, + patch.PatchOptions{KeepOriginalHeader: false}, ) applyFlags := []string{"cached"}