mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-02-07 13:42:01 +02:00
Rename WillBeAppliedReverse to Reverse
This is the only "reverse"-related option that is left, so use a less clumsy name for it.
This commit is contained in:
parent
45cf993982
commit
4bd1322941
@ -45,7 +45,7 @@ func headerInfo(header string) (int, int, string) {
|
||||
return oldStart, newStart, heading
|
||||
}
|
||||
|
||||
func (hunk *PatchHunk) updatedLines(lineIndices []int, willBeAppliedReverse bool) []string {
|
||||
func (hunk *PatchHunk) updatedLines(lineIndices []int, reverse bool) []string {
|
||||
skippedNewlineMessageIndex := -1
|
||||
newLines := []string{}
|
||||
|
||||
@ -58,7 +58,7 @@ func (hunk *PatchHunk) updatedLines(lineIndices []int, willBeAppliedReverse bool
|
||||
isLineSelected := lo.Contains(lineIndices, lineIdx)
|
||||
|
||||
firstChar, content := line[:1], line[1:]
|
||||
transformedFirstChar := transformedFirstChar(firstChar, willBeAppliedReverse, isLineSelected)
|
||||
transformedFirstChar := transformedFirstChar(firstChar, reverse, isLineSelected)
|
||||
|
||||
if isLineSelected || (transformedFirstChar == "\\" && skippedNewlineMessageIndex != lineIdx) || transformedFirstChar == " " {
|
||||
newLines = append(newLines, transformedFirstChar+content)
|
||||
@ -74,9 +74,9 @@ func (hunk *PatchHunk) updatedLines(lineIndices []int, willBeAppliedReverse bool
|
||||
return newLines
|
||||
}
|
||||
|
||||
func transformedFirstChar(firstChar string, willBeAppliedReverse bool, isLineSelected bool) string {
|
||||
func transformedFirstChar(firstChar string, reverse bool, isLineSelected bool) string {
|
||||
linesToKeepInPatchContext := "-"
|
||||
if willBeAppliedReverse {
|
||||
if reverse {
|
||||
linesToKeepInPatchContext = "+"
|
||||
}
|
||||
if !isLineSelected && firstChar == linesToKeepInPatchContext {
|
||||
@ -90,8 +90,8 @@ 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, willBeAppliedReverse bool, startOffset int) (int, string) {
|
||||
bodyLines := hunk.updatedLines(lineIndices, willBeAppliedReverse)
|
||||
func (hunk *PatchHunk) formatWithChanges(lineIndices []int, reverse bool, startOffset int) (int, string) {
|
||||
bodyLines := hunk.updatedLines(lineIndices, reverse)
|
||||
startOffset, header, ok := hunk.updatedHeader(bodyLines, startOffset)
|
||||
if !ok {
|
||||
return startOffset, ""
|
||||
|
@ -162,7 +162,7 @@ func (p *PatchManager) RemoveFileLineRange(filename string, firstLineIdx, lastLi
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *PatchManager) renderPlainPatchForFile(filename string, willBeAppliedReverse bool) string {
|
||||
func (p *PatchManager) renderPlainPatchForFile(filename string, reverse bool) string {
|
||||
info, err := p.getFileInfo(filename)
|
||||
if err != nil {
|
||||
p.Log.Error(err)
|
||||
@ -178,16 +178,16 @@ 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{
|
||||
WillBeAppliedReverse: willBeAppliedReverse,
|
||||
KeepOriginalHeader: true,
|
||||
Reverse: reverse,
|
||||
KeepOriginalHeader: true,
|
||||
})
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PatchManager) RenderPatchForFile(filename string, plain bool, willBeAppliedReverse bool) string {
|
||||
patch := p.renderPlainPatchForFile(filename, willBeAppliedReverse)
|
||||
func (p *PatchManager) RenderPatchForFile(filename string, plain bool, reverse bool) string {
|
||||
patch := p.renderPlainPatchForFile(filename, reverse)
|
||||
if plain {
|
||||
return patch
|
||||
}
|
||||
|
@ -14,12 +14,12 @@ var (
|
||||
)
|
||||
|
||||
type PatchOptions struct {
|
||||
// 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.
|
||||
WillBeAppliedReverse bool
|
||||
// Create a patch that will applied in reverse with `git apply --reverse`.
|
||||
// This affects how unselected lines are treated when only parts of a hunk
|
||||
// are selected: usually, for unselected lines we change '-' lines to
|
||||
// context lines and remove '+' lines, but when Reverse is true we need to
|
||||
// turn '+' lines into context lines and remove '-' lines.
|
||||
Reverse bool
|
||||
|
||||
// Whether to keep or discard the original diff header including the
|
||||
// "index deadbeef..fa1afe1 100644" line.
|
||||
@ -109,7 +109,7 @@ outer:
|
||||
var formattedHunk string
|
||||
for _, hunk := range hunksInRange {
|
||||
startOffset, formattedHunk = hunk.formatWithChanges(
|
||||
lineIndices, opts.WillBeAppliedReverse, startOffset)
|
||||
lineIndices, opts.Reverse, startOffset)
|
||||
formattedHunks += formattedHunk
|
||||
}
|
||||
|
||||
|
@ -115,13 +115,13 @@ const exampleHunk = `@@ -1,5 +1,5 @@
|
||||
// TestModifyPatchForRange is a function.
|
||||
func TestModifyPatchForRange(t *testing.T) {
|
||||
type scenario struct {
|
||||
testName string
|
||||
filename string
|
||||
diffText string
|
||||
firstLineIndex int
|
||||
lastLineIndex int
|
||||
willBeAppliedReverse bool
|
||||
expected string
|
||||
testName string
|
||||
filename string
|
||||
diffText string
|
||||
firstLineIndex int
|
||||
lastLineIndex int
|
||||
reverse bool
|
||||
expected string
|
||||
}
|
||||
|
||||
scenarios := []scenario{
|
||||
@ -366,12 +366,12 @@ func TestModifyPatchForRange(t *testing.T) {
|
||||
`,
|
||||
},
|
||||
{
|
||||
testName: "adding part of a hunk",
|
||||
filename: "filename",
|
||||
firstLineIndex: 6,
|
||||
lastLineIndex: 7,
|
||||
willBeAppliedReverse: false,
|
||||
diffText: twoChangesInOneHunk,
|
||||
testName: "adding part of a hunk",
|
||||
filename: "filename",
|
||||
firstLineIndex: 6,
|
||||
lastLineIndex: 7,
|
||||
reverse: false,
|
||||
diffText: twoChangesInOneHunk,
|
||||
expected: `--- a/filename
|
||||
+++ b/filename
|
||||
@@ -1,5 +1,5 @@
|
||||
@ -384,12 +384,12 @@ func TestModifyPatchForRange(t *testing.T) {
|
||||
`,
|
||||
},
|
||||
{
|
||||
testName: "adding part of a hunk, will-be-applied-reverse",
|
||||
filename: "filename",
|
||||
firstLineIndex: 6,
|
||||
lastLineIndex: 7,
|
||||
willBeAppliedReverse: true,
|
||||
diffText: twoChangesInOneHunk,
|
||||
testName: "adding part of a hunk, reverse",
|
||||
filename: "filename",
|
||||
firstLineIndex: 6,
|
||||
lastLineIndex: 7,
|
||||
reverse: true,
|
||||
diffText: twoChangesInOneHunk,
|
||||
expected: `--- a/filename
|
||||
+++ b/filename
|
||||
@@ -1,5 +1,5 @@
|
||||
@ -408,8 +408,8 @@ 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{
|
||||
WillBeAppliedReverse: s.willBeAppliedReverse,
|
||||
KeepOriginalHeader: false,
|
||||
Reverse: s.reverse,
|
||||
KeepOriginalHeader: false,
|
||||
})
|
||||
if !assert.Equal(t, s.expected, result) {
|
||||
fmt.Println(result)
|
||||
|
@ -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{WillBeAppliedReverse: reverse, KeepOriginalHeader: false})
|
||||
patch.PatchOptions{Reverse: 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{WillBeAppliedReverse: self.staged, KeepOriginalHeader: false},
|
||||
patch.PatchOptions{Reverse: self.staged, KeepOriginalHeader: false},
|
||||
)
|
||||
patchFilepath, err := self.git.WorkingTree.SaveTemporaryPatch(patchText)
|
||||
if err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user