diff options
author | Ayman Bagabas <ayman.bagabas@gmail.com> | 2023-11-17 15:28:25 -0500 |
---|---|---|
committer | Ayman Bagabas <ayman.bagabas@gmail.com> | 2023-11-23 11:46:54 -0500 |
commit | e2c6ae3333a3facd13aa52e1986a2ba2dbc56a9d (patch) | |
tree | 99aa669c39e7c2530ed7ef8af88c3c5d62931c1f /plumbing/format/pktline/error.go | |
parent | e1875336041ead704df7f481f8691452ef939556 (diff) | |
download | go-git-e2c6ae3333a3facd13aa52e1986a2ba2dbc56a9d.tar.gz |
plumbing: handle pktline erro-line as errors
Error when encountering an error-line
See https://git-scm.com/docs/pack-protocol
Update plumbing/format/pktline/error.go
Co-authored-by: Paulo Gomes <paulo.gomes.uk@gmail.com>
Update plumbing/format/pktline/error.go
Co-authored-by: Paulo Gomes <paulo.gomes.uk@gmail.com>
feat: format newline
Diffstat (limited to 'plumbing/format/pktline/error.go')
-rw-r--r-- | plumbing/format/pktline/error.go | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/plumbing/format/pktline/error.go b/plumbing/format/pktline/error.go new file mode 100644 index 0000000..2c0e5a7 --- /dev/null +++ b/plumbing/format/pktline/error.go @@ -0,0 +1,51 @@ +package pktline + +import ( + "bytes" + "errors" + "io" + "strings" +) + +var ( + // ErrInvalidErrorLine is returned by Decode when the packet line is not an + // error line. + ErrInvalidErrorLine = errors.New("expected an error-line") + + errPrefix = []byte("ERR ") +) + +// ErrorLine is a packet line that contains an error message. +// Once this packet is sent by client or server, the data transfer process is +// terminated. +// See https://git-scm.com/docs/pack-protocol#_pkt_line_format +type ErrorLine struct { + Text string +} + +// Error implements the error interface. +func (e *ErrorLine) Error() string { + return e.Text +} + +// Encode encodes the ErrorLine into a packet line. +func (e *ErrorLine) Encode(w io.Writer) error { + p := NewEncoder(w) + return p.Encodef("%s%s\n", string(errPrefix), e.Text) +} + +// Decode decodes a packet line into an ErrorLine. +func (e *ErrorLine) Decode(r io.Reader) error { + s := NewScanner(r) + if !s.Scan() { + return s.Err() + } + + line := s.Bytes() + if !bytes.HasPrefix(line, errPrefix) { + return ErrInvalidErrorLine + } + + e.Text = strings.TrimSpace(string(line[4:])) + return nil +} |