-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace.go
More file actions
415 lines (386 loc) · 11.7 KB
/
Copy pathworkspace.go
File metadata and controls
415 lines (386 loc) · 11.7 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package hpatch
import (
"context"
"fmt"
"io/fs"
"path/filepath"
)
type loadedFile struct {
content string
mode fs.FileMode
}
type (
fileLoader func(path string) (loadedFile, error)
pathProbe func(path string) (fs.FileMode, bool, error)
pathResolver func(path string) (string, error)
)
type fileState struct {
originalPath string
path string
original string
mode fs.FileMode
created bool
deleted bool
mutationOrigin editOrigin
editor editor
}
type changeKind int
const (
changeAdd changeKind = iota
changeUpdate
changeDelete
)
type change struct {
kind changeKind
originalPath string
path string
original string
content string
mode fs.FileMode
}
type workspace struct {
paths map[string]*fileState
blocked map[string]bool
reserved map[string]bool
files []*fileState
active *fileState
initializable *fileState
reportedEdits []*reportedEdit
load fileLoader
exists pathProbe
}
// evaluate evaluates the program instructions against the workspace.
func (p *program) evaluate(ctx context.Context, resolve pathResolver, load fileLoader, exists pathProbe) ([]change, string, []TargetAlias, error) {
w := &workspace{
paths: make(map[string]*fileState),
blocked: make(map[string]bool),
reserved: make(map[string]bool),
load: load,
exists: exists,
}
var baselineFailures []*commandError
for commandIndex, command := range p.instructions {
if err := ctx.Err(); err != nil {
return nil, "", nil, err
}
diagnosticPath := command.path
if command.path != "" {
resolved, err := resolve(command.path)
if err != nil {
if failure := w.indentationFailure(); failure != nil {
return nil, "", nil, failure
}
reason := reasonPath
return nil, "", nil, &commandError{Target: command.target.variant(), Reason: reason, Command: commandIndex + 1, Line: command.line, Operation: command.operation, Path: diagnosticPath, Category: commandCategory(command.operation), Source: command.source, Message: err.Error()}
}
command.path = resolved
}
if err := w.execute(command, commandIndex+1); err != nil {
if failure := w.indentationFailure(); failure != nil {
return nil, "", nil, failure
}
reason := reasonOf(err, reasonOther)
failure := &commandError{Target: command.target.variant(), Reason: reason, Command: commandIndex + 1, Line: command.line, Operation: command.operation, Path: w.diagnosticPath(command), Category: commandCategory(command.operation), Source: command.source, Message: err.Error(), Repair: w.repairContext(command, reason)}
if independentlyDetectableBaselineFailure(reason) {
baselineFailures = append(baselineFailures, failure)
continue
}
return nil, "", nil, failure
}
}
if len(baselineFailures) != 0 {
return nil, "", nil, commandFailures(baselineFailures)
}
if failure := w.indentationFailure(); failure != nil {
return nil, "", nil, failure
}
if err := ctx.Err(); err != nil {
return nil, "", nil, err
}
if failure := w.renderFinal(ctx); failure != nil {
return nil, "", nil, failure
}
if err := ctx.Err(); err != nil {
return nil, "", nil, err
}
changes := w.changes()
report, aliases := w.finalStateReport(changes)
return changes, report, aliases, nil
}
// independentlyDetectableBaselineFailure reports whether a failure can be detected independently.
func independentlyDetectableBaselineFailure(reason failureReason) bool {
switch reason {
case reasonRowMissing, reasonRowStale, reasonOccurrenceMissing, reasonTargetOrder:
return true
default:
return false
}
}
// commandFailures wraps one or more command failures as an error.
func commandFailures(failures []*commandError) error {
if len(failures) == 1 {
return failures[0]
}
return &commandGroupError{commands: failures}
}
// indentationFailure finds the earliest indentation-only edit that should be rejected.
func (w *workspace) indentationFailure() *commandError {
var earliest *commandError
for _, file := range w.files {
for _, edit := range file.editor.edits {
if edit.indentation == nil ||
edit.indentation.candidate.kind != indentationCorrectionExact ||
indentationPolicy(file.path) != indentationPolicyReject {
continue
}
command := edit.indentation.command
path := edit.indentation.path
if path == "" {
path = file.path
}
failure := &commandError{
Target: command.target.variant(),
Reason: reasonEditConflict,
Command: edit.command,
Line: command.line,
Operation: command.operation,
Path: path,
Category: commandCategory(command.operation),
Source: command.source,
Message: edit.indentation.candidate.correction.Error(),
Repair: edit.indentation.candidate.correction.diagnostic(),
CorrectionScope: "field-local",
}
if earliest == nil || failure.Command < earliest.Command {
earliest = failure
}
}
}
return earliest
}
// diagnosticPath returns the appropriate file path for diagnostic messages.
func (w *workspace) diagnosticPath(command instruction) string {
if command.path != "" {
return command.path
}
if w.active != nil {
return w.active.path
}
return ""
}
// commandCategory returns the diagnostic category for an operation.
func commandCategory(operation string) string {
switch operation {
case "in", "new", "mv", "rm":
return "file"
case "type", "add":
return "edit"
default:
panic("parsed instruction has no diagnostic category: " + operation)
}
}
// execute executes a single instruction against the workspace.
func (w *workspace) execute(command instruction, commandIndex int) error {
origin := editOrigin{
command: commandIndex, line: command.line,
operation: command.operation, target: command.target.variant(), targetSpec: command.target,
multilineValue: command.delimiter != "",
}
initializing := command.operation == "type" && command.target.kind == targetNone && w.initializable == w.active
if !initializing {
w.initializable = nil
}
switch command.operation {
case "in":
return w.selectFile(command.path)
case "new":
return w.newFile(command.path, origin)
case "mv":
return w.moveFile(command.path, origin)
case "rm":
return w.removeFile()
}
if w.active == nil {
return withReason(reasonActiveFile, fmt.Errorf("%s requires an active file", command.operation))
}
file := w.active
if command.target.kind == targetNone {
if !initializing || !file.created {
return withReason(reasonInitialization, fmt.Errorf("bare type VALUE only initializes the immediately preceding new; editing an existing file requires a line, range, or text target"))
}
file.editor.initialize(command.text, origin)
w.initializable = nil
} else {
if file.created {
return withReason(reasonInitialization, fmt.Errorf("new file content is not targetable before a successful invocation and hread"))
}
err := file.editor.applyMutation(command.operation, command.target, command.text, origin, command, file.path)
if err != nil {
return err
}
}
if edit := file.editor.reportedEdit(origin); edit != nil {
edit.file = file
w.reportedEdits = append(w.reportedEdits, edit)
}
return nil
}
// selectFile makes an existing file the active file, loading it if necessary.
func (w *workspace) selectFile(path string) error {
if file := w.paths[path]; file != nil {
w.active = file
return nil
}
if w.blocked[path] {
return withReason(reasonFileMissing, fmt.Errorf("%s does not exist in the pending workspace", path))
}
loaded, err := w.load(path)
if err != nil {
return err
}
file := &fileState{
originalPath: path,
path: path,
original: loaded.content,
mode: loaded.mode,
editor: editor{baseline: loaded.content},
}
w.paths[path] = file
w.files = append(w.files, file)
w.active = file
return nil
}
// newFile creates a new file at the given path.
func (w *workspace) newFile(path string, origin editOrigin) error {
if err := w.validateFreeDestination(path); err != nil {
return err
}
file := &fileState{path: path, created: true, mutationOrigin: origin}
w.paths[path] = file
w.files = append(w.files, file)
w.active = file
w.initializable = file
return nil
}
// moveFile moves the active file to a new path.
func (w *workspace) moveFile(path string, origin editOrigin) error {
if w.active == nil {
return withReason(reasonActiveFile, fmt.Errorf("mv requires an active file"))
}
if err := w.validateFreeDestination(path); err != nil {
return err
}
oldPath := w.active.path
delete(w.paths, oldPath)
w.blocked[oldPath] = true
w.reserved[oldPath] = true
w.active.path = path
w.active.mutationOrigin = origin
w.paths[path] = w.active
return nil
}
// removeFile deletes the active file from the workspace.
func (w *workspace) removeFile() error {
if w.active == nil {
return withReason(reasonActiveFile, fmt.Errorf("rm requires an active file"))
}
file := w.active
if !file.created {
if edit, ok := file.editor.firstEdit(); ok {
return withReason(reasonEditConflict, fmt.Errorf(
"cannot remove a baseline file after content edit from command %d (source line %d, operation %q)",
edit.command,
edit.line,
edit.operation,
))
}
}
removedPath := file.path
delete(w.paths, removedPath)
w.blocked[removedPath] = true
w.reserved[removedPath] = true
if !file.created {
w.blocked[file.originalPath] = true
}
file.deleted = true
retained := w.reportedEdits[:0]
for _, edit := range w.reportedEdits {
if edit.file != file {
retained = append(retained, edit)
}
}
w.reportedEdits = retained
w.active = nil
w.initializable = nil
return nil
}
// pathOccupied checks if a path is occupied in the workspace or filesystem.
func (w *workspace) pathOccupied(path string) (bool, error) {
if w.paths[path] != nil || w.reserved[path] {
return true, nil
}
if w.blocked[path] {
return false, nil
}
_, occupied, err := w.exists(path)
if err != nil {
return false, fmt.Errorf("checking destination %s: %w", path, err)
}
return occupied, nil
}
// validateDestinationParent checks that the parent directory exists.
func (w *workspace) validateDestinationParent(path string) error {
parent := filepath.Dir(path)
mode, exists, err := w.exists(parent)
if err != nil {
return fmt.Errorf("checking parent directory %s: %w", parent, err)
}
if !exists || !mode.IsDir() {
return withReason(reasonPath, fmt.Errorf("parent directory %s does not exist", parent))
}
return nil
}
// validateFreeDestination checks that a destination path is available.
func (w *workspace) validateFreeDestination(path string) error {
if err := w.validateDestinationParent(path); err != nil {
return err
}
occupied, err := w.pathOccupied(path)
if err != nil {
return err
}
if occupied {
return withReason(reasonFileConflict, fmt.Errorf("destination %s already exists", path))
}
return nil
}
// changes computes the final set of filesystem changes.
func (w *workspace) changes() []change {
changes := make([]change, 0, len(w.files))
for _, file := range w.files {
content := file.editor.content()
switch {
case file.created && file.deleted:
continue
case file.created:
changes = append(changes, change{kind: changeAdd, path: file.path, content: content, mode: 0o644})
case file.deleted:
changes = append(changes, change{
kind: changeDelete,
originalPath: file.originalPath,
original: file.original,
mode: file.mode,
})
case file.originalPath != file.path || file.original != content:
changes = append(changes, change{
kind: changeUpdate,
originalPath: file.originalPath,
path: file.path,
original: file.original,
content: content,
mode: file.mode,
})
}
}
return changes
}