summaryrefslogtreecommitdiff
path: root/frontend.go
blob: 256ba520e537d84fdd0d0e56a7b20e8f37de9bdd (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
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
			continue
		case strings.HasPrefix(line, "feature "):
			// 'feature' SP <feature> ('=' <argument>)? LF
			str := strings.TrimSuffix(strings.TrimPrefix(line, "feature "), "\n")
			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: strings.TrimSuffix(strings.TrimPrefix(line, "option "), "\n")}
		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)?
			c := CmdReset{
				RefName: strings.TrimSuffix(strings.TrimPrefix(line, "reset "), "\n")
			}
			line, err = f.nextLine()
			if err != nil {
				return err
			}
			if strings.HasPrefix(line, "from ") {
				c.CommitIsh = strings.TrimSuffix(strings.TrimPrefix(line, "from "), "\n")
				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
			// 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()
}