-
Notifications
You must be signed in to change notification settings - Fork 8
/
patch.go
110 lines (97 loc) · 2.04 KB
/
patch.go
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021 Hajime Hoshi
package hitsumabushi
import (
"bufio"
"bytes"
"fmt"
"io"
"strings"
)
type patch struct {
name string
replaces map[string]string
append string
}
func parsePatch(name string, r io.Reader) (*patch, error) {
p := &patch{
name: name,
replaces: map[string]string{},
}
var from string
var to string
const (
phaseInit = iota
phaseFrom
phaseTo
phaseAppend
)
var phase int
s := bufio.NewScanner(r)
var i int
for s.Scan() {
switch line := s.Text(); line {
case "//--from":
if phase == phaseFrom {
return nil, fmt.Errorf("unexpected //--from at %s:L%d", p.name, i)
}
if phase != phaseInit {
p.replaces[from] = to
from = ""
to = ""
}
phase = phaseFrom
case "//--to":
if phase != phaseFrom {
return nil, fmt.Errorf("unexpected //--to at %s:L%d", p.name, i)
}
phase = phaseTo
case "//--append":
if phase == phaseFrom {
return nil, fmt.Errorf("unexpected //--append at %s:L%d", p.name, i)
}
if phase != phaseInit {
p.replaces[from] = to
from = ""
to = ""
}
phase = phaseAppend
default:
switch phase {
case phaseInit:
return nil, fmt.Errorf("unexpected content at %s:L%d", p.name, i)
case phaseFrom:
from += line + "\n"
case phaseTo:
to += line + "\n"
case phaseAppend:
p.append += line + "\n"
}
}
i++
}
if from != "" {
p.replaces[from] = to
}
return p, nil
}
func (p *patch) apply(r io.Reader) (io.Reader, error) {
buf, err := io.ReadAll(r)
if err != nil {
return nil, err
}
str := string(buf)
for from, to := range p.replaces {
if !strings.Contains(str, from) {
return nil, fmt.Errorf("hitsumabushi: patching %s failed: %s", p.name, from[:strings.IndexByte(from, '\n')])
}
old := str
new := strings.Replace(old, from, to, 1)
if old == new {
return nil, fmt.Errorf("hitsumabushi: patching %s failed: replacing result is the same", p.name)
}
str = new
}
str += p.append
return bytes.NewBufferString(str), nil
}