-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslate.go
More file actions
183 lines (168 loc) · 5.02 KB
/
Copy pathtranslate.go
File metadata and controls
183 lines (168 loc) · 5.02 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package hpatch
import (
"fmt"
"slices"
"strings"
"unicode/utf8"
"github.com/pmezard/go-difflib/difflib"
)
func renderFileWritePatch(path, content string) (string, error) {
if path == "" || strings.TrimSpace(path) != path || strings.ContainsAny(path, "\r\n\x00") || !utf8.ValidString(path) {
return "", fmt.Errorf("file write path is not representable in apply_patch")
}
if !utf8.ValidString(content) || strings.ContainsAny(content, "\r\x00") || content != "" && !strings.HasSuffix(content, "\n") {
return "", fmt.Errorf("file write requires empty or LF-terminated UTF-8 text")
}
var patch strings.Builder
patch.WriteString("*** Begin Patch\n")
if content == "" {
fmt.Fprintf(&patch, "*** Add File: %s\n", path)
} else {
// Each addition row supplies its own LF in the host parser.
writeAddition(&patch, path, strings.TrimSuffix(content, "\n"))
}
patch.WriteString("*** End Patch\n")
return patch.String(), nil
}
func translate(changes []change) (string, error) {
if len(changes) == 0 {
return "", nil
}
var patch strings.Builder
patch.WriteString("*** Begin Patch\n")
for _, change := range changes {
switch change.kind {
case changeAdd:
writeAddition(&patch, change.path, change.content)
case changeDelete:
fmt.Fprintf(&patch, "*** Delete File: %s\n", change.originalPath)
case changeUpdate:
if err := writeUpdate(&patch, change); err != nil {
return "", err
}
}
}
patch.WriteString("*** End Patch\n")
return patch.String(), nil
}
func writeAddition(patch *strings.Builder, path, content string) {
fmt.Fprintf(patch, "*** Add File: %s\n", path)
content = normalizeLineEndings(content)
for line := range strings.SplitSeq(content, "\n") {
patch.WriteByte('+')
patch.WriteString(line)
patch.WriteByte('\n')
}
}
func writeUpdate(patch *strings.Builder, change change) error {
fmt.Fprintf(patch, "*** Update File: %s\n", change.originalPath)
if change.path != change.originalPath {
fmt.Fprintf(patch, "*** Move to: %s\n", change.path)
}
if change.original == change.content {
writeMoveVerification(patch, change.original)
return nil
}
diff, err := unambiguousDiff(change)
if err != nil {
return err
}
firstNewline := strings.IndexByte(diff, '\n')
if firstNewline < 0 {
return fmt.Errorf("rendering update for %s produced no header", change.originalPath)
}
secondRelative := strings.IndexByte(diff[firstNewline+1:], '\n')
if secondRelative < 0 {
return fmt.Errorf("rendering update for %s produced no hunks", change.originalPath)
}
hunks := diff[firstNewline+1+secondRelative+1:]
if !strings.HasPrefix(hunks, "@@") {
return fmt.Errorf("rendering update for %s produced no hunks", change.originalPath)
}
hunks = normalizeHunkHeaders(hunks)
patch.WriteString(hunks)
if !strings.HasSuffix(hunks, "\n") {
patch.WriteByte('\n')
}
return nil
}
func unambiguousDiff(change change) (string, error) {
original := normalizeLineEndings(change.original)
result := normalizeLineEndings(change.content)
lineCount := len(strings.Split(original, "\n"))
for contextLines := 3; ; contextLines = min(contextLines*2, lineCount) {
diff, err := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
A: difflib.SplitLines(original),
B: difflib.SplitLines(result),
FromFile: change.originalPath,
ToFile: change.path,
Context: contextLines,
})
if err != nil {
return "", fmt.Errorf("rendering update for %s: %w", change.originalPath, err)
}
if diffHunksAreUnique(original, diff) {
return diff, nil
}
if contextLines == lineCount {
return "", fmt.Errorf("rendering update for %s produced ambiguous hunks", change.originalPath)
}
}
}
func diffHunksAreUnique(original, diff string) bool {
lines := strings.Split(diff, "\n")
originalLines := strings.Split(original, "\n")
for index := 2; index < len(lines); {
if !strings.HasPrefix(lines[index], "@@") {
index++
continue
}
index++
var sought []string
for index < len(lines) && !strings.HasPrefix(lines[index], "@@") {
line := lines[index]
if line != "" && line[0] != '+' {
sought = append(sought, line[1:])
}
index++
}
if countLineSequence(originalLines, sought) != 1 {
return false
}
}
return true
}
func countLineSequence(lines, sought []string) int {
count := 0
for start := 0; start+len(sought) <= len(lines); start++ {
if slices.Equal(lines[start:start+len(sought)], sought) {
count++
}
}
return count
}
func normalizeLineEndings(text string) string {
text = strings.ReplaceAll(text, "\r\n", "\n")
return strings.ReplaceAll(text, "\r", "\n")
}
func normalizeHunkHeaders(hunks string) string {
lines := strings.Split(hunks, "\n")
for index, line := range lines {
if strings.HasPrefix(line, "@@ ") {
lines[index] = "@@"
}
}
return strings.Join(lines, "\n")
}
func writeMoveVerification(patch *strings.Builder, content string) {
content = normalizeLineEndings(content)
patch.WriteString("@@\n")
if content == "" {
patch.WriteString("-\n+\n")
return
}
line, _, _ := strings.Cut(content, "\n")
patch.WriteByte(' ')
patch.WriteString(line)
patch.WriteByte('\n')
}