-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.go
More file actions
278 lines (257 loc) · 7.6 KB
/
Copy pathpatch.go
File metadata and controls
278 lines (257 loc) · 7.6 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2025 OpenAI
// Copyright 2026 Fredrik Gustafsson
// SPDX-License-Identifier: Apache-2.0
//
// The patch format of apply_patch in OpenAI Codex
// (https://github.com/openai/codex, codex-rs/apply-patch: parser.rs and
// streaming_parser.rs), ported to Go by Fredrik Gustafsson. Changed to keep
// update bodies as text so that either Mode can apply them. See
// LICENSE-APACHE and NOTICE.
package applypatch
import (
"errors"
"fmt"
"strings"
"unicode"
)
const (
beginPatch = "*** Begin Patch"
endPatch = "*** End Patch"
addFile = "*** Add File: "
deleteFile = "*** Delete File: "
updateFile = "*** Update File: "
moveTo = "*** Move to: "
environmentID = "*** Environment ID:"
)
// OperationKind says what an Operation does to its file.
type OperationKind int
const (
// AddFile creates a file, replacing any file at the same path.
AddFile OperationKind = iota + 1
// DeleteFile removes an existing file.
DeleteFile
// UpdateFile applies an update diff to an existing file and can move it.
UpdateFile
)
// String returns "add", "delete", or "update".
func (k OperationKind) String() string {
switch k {
case AddFile:
return "add"
case DeleteFile:
return "delete"
case UpdateFile:
return "update"
}
return fmt.Sprintf("OperationKind(%d)", int(k))
}
// Operation is one file operation of a patch.
type Operation struct {
Kind OperationKind
Path string
// MoveTo is the new path of an updated file, or empty.
MoveTo string
// Contents is the text of an added file: each "+" line of the patch
// followed by a newline.
Contents string
// Diff is the update diff of an updated file: each line of the patch
// followed by "\n". Mode.Apply(contents, Diff) applies it exactly as
// ApplyFiles would.
Diff string
}
// Patch is a parsed apply_patch patch.
type Patch struct {
// EnvironmentID is the value of an optional "*** Environment ID:" line.
EnvironmentID string
Operations []Operation
}
type parseMode int
const (
notStarted parseMode = iota
startedPatch
inAddFile
inDeleteFile
inUpdateFile
endedPatch
)
type patchParser struct {
patch *Patch
mode parseMode
// lines collects the "+" lines of an add or the diff lines of an update,
// joined into Contents or Diff when the operation ends.
lines []string
// hasChunk is set once an update has a diff line other than
// "*** End of File", which Codex ignores before the first section.
hasChunk bool
}
// Parse parses an apply_patch patch the way Codex does: it accepts
// whitespace around the markers and a patch wrapped in a <<EOF heredoc.
// Parse checks the structure of the patch; the diffs of updates are checked
// when a Mode applies them.
func Parse(patch string) (*Patch, error) {
lines, err := patchLines(patch)
if err != nil {
return nil, err
}
parser := &patchParser{patch: &Patch{}}
for i, line := range lines {
if i == len(lines)-1 {
// The boundary check guarantees the last line is "*** End Patch".
err = parser.header(endPatch)
break
}
err = parser.line(strings.TrimSuffix(line, "\r"))
if err != nil {
break
}
}
if err != nil {
return nil, err
}
return parser.patch, nil
}
// patchLines splits a patch into lines like Rust's str::lines and removes a
// heredoc wrapper, then checks the first and last lines.
func patchLines(patch string) ([]string, error) {
text := strings.TrimSpace(patch)
if text == "" {
return nil, errors.New("invalid patch: The first line of the patch must be '*** Begin Patch'")
}
lines := strings.Split(text, "\n")
for i := range lines[:len(lines)-1] {
lines[i] = strings.TrimSuffix(lines[i], "\r")
}
err := checkBoundaries(lines)
if err == nil {
return lines, nil
}
first, last := lines[0], lines[len(lines)-1]
if (first == "<<EOF" || first == "<<'EOF'" || first == "<<\"EOF\"") &&
strings.HasSuffix(last, "EOF") && len(lines) >= 4 {
inner := lines[1 : len(lines)-1]
return inner, checkBoundaries(inner)
}
return nil, err
}
func checkBoundaries(lines []string) error {
if strings.TrimSpace(lines[0]) != beginPatch {
return errors.New("invalid patch: The first line of the patch must be '*** Begin Patch'")
}
if strings.TrimSpace(lines[len(lines)-1]) != endPatch {
return errors.New("invalid patch: The last line of the patch must be '*** End Patch'")
}
return nil
}
func (p *patchParser) line(line string) error {
trimmed := strings.TrimSpace(line)
switch p.mode {
case notStarted:
// patchLines checked that this line is "*** Begin Patch".
p.mode = startedPatch
return nil
case startedPatch:
rest, ok := strings.CutPrefix(trimmed, environmentID)
if ok {
return p.environmentID(strings.TrimSpace(rest))
}
case inAddFile:
if !isHeader(trimmed) {
contents, ok := strings.CutPrefix(line, "+")
if !ok {
return invalidHeader(trimmed)
}
p.lines = append(p.lines, contents)
return nil
}
case inUpdateFile:
marker := strings.TrimRightFunc(line, unicode.IsSpace)
if isHeader(marker) {
return p.header(marker)
}
operation := &p.patch.Operations[len(p.patch.Operations)-1]
destination, ok := strings.CutPrefix(marker, moveTo)
if ok && !p.hasChunk && operation.MoveTo == "" {
operation.MoveTo = destination
return nil
}
p.hasChunk = p.hasChunk || marker != endFile
p.lines = append(p.lines, line)
return nil
case endedPatch:
if trimmed != "" {
return errors.New("invalid patch: The last line of the patch must be '*** End Patch'")
}
return nil
case inDeleteFile:
}
if isHeader(trimmed) {
return p.header(trimmed)
}
return invalidHeader(trimmed)
}
func (p *patchParser) environmentID(value string) error {
switch {
case p.patch.EnvironmentID != "":
return errors.New("invalid patch: apply_patch environment_id cannot be specified more than once")
case value == "":
return errors.New("invalid patch: apply_patch environment_id cannot be empty")
}
p.patch.EnvironmentID = value
return nil
}
func isHeader(line string) bool {
return line == endPatch || strings.HasPrefix(line, addFile) ||
strings.HasPrefix(line, deleteFile) || strings.HasPrefix(line, updateFile)
}
// header ends the operation being parsed and starts the next one.
func (p *patchParser) header(line string) error {
if p.mode == inAddFile || p.mode == inUpdateFile {
operation := &p.patch.Operations[len(p.patch.Operations)-1]
if p.mode == inUpdateFile && len(p.lines) == 0 {
return fmt.Errorf("invalid patch hunk: Update file hunk for path '%s' is empty", operation.Path)
}
text := joinLines(p.lines)
if p.mode == inAddFile {
operation.Contents = text
} else {
operation.Diff = text
}
}
p.lines, p.hasChunk = nil, false
if line == endPatch {
p.mode = endedPatch
return nil
}
for _, kind := range []struct {
prefix string
kind OperationKind
mode parseMode
}{{addFile, AddFile, inAddFile}, {deleteFile, DeleteFile, inDeleteFile}, {updateFile, UpdateFile, inUpdateFile}} {
path, ok := strings.CutPrefix(line, kind.prefix)
if ok {
p.patch.Operations = append(p.patch.Operations, Operation{Kind: kind.kind, Path: path})
p.mode = kind.mode
break
}
}
return nil
}
func invalidHeader(line string) error {
return fmt.Errorf("invalid patch hunk: '%s' is not a valid hunk header. Valid hunk headers: "+
"'*** Add File: {path}', '*** Delete File: {path}', '*** Update File: {path}'", line)
}
// joinLines joins lines, each followed by "\n".
func joinLines(lines []string) string {
if len(lines) == 0 {
return ""
}
return strings.Join(lines, "\n") + "\n"
}
// diffLines splits text whose lines each end in a newline, like
// Operation.Diff, back into its lines.
func diffLines(text string) []string {
if text == "" {
return nil
}
return strings.Split(strings.TrimSuffix(text, "\n"), "\n")
}