| 1 | package pktlinescip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/ |
| 2 | |
| 3 | import ( |
| 4 | "bufioscip-go gomod github.com/golang/go/src go1.12 bufio/" |
| 5 | "errorsscip-go gomod github.com/golang/go/src go1.12 errors/" |
| 6 | "fmtscip-go gomod github.com/golang/go/src go1.12 fmt/" |
| 7 | "ioscip-go gomod github.com/golang/go/src go1.12 io/" |
| 8 | "io/ioutilscip-go gomod github.com/golang/go/src go1.12 `io/ioutil`/" |
| 9 | "strconvscip-go gomod github.com/golang/go/src go1.12 strconv/" |
| 10 | "stringsscip-go gomod github.com/golang/go/src go1.12 strings/" |
| 11 | ) |
| 12 | |
| 13 | const ( |
| 14 | // MaxPacketLength is the maximum total (header+payload) length |
| 15 | // encode-able within one packet using Git's pkt-line protocol. |
| 16 | MaxPacketLengthscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/MaxPacketLength. = 65516 |
| 17 | ) |
| 18 | |
| 19 | type Pktlinescip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline# struct { |
| 20 | rscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#r. *bufioscip-go gomod github.com/golang/go/src go1.12 bufio/.Readerscip-go gomod github.com/golang/go/src go1.12 bufio/Reader# |
| 21 | wscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#w. *bufioscip-go gomod github.com/golang/go/src go1.12 bufio/.Writerscip-go gomod github.com/golang/go/src go1.12 bufio/Writer# |
| 22 | } |
| 23 | |
| 24 | func NewPktlinescip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/NewPktline().(r ioscip-go gomod github.com/golang/go/src go1.12 io/.Readerscip-go gomod github.com/golang/go/src go1.12 io/Reader#, w ioscip-go gomod github.com/golang/go/src go1.12 io/.Writerscip-go gomod github.com/golang/go/src go1.12 io/Writer#) *Pktlinescip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline# { |
| 25 | return &Pktlinescip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#{ |
| 26 | rscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#r.: bufioscip-go gomod github.com/golang/go/src go1.12 bufio/.NewReaderscip-go gomod github.com/golang/go/src go1.12 bufio/NewReader().(r), |
| 27 | wscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#w.: bufioscip-go gomod github.com/golang/go/src go1.12 bufio/.NewWriterscip-go gomod github.com/golang/go/src go1.12 bufio/NewWriter().(w), |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // ReadPacket reads a single packet entirely and returns the data encoded within |
| 32 | // it. Errors can occur in several cases, as described below. |
| 33 | // |
| 34 | // 1) If no data was present in the reader, and no more data could be read (the |
| 35 | // pipe was closed, etc) than an io.EOF will be returned. |
| 36 | // 2) If there was some data to be read, but the pipe or reader was closed |
| 37 | // before an entire packet (or header) could be ingested, an |
| 38 | // io.ErrShortBuffer error will be returned. |
| 39 | // 3) If there was a valid header, but no body associated with the packet, an |
| 40 | // "Invalid packet length." error will be returned. |
| 41 | // 4) If the data in the header could not be parsed as a hexadecimal length in |
| 42 | // the Git Pktline format, the parse error will be returned. |
| 43 | // |
| 44 | // If none of the above cases fit the state of the data on the wire, the packet |
| 45 | // is returned along with a nil error. |
| 46 | func (p *Pktlinescip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#) ReadPacketscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#ReadPacket().() ([]byte, error) { |
| 47 | slice, _, err := p.ReadPacketWithLengthscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#ReadPacketWithLength().() |
| 48 | return slice, err |
| 49 | } |
| 50 | |
| 51 | // ReadPacketWithLength is exactly like ReadPacket, but on success, it also |
| 52 | // returns the packet length header value. This is useful to distinguish |
| 53 | // between flush and delim packets, which will return 0 and 1 respectively. For |
| 54 | // data packets, the length will be four more than the number of bytes in the |
| 55 | // slice. |
| 56 | func (p *Pktlinescip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#) ReadPacketWithLengthscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#ReadPacketWithLength().() ([]byte, int, error) { |
| 57 | var pktLenHex [4]byte |
| 58 | if n, err := ioscip-go gomod github.com/golang/go/src go1.12 io/.ReadFullscip-go gomod github.com/golang/go/src go1.12 io/ReadFull().(p.rscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#r., pktLenHex[:]); err != nil { |
| 59 | return nil, 0, err |
| 60 | } else if n != 4 { |
| 61 | return nil, 0, ioscip-go gomod github.com/golang/go/src go1.12 io/.ErrShortBufferscip-go gomod github.com/golang/go/src go1.12 io/ErrShortBuffer. |
| 62 | } |
| 63 | |
| 64 | pktLen, err := strconvscip-go gomod github.com/golang/go/src go1.12 strconv/.ParseIntscip-go gomod github.com/golang/go/src go1.12 strconv/ParseInt().(string(pktLenHex[:]), 16, 0) |
| 65 | if err != nil { |
| 66 | return nil, 0, err |
| 67 | } |
| 68 | |
| 69 | // 0: flush packet, 1: delim packet |
| 70 | if pktLen == 0 || pktLen == 1 { |
| 71 | return nil, int(pktLen), nil |
| 72 | } |
| 73 | if pktLen < 4 { |
| 74 | return nil, int(pktLen), errorsscip-go gomod github.com/golang/go/src go1.12 errors/.Newscip-go gomod github.com/golang/go/src go1.12 errors/New().("Invalid packet length.") |
| 75 | } |
| 76 | |
| 77 | payload, err := ioutilscip-go gomod github.com/golang/go/src go1.12 `io/ioutil`/.ReadAllscip-go gomod github.com/golang/go/src go1.12 `io/ioutil`/ReadAll().(ioscip-go gomod github.com/golang/go/src go1.12 io/.LimitReaderscip-go gomod github.com/golang/go/src go1.12 io/LimitReader().(p.rscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#r., pktLen-4)) |
| 78 | return payload, int(pktLen), err |
| 79 | } |
| 80 | |
| 81 | // ReadPacketText follows identical semantics to the `readPacket()` function, |
| 82 | // but additionally removes the trailing `\n` LF from the end of the packet, if |
| 83 | // present. |
| 84 | func (p *Pktlinescip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#) ReadPacketTextscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#ReadPacketText().() (string, error) { |
| 85 | data, err := p.ReadPacketscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#ReadPacket().() |
| 86 | return stringsscip-go gomod github.com/golang/go/src go1.12 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.12 strings/TrimSuffix().(string(data), "\n"), err |
| 87 | } |
| 88 | |
| 89 | // ReadPacketTextWithLength follows identical semantics to the |
| 90 | // `ReadPacketWithLength()` function, but additionally removes the trailing `\n` |
| 91 | // LF from the end of the packet, if present. The length field is not modified. |
| 92 | func (p *Pktlinescip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#) ReadPacketTextWithLengthscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#ReadPacketTextWithLength().() (string, int, error) { |
| 93 | data, pktLen, err := p.ReadPacketWithLengthscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#ReadPacketWithLength().() |
| 94 | return stringsscip-go gomod github.com/golang/go/src go1.12 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.12 strings/TrimSuffix().(string(data), "\n"), pktLen, err |
| 95 | } |
| 96 | |
| 97 | // ReadPacketList reads as many packets as possible using the `readPacketText` |
| 98 | // function before encountering a flush packet. It returns a slice of all the |
| 99 | // packets it read, or an error if one was encountered. |
| 100 | func (p *Pktlinescip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#) ReadPacketListscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#ReadPacketList().() ([]string, error) { |
| 101 | var list []string |
| 102 | for { |
| 103 | data, pktLen, err := p.ReadPacketTextWithLengthscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#ReadPacketTextWithLength().() |
| 104 | if err != nil { |
| 105 | return nil, err |
| 106 | } |
| 107 | |
| 108 | if pktLen == 0 { |
| 109 | break |
| 110 | } |
| 111 | |
| 112 | list = append(list, data) |
| 113 | } |
| 114 | |
| 115 | return list, nil |
| 116 | } |
| 117 | |
| 118 | // WritePacket writes the given data in "data" to the underlying data stream |
| 119 | // using Git's `pkt-line` format. |
| 120 | // |
| 121 | // If the data was longer than MaxPacketLength, an error will be returned. If |
| 122 | // there was any error encountered while writing any component of the packet |
| 123 | // (hdr, payload), it will be returned. |
| 124 | // |
| 125 | // NB: writePacket does _not_ flush the underlying buffered writer. See instead: |
| 126 | // `writeFlush()`. |
| 127 | func (p *Pktlinescip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#) WritePacketscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#WritePacket().(data []byte) error { |
| 128 | if len(data) > MaxPacketLengthscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/MaxPacketLength. { |
| 129 | return errorsscip-go gomod github.com/golang/go/src go1.12 errors/.Newscip-go gomod github.com/golang/go/src go1.12 errors/New().("Packet length exceeds maximal length") |
| 130 | } |
| 131 | |
| 132 | if _, err := p.wscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#w..WriteStringscip-go gomod github.com/golang/go/src go1.12 bufio/Writer#WriteString().(fmtscip-go gomod github.com/golang/go/src go1.12 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.12 fmt/Sprintf().("%04x", len(data)+4)); err != nil { |
| 133 | return err |
| 134 | } |
| 135 | |
| 136 | if _, err := p.wscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#w..Writescip-go gomod github.com/golang/go/src go1.12 bufio/Writer#Write().(data); err != nil { |
| 137 | return err |
| 138 | } |
| 139 | |
| 140 | return nil |
| 141 | } |
| 142 | |
| 143 | // WriteDelim writes the separating "delim" packet and then flushes the |
| 144 | // underlying buffered writer. |
| 145 | // |
| 146 | // If any error was encountered along the way, it will be returned immediately |
| 147 | func (p *Pktlinescip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#) WriteDelimscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#WriteDelim().() error { |
| 148 | if _, err := p.wscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#w..WriteStringscip-go gomod github.com/golang/go/src go1.12 bufio/Writer#WriteString().(fmtscip-go gomod github.com/golang/go/src go1.12 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.12 fmt/Sprintf().("%04x", 1)); err != nil { |
| 149 | return err |
| 150 | } |
| 151 | |
| 152 | if err := p.wscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#w..Flushscip-go gomod github.com/golang/go/src go1.12 bufio/Writer#Flush().(); err != nil { |
| 153 | return err |
| 154 | } |
| 155 | |
| 156 | return nil |
| 157 | } |
| 158 | |
| 159 | // WriteFlush writes the terminating "flush" packet and then flushes the |
| 160 | // underlying buffered writer. |
| 161 | // |
| 162 | // If any error was encountered along the way, it will be returned immediately |
| 163 | func (p *Pktlinescip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#) WriteFlushscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#WriteFlush().() error { |
| 164 | if _, err := p.wscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#w..WriteStringscip-go gomod github.com/golang/go/src go1.12 bufio/Writer#WriteString().(fmtscip-go gomod github.com/golang/go/src go1.12 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.12 fmt/Sprintf().("%04x", 0)); err != nil { |
| 165 | return err |
| 166 | } |
| 167 | |
| 168 | if err := p.wscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#w..Flushscip-go gomod github.com/golang/go/src go1.12 bufio/Writer#Flush().(); err != nil { |
| 169 | return err |
| 170 | } |
| 171 | |
| 172 | return nil |
| 173 | } |
| 174 | |
| 175 | // WritePacketText follows the same semantics as `writePacket`, but appends a |
| 176 | // trailing "\n" LF character to the end of the data. |
| 177 | func (p *Pktlinescip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#) WritePacketTextscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#WritePacketText().(data string) error { |
| 178 | return p.WritePacketscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#WritePacket().([]byte(data + "\n")) |
| 179 | } |
| 180 | |
| 181 | // WritePacketList writes a slice of strings using the semantics of |
| 182 | // and then writes a terminating flush sequence afterwords. |
| 183 | // |
| 184 | // If any error was encountered, it will be returned immediately. |
| 185 | func (p *Pktlinescip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#) WritePacketListscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#WritePacketList().(list []string) error { |
| 186 | for _, i := range list { |
| 187 | if err := p.WritePacketTextscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#WritePacketText().(i); err != nil { |
| 188 | return err |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | return p.WriteFlushscip-go gomod github.com/git-lfs/pktline . `github.com/git-lfs/pktline`/Pktline#WriteFlush().() |
| 193 | } |