Debian Codegraph @

plumbing/protocol/packp/report_status.go

raw
1 package packp
2
3 import (
4 "bytes"
5 "fmt"
6 "io"
7 "strings"
8
9 "github.com/go-git/go-git/v5/plumbing"
10 "github.com/go-git/go-git/v5/plumbing/format/pktline"
11 )
12
13 const (
14 ok = "ok"
15 )
16
17 // ReportStatus is a report status message, as used in the git-receive-pack
18 // process whenever the 'report-status' capability is negotiated.
19 type ReportStatus struct {
20 UnpackStatus string
21 CommandStatuses []*CommandStatus
22 }
23
24 // NewReportStatus creates a new ReportStatus message.
25 func NewReportStatus() *ReportStatus {
26 return &ReportStatus{}
27 }
28
29 // Error returns the first error if any.
30 func (s *ReportStatus) Error() error {
31 if s.UnpackStatus != ok {
32 return fmt.Errorf("unpack error: %s", s.UnpackStatus)
33 }
34
35 for _, s := range s.CommandStatuses {
36 if err := s.Error(); err != nil {
37 return err
38 }
39 }
40
41 return nil
42 }
43
44 // Encode writes the report status to a writer.
45 func (s *ReportStatus) Encode(w io.Writer) error {
46 e := pktline.NewEncoder(w)
47 if err := e.Encodef("unpack %s\n", s.UnpackStatus); err != nil {
48 return err
49 }
50
51 for _, cs := range s.CommandStatuses {
52 if err := cs.encode(w); err != nil {
53 return err
54 }
55 }
56
57 return e.Flush()
58 }
59
60 // Decode reads from the given reader and decodes a report-status message. It
61 // does not read more input than what is needed to fill the report status.
62 func (s *ReportStatus) Decode(r io.Reader) error {
63 scan := pktline.NewScanner(r)
64 if err := s.scanFirstLine(scan); err != nil {
65 return err
66 }
67
68 if err := s.decodeReportStatus(scan.Bytes()); err != nil {
69 return err
70 }
71
72 flushed := false
73 for scan.Scan() {
74 b := scan.Bytes()
75 if isFlush(b) {
76 flushed = true
77 break
78 }
79
80 if err := s.decodeCommandStatus(b); err != nil {
81 return err
82 }
83 }
84
85 if !flushed {
86 return fmt.Errorf("missing flush")
87 }
88
89 return scan.Err()
90 }
91
92 func (s *ReportStatus) scanFirstLine(scan *pktline.Scanner) error {
93 if scan.Scan() {
94 return nil
95 }
96
97 if scan.Err() != nil {
98 return scan.Err()
99 }
100
101 return io.ErrUnexpectedEOF
102 }
103
104 func (s *ReportStatus) decodeReportStatus(b []byte) error {
105 if isFlush(b) {
106 return fmt.Errorf("premature flush")
107 }
108
109 b = bytes.TrimSuffix(b, eol)
110
111 line := string(b)
112 fields := strings.SplitN(line, " ", 2)
113 if len(fields) != 2 || fields[0] != "unpack" {
114 return fmt.Errorf("malformed unpack status: %s", line)
115 }
116
117 s.UnpackStatus = fields[1]
118 return nil
119 }
120
121 func (s *ReportStatus) decodeCommandStatus(b []byte) error {
122 b = bytes.TrimSuffix(b, eol)
123
124 line := string(b)
125 fields := strings.SplitN(line, " ", 3)
126 status := ok
127 if len(fields) == 3 && fields[0] == "ng" {
128 status = fields[2]
129 } else if len(fields) != 2 || fields[0] != "ok" {
130 return fmt.Errorf("malformed command status: %s", line)
131 }
132
133 cs := &CommandStatus{
134 ReferenceName: plumbing.ReferenceName(fields[1]),
135 Status: status,
136 }
137 s.CommandStatuses = append(s.CommandStatuses, cs)
138 return nil
139 }
140
141 // CommandStatus is the status of a reference in a report status.
142 // See ReportStatus struct.
143 type CommandStatus struct {
144 ReferenceName plumbing.ReferenceName
145 Status string
146 }
147
148 // Error returns the error, if any.
149 func (s *CommandStatus) Error() error {
150 if s.Status == ok {
151 return nil
152 }
153
154 return fmt.Errorf("command error on %s: %s",
155 s.ReferenceName.String(), s.Status)
156 }
157
158 func (s *CommandStatus) encode(w io.Writer) error {
159 e := pktline.NewEncoder(w)
160 if s.Error() == nil {
161 return e.Encodef("ok %s\n", s.ReferenceName.String())
162 }
163
164 return e.Encodef("ng %s %s\n", s.ReferenceName.String(), s.Status)
165 }