-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpatchset_apply.go
More file actions
151 lines (136 loc) · 4.43 KB
/
patchset_apply.go
File metadata and controls
151 lines (136 loc) · 4.43 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
package git_diff_parser
import "fmt"
const patchsetOperationModify patchsetOperation = "modify"
func applyPatchsetFile(tree map[string][]byte, file *patchsetFile) error {
if file.Diff.IsBinary {
return &unsupportedPatchError{
Operation: patchsetOperationBinary,
Path: firstNonEmpty(file.Diff.ToFile, file.Diff.FromFile),
}
}
op, sourcePath, targetPath, err := determinePatchsetOperation(tree, &file.Diff)
if err != nil {
return err
}
switch op {
case patchsetOperationCreate:
if _, exists := tree[targetPath]; exists {
return fmt.Errorf("cannot create existing file %q", targetPath)
}
content, err := applyPatchsetContent(nil, file)
if err != nil {
return err
}
tree[targetPath] = append([]byte(nil), content...)
return nil
case patchsetOperationDelete:
content, exists := tree[sourcePath]
if !exists {
return fmt.Errorf("cannot delete missing file %q", sourcePath)
}
if len(file.Diff.Hunks) > 0 {
if _, err := applyPatchsetContent(content, file); err != nil {
return err
}
}
delete(tree, sourcePath)
return nil
case patchsetOperationRename:
content, exists := tree[sourcePath]
if !exists {
return fmt.Errorf("cannot rename missing file %q", sourcePath)
}
if targetPath != sourcePath {
if _, exists := tree[targetPath]; exists {
return fmt.Errorf("cannot rename %q to existing file %q", sourcePath, targetPath)
}
}
applied, err := applyPatchsetContent(content, file)
if err != nil {
return err
}
delete(tree, sourcePath)
tree[targetPath] = append([]byte(nil), applied...)
return nil
case patchsetOperationCopy:
content, exists := tree[sourcePath]
if !exists {
return fmt.Errorf("cannot copy missing file %q", sourcePath)
}
if _, exists := tree[targetPath]; exists {
return fmt.Errorf("cannot copy to existing file %q", targetPath)
}
applied, err := applyPatchsetContent(content, file)
if err != nil {
return err
}
tree[targetPath] = append([]byte(nil), applied...)
return nil
case patchsetOperationModeChange, patchsetOperationModify:
content, exists := tree[targetPath]
if !exists {
return fmt.Errorf("cannot modify missing file %q", targetPath)
}
applied, err := applyPatchsetContent(content, file)
if err != nil {
return err
}
tree[targetPath] = append([]byte(nil), applied...)
return nil
default:
return fmt.Errorf("unsupported patch operation")
}
}
func determinePatchsetOperation(tree map[string][]byte, fileDiff *fileDiff) (op patchsetOperation, sourcePath, targetPath string, err error) {
sourcePath, targetPath = patchsetPaths(fileDiff)
switch {
case fileDiff.RenameFrom != "" || fileDiff.RenameTo != "":
return patchsetOperationRename, sourcePath, targetPath, nil
case fileDiff.CopyFrom != "" || fileDiff.CopyTo != "":
return patchsetOperationCopy, sourcePath, targetPath, nil
case fileDiff.Type == fileDiffTypeAdded:
return patchsetOperationCreate, "", targetPath, nil
case fileDiff.Type == fileDiffTypeDeleted:
return patchsetOperationDelete, sourcePath, "", nil
}
if fileDiff.NewMode != "" && fileDiff.OldMode == "" {
if _, exists := tree[targetPath]; exists {
return "", "", "", fmt.Errorf("cannot create existing file %q", targetPath)
}
return patchsetOperationCreate, "", targetPath, nil
}
if fileDiff.OldMode != "" || fileDiff.NewMode != "" {
return patchsetOperationModeChange, sourcePath, targetPath, nil
}
return patchsetOperationModify, sourcePath, targetPath, nil
}
func patchsetPaths(fileDiff *fileDiff) (sourcePath, targetPath string) {
sourcePath = firstNonEmpty(fileDiff.RenameFrom, fileDiff.CopyFrom, fileDiff.FromFile, fileDiff.ToFile)
targetPath = firstNonEmpty(fileDiff.RenameTo, fileDiff.CopyTo, fileDiff.ToFile, fileDiff.FromFile)
return sourcePath, targetPath
}
func applyPatchsetContent(pristine []byte, file *patchsetFile) ([]byte, error) {
if len(file.Diff.Hunks) == 0 {
return append([]byte(nil), pristine...), nil
}
hunks := make([]patchHunk, 0, len(file.Diff.Hunks))
for i := range file.Diff.Hunks {
hunks = append(hunks, patchHunkFromHunk(&file.Diff.Hunks[i]))
}
result, err := newPatchApply(applyOptions{Mode: applyModeApply}).applyValidatedPatch(pristine, validatedPatch{
rejectHead: formatRejectHeader(&file.Diff),
hunks: hunks,
})
if err != nil {
return nil, err
}
return append([]byte(nil), result.Content...), nil
}
func firstNonEmpty(values ...string) string {
for _, value := range values {
if value != "" {
return value
}
}
return ""
}