-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunified.go
151 lines (141 loc) · 3.84 KB
/
unified.go
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package patience
// Hunk represents a subsection of a diff.
type Hunk struct {
Diffs []DiffLine
SrcStart int
SrcLines int
DstStart int
DstLines int
}
// makeHunks returns the hunks of a diff.
func makeHunks(diffs []DiffLine, precontext, postcontext int) []Hunk {
if len(diffs) == 0 {
return nil
}
hunks := []Hunk{}
// Update hunks with a diff block.
updateHunks := func(block Hunk, lastBlock bool) {
curHunk := len(hunks) - 1
if block.Diffs[0].Type == Equal {
// Unmodified block.
if len(hunks) == 0 {
// Start a new hunk with the tail of the block.
ctxLen := min(precontext, len(block.Diffs))
hunks = append(
hunks,
Hunk{
Diffs: block.Diffs[len(block.Diffs)-ctxLen:],
SrcStart: len(block.Diffs) - ctxLen + block.SrcStart,
SrcLines: ctxLen,
DstStart: len(block.Diffs) - ctxLen + block.DstStart,
DstLines: ctxLen,
},
)
} else {
// Update the current hunk.
maxNonContext := precontext + postcontext
if lastBlock {
maxNonContext = postcontext
}
if len(block.Diffs) <= maxNonContext {
// Block is small enough to be appended to the current hunk.
hunks[curHunk].Diffs = append(hunks[curHunk].Diffs, block.Diffs...)
hunks[curHunk].SrcLines += len(block.Diffs)
hunks[curHunk].DstLines += len(block.Diffs)
} else {
// Append the head of the block to the current hunk.
hunks[curHunk].Diffs = append(hunks[curHunk].Diffs, block.Diffs[:postcontext]...)
hunks[curHunk].SrcLines += postcontext
hunks[curHunk].DstLines += postcontext
if !lastBlock {
// Start a new hunk with the tail of the block.
hunks = append(
hunks,
Hunk{
Diffs: block.Diffs[len(block.Diffs)-precontext:],
SrcStart: len(block.Diffs) - precontext + block.SrcStart,
SrcLines: precontext,
DstStart: len(block.Diffs) - precontext + block.DstStart,
DstLines: precontext,
},
)
}
}
// Update starting line numbers if the current hunk had no source or destination diff.
if hunks[curHunk].SrcStart == 0 {
hunks[curHunk].SrcStart = block.SrcStart
}
if hunks[curHunk].DstStart == 0 {
hunks[curHunk].DstStart = block.DstStart
}
}
} else {
// Modified block.
if len(hunks) > 0 {
hunks[curHunk].Diffs = append(hunks[curHunk].Diffs, block.Diffs...)
hunks[curHunk].SrcLines += block.SrcLines
hunks[curHunk].DstLines += block.DstLines
} else {
hunks = append(
hunks,
Hunk{
Diffs: block.Diffs,
SrcStart: block.SrcStart,
SrcLines: block.SrcLines,
DstStart: block.DstStart,
DstLines: block.DstLines,
},
)
}
}
}
// Aggregate blocks of modified and unmodified diff lines, creating
// or updating hunks after each block.
var block Hunk
modifiedLines := 0 //nolint:ifshort
srcLineNum, dstLineNum := 0, 0
for _, l := range diffs {
if len(block.Diffs) == 0 ||
block.Diffs[0].Type == l.Type ||
(block.Diffs[0].Type != l.Type && block.Diffs[0].Type != Equal && l.Type != Equal) {
block.Diffs = append(block.Diffs, l)
} else {
updateHunks(block, false)
block = Hunk{Diffs: []DiffLine{l}}
}
switch l.Type {
case Delete:
srcLineNum++
block.SrcLines++
modifiedLines++
case Insert:
dstLineNum++
block.DstLines++
modifiedLines++
case Equal:
srcLineNum++
dstLineNum++
block.SrcLines++
block.DstLines++
}
if block.SrcStart == 0 && (l.Type == Equal || l.Type == Delete) {
block.SrcStart = srcLineNum
}
if block.DstStart == 0 && (l.Type == Equal || l.Type == Insert) {
block.DstStart = dstLineNum
}
}
updateHunks(block, true)
// Return no hunks if the diffs contain only equal lines.
if modifiedLines == 0 {
return nil
}
return hunks
}
// min returns the minimum of two integers.
func min(a, b int) int {
if a < b {
return a
}
return b
}