From 4bd1322941af599f4cf9e406cc72adf7a5bc6ff4 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 7 Mar 2023 10:16:30 +0100 Subject: Rename WillBeAppliedReverse to Reverse This is the only "reverse"-related option that is left, so use a less clumsy name for it. --- pkg/commands/patch/hunk.go | 12 ++++----- pkg/commands/patch/patch_manager.go | 10 ++++---- pkg/commands/patch/patch_modifier.go | 14 +++++------ pkg/commands/patch/patch_modifier_test.go | 42 +++++++++++++++---------------- pkg/gui/controllers/staging_controller.go | 4 +-- 5 files changed, 41 insertions(+), 41 deletions(-) diff --git a/pkg/commands/patch/hunk.go b/pkg/commands/patch/hunk.go index e0aeb4157..605c473c1 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, 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, "" diff --git a/pkg/commands/patch/patch_manager.go b/pkg/commands/patch/patch_manager.go index 7c7197583..3a379b97e 100644 --- a/pkg/commands/patch/patch_manager.go +++ b/pkg/commands/patch/patch_manager.go @@ -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 } diff --git a/pkg/commands/patch/patch_modifier.go b/pkg/commands/patch/patch_modifier.go index c8e97e42d..79f7b7d31 100644 --- a/pkg/commands/patch/patch_modifier.go +++ b/pkg/commands/patch/patch_modifier.go @@ -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 } diff --git a/pkg/commands/patch/patch_modifier_test.go b/pkg/commands/patch/patch_modifier_test.go index 55fdfb547..a6dfc2716 100644 --- a/pkg/commands/patch/patch_modifier_test.go +++ b/pkg/commands/patch/patch_modifier_test.go @@ -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) diff --git a/pkg/gui/controllers/staging_controller.go b/pkg/gui/controllers/staging_controller.go index 2fef137d0..78c271640 100644 --- a/pkg/gui/controllers/staging_controller.go +++ b/pkg/gui/controllers/staging_controller.go @@ -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 { -- cgit v1.2.3