summaryrefslogtreecommitdiff
path: root/frontend.go
blob: 405ead440c692a14a60af462bee4e78a31cfdfc6 (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
package libfastimport

import (
	"bufio"
	"io"
	"strings"

	"git.lukeshu.com/go/libfastimport/textproto"
)

type UnsupportedCommand string

func (e UnsupportedCommand) Error() string {
	return "Unsupported command: " + string(e)
}

type cmderror struct {
	Cmd
	error
}

// 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
	c   chan cmderror
}

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)
	}
	return &ret
}

func (f *Frontend) nextLine() (line string, err error) {
retry:
	line, err = f.fir.ReadLine()
	if err != nil {
		return
	}
	switch {
	case strings.HasPrefix(line, "#"):
		f.c <- cmderror{CmdComment{Comment: line[1:]}, nil}
		goto retry
	case strings.HasPrefix(line, "cat-blob "):
		f.c <- parse_cat_blob(line)
		goto retry
	case strings.HasPrefix(line, "get-mark "):
		f.c <- parse_get_mark(line)
		goto retry
	default:
		return
	}
}

func (f *Frontend) parse() {
	for {
		line, err := f.nextLine()
		if err != nil {
			f.c <- cmderror{nil, err}
			return
		}
		switch {
		case strings.HasPrefix(line, "blob "):
		case strings.HasPrefix(line, "commit "):
		case strings.HasPrefix(line, "checkpoint\n"):
		case strings.HasPrefix(line, "done\n"):
		case strings.HasPrefix(line, "feature "):
		case strings.HasPrefix(line, "ls "):
		case strings.HasPrefix(line, "option "):
		case strings.HasPrefix(line, "progress "):
		case strings.HasPrefix(line, "reset "):
		case strings.HasPrefix(line, "tag "):
		default:
			f.c <- cmderror{nil, UnsupportedCommand(line)}
			return
		}
	}
}

func (f *Frontend) ReadCmd() (Cmd, error) {
	cmderror := <-f.c
	cmd := cmderror.Cmd
	err := cmderror.error
	return cmderror.Cmd, cmderror.error
}

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()
}