Debian Codegraph @

plumbing/protocol/packp/srvresp.go

raw
1 package packp
2
3 import (
4 "bufio"
5 "bytes"
6 "errors"
7 "fmt"
8 "io"
9
10 "github.com/go-git/go-git/v5/plumbing"
11 "github.com/go-git/go-git/v5/plumbing/format/pktline"
12 )
13
14 const ackLineLen = 44
15
16 // ServerResponse object acknowledgement from upload-pack service
17 type ServerResponse struct {
18 ACKs []plumbing.Hash
19 }
20
21 // Decode decodes the response into the struct, isMultiACK should be true, if
22 // the request was done with multi_ack or multi_ack_detailed capabilities.
23 func (r *ServerResponse) Decode(reader *bufio.Reader, isMultiACK bool) error {
24 s := pktline.NewScanner(reader)
25
26 for s.Scan() {
27 line := s.Bytes()
28
29 if err := r.decodeLine(line); err != nil {
30 return err
31 }
32
33 // we need to detect when the end of a response header and the beginning
34 // of a packfile header happened, some requests to the git daemon
35 // produces a duplicate ACK header even when multi_ack is not supported.
36 stop, err := r.stopReading(reader)
37 if err != nil {
38 return err
39 }
40
41 if stop {
42 break
43 }
44 }
45
46 // isMultiACK is true when the remote server advertises the related
47 // capabilities when they are not in transport.UnsupportedCapabilities.
48 //
49 // Users may decide to remove multi_ack and multi_ack_detailed from the
50 // unsupported capabilities list, which allows them to do initial clones
51 // from Azure DevOps.
52 //
53 // Follow-up fetches may error, therefore errors are wrapped with additional
54 // information highlighting that this capabilities are not supported by go-git.
55 //
56 // TODO: Implement support for multi_ack or multi_ack_detailed responses.
57 err := s.Err()
58 if err != nil && isMultiACK {
59 return fmt.Errorf("multi_ack and multi_ack_detailed are not supported: %w", err)
60 }
61
62 return err
63 }
64
65 // stopReading detects when a valid command such as ACK or NAK is found to be
66 // read in the buffer without moving the read pointer.
67 func (r *ServerResponse) stopReading(reader *bufio.Reader) (bool, error) {
68 ahead, err := reader.Peek(7)
69 if err == io.EOF {
70 return true, nil
71 }
72
73 if err != nil {
74 return false, err
75 }
76
77 if len(ahead) > 4 && r.isValidCommand(ahead[0:3]) {
78 return false, nil
79 }
80
81 if len(ahead) == 7 && r.isValidCommand(ahead[4:]) {
82 return false, nil
83 }
84
85 return true, nil
86 }
87
88 func (r *ServerResponse) isValidCommand(b []byte) bool {
89 commands := [][]byte{ack, nak}
90 for _, c := range commands {
91 if bytes.Equal(b, c) {
92 return true
93 }
94 }
95
96 return false
97 }
98
99 func (r *ServerResponse) decodeLine(line []byte) error {
100 if len(line) == 0 {
101 return fmt.Errorf("unexpected flush")
102 }
103
104 if len(line) >= 3 {
105 if bytes.Equal(line[0:3], ack) {
106 return r.decodeACKLine(line)
107 }
108
109 if bytes.Equal(line[0:3], nak) {
110 return nil
111 }
112 }
113
114 return fmt.Errorf("unexpected content %q", string(line))
115 }
116
117 func (r *ServerResponse) decodeACKLine(line []byte) error {
118 if len(line) < ackLineLen {
119 return fmt.Errorf("malformed ACK %q", line)
120 }
121
122 sp := bytes.Index(line, []byte(" "))
123 if sp+41 > len(line) {
124 return fmt.Errorf("malformed ACK %q", line)
125 }
126 h := plumbing.NewHash(string(line[sp+1 : sp+41]))
127 r.ACKs = append(r.ACKs, h)
128 return nil
129 }
130
131 // Encode encodes the ServerResponse into a writer.
132 func (r *ServerResponse) Encode(w io.Writer, isMultiACK bool) error {
133 if len(r.ACKs) > 1 && !isMultiACK {
134 // For further information, refer to comments in the Decode func above.
135 return errors.New("multi_ack and multi_ack_detailed are not supported")
136 }
137
138 e := pktline.NewEncoder(w)
139 if len(r.ACKs) == 0 {
140 return e.Encodef("%s\n", nak)
141 }
142
143 return e.Encodef("%s %s\n", ack, r.ACKs[0].String())
144 }