Debian Codegraph @

plumbing/protocol/packp/advrefs_encode.go

raw
1 package packp
2
3 import (
4 "bytes"
5 "fmt"
6 "io"
7 "sort"
8
9 "github.com/go-git/go-git/v5/plumbing"
10 "github.com/go-git/go-git/v5/plumbing/format/pktline"
11 "github.com/go-git/go-git/v5/plumbing/protocol/packp/capability"
12 )
13
14 // Encode writes the AdvRefs encoding to a writer.
15 //
16 // All the payloads will end with a newline character. Capabilities,
17 // references and shallows are written in alphabetical order, except for
18 // peeled references that always follow their corresponding references.
19 func (a *AdvRefs) Encode(w io.Writer) error {
20 e := newAdvRefsEncoder(w)
21 return e.Encode(a)
22 }
23
24 type advRefsEncoder struct {
25 data *AdvRefs // data to encode
26 pe *pktline.Encoder // where to write the encoded data
27 firstRefName string // reference name to encode in the first pkt-line (HEAD if present)
28 firstRefHash plumbing.Hash // hash referenced to encode in the first pkt-line (HEAD if present)
29 sortedRefs []string // hash references to encode ordered by increasing order
30 err error // sticky error
31
32 }
33
34 func newAdvRefsEncoder(w io.Writer) *advRefsEncoder {
35 return &advRefsEncoder{
36 pe: pktline.NewEncoder(w),
37 }
38 }
39
40 func (e *advRefsEncoder) Encode(v *AdvRefs) error {
41 e.data = v
42 e.sortRefs()
43 e.setFirstRef()
44
45 for state := encodePrefix; state != nil; {
46 state = state(e)
47 }
48
49 return e.err
50 }
51
52 func (e *advRefsEncoder) sortRefs() {
53 if len(e.data.References) > 0 {
54 refs := make([]string, 0, len(e.data.References))
55 for refName := range e.data.References {
56 refs = append(refs, refName)
57 }
58
59 sort.Strings(refs)
60 e.sortedRefs = refs
61 }
62 }
63
64 func (e *advRefsEncoder) setFirstRef() {
65 if e.data.Head != nil {
66 e.firstRefName = head
67 e.firstRefHash = *e.data.Head
68 return
69 }
70
71 if len(e.sortedRefs) > 0 {
72 refName := e.sortedRefs[0]
73 e.firstRefName = refName
74 e.firstRefHash = e.data.References[refName]
75 }
76 }
77
78 type encoderStateFn func(*advRefsEncoder) encoderStateFn
79
80 func encodePrefix(e *advRefsEncoder) encoderStateFn {
81 for _, p := range e.data.Prefix {
82 if bytes.Equal(p, pktline.Flush) {
83 if e.err = e.pe.Flush(); e.err != nil {
84 return nil
85 }
86 continue
87 }
88 if e.err = e.pe.Encodef("%s\n", string(p)); e.err != nil {
89 return nil
90 }
91 }
92
93 return encodeFirstLine
94 }
95
96 // Adds the first pkt-line payload: head hash, head ref and capabilities.
97 // If HEAD ref is not found, the first reference ordered in increasing order will be used.
98 // If there aren't HEAD neither refs, the first line will be "PKT-LINE(zero-id SP "capabilities^{}" NUL capability-list)".
99 // See: https://github.com/git/git/blob/master/Documentation/technical/pack-protocol.txt
100 // See: https://github.com/git/git/blob/master/Documentation/technical/protocol-common.txt
101 func encodeFirstLine(e *advRefsEncoder) encoderStateFn {
102 const formatFirstLine = "%s %s\x00%s\n"
103 var firstLine string
104 capabilities := formatCaps(e.data.Capabilities)
105
106 if e.firstRefName == "" {
107 firstLine = fmt.Sprintf(formatFirstLine, plumbing.ZeroHash.String(), "capabilities^{}", capabilities)
108 } else {
109 firstLine = fmt.Sprintf(formatFirstLine, e.firstRefHash.String(), e.firstRefName, capabilities)
110
111 }
112
113 if e.err = e.pe.EncodeString(firstLine); e.err != nil {
114 return nil
115 }
116
117 return encodeRefs
118 }
119
120 func formatCaps(c *capability.List) string {
121 if c == nil {
122 return ""
123 }
124
125 return c.String()
126 }
127
128 // Adds the (sorted) refs: hash SP refname EOL
129 // and their peeled refs if any.
130 func encodeRefs(e *advRefsEncoder) encoderStateFn {
131 for _, r := range e.sortedRefs {
132 if r == e.firstRefName {
133 continue
134 }
135
136 hash := e.data.References[r]
137 if e.err = e.pe.Encodef("%s %s\n", hash.String(), r); e.err != nil {
138 return nil
139 }
140
141 if hash, ok := e.data.Peeled[r]; ok {
142 if e.err = e.pe.Encodef("%s %s^{}\n", hash.String(), r); e.err != nil {
143 return nil
144 }
145 }
146 }
147
148 return encodeShallow
149 }
150
151 // Adds the (sorted) shallows: "shallow" SP hash EOL
152 func encodeShallow(e *advRefsEncoder) encoderStateFn {
153 sorted := sortShallows(e.data.Shallows)
154 for _, hash := range sorted {
155 if e.err = e.pe.Encodef("shallow %s\n", hash); e.err != nil {
156 return nil
157 }
158 }
159
160 return encodeFlush
161 }
162
163 func sortShallows(c []plumbing.Hash) []string {
164 ret := []string{}
165 for _, h := range c {
166 ret = append(ret, h.String())
167 }
168 sort.Strings(ret)
169
170 return ret
171 }
172
173 func encodeFlush(e *advRefsEncoder) encoderStateFn {
174 e.err = e.pe.Flush()
175 return nil
176 }