diff options
Diffstat (limited to 'pkg/commands/patch/hunk.go')
-rw-r--r-- | pkg/commands/patch/hunk.go | 33 |
1 files changed, 8 insertions, 25 deletions
diff --git a/pkg/commands/patch/hunk.go b/pkg/commands/patch/hunk.go index a2727f2c9..e0aeb4157 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, 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 |