mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-02-03 13:21:56 +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:
parent
e4659145e8
commit
45cf993982
@ -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
|
||||
|
@ -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,
|
||||
})
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
})
|
||||
|
@ -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"}
|
||||
|
Loading…
x
Reference in New Issue
Block a user