diff options
-rw-r--r-- | backend.go | 32 |
1 files changed, 18 insertions, 14 deletions
@@ -11,10 +11,10 @@ import ( // A Backend is something that consumes a fast-import stream; the // Backend object provides methods for writing to it. type Backend struct { - w io.WriteCloser - bw *bufio.Writer - fiw *textproto.FIWriter - cbr *textproto.CatBlobReader + fastImportClose io.Closer + fastImportFlush *bufio.Writer + fastImportWrite *textproto.FIWriter + catBlob *textproto.CatBlobReader inCommit bool @@ -24,18 +24,21 @@ type Backend struct { func NewBackend(fastImport io.WriteCloser, catBlob io.Reader, onErr func(error) error) *Backend { ret := &Backend{} - ret.w = fastImport - ret.bw = bufio.NewWriter(ret.w) - ret.fiw = textproto.NewFIWriter(ret.bw) + + ret.fastImportClose = fastImport + ret.fastImportFlush = bufio.NewWriter(fastImport) + ret.fastImportWrite = textproto.NewFIWriter(ret.fastImportFlush) + if catBlob != nil { - ret.cbr = textproto.NewCatBlobReader(catBlob) + ret.catBlob = textproto.NewCatBlobReader(catBlob) } + ret.onErr = func(err error) error { ret.err = err // Close the underlying writer, but don't let the // error mask the previous error. - err = ret.w.Close() + err = ret.fastImportClose.Close() if ret.err == nil { ret.err = err } @@ -45,6 +48,7 @@ func NewBackend(fastImport io.WriteCloser, catBlob io.Reader, onErr func(error) } return ret.err } + return ret } @@ -68,11 +72,11 @@ func (b *Backend) Do(cmd Cmd) error { panic(fmt.Errorf("invalid cmdClass: %d", cmd.fiCmdClass())) } - err := cmd.fiCmdWrite(b.fiw) + err := cmd.fiCmdWrite(b.fastImportWrite) if err != nil { return b.onErr(err) } - err = b.bw.Flush() + err = b.fastImportFlush.Flush() if err != nil { return b.onErr(err) } @@ -89,7 +93,7 @@ func (b *Backend) GetMark(cmd CmdGetMark) (sha1 string, err error) { if err != nil { return } - line, err := b.cbr.ReadLine() + line, err := b.catBlob.ReadLine() if err != nil { err = b.onErr(err) return @@ -106,7 +110,7 @@ func (b *Backend) CatBlob(cmd CmdCatBlob) (sha1 string, data string, err error) if err != nil { return } - line, err := b.cbr.ReadLine() + line, err := b.catBlob.ReadLine() if err != nil { err = b.onErr(err) return @@ -123,7 +127,7 @@ func (b *Backend) Ls(cmd CmdLs) (mode textproto.Mode, dataref string, path textp if err != nil { return } - line, err := b.cbr.ReadLine() + line, err := b.catBlob.ReadLine() if err != nil { err = b.onErr(err) return |