-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathgit.go
285 lines (261 loc) · 8.15 KB
/
git.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package gitfs presents a file tree downloaded from a remote Git repo as an in-memory fs.FS.
package gitfs
import (
"bytes"
"encoding/hex"
"fmt"
"io"
"io/fs"
"net/http"
"strings"
)
// A Repo is a connection to a remote repository served over HTTP or HTTPS.
type Repo struct {
url string // trailing slash removed
caps map[string]string
}
// NewRepo connects to a Git repository at the given http:// or https:// URL.
func NewRepo(url string) (*Repo, error) {
r := &Repo{url: strings.TrimSuffix(url, "/")}
if err := r.handshake(); err != nil {
return nil, err
}
return r, nil
}
// handshake runs the initial Git opening handshake, learning the capabilities of the server.
// See https://git-scm.com/docs/protocol-v2#_initial_client_request.
func (r *Repo) handshake() error {
req, _ := http.NewRequest("GET", r.url+"/info/refs?service=git-upload-pack", nil)
req.Header.Set("Accept", "*/*")
req.Header.Set("Git-Protocol", "version=2")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("handshake: %v", err)
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if resp.StatusCode != 200 {
return fmt.Errorf("handshake: %v\n%s", resp.Status, data)
}
if err != nil {
return fmt.Errorf("handshake: reading body: %v", err)
}
if ct := resp.Header.Get("Content-Type"); ct != "application/x-git-upload-pack-advertisement" {
return fmt.Errorf("handshake: invalid response Content-Type: %v", ct)
}
pr := newPktLineReader(bytes.NewReader(data))
lines, err := pr.Lines()
if len(lines) == 1 && lines[0] == "# service=git-upload-pack" {
lines, err = pr.Lines()
}
if err != nil {
return fmt.Errorf("handshake: parsing response: %v", err)
}
caps := make(map[string]string)
for _, line := range lines {
verb, args, _ := strings.Cut(line, "=")
caps[verb] = args
}
if _, ok := caps["version 2"]; !ok {
return fmt.Errorf("handshake: not version 2: %q", lines)
}
r.caps = caps
return nil
}
// Resolve looks up the given ref and returns the corresponding Hash.
func (r *Repo) Resolve(ref string) (Hash, error) {
if h, err := parseHash(ref); err == nil {
return h, nil
}
fail := func(err error) (Hash, error) {
return Hash{}, fmt.Errorf("resolve %s: %v", ref, err)
}
refs, err := r.refs(ref)
if err != nil {
return fail(err)
}
for _, known := range refs {
if known.name == ref {
return known.hash, nil
}
}
return fail(fmt.Errorf("unknown ref"))
}
// A ref is a single Git reference, like refs/heads/main, refs/tags/v1.0.0, or HEAD.
type ref struct {
name string // "refs/heads/main", "refs/tags/v1.0.0", "HEAD"
hash Hash // hexadecimal hash
}
// refs executes an ls-refs command on the remote server
// to look up refs with the given prefixes.
// See https://git-scm.com/docs/protocol-v2#_ls_refs.
func (r *Repo) refs(prefixes ...string) ([]ref, error) {
if _, ok := r.caps["ls-refs"]; !ok {
return nil, fmt.Errorf("refs: server does not support ls-refs")
}
var buf bytes.Buffer
pw := newPktLineWriter(&buf)
pw.WriteString("command=ls-refs")
pw.Delim()
pw.WriteString("peel")
pw.WriteString("symrefs")
for _, prefix := range prefixes {
pw.WriteString("ref-prefix " + prefix)
}
pw.Close()
postbody := buf.Bytes()
req, _ := http.NewRequest("POST", r.url+"/git-upload-pack", &buf)
req.Header.Set("Content-Type", "application/x-git-upload-pack-request")
req.Header.Set("Accept", "application/x-git-upload-pack-result")
req.Header.Set("Git-Protocol", "version=2")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("refs: %v", err)
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if resp.StatusCode != 200 {
return nil, fmt.Errorf("refs: %v\n%s", resp.Status, data)
}
if err != nil {
return nil, fmt.Errorf("refs: reading body: %v", err)
}
if ct := resp.Header.Get("Content-Type"); ct != "application/x-git-upload-pack-result" {
return nil, fmt.Errorf("refs: invalid response Content-Type: %v", ct)
}
var refs []ref
lines, err := newPktLineReader(bytes.NewReader(data)).Lines()
if err != nil {
return nil, fmt.Errorf("refs: parsing response: %v %d\n%s\n%s", err, len(data), hex.Dump(postbody), hex.Dump(data))
}
for _, line := range lines {
hash, rest, ok := strings.Cut(line, " ")
if !ok {
return nil, fmt.Errorf("refs: parsing response: invalid line: %q", line)
}
h, err := parseHash(hash)
if err != nil {
return nil, fmt.Errorf("refs: parsing response: invalid line: %q", line)
}
name, _, _ := strings.Cut(rest, " ")
refs = append(refs, ref{hash: h, name: name})
}
return refs, nil
}
// Clone resolves the given ref to a hash and returns the corresponding fs.FS.
func (r *Repo) Clone(ref string) (Hash, fs.FS, error) {
fail := func(err error) (Hash, fs.FS, error) {
return Hash{}, nil, fmt.Errorf("clone %s: %v", ref, err)
}
h, err := r.Resolve(ref)
if err != nil {
return fail(err)
}
tfs, err := r.fetch(h)
if err != nil {
return fail(err)
}
return h, tfs, nil
}
// CloneHash returns the fs.FS for the given hash.
func (r *Repo) CloneHash(h Hash) (fs.FS, error) {
tfs, err := r.fetch(h)
if err != nil {
return nil, fmt.Errorf("clone %s: %v", h, err)
}
return tfs, nil
}
// fetch returns the fs.FS for a given hash.
func (r *Repo) fetch(h Hash) (fs.FS, error) {
// Fetch a shallow packfile from the remote server.
// Shallow means it only contains the tree at that one commit,
// not the entire history of the repo.
// See https://git-scm.com/docs/protocol-v2#_fetch.
opts, ok := r.caps["fetch"]
if !ok {
return nil, fmt.Errorf("fetch: server does not support fetch")
}
if !strings.Contains(" "+opts+" ", " shallow ") {
return nil, fmt.Errorf("fetch: server does not support shallow fetch")
}
// Prepare and send request for pack file.
var buf bytes.Buffer
pw := newPktLineWriter(&buf)
pw.WriteString("command=fetch")
pw.Delim()
pw.WriteString("deepen 1")
pw.WriteString("want " + h.String())
pw.WriteString("done")
pw.Close()
postbody := buf.Bytes()
req, _ := http.NewRequest("POST", r.url+"/git-upload-pack", &buf)
req.Header.Set("Content-Type", "application/x-git-upload-pack-request")
req.Header.Set("Accept", "application/x-git-upload-pack-result")
req.Header.Set("Git-Protocol", "version=2")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("fetch: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
data, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("fetch: %v\n%s\n%s", resp.Status, data, hex.Dump(postbody))
}
if ct := resp.Header.Get("Content-Type"); ct != "application/x-git-upload-pack-result" {
return nil, fmt.Errorf("fetch: invalid response Content-Type: %v", ct)
}
// Response is sequence of pkt-line packets.
// It is plain text output (printed by git) until we find "packfile".
// Then it switches to packets with a single prefix byte saying
// what kind of data is in that packet:
// 1 for pack file data, 2 for text output, 3 for errors.
var data []byte
pr := newPktLineReader(resp.Body)
sawPackfile := false
for {
line, err := pr.Next()
if err != nil {
if err == io.EOF {
break
}
return nil, fmt.Errorf("fetch: parsing response: %v", err)
}
if line == nil { // ignore delimiter
continue
}
if !sawPackfile {
// Discard response lines until we get to packfile start.
if strings.TrimSuffix(string(line), "\n") == "packfile" {
sawPackfile = true
}
continue
}
if len(line) == 0 || line[0] == 0 || line[0] > 3 {
return nil, fmt.Errorf("fetch: malformed response: invalid sideband: %q", line)
}
switch line[0] {
case 1:
data = append(data, line[1:]...)
case 2:
fmt.Printf("%s\n", line[1:])
case 3:
return nil, fmt.Errorf("fetch: server error: %s", line[1:])
}
}
if !bytes.HasPrefix(data, []byte("PACK")) {
return nil, fmt.Errorf("fetch: malformed response: not packfile")
}
// Unpack pack file and return fs.FS for the commit we downloaded.
var s store
if err := unpack(&s, data); err != nil {
return nil, fmt.Errorf("fetch: %v", err)
}
tfs, err := s.commit(h)
if err != nil {
return nil, fmt.Errorf("fetch: %v", err)
}
return tfs, nil
}