Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit b024ef7

Browse files
committed
clients/common: GitUploadPackInfo correct handling capabilities and symrefs
1 parent d7e1fee commit b024ef7

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

clients/common/common.go

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,6 @@ func NewCapabilities() *Capabilities {
9797

9898
// Decode decodes a string
9999
func (c *Capabilities) Decode(raw string) {
100-
parts := strings.SplitN(raw, "HEAD", 2)
101-
if len(parts) == 2 {
102-
raw = parts[1]
103-
}
104-
105100
params := strings.Split(raw, " ")
106101
for _, p := range params {
107102
s := strings.SplitN(p, "=", 2)
@@ -234,12 +229,10 @@ func (r *GitUploadPackInfo) read(d *pktline.Decoder) error {
234229
continue
235230
}
236231

237-
if len(r.Capabilities.o) == 0 {
238-
r.decodeHeaderLine(line)
239-
continue
232+
if err := r.readLine(line); err != nil {
233+
return err
240234
}
241235

242-
r.readLine(line)
243236
isEmpty = false
244237
}
245238

@@ -250,29 +243,31 @@ func (r *GitUploadPackInfo) read(d *pktline.Decoder) error {
250243
return nil
251244
}
252245

253-
func (r *GitUploadPackInfo) decodeHeaderLine(line string) {
254-
r.Capabilities.Decode(line)
255-
256-
name := r.Capabilities.SymbolicReference("HEAD")
257-
if len(name) == 0 {
258-
return
259-
}
260-
261-
refName := core.ReferenceName(name)
262-
r.Refs.Set(core.NewSymbolicReference(core.HEAD, refName))
263-
}
264-
265246
func (r *GitUploadPackInfo) isValidLine(line string) bool {
266247
return line[0] != '#'
267248
}
268249

269-
func (r *GitUploadPackInfo) readLine(line string) {
270-
parts := strings.Split(strings.Trim(line, " \n"), " ")
271-
if len(parts) != 2 {
272-
return
250+
func (r *GitUploadPackInfo) readLine(line string) error {
251+
hashEnd := strings.Index(line, " ")
252+
hash := line[:hashEnd]
253+
254+
zeroID := strings.Index(line, string([]byte{0}))
255+
if zeroID == -1 {
256+
name := line[hashEnd+1 : len(line)-1]
257+
ref := core.NewReferenceFromStrings(name, hash)
258+
return r.Refs.Set(ref)
259+
}
260+
261+
name := line[hashEnd+1 : zeroID]
262+
r.Capabilities.Decode(line[zeroID+1 : len(line)-1])
263+
if !r.Capabilities.Supports("symref") {
264+
ref := core.NewReferenceFromStrings(name, hash)
265+
return r.Refs.Set(ref)
273266
}
274267

275-
r.Refs.Set(core.NewReferenceFromStrings(parts[1], parts[0]))
268+
target := r.Capabilities.SymbolicReference(name)
269+
ref := core.NewSymbolicReference(core.ReferenceName(name), core.ReferenceName(target))
270+
return r.Refs.Set(ref)
276271
}
277272

278273
func (r *GitUploadPackInfo) Head() *core.Reference {

clients/common/common_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ func (s *SuiteCommon) TestGitUploadPackInfo(c *C) {
6464
c.Assert(ref.Target(), Equals, core.ReferenceName(name))
6565
}
6666

67+
const GitUploadPackInfoNoHEADFixture = "MDAxZSMgc2VydmljZT1naXQtdXBsb2FkLXBhY2sKMDAwMDAwYmNkN2UxZmVlMjYxMjM0YmIzYTQzYzA5NmY1NTg3NDhhNTY5ZDc5ZWZmIHJlZnMvaGVhZHMvdjQAbXVsdGlfYWNrIHRoaW4tcGFjayBzaWRlLWJhbmQgc2lkZS1iYW5kLTY0ayBvZnMtZGVsdGEgc2hhbGxvdyBuby1wcm9ncmVzcyBpbmNsdWRlLXRhZyBtdWx0aV9hY2tfZGV0YWlsZWQgbm8tZG9uZSBhZ2VudD1naXQvMS45LjEKMDAwMA=="
68+
69+
func (s *SuiteCommon) TestGitUploadPackInfoNoHEAD(c *C) {
70+
b, _ := base64.StdEncoding.DecodeString(GitUploadPackInfoNoHEADFixture)
71+
72+
i := NewGitUploadPackInfo()
73+
err := i.Decode(pktline.NewDecoder(bytes.NewBuffer(b)))
74+
c.Assert(err, IsNil)
75+
76+
name := i.Capabilities.SymbolicReference("HEAD")
77+
c.Assert(name, Equals, "")
78+
c.Assert(i.Refs, HasLen, 1)
79+
80+
ref := i.Refs["refs/heads/v4"]
81+
c.Assert(ref, NotNil)
82+
c.Assert(ref.Hash().String(), Equals, "d7e1fee261234bb3a43c096f558748a569d79eff")
83+
}
84+
6785
func (s *SuiteCommon) TestGitUploadPackInfoEmpty(c *C) {
6886
b := bytes.NewBuffer(nil)
6987

0 commit comments

Comments
 (0)