-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpatchset.go
More file actions
190 lines (167 loc) · 4.72 KB
/
patchset.go
File metadata and controls
190 lines (167 loc) · 4.72 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
package git_diff_parser
import (
"bytes"
"errors"
"fmt"
"strings"
)
var (
errPatchCreate = errors.New("patch creates are not supported")
errPatchDelete = errors.New("patch deletes are not supported")
errPatchRename = errors.New("patch renames are not supported")
errPatchModeChange = errors.New("patch mode changes are not supported")
errPatchBinary = errors.New("binary patches are not supported")
)
type patchsetOperation string
const (
patchsetOperationCreate patchsetOperation = "create"
patchsetOperationDelete patchsetOperation = "delete"
patchsetOperationRename patchsetOperation = "rename"
patchsetOperationCopy patchsetOperation = "copy"
patchsetOperationModeChange patchsetOperation = "mode change"
patchsetOperationBinary patchsetOperation = "binary"
)
type unsupportedPatchError struct {
Operation patchsetOperation
Path string
From string
To string
}
func (e *unsupportedPatchError) Error() string {
switch e.Operation {
case patchsetOperationCreate:
if e.Path != "" {
return fmt.Sprintf("patch creates are not supported for %q", e.Path)
}
return "patch creates are not supported"
case patchsetOperationDelete:
if e.Path != "" {
return fmt.Sprintf("patch deletes are not supported for %q", e.Path)
}
return "patch deletes are not supported"
case patchsetOperationRename:
if e.From != "" || e.To != "" {
return fmt.Sprintf("patch renames are not supported: %q -> %q", e.From, e.To)
}
return "patch renames are not supported"
case patchsetOperationModeChange:
if e.Path != "" {
return fmt.Sprintf("patch mode changes are not supported for %q", e.Path)
}
return "patch mode changes are not supported"
case patchsetOperationBinary:
if e.Path != "" {
return fmt.Sprintf("binary patches are not supported for %q", e.Path)
}
return "binary patches are not supported"
default:
return "unsupported patch"
}
}
func (e *unsupportedPatchError) Is(target error) bool {
switch target {
case errPatchCreate:
return e.Operation == patchsetOperationCreate
case errPatchDelete:
return e.Operation == patchsetOperationDelete
case errPatchRename:
return e.Operation == patchsetOperationRename
case errPatchModeChange:
return e.Operation == patchsetOperationModeChange
case errPatchBinary:
return e.Operation == patchsetOperationBinary
default:
return false
}
}
type patchset struct {
Files []patchsetFile
}
type patchsetFile struct {
Diff fileDiff
Patch []byte
Operation patchsetOperation
SourcePath string
TargetPath string
}
func parsePatchset(patchData []byte) (patchset, []error) {
parsed, errs := parse(string(patchData))
if len(errs) > 0 {
return patchset{}, errs
}
chunks := splitPatchsetChunks(patchData)
if len(chunks) != len(parsed.FileDiff) {
return patchset{}, []error{
fmt.Errorf("parsed %d file diffs but split %d patch fragments", len(parsed.FileDiff), len(chunks)),
}
}
files := make([]patchsetFile, len(chunks))
var operationErrs []error
for i := range chunks {
files[i] = patchsetFile{
Diff: parsed.FileDiff[i],
Patch: chunks[i],
}
if err := classifyPatchsetFile(&files[i]); err != nil {
operationErrs = append(operationErrs, err)
}
}
if len(operationErrs) > 0 {
return patchset{}, operationErrs
}
return patchset{Files: files}, nil
}
func (p patchset) apply(tree map[string][]byte) (map[string][]byte, error) {
return applyPatchsetFiles(tree, p.filePointers())
}
func applyPatchset(tree map[string][]byte, patchData []byte) (map[string][]byte, error) {
patchset, errs := parsePatchset(patchData)
if len(errs) > 0 {
return nil, fmt.Errorf("unsupported patch syntax: %w", errs[0])
}
return patchset.apply(tree)
}
func ApplyPatchset(tree map[string][]byte, patchData []byte) (map[string][]byte, error) {
return applyPatchset(tree, patchData)
}
func (p patchset) filePointers() []*patchsetFile {
files := make([]*patchsetFile, len(p.Files))
for i := range p.Files {
files[i] = &p.Files[i]
}
return files
}
func cloneTree(tree map[string][]byte) map[string][]byte {
out := make(map[string][]byte, len(tree))
for path, content := range tree {
out[path] = append([]byte(nil), content...)
}
return out
}
func splitPatchsetChunks(patchData []byte) [][]byte {
lines := splitLinesPreserveNewline(string(patchData))
if len(lines) == 0 {
return nil
}
chunks := make([][]byte, 0)
var buf bytes.Buffer
started := false
flush := func() {
if !started || buf.Len() == 0 {
return
}
chunks = append(chunks, append([]byte(nil), buf.Bytes()...))
buf.Reset()
}
for _, line := range lines {
if strings.HasPrefix(strings.TrimRight(line, "\n"), "diff --git ") {
flush()
started = true
}
if started {
buf.WriteString(line)
}
}
flush()
return chunks
}