diff options
author | Stefan Haller <stefan@haller-berlin.de> | 2023-03-07 09:31:38 +0100 |
---|---|---|
committer | Stefan Haller <stefan@haller-berlin.de> | 2023-03-07 13:39:45 +0100 |
commit | 45cf993982f9b56afedd5fd6585c6d6fcd858181 (patch) | |
tree | 752f9dba99f340a7937853080c0f92024e2083b4 /pkg/commands/patch | |
parent | e4659145e89f8b84e4c12078eca5361ce0715611 (diff) |
Remove the PatchOptions.Reverse option
All callers pass false now (except for the tests, which we simply remove), so we
don't need the option any more.
Diffstat (limited to 'pkg/commands/patch')
-rw-r--r-- | pkg/commands/patch/hunk.go | 33 | ||||
-rw-r--r-- | pkg/commands/patch/patch_manager.go | 1 | ||||
-rw-r--r-- | pkg/commands/patch/patch_modifier.go | 12 | ||||
-rw-r--r-- | pkg/commands/patch/patch_modifier_test.go | 161 |
4 files changed, 9 insertions, 198 deletions
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 @@ -216,64 +209,10 @@ 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 - ... - ... - ... -`, - }, - { testName: "add newline to end of file", filename: "filename", firstLineIndex: -100, lastLineIndex: 100, - reverse: false, diffText: addNewlineToEndOfFile, expected: `--- a/filename +++ b/filename @@ -287,45 +226,10 @@ func TestModifyPatchForRange(t *testing.T) { `, }, { - 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 -`, - }, - { testName: "remove newline from end of file", 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 @@ -426,37 +326,10 @@ 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 - ... - ... - ... -`, - }, - { testName: "adding a new file", filename: "newfile", firstLineIndex: -100, lastLineIndex: 100, - reverse: false, diffText: newFile, expected: `--- a/newfile +++ b/newfile @@ -471,7 +344,6 @@ func TestModifyPatchForRange(t *testing.T) { filename: "newfile", firstLineIndex: 6, lastLineIndex: 7, - reverse: false, diffText: newFile, expected: `--- a/newfile +++ b/newfile @@ -481,26 +353,10 @@ func TestModifyPatchForRange(t *testing.T) { `, }, { - 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 -`, - }, - { testName: "adding a new line to a previously empty file", filename: "newfile", firstLineIndex: -100, lastLineIndex: 100, - reverse: false, diffText: addNewlineToPreviouslyEmptyFile, expected: `--- a/newfile +++ b/newfile @@ -510,25 +366,10 @@ func TestModifyPatchForRange(t *testing.T) { `, }, { - 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 -`, - }, - { testName: "adding part of a hunk", 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, }) |