1package sideband 2 3import ( 4 "io" 5 6 "github.com/go-git/go-git/v5/plumbing/format/pktline" 7) 8 9// Muxer multiplex the packfile along with the progress messages and the error 10// information. The multiplex is perform using pktline format. 11type Muxer struct { 12 max int 13 e *pktline.Encoder 14} 15 16const chLen = 1 17 18// NewMuxer returns a new Muxer for the given t that writes on w. 19// 20// If t is equal to `Sideband` the max pack size is set to MaxPackedSize, in any 21// other value is given, max pack is set to MaxPackedSize64k, that is the 22// maximum length of a line in pktline format. 23func NewMuxer(t Type, w io.Writer) *Muxer { 24 max := MaxPackedSize64k 25 if t == Sideband { 26 max = MaxPackedSize 27 } 28 29 return &Muxer{ 30 max: max - chLen, 31 e: pktline.NewEncoder(w), 32 } 33} 34 35// Write writes p in the PackData channel 36func (m *Muxer) Write(p []byte) (int, error) { 37 return m.WriteChannel(PackData, p) 38} 39 40// WriteChannel writes p in the given channel. This method can be used with any 41// channel, but is recommend use it only for the ProgressMessage and 42// ErrorMessage channels and use Write for the PackData channel 43func (m *Muxer) WriteChannel(t Channel, p []byte) (int, error) { 44 wrote := 0 45 size := len(p) 46 for wrote < size { 47 n, err := m.doWrite(t, p[wrote:]) 48 wrote += n 49 50 if err != nil { 51 return wrote, err 52 } 53 } 54 55 return wrote, nil 56} 57 58func (m *Muxer) doWrite(ch Channel, p []byte) (int, error) { 59 sz := len(p) 60 if sz > m.max { 61 sz = m.max 62 } 63 64 return sz, m.e.Encode(ch.WithPayload(p[:sz])) 65} 66