Debian Codegraph @

plumbing/format/pktline/error.go

raw
1 package pktline
2
3 import (
4 "bytes"
5 "errors"
6 "io"
7 "strings"
8 )
9
10 var (
11 // ErrInvalidErrorLine is returned by Decode when the packet line is not an
12 // error line.
13 ErrInvalidErrorLine = errors.New("expected an error-line")
14
15 errPrefix = []byte("ERR ")
16 )
17
18 // ErrorLine is a packet line that contains an error message.
19 // Once this packet is sent by client or server, the data transfer process is
20 // terminated.
21 // See https://git-scm.com/docs/pack-protocol#_pkt_line_format
22 type ErrorLine struct {
23 Text string
24 }
25
26 // Error implements the error interface.
27 func (e *ErrorLine) Error() string {
28 return e.Text
29 }
30
31 // Encode encodes the ErrorLine into a packet line.
32 func (e *ErrorLine) Encode(w io.Writer) error {
33 p := NewEncoder(w)
34 return p.Encodef("%s%s\n", string(errPrefix), e.Text)
35 }
36
37 // Decode decodes a packet line into an ErrorLine.
38 func (e *ErrorLine) Decode(r io.Reader) error {
39 s := NewScanner(r)
40 if !s.Scan() {
41 return s.Err()
42 }
43
44 line := s.Bytes()
45 if !bytes.HasPrefix(line, errPrefix) {
46 return ErrInvalidErrorLine
47 }
48
49 e.Text = strings.TrimSpace(string(line[4:]))
50 return nil
51 }