/
protocol.go
230 lines (177 loc) · 5.31 KB
/
protocol.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package gitserver
import (
"fmt"
"io"
"log"
"strings"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/storer"
"github.com/euforia/go-git-server/packfile"
"github.com/euforia/go-git-server/pktline"
"github.com/euforia/go-git-server/repository"
)
// GitServiceType can be either receive or upload pack
type GitServiceType string
const (
// GitServiceRecvPack constant for receive-pack
GitServiceRecvPack = GitServiceType("git-receive-pack")
// GitServiceUploadPack constant for upload-pack
GitServiceUploadPack = GitServiceType("git-upload-pack")
)
// TxRef is a transaction to update a repo reference
type TxRef struct {
oldHash plumbing.Hash
newHash plumbing.Hash
ref string
}
// Protocol implements the git pack protocol
type Protocol struct {
w io.Writer
r io.Reader
}
// NewProtocol instantiates a new protocol with the given reader and writer
func NewProtocol(w io.Writer, r io.Reader) *Protocol {
return &Protocol{w: w, r: r}
}
// ListReferences writes the references in the pack protocol given the repository
// and service type
func (proto *Protocol) ListReferences(service GitServiceType, refs *repository.RepositoryReferences) {
// Start sending info
enc := pktline.NewEncoder(proto.w)
enc.Encode([]byte(fmt.Sprintf("# service=%s\n", service)))
enc.Encode(nil)
// Repo empty so send zeros
if (refs.Heads == nil || len(refs.Heads) == 0) && (refs.Tags == nil || len(refs.Tags) == 0) {
b0 := append([]byte("0000000000000000000000000000000000000000"), 32)
b0 = append(b0, nullCapabilities()...)
enc.Encode(append(b0, 10))
enc.Encode(nil)
return
}
// Send HEAD info
head := refs.Head
lh := append([]byte(fmt.Sprintf("%s HEAD", head.Hash.String())), '\x00')
lh = append(lh, capabilities()...)
if service == GitServiceUploadPack {
lh = append(lh, []byte(" symref=HEAD:refs/"+head.Ref)...)
}
enc.Encode(append(lh, 10))
// Send refs - heads
for href, h := range refs.Heads {
enc.Encode([]byte(fmt.Sprintf("%s refs/heads/%s\n", h.String(), href)))
}
// Send refs - tags
for tref, h := range refs.Tags {
enc.Encode([]byte(fmt.Sprintf("%s refs/tags/%s\n", h.String(), tref)))
}
enc.Encode(nil)
}
// UploadPack implements the git upload pack protocol
func (proto *Protocol) UploadPack(store storer.EncodedObjectStorer) ([]byte, error) {
wants, haves, err := parseUploadPackWantsAndHaves(proto.r)
if err != nil {
return nil, err
}
log.Printf("DBG [upload-pack] wants=%d haves=%d", len(wants), len(haves))
enc := pktline.NewEncoder(proto.w)
enc.Encode([]byte("NAK\n"))
packenc := packfile.NewEncoder(proto.w, store)
return packenc.Encode(wants...)
}
// ReceivePack implements the git receive pack protocol
func (proto *Protocol) ReceivePack(repo *repository.Repository, repostore repository.RepositoryStore, objstore storer.EncodedObjectStorer) error {
enc := pktline.NewEncoder(proto.w)
//var txs []TxRef
txs, err := parseReceivePackClientRefLines(proto.r)
if err != nil {
enc.Encode([]byte(fmt.Sprintf("unpack %v", err)))
return err
}
// Decode packfile
packdec := packfile.NewDecoder(proto.r, objstore)
if err = packdec.Decode(); err != nil {
enc.Encode([]byte(fmt.Sprintf("unpack %v", err)))
return err
}
enc.Encode([]byte("unpack ok\n"))
// Update repo refs
for _, tx := range txs {
if er := repo.Refs.UpdateRef(tx.ref, tx.oldHash, tx.newHash); er != nil {
log.Println("ERR [receive-pack]", er)
continue
}
enc.Encode([]byte(fmt.Sprintf("ok %s\n", tx.ref)))
}
// Store update repo
if err = repostore.UpdateRepo(repo); err != nil {
log.Println("ERR [receive-pack] Failed to update repo:", err)
}
enc.Encode(nil)
return err
}
func parseReceivePackClientRefLines(r io.Reader) ([]TxRef, error) {
dec := pktline.NewDecoder(r)
var lines [][]byte
// Read refs from client
if err := dec.DecodeUntilFlush(&lines); err != nil {
//log.Printf("[receive-pack] ERR %v", e)
return nil, err
}
txs := make([]TxRef, len(lines))
for i, l := range lines {
log.Printf("DBG [receive-pack] %s", l)
rt, err := parseReceiveRefUpdateLine(l)
if err != nil {
return nil, err
}
txs[i] = rt
}
return txs, nil
}
// Parses old hash, new hash, and ref from a line
func parseReceiveRefUpdateLine(line []byte) (rt TxRef, err error) {
s := string(line)
arr := strings.Split(s, " ")
if len(arr) < 3 {
err = fmt.Errorf("invalid line: %s", line)
return
}
rt = TxRef{
oldHash: plumbing.NewHash(arr[0]),
newHash: plumbing.NewHash(arr[1]),
ref: strings.TrimSuffix(arr[2], string([]byte{0})),
}
return
}
func parseUploadPackWantsAndHaves(r io.Reader) (wants, haves []plumbing.Hash, err error) {
dec := pktline.NewDecoder(r)
for {
var line []byte
if err = dec.Decode(&line); err != nil {
break
} else if len(line) == 0 {
continue
} else {
line = line[:len(line)-1]
}
if string(line) == "done" {
break
}
log.Printf("DBG [upload-pack] %s", line)
op := strings.Split(string(line), " ")
switch op[0] {
case "want":
wants = append(wants, plumbing.NewHash(op[1]))
case "have":
haves = append(haves, plumbing.NewHash(op[1]))
}
}
return
}
func capabilities() []byte {
//return []byte("report-status delete-refs ofs-delta multi_ack_detailed")
return []byte("report-status delete-refs ofs-delta")
}
func nullCapabilities() []byte {
return append(append([]byte("capabilities^{}"), '\x00'), capabilities()...)
}