diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2017-11-24 12:21:55 -0500 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2017-11-24 12:33:03 -0500 |
commit | 221873029a6166acaba1fef37508f9f1544180cd (patch) | |
tree | 8fedd9d27d25c0591e2f60468670335a4ded9a25 /textproto/catblob.go | |
parent | 4a566395874113e38b5785fc5e436c96c001b032 (diff) |
clean up textproto; move types.go into libfastimport
Diffstat (limited to 'textproto/catblob.go')
-rw-r--r-- | textproto/catblob.go | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/textproto/catblob.go b/textproto/catblob.go new file mode 100644 index 0000000..9b0f8d7 --- /dev/null +++ b/textproto/catblob.go @@ -0,0 +1,77 @@ +package textproto + +import ( + "bufio" + "fmt" + "io" + "strconv" +) + +type CatBlobReader struct { + r *bufio.Reader +} + +func NewCatBlobReader(r io.Reader) *CatBlobReader { + return &CatBlobReader{ + r: bufio.NewReader(r), + } +} + +func (cbr *CatBlobReader) ReadLine() (line string, err error) { + for len(line) <= 1 { + line, err = cbr.r.ReadString('\n') + if err != nil { + return + } + } + + // get-mark : <sha1> LF + // cat-blob : <sha1> SP 'blob' SP <size> LF + // <data> LF + // ls : <mode> SP ('blob' | 'tree' | 'commit') SP <dataref> HT <path> LF + // ls : 'missing' SP <path> LF + + // decide if we have a cat-blob result (return early if we don't) + if len(line) <= 46 || line[40:46] != " blob " { + return + } + for _, b := range line[:40] { + if !(('0' <= b && b <= '9') || ('a' <= b && b <= 'f')) { + return + } + } + // we have a cat-blob result + var size int + size, err = strconv.Atoi(line[46 : len(line)-1]) + if err != nil { + return + } + data := make([]byte, size+1) + _, err = io.ReadFull(cbr.r, data) + line += string(data[:size]) + return +} + +type CatBlobWriter struct { + w io.Writer +} + +func NewCatBlobWriter(w io.Writer) *CatBlobWriter { + return &CatBlobWriter{ + w: w, + } +} + +func (cbw *CatBlobWriter) WriteLine(a ...interface{}) error { + _, err := fmt.Fprintln(cbw.w, a...) + return err +} + +func (cbw *CatBlobWriter) WriteBlob(sha1 string, data string) error { + err := cbw.WriteLine(sha1, "blob", len(data)) + if err != nil { + return err + } + _, err = io.WriteString(cbw.w, data) + return err +} |