1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-05-23 22:50:41 +02:00

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.
This commit is contained in:
Stefan Haller 2023-03-07 09:31:38 +01:00
parent e4659145e8
commit 45cf993982
5 changed files with 12 additions and 201 deletions

View File

@ -45,7 +45,7 @@ func headerInfo(header string) (int, int, string) {
return oldStart, newStart, heading 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 skippedNewlineMessageIndex := -1
newLines := []string{} newLines := []string{}
@ -58,7 +58,7 @@ func (hunk *PatchHunk) updatedLines(lineIndices []int, reverse bool, willBeAppli
isLineSelected := lo.Contains(lineIndices, lineIdx) isLineSelected := lo.Contains(lineIndices, lineIdx)
firstChar, content := line[:1], line[1:] firstChar, content := line[:1], line[1:]
transformedFirstChar := transformedFirstChar(firstChar, reverse, willBeAppliedReverse, isLineSelected) transformedFirstChar := transformedFirstChar(firstChar, willBeAppliedReverse, isLineSelected)
if isLineSelected || (transformedFirstChar == "\\" && skippedNewlineMessageIndex != lineIdx) || transformedFirstChar == " " { if isLineSelected || (transformedFirstChar == "\\" && skippedNewlineMessageIndex != lineIdx) || transformedFirstChar == " " {
newLines = append(newLines, transformedFirstChar+content) newLines = append(newLines, transformedFirstChar+content)
@ -74,19 +74,7 @@ func (hunk *PatchHunk) updatedLines(lineIndices []int, reverse bool, willBeAppli
return newLines return newLines
} }
func transformedFirstChar(firstChar string, reverse bool, willBeAppliedReverse bool, isLineSelected bool) string { func transformedFirstChar(firstChar string, willBeAppliedReverse bool, isLineSelected bool) string {
if reverse {
if !isLineSelected && firstChar == "+" {
return " "
} else if firstChar == "-" {
return "+"
} else if firstChar == "+" {
return "-"
} else {
return firstChar
}
}
linesToKeepInPatchContext := "-" linesToKeepInPatchContext := "-"
if willBeAppliedReverse { if willBeAppliedReverse {
linesToKeepInPatchContext = "+" 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) 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) { func (hunk *PatchHunk) formatWithChanges(lineIndices []int, willBeAppliedReverse bool, startOffset int) (int, string) {
bodyLines := hunk.updatedLines(lineIndices, reverse, willBeAppliedReverse) bodyLines := hunk.updatedLines(lineIndices, willBeAppliedReverse)
startOffset, header, ok := hunk.updatedHeader(bodyLines, startOffset, reverse) startOffset, header, ok := hunk.updatedHeader(bodyLines, startOffset)
if !ok { if !ok {
return startOffset, "" return startOffset, ""
} }
return startOffset, header + strings.Join(bodyLines, "") 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{"+", "-"}) changeCount := nLinesWithPrefix(newBodyLines, []string{"+", "-"})
oldLength := nLinesWithPrefix(newBodyLines, []string{" ", "-"}) oldLength := nLinesWithPrefix(newBodyLines, []string{" ", "-"})
newLength := nLinesWithPrefix(newBodyLines, []string{"+", " "}) newLength := nLinesWithPrefix(newBodyLines, []string{"+", " "})
@ -121,12 +109,7 @@ func (hunk *PatchHunk) updatedHeader(newBodyLines []string, startOffset int, rev
return startOffset, "", false return startOffset, "", false
} }
var oldStart int oldStart := hunk.oldStart
if reverse {
oldStart = hunk.newStart
} else {
oldStart = hunk.oldStart
}
var newStartOffset int var newStartOffset int
// if the hunk went from zero to positive length, we need to increment the starting point by one // if the hunk went from zero to positive length, we need to increment the starting point by one

View File

@ -178,7 +178,6 @@ func (p *PatchManager) renderPlainPatchForFile(filename string, willBeAppliedRev
// generate a new diff with just the selected lines // generate a new diff with just the selected lines
return ModifiedPatchForLines(p.Log, filename, info.diff, info.includedLineIndices, return ModifiedPatchForLines(p.Log, filename, info.diff, info.includedLineIndices,
PatchOptions{ PatchOptions{
Reverse: false,
WillBeAppliedReverse: willBeAppliedReverse, WillBeAppliedReverse: willBeAppliedReverse,
KeepOriginalHeader: true, KeepOriginalHeader: true,
}) })

View File

@ -14,17 +14,11 @@ var (
) )
type PatchOptions struct { 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 // 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 // "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 // '-' 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 // applying. This has consequences for which lines we need to keep or
// discard when filtering lines from partial hunks. // discard when filtering lines from partial hunks.
//
// Currently incompatible with Reverse.
WillBeAppliedReverse bool WillBeAppliedReverse bool
// Whether to keep or discard the original diff header including the // 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 { 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 // step one is getting only those hunks which we care about
hunksInRange := []*PatchHunk{} hunksInRange := []*PatchHunk{}
outer: outer:
@ -119,7 +109,7 @@ outer:
var formattedHunk string var formattedHunk string
for _, hunk := range hunksInRange { for _, hunk := range hunksInRange {
startOffset, formattedHunk = hunk.formatWithChanges( startOffset, formattedHunk = hunk.formatWithChanges(
lineIndices, opts.Reverse, opts.WillBeAppliedReverse, startOffset) lineIndices, opts.WillBeAppliedReverse, startOffset)
formattedHunks += formattedHunk formattedHunks += formattedHunk
} }

View File

@ -120,7 +120,6 @@ func TestModifyPatchForRange(t *testing.T) {
diffText string diffText string
firstLineIndex int firstLineIndex int
lastLineIndex int lastLineIndex int
reverse bool
willBeAppliedReverse bool willBeAppliedReverse bool
expected string expected string
} }
@ -131,7 +130,6 @@ func TestModifyPatchForRange(t *testing.T) {
filename: "filename", filename: "filename",
firstLineIndex: -1, firstLineIndex: -1,
lastLineIndex: -1, lastLineIndex: -1,
reverse: false,
diffText: simpleDiff, diffText: simpleDiff,
expected: "", expected: "",
}, },
@ -140,7 +138,6 @@ func TestModifyPatchForRange(t *testing.T) {
filename: "filename", filename: "filename",
firstLineIndex: 5, firstLineIndex: 5,
lastLineIndex: 5, lastLineIndex: 5,
reverse: false,
diffText: simpleDiff, diffText: simpleDiff,
expected: "", expected: "",
}, },
@ -149,7 +146,6 @@ func TestModifyPatchForRange(t *testing.T) {
filename: "filename", filename: "filename",
firstLineIndex: 0, firstLineIndex: 0,
lastLineIndex: 11, lastLineIndex: 11,
reverse: false,
diffText: simpleDiff, diffText: simpleDiff,
expected: `--- a/filename expected: `--- a/filename
+++ b/filename +++ b/filename
@ -167,7 +163,6 @@ func TestModifyPatchForRange(t *testing.T) {
filename: "filename", filename: "filename",
firstLineIndex: 6, firstLineIndex: 6,
lastLineIndex: 6, lastLineIndex: 6,
reverse: false,
diffText: simpleDiff, diffText: simpleDiff,
expected: `--- a/filename expected: `--- a/filename
+++ b/filename +++ b/filename
@ -184,7 +179,6 @@ func TestModifyPatchForRange(t *testing.T) {
filename: "filename", filename: "filename",
firstLineIndex: 7, firstLineIndex: 7,
lastLineIndex: 7, lastLineIndex: 7,
reverse: false,
diffText: simpleDiff, diffText: simpleDiff,
expected: `--- a/filename expected: `--- a/filename
+++ b/filename +++ b/filename
@ -202,7 +196,6 @@ func TestModifyPatchForRange(t *testing.T) {
filename: "filename", filename: "filename",
firstLineIndex: -100, firstLineIndex: -100,
lastLineIndex: 100, lastLineIndex: 100,
reverse: false,
diffText: simpleDiff, diffText: simpleDiff,
expected: `--- a/filename expected: `--- a/filename
+++ b/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", filename: "filename",
firstLineIndex: -100, firstLineIndex: -100,
lastLineIndex: 100, lastLineIndex: 100,
reverse: false,
diffText: addNewlineToEndOfFile, diffText: addNewlineToEndOfFile,
expected: `--- a/filename expected: `--- a/filename
+++ b/filename +++ b/filename
@ -284,40 +223,6 @@ func TestModifyPatchForRange(t *testing.T) {
-last line -last line
\ No newline at end of file \ No newline at end of file
+last line +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", filename: "filename",
firstLineIndex: -100, firstLineIndex: -100,
lastLineIndex: 100, lastLineIndex: 100,
reverse: false,
diffText: removeNewlinefromEndOfFile, diffText: removeNewlinefromEndOfFile,
expected: `--- a/filename expected: `--- a/filename
+++ b/filename +++ b/filename
@ -343,7 +247,6 @@ func TestModifyPatchForRange(t *testing.T) {
filename: "filename", filename: "filename",
firstLineIndex: 8, firstLineIndex: 8,
lastLineIndex: 8, lastLineIndex: 8,
reverse: false,
diffText: removeNewlinefromEndOfFile, diffText: removeNewlinefromEndOfFile,
expected: `--- a/filename expected: `--- a/filename
+++ b/filename +++ b/filename
@ -359,7 +262,6 @@ func TestModifyPatchForRange(t *testing.T) {
filename: "filename", filename: "filename",
firstLineIndex: 9, firstLineIndex: 9,
lastLineIndex: 9, lastLineIndex: 9,
reverse: false,
diffText: removeNewlinefromEndOfFile, diffText: removeNewlinefromEndOfFile,
expected: `--- a/filename expected: `--- a/filename
+++ b/filename +++ b/filename
@ -377,7 +279,6 @@ func TestModifyPatchForRange(t *testing.T) {
filename: "filename", filename: "filename",
firstLineIndex: -100, firstLineIndex: -100,
lastLineIndex: 100, lastLineIndex: 100,
reverse: false,
diffText: twoHunks, diffText: twoHunks,
expected: `--- a/filename expected: `--- a/filename
+++ b/filename +++ b/filename
@ -404,7 +305,6 @@ func TestModifyPatchForRange(t *testing.T) {
filename: "filename", filename: "filename",
firstLineIndex: 7, firstLineIndex: 7,
lastLineIndex: 15, lastLineIndex: 15,
reverse: false,
diffText: twoHunks, diffText: twoHunks,
expected: `--- a/filename expected: `--- a/filename
+++ b/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", filename: "newfile",
firstLineIndex: -100, firstLineIndex: -100,
lastLineIndex: 100, lastLineIndex: 100,
reverse: false,
diffText: newFile, diffText: newFile,
expected: `--- a/newfile expected: `--- a/newfile
+++ b/newfile +++ b/newfile
@ -471,28 +344,12 @@ func TestModifyPatchForRange(t *testing.T) {
filename: "newfile", filename: "newfile",
firstLineIndex: 6, firstLineIndex: 6,
lastLineIndex: 7, lastLineIndex: 7,
reverse: false,
diffText: newFile, diffText: newFile,
expected: `--- a/newfile expected: `--- a/newfile
+++ b/newfile +++ b/newfile
@@ -0,0 +1,2 @@ @@ -0,0 +1,2 @@
+apple +apple
+orange +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", filename: "newfile",
firstLineIndex: -100, firstLineIndex: -100,
lastLineIndex: 100, lastLineIndex: 100,
reverse: false,
diffText: addNewlineToPreviouslyEmptyFile, diffText: addNewlineToPreviouslyEmptyFile,
expected: `--- a/newfile expected: `--- a/newfile
+++ b/newfile +++ b/newfile
@@ -0,0 +1,1 @@ @@ -0,0 +1,1 @@
+new line +new line
\ No newline at end of file \ 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", filename: "filename",
firstLineIndex: 6, firstLineIndex: 6,
lastLineIndex: 7, lastLineIndex: 7,
reverse: false,
willBeAppliedReverse: false, willBeAppliedReverse: false,
diffText: twoChangesInOneHunk, diffText: twoChangesInOneHunk,
expected: `--- a/filename expected: `--- a/filename
@ -547,7 +388,6 @@ func TestModifyPatchForRange(t *testing.T) {
filename: "filename", filename: "filename",
firstLineIndex: 6, firstLineIndex: 6,
lastLineIndex: 7, lastLineIndex: 7,
reverse: false,
willBeAppliedReverse: true, willBeAppliedReverse: true,
diffText: twoChangesInOneHunk, diffText: twoChangesInOneHunk,
expected: `--- a/filename expected: `--- a/filename
@ -568,7 +408,6 @@ func TestModifyPatchForRange(t *testing.T) {
t.Run(s.testName, func(t *testing.T) { t.Run(s.testName, func(t *testing.T) {
result := ModifiedPatchForRange(nil, s.filename, s.diffText, s.firstLineIndex, s.lastLineIndex, result := ModifiedPatchForRange(nil, s.filename, s.diffText, s.firstLineIndex, s.lastLineIndex,
PatchOptions{ PatchOptions{
Reverse: s.reverse,
WillBeAppliedReverse: s.willBeAppliedReverse, WillBeAppliedReverse: s.willBeAppliedReverse,
KeepOriginalHeader: false, KeepOriginalHeader: false,
}) })

View File

@ -182,7 +182,7 @@ func (self *StagingController) applySelection(reverse bool) error {
firstLineIdx, lastLineIdx := state.SelectedRange() firstLineIdx, lastLineIdx := state.SelectedRange()
patch := patch.ModifiedPatchForRange(self.c.Log, path, state.GetDiff(), firstLineIdx, lastLineIdx, 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 == "" { if patch == "" {
return nil return nil
@ -232,7 +232,7 @@ func (self *StagingController) editHunk() error {
hunk := state.CurrentHunk() hunk := state.CurrentHunk()
patchText := patch.ModifiedPatchForRange( patchText := patch.ModifiedPatchForRange(
self.c.Log, path, state.GetDiff(), hunk.FirstLineIdx, hunk.LastLineIdx(), 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) patchFilepath, err := self.git.WorkingTree.SaveTemporaryPatch(patchText)
if err != nil { if err != nil {
@ -255,7 +255,7 @@ func (self *StagingController) editHunk() error {
lineCount := strings.Count(editedPatchText, "\n") + 1 lineCount := strings.Count(editedPatchText, "\n") + 1
newPatchText := patch.ModifiedPatchForRange( newPatchText := patch.ModifiedPatchForRange(
self.c.Log, path, editedPatchText, 0, lineCount, self.c.Log, path, editedPatchText, 0, lineCount,
patch.PatchOptions{Reverse: false, KeepOriginalHeader: false}, patch.PatchOptions{KeepOriginalHeader: false},
) )
applyFlags := []string{"cached"} applyFlags := []string{"cached"}