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
|
package libfastimport
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
"git.lukeshu.com/go/libfastimport/textproto"
)
type UnsupportedCommand string
func (e UnsupportedCommand) Error() string {
return "Unsupported command: " + string(e)
}
// A Frontend is something that produces a fast-import stream; the
// Frontend object provides methods for reading from it.
type Frontend struct {
fir *textproto.FIReader
cbw *textproto.CatBlobWriter
w *bufio.Writer
cmd chan Cmd
err error
}
func NewFrontend(fastImport io.Reader, catBlob io.Writer) *Frontend {
ret := &Frontend{}
ret.fir = textproto.NewFIReader(fastImport)
if catBlob != nil {
ret.w = bufio.NewWriter(catBlob)
ret.cbw = textproto.NewCatBlobWriter(ret.w)
}
ret.cmd = make(chan Cmd)
go func() {
ret.err = ret.parse()
close(ret.cmd)
}()
return ret
}
func (f *Frontend) nextLine() (line string, err error) {
for {
line, err = f.fir.ReadLine()
if err != nil {
return
}
switch {
case strings.HasPrefix(line, "#"):
f.cmd <- CmdComment{Comment: line[1:]}
case strings.HasPrefix(line, "cat-blob "):
// 'cat-blob' SP <dataref> LF
dataref := strings.TrimSuffix(strings.TrimPrefix(line, "cat-blob "), "\n")
f.cmd <- CmdCatBlob{DataRef: dataref}
case strings.HasPrefix(line, "get-mark :"):
// 'get-mark' SP ':' <idnum> LF
strIdnum := strings.TrimSuffix(strings.TrimPrefix(line, "get-mark :"), "\n")
var nIdnum int
nIdnum, err = strconv.Atoi(strIdnum)
if err != nil {
line = ""
err = fmt.Errorf("get-mark: %v", err)
return
}
f.cmd <- CmdGetMark{Mark: nIdnum}
default:
return
}
}
}
func parse_data(line string) (data string, err error) {
nl := strings.IndexByte(line, '\n')
if nl < 0 {
return "", fmt.Errorf("data: expected newline: %v", data)
}
head := line[:nl]
rest := line[nl+1:]
if !strings.HasPrefix(head, "data ") {
return "", fmt.Errorf("data: could not parse: %v", data)
}
if strings.HasPrefix(head, "data <<") {
// Delimited format
delim := strings.TrimPrefix(head, "data <<")
suffix := "\n" + delim + "\n"
if !strings.HasSuffix(rest, suffix) {
return "", fmt.Errorf("data: did not find suffix: %v", suffix)
}
data = strings.TrimSuffix(rest, suffix)
} else {
// Exact byte count format
strN := strings.TrimSuffix(head, "data ")
intN, err := strconv.Atoi(strN)
if err != nil {
return "", err
}
if intN != len(rest) {
panic("FIReader should not have let this happen")
}
data = rest
}
return
}
func (f *Frontend) parse() error {
line, err := f.nextLine()
if err != nil {
return err
}
for {
switch {
case line == "blob\n":
// 'blob' LF
// mark?
// data
c := CmdBlob{}
line, err = f.nextLine()
if err != nil {
return err
}
if strings.HasPrefix(line, "mark :") {
str := strings.TrimSuffix(strings.TrimPrefix(line, "mark :"), "\n")
c.Mark, err = strconv.Atoi(str)
if err != nil {
return err
}
line, err = f.nextLine()
if err != nil {
return err
}
}
if !strings.HasPrefix(line, "data ") {
return fmt.Errorf("Unexpected command in blob: %q", line)
}
c.Data, err = parse_data(line)
if err != nil {
return err
}
f.cmd <- c
case line == "checkpoint\n":
f.cmd <- CmdCheckpoint{}
case line == "done\n":
f.cmd <- CmdDone{}
case strings.HasPrefix(line, "commit "):
// 'commit' SP <ref> LF
// mark?
// ('author' (SP <name>)? SP LT <email> GT SP <when> LF)?
// 'committer' (SP <name>)? SP LT <email> GT SP <when> LF
// data
// ('from' SP <commit-ish> LF)?
// ('merge' SP <commit-ish> LF)*
// (filemodify | filedelete | filecopy | filerename | filedeleteall | notemodify)*
// TODO
case strings.HasPrefix(line, "feature "):
// 'feature' SP <feature> ('=' <argument>)? LF
// TODO
case strings.HasPrefix(line, "ls "):
// 'ls' SP <dataref> SP <path> LF
// TODO
case strings.HasPrefix(line, "option "):
// 'option' SP <option> LF
// TODO
case strings.HasPrefix(line, "progress "):
// 'progress' SP <any> LF
str := strings.TrimSuffix(strings.TrimPrefix(line, "progress "), "\n")
f.cmd <- CmdProgress{Str: str}
case strings.HasPrefix(line, "reset "):
// 'reset' SP <ref> LF
// ('from' SP <commit-ish> LF)?
// TODO
case strings.HasPrefix(line, "tag "):
// 'tag' SP <name> LF
// 'from' SP <commit-ish> LF
// 'tagger' (SP <name>)? SP LT <email> GT SP <when> LF
// data
// TODO
default:
return UnsupportedCommand(line)
}
line, err = f.nextLine()
if err != nil {
return err
}
}
}
func (f *Frontend) ReadCmd() (Cmd, error) {
cmd, ok := <-f.cmd
if ok {
return cmd, nil
}
return nil, f.err
}
func (f *Frontend) RespondGetMark(sha1 string) error {
// TODO
return f.w.Flush()
}
func (f *Frontend) RespondCatBlob(sha1 string, data string) error {
// TODO
return f.w.Flush()
}
func (f *Frontend) RespondLs(mode textproto.Mode, dataref string, path textproto.Path) error {
// TODO
return f.w.Flush()
}
|