summaryrefslogtreecommitdiffstats
path: root/pkg/commands/patch/hunk.go
blob: e0aeb415700e36625610a07d909bac46f6f10791 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package patch

import (
	"fmt"
	"strings"

	"github.com/jesseduffield/lazygit/pkg/utils"
	"github.com/samber/lo"
)

type PatchHunk struct {
	FirstLineIdx int
	oldStart     int
	newStart     int
	heading      string
	bodyLines    []string
}

func (hunk *PatchHunk) LastLineIdx() int {
	return hunk.FirstLineIdx + len(hunk.bodyLines)
}

func newHunk(lines []string, firstLineIdx int) *PatchHunk {
	header := lines[0]
	bodyLines := lines[1:]

	oldStart, newStart, heading := headerInfo(header)

	return &PatchHunk{
		oldStart:     oldStart,
		newStart:     newStart,
		heading:      heading,
		FirstLineIdx: firstLineIdx,
		bodyLines:    bodyLines,
	}
}

func headerInfo(header string) (int, int, string) {
	match := hunkHeaderRegexp.FindStringSubmatch(header)

	oldStart := utils.MustConvertToInt(match[1])
	newStart := utils.MustConvertToInt(match[2])
	heading := match[3]

	return oldStart, newStart, heading
}

func (hunk *PatchHunk) updatedLines(lineIndices []int, willBeAppliedReverse bool) []string {
	skippedNewlineMessageIndex := -1
	newLines := []string{}

	lineIdx := hunk.FirstLineIdx
	for _, line := range hunk.bodyLines {
		lineIdx++ // incrementing at the start to skip the header line
		if line == "" {
			break
		}
		isLineSelected := lo.Contains(lineIndices, lineIdx)

		firstChar, content := line[:1], line[1:]
		transformedFirstChar := transformedFirstChar(firstChar, willBeAppliedReverse, isLineSelected)

		if isLineSelected || (transformedFirstChar == "\\" && skippedNewlineMessageIndex != lineIdx) || transformedFirstChar == " " {
			newLines = append(newLines, transformedFirstChar+content)
			continue
		}

		if transformedFirstChar == "+" {
			// we don't want to include the 'newline at end of file' line if it involves an addition we're not including
			skippedNewlineMessageIndex = lineIdx + 1
		}
	}

	return newLines
}

func transformedFirstChar(firstChar string, willBeAppliedReverse bool, isLineSelected bool) string {
	linesToKeepInPatchContext := "-"
	if willBeAppliedReverse {
		linesToKeepInPatchContext = "+"
	}
	if !isLineSelected && firstChar == linesToKeepInPatchContext {
		return " "
	}

	return firstChar
}

func (hunk *PatchHunk) formatHeader(oldStart int, oldLength int, newStart int, newLength int, heading string) string {
	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)
	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) (int, string, bool) {
	changeCount := nLinesWithPrefix(newBodyLines, []string{"+", "-"})
	oldLength := nLinesWithPrefix(newBodyLines, []string{" ", "-"})
	newLength := nLinesWithPrefix(newBodyLines, []string{"+", " "})

	if changeCount == 0 {
		// if nothing has changed we just return nothing
		return startOffset, "", false
	}

	oldStart := hunk.oldStart

	var newStartOffset int
	// if the hunk went from zero to positive length, we need to increment the starting point by one
	// if the hunk went from positive to zero length, we need to decrement the starting point by one
	if oldLength == 0 {
		newStartOffset = 1
	} else if newLength == 0 {
		newStartOffset = -1
	} else {
		newStartOffset = 0
	}

	newStart := oldStart + startOffset + newStartOffset

	newStartOffset = startOffset + newLength - oldLength
	formattedHeader := hunk.formatHeader(oldStart, oldLength, newStart, newLength, hunk.heading)
	return newStartOffset, formattedHeader, true
}