mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-17 00:18:05 +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:
@ -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, willBeAppliedReverse bool) []string {
|
func (hunk *PatchHunk) updatedLines(lineIndices []int, reverse bool) []string {
|
||||||
skippedNewlineMessageIndex := -1
|
skippedNewlineMessageIndex := -1
|
||||||
newLines := []string{}
|
newLines := []string{}
|
||||||
|
|
||||||
@ -58,7 +58,7 @@ func (hunk *PatchHunk) updatedLines(lineIndices []int, willBeAppliedReverse bool
|
|||||||
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, willBeAppliedReverse, isLineSelected)
|
transformedFirstChar := transformedFirstChar(firstChar, reverse, isLineSelected)
|
||||||
|
|
||||||
if isLineSelected || (transformedFirstChar == "\\" && skippedNewlineMessageIndex != lineIdx) || transformedFirstChar == " " {
|
if isLineSelected || (transformedFirstChar == "\\" && skippedNewlineMessageIndex != lineIdx) || transformedFirstChar == " " {
|
||||||
newLines = append(newLines, transformedFirstChar+content)
|
newLines = append(newLines, transformedFirstChar+content)
|
||||||
@ -74,9 +74,9 @@ func (hunk *PatchHunk) updatedLines(lineIndices []int, willBeAppliedReverse bool
|
|||||||
return newLines
|
return newLines
|
||||||
}
|
}
|
||||||
|
|
||||||
func transformedFirstChar(firstChar string, willBeAppliedReverse bool, isLineSelected bool) string {
|
func transformedFirstChar(firstChar string, reverse bool, isLineSelected bool) string {
|
||||||
linesToKeepInPatchContext := "-"
|
linesToKeepInPatchContext := "-"
|
||||||
if willBeAppliedReverse {
|
if reverse {
|
||||||
linesToKeepInPatchContext = "+"
|
linesToKeepInPatchContext = "+"
|
||||||
}
|
}
|
||||||
if !isLineSelected && firstChar == 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)
|
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) {
|
func (hunk *PatchHunk) formatWithChanges(lineIndices []int, reverse bool, startOffset int) (int, string) {
|
||||||
bodyLines := hunk.updatedLines(lineIndices, willBeAppliedReverse)
|
bodyLines := hunk.updatedLines(lineIndices, reverse)
|
||||||
startOffset, header, ok := hunk.updatedHeader(bodyLines, startOffset)
|
startOffset, header, ok := hunk.updatedHeader(bodyLines, startOffset)
|
||||||
if !ok {
|
if !ok {
|
||||||
return startOffset, ""
|
return startOffset, ""
|
||||||
|
@ -162,7 +162,7 @@ func (p *PatchManager) RemoveFileLineRange(filename string, firstLineIdx, lastLi
|
|||||||
return nil
|
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)
|
info, err := p.getFileInfo(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.Log.Error(err)
|
p.Log.Error(err)
|
||||||
@ -178,7 +178,7 @@ 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{
|
||||||
WillBeAppliedReverse: willBeAppliedReverse,
|
Reverse: reverse,
|
||||||
KeepOriginalHeader: true,
|
KeepOriginalHeader: true,
|
||||||
})
|
})
|
||||||
default:
|
default:
|
||||||
@ -186,8 +186,8 @@ func (p *PatchManager) renderPlainPatchForFile(filename string, willBeAppliedRev
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PatchManager) RenderPatchForFile(filename string, plain bool, willBeAppliedReverse bool) string {
|
func (p *PatchManager) RenderPatchForFile(filename string, plain bool, reverse bool) string {
|
||||||
patch := p.renderPlainPatchForFile(filename, willBeAppliedReverse)
|
patch := p.renderPlainPatchForFile(filename, reverse)
|
||||||
if plain {
|
if plain {
|
||||||
return patch
|
return patch
|
||||||
}
|
}
|
||||||
|
@ -14,12 +14,12 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type PatchOptions struct {
|
type PatchOptions struct {
|
||||||
// If true, we're building a patch that we are going to apply using
|
// Create a patch that will applied in reverse with `git apply --reverse`.
|
||||||
// "git apply --reverse". In other words, we are not flipping the '+' and
|
// This affects how unselected lines are treated when only parts of a hunk
|
||||||
// '-' ourselves while creating the patch, but git is going to do that when
|
// are selected: usually, for unselected lines we change '-' lines to
|
||||||
// applying. This has consequences for which lines we need to keep or
|
// context lines and remove '+' lines, but when Reverse is true we need to
|
||||||
// discard when filtering lines from partial hunks.
|
// turn '+' lines into context lines and remove '-' lines.
|
||||||
WillBeAppliedReverse bool
|
Reverse bool
|
||||||
|
|
||||||
// Whether to keep or discard the original diff header including the
|
// Whether to keep or discard the original diff header including the
|
||||||
// "index deadbeef..fa1afe1 100644" line.
|
// "index deadbeef..fa1afe1 100644" line.
|
||||||
@ -109,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.WillBeAppliedReverse, startOffset)
|
lineIndices, opts.Reverse, startOffset)
|
||||||
formattedHunks += formattedHunk
|
formattedHunks += formattedHunk
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,7 +120,7 @@ func TestModifyPatchForRange(t *testing.T) {
|
|||||||
diffText string
|
diffText string
|
||||||
firstLineIndex int
|
firstLineIndex int
|
||||||
lastLineIndex int
|
lastLineIndex int
|
||||||
willBeAppliedReverse bool
|
reverse bool
|
||||||
expected string
|
expected string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -370,7 +370,7 @@ func TestModifyPatchForRange(t *testing.T) {
|
|||||||
filename: "filename",
|
filename: "filename",
|
||||||
firstLineIndex: 6,
|
firstLineIndex: 6,
|
||||||
lastLineIndex: 7,
|
lastLineIndex: 7,
|
||||||
willBeAppliedReverse: false,
|
reverse: false,
|
||||||
diffText: twoChangesInOneHunk,
|
diffText: twoChangesInOneHunk,
|
||||||
expected: `--- a/filename
|
expected: `--- a/filename
|
||||||
+++ b/filename
|
+++ b/filename
|
||||||
@ -384,11 +384,11 @@ func TestModifyPatchForRange(t *testing.T) {
|
|||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testName: "adding part of a hunk, will-be-applied-reverse",
|
testName: "adding part of a hunk, reverse",
|
||||||
filename: "filename",
|
filename: "filename",
|
||||||
firstLineIndex: 6,
|
firstLineIndex: 6,
|
||||||
lastLineIndex: 7,
|
lastLineIndex: 7,
|
||||||
willBeAppliedReverse: true,
|
reverse: true,
|
||||||
diffText: twoChangesInOneHunk,
|
diffText: twoChangesInOneHunk,
|
||||||
expected: `--- a/filename
|
expected: `--- a/filename
|
||||||
+++ b/filename
|
+++ b/filename
|
||||||
@ -408,7 +408,7 @@ 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{
|
||||||
WillBeAppliedReverse: s.willBeAppliedReverse,
|
Reverse: s.reverse,
|
||||||
KeepOriginalHeader: false,
|
KeepOriginalHeader: false,
|
||||||
})
|
})
|
||||||
if !assert.Equal(t, s.expected, result) {
|
if !assert.Equal(t, s.expected, result) {
|
||||||
|
@ -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{WillBeAppliedReverse: reverse, KeepOriginalHeader: false})
|
patch.PatchOptions{Reverse: 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{WillBeAppliedReverse: self.staged, KeepOriginalHeader: false},
|
patch.PatchOptions{Reverse: self.staged, KeepOriginalHeader: false},
|
||||||
)
|
)
|
||||||
patchFilepath, err := self.git.WorkingTree.SaveTemporaryPatch(patchText)
|
patchFilepath, err := self.git.WorkingTree.SaveTemporaryPatch(patchText)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Reference in New Issue
Block a user