summaryrefslogtreecommitdiff
path: root/frontend.go
blob: c08e1b8fc347f01b3c9d6e25dd54421fe582a442 (plain)
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
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)
}

func trimLinePrefix(line string, prefix string) string {
	if !strings.HasPrefix(line, prefix) {
		panic("line didn't have prefix")
	}
	if !strings.HasSuffix(line, "\n") {
		panic("line didn't have prefix")
	}
	return strings.TrimSuffix(strings.TrimPrefix(line, prefix), "\n")
}

// 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
			f.cmd <- CmdCatBlob{DataRef: trimLinePrefix(line, "cat-blob ")}
		case strings.HasPrefix(line, "get-mark :"):
			// 'get-mark' SP ':' <idnum> LF
			c := CmdGetMark{}
			c.Mark, err = strconv.Atoi(trimLinePrefix(line, "get-mark :"))
			if err != nil {
				line = ""
				err = fmt.Errorf("get-mark: %v", err)
				return
			}
			f.cmd <- c
		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+1]
	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 := trimLinePrefix(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
		size, err := strconv.Atoi(trimLinePrefix(head, "data "))
		if err != nil {
			return "", err
		}
		if size != 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 :") {
				c.Mark, err = strconv.Atoi(trimLinePrefix(line, "mark :"))
				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
			continue
		case strings.HasPrefix(line, "feature "):
			// 'feature' SP <feature> ('=' <argument>)? LF
			str := trimLinePrefix(line, "feature ")
			eq := strings.IndexByte(str, '=')
			if eq < 0 {
				f.cmd <- CmdFeature{
					Feature: str,
				}
			} else {
				f.cmd <- CmdFeature{
					Feature:  str[:eq],
					Argument: str[eq+1:],
				}
			}
		case strings.HasPrefix(line, "ls "):
			// 'ls' SP <dataref> SP <path> LF
			sp1 := strings.IndexByte(line, ' ')
			sp2 := strings.IndexByte(line[sp1+1:], ' ')
			lf := strings.IndexByte(line[sp2+1:], '\n')
			if sp1 < 0 || sp2 < 0 || lf < 0 {
				return fmt.Errorf("ls: outside of a commit both <dataref> and <path> are required: %v", line)
			}
			f.cmd <- CmdLs{
				DataRef: line[sp1+1 : sp2],
				Path:    textproto.PathUnescape(line[sp2+1 : lf]),
			}
		case strings.HasPrefix(line, "option "):
			// 'option' SP <option> LF
			f.cmd <- CmdOption{Option: trimLinePrefix(line, "option ")}
		case strings.HasPrefix(line, "progress "):
			// 'progress' SP <any> LF
			f.cmd <- CmdProgress{Str: trimLinePrefix(line, "progress ")}
		case strings.HasPrefix(line, "reset "):
			// 'reset' SP <ref> LF
			// ('from' SP <commit-ish> LF)?
			c := CmdReset{RefName: trimLinePrefix(line, "reset ")}
			line, err = f.nextLine()
			if err != nil {
				return err
			}
			if strings.HasPrefix(line, "from ") {
				c.CommitIsh = trimLinePrefix(line, "from ")
				line, err = f.nextLine()
				if err != nil {
					return err
				}
			}
			f.cmd <- c
			continue
		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
			c := CmdTag{RefName: trimLinePrefix(line, "tag ")}

			line, err = f.nextLine()
			if err != nil {
				return err
			}
			if !strings.HasPrefix(line, "from ") {
				return fmt.Errorf("tag: expected from command: %v", line)
			}
			c.CommitIsh = trimLinePrefix(line, "from ")

			line, err = f.nextLine()
			if err != nil {
				return err
			}
			if !strings.HasPrefix(line, "tagger ") {
				return fmt.Errorf("tag: expected tagger command: %v", line)
			}
			c.Tagger, err = textproto.ParseUserTime(trimLinePrefix(line, "tagger "))
			if err != nil {
				return err
			}

			line, err = f.nextLine()
			if err != nil {
				return err
			}
			c.Data, err = parse_data(line)
			if err != nil {
				return err
			}
			f.cmd <- c
		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()
}