-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatchapply.go
More file actions
286 lines (259 loc) · 7.49 KB
/
patchapply.go
File metadata and controls
286 lines (259 loc) · 7.49 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
// Package patchapply applies parsed patches to files.
//
// It is the apply half of [go-mailpatch]: mailpatch turns a format-patch email
// into structured FileChanges; patchapply takes those (or a bare unified diff)
// and writes the changes to a filesystem — creating, modifying, deleting,
// renaming, and copying files. It can also reverse a patch and dry-run one
// without writing.
//
// Apply runs against an FS, not directly against the OS. Use DirFS to confine
// every path to a directory (patches that try to escape via "../" fail with
// ErrUnsafePath), or MemFS to apply entirely in memory.
//
// Application is transactional: every file is read and every hunk is placed in
// memory first, so if any hunk fails to apply (ErrConflict) nothing is written.
//
// It never executes git.
//
// [go-mailpatch]: https://github.com/floatpane/go-mailpatch
package patchapply
import (
"errors"
"io/fs"
"strconv"
"github.com/floatpane/go-mailpatch"
)
// Sentinel errors. Compare with errors.Is.
var (
// ErrConflict is returned (wrapped, with the file and hunk) when a hunk's
// context cannot be matched in the target file.
ErrConflict = errors.New("patchapply: hunk does not apply")
// ErrUnsafePath is returned by DirFS when a patch path escapes the root.
ErrUnsafePath = errors.New("patchapply: unsafe path")
// ErrExists is returned when a patch adds a file that is already present.
ErrExists = errors.New("patchapply: file already exists")
// ErrMissing is returned when a patch modifies, deletes, or renames a file
// that is not present.
ErrMissing = errors.New("patchapply: target file not found")
)
// PathError annotates an error with the offending path.
type PathError struct {
Path string
Err error
}
func (e *PathError) Error() string { return e.Err.Error() + ": " + e.Path }
func (e *PathError) Unwrap() error { return e.Err }
// Options tunes an apply.
type Options struct {
// Reverse undoes the patch instead of applying it.
Reverse bool
// DryRun validates the whole patch (reads and places every hunk) but
// writes nothing. A nil error means a real apply would succeed.
DryRun bool
}
// Status is what happened to a file.
type Status int
const (
// Created means the file was written and did not exist before.
Created Status = iota
// Updated means an existing file's contents changed.
Updated
// Removed means the file was deleted.
Removed
// Renamed means the file moved (and possibly changed).
Renamed
)
func (s Status) String() string {
switch s {
case Created:
return "created"
case Updated:
return "updated"
case Removed:
return "removed"
case Renamed:
return "renamed"
default:
return "unknown"
}
}
// FileResult records what an apply did (or, for a dry run, would do) to one
// file.
type FileResult struct {
Path string // resulting path; for a removal, the path removed
OldPath string // previous path for a rename, else empty
Status Status
Hunks int // hunks applied
}
// Result is the outcome of an apply.
type Result struct {
Files []FileResult
}
// Apply applies files to fsys. opts may be nil.
func Apply(fsys FS, files []mailpatch.FileChange, opts *Options) (*Result, error) {
if opts == nil {
opts = &Options{}
}
// Phase 1: read + compute every change in memory. Nothing is written, so a
// failure here leaves fsys untouched.
plan := make([]planContent, 0, len(files))
for _, f := range files {
if opts.Reverse {
f = reverseFile(f)
}
p, err := planFile(fsys, f)
if err != nil {
return nil, err
}
plan = append(plan, p)
}
res := &Result{Files: make([]FileResult, 0, len(plan))}
for _, p := range plan {
res.Files = append(res.Files, p.result)
}
if opts.DryRun {
return res, nil
}
// Phase 2: commit. Writes first, then removes (so a rename's new file
// exists before the old one goes away).
for _, p := range plan {
if p.doWrite {
if err := fsys.WriteFile(p.writePath, p.content, p.perm); err != nil {
return nil, err
}
}
}
for _, p := range plan {
if p.doRemove {
if err := fsys.Remove(p.removePath); err != nil {
return nil, err
}
}
}
return res, nil
}
// planContent is the per-file plan computed in phase 1.
type planContent struct {
writePath string
content []byte
perm fs.FileMode
doWrite bool
removePath string
doRemove bool
result FileResult
}
func planFile(fsys FS, f mailpatch.FileChange) (planContent, error) {
var p planContent
switch f.Type {
case mailpatch.Added:
target := f.Path()
if fsys.Exists(target) {
return p, &PathError{Path: target, Err: ErrExists}
}
content, err := ApplyToBytes(nil, f)
if err != nil {
return p, err
}
p = planContent{
writePath: target, content: content, perm: parseMode(f.NewMode), doWrite: true,
result: FileResult{Path: target, Status: Created, Hunks: len(f.Hunks)},
}
case mailpatch.Deleted:
target := f.OldPath
if target == "" {
target = f.Path()
}
if !fsys.Exists(target) {
return p, &PathError{Path: target, Err: ErrMissing}
}
p = planContent{
removePath: target, doRemove: true,
result: FileResult{Path: target, Status: Removed, Hunks: len(f.Hunks)},
}
case mailpatch.Renamed:
orig, err := readExisting(fsys, f.OldPath)
if err != nil {
return p, err
}
content, err := ApplyToBytes(orig, f)
if err != nil {
return p, err
}
p = planContent{
writePath: f.NewPath, content: content, perm: parseMode(f.NewMode), doWrite: true,
removePath: f.OldPath, doRemove: true,
result: FileResult{Path: f.NewPath, OldPath: f.OldPath, Status: Renamed, Hunks: len(f.Hunks)},
}
case mailpatch.Copied:
orig, err := readExisting(fsys, f.OldPath)
if err != nil {
return p, err
}
content, err := ApplyToBytes(orig, f)
if err != nil {
return p, err
}
p = planContent{
writePath: f.NewPath, content: content, perm: parseMode(f.NewMode), doWrite: true,
result: FileResult{Path: f.NewPath, Status: Created, Hunks: len(f.Hunks)},
}
case mailpatch.Modified:
return planModify(fsys, f)
default:
// Unknown change type: treat it as a modification.
return planModify(fsys, f)
}
return p, nil
}
// planModify computes the plan for an in-place edit of an existing file.
func planModify(fsys FS, f mailpatch.FileChange) (planContent, error) {
target := f.Path()
orig, err := readExisting(fsys, target)
if err != nil {
return planContent{}, err
}
content, err := ApplyToBytes(orig, f)
if err != nil {
return planContent{}, err
}
return planContent{
writePath: target, content: content, perm: parseMode(f.NewMode), doWrite: true,
result: FileResult{Path: target, Status: Updated, Hunks: len(f.Hunks)},
}, nil
}
func readExisting(fsys FS, name string) ([]byte, error) {
b, err := fsys.ReadFile(name)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil, &PathError{Path: name, Err: ErrMissing}
}
return nil, err
}
return b, nil
}
func parseMode(mode string) fs.FileMode {
if mode == "" {
return 0
}
// git modes look like "100644"; the low 9 bits are the unix permission.
n, err := strconv.ParseInt(mode, 8, 32)
if err != nil {
return 0
}
return fs.FileMode(n).Perm()
}
// ApplyDiff parses a bare unified diff and applies it to fsys.
func ApplyDiff(fsys FS, diff string, opts *Options) (*Result, error) {
files, err := mailpatch.ParseDiff(diff)
if err != nil {
return nil, err
}
return Apply(fsys, files, opts)
}
// ApplyPatch applies a parsed format-patch email (its FileChanges) to fsys.
func ApplyPatch(fsys FS, p *mailpatch.Patch, opts *Options) (*Result, error) {
if p == nil {
return &Result{}, nil
}
return Apply(fsys, p.Files, opts)
}