diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-31 19:38:42 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-31 19:38:42 +0200 |
commit | 484d472550d74138731bfdef4e68d8b810abb93f (patch) | |
tree | 235b361b6abb725b6bae08cf7b0904b34785912b /clients | |
parent | d72a19796ef0f556db93b553547f2ac085b59a1a (diff) | |
download | go-git-484d472550d74138731bfdef4e68d8b810abb93f.tar.gz |
clients/common: GitUploadPackInfo correct handling capabilities and symrefs
Diffstat (limited to 'clients')
-rw-r--r-- | clients/common/common.go | 47 | ||||
-rw-r--r-- | clients/common/common_test.go | 18 |
2 files changed, 39 insertions, 26 deletions
diff --git a/clients/common/common.go b/clients/common/common.go index e1140ec..0d0026b 100644 --- a/clients/common/common.go +++ b/clients/common/common.go @@ -97,11 +97,6 @@ func NewCapabilities() *Capabilities { // Decode decodes a string func (c *Capabilities) Decode(raw string) { - parts := strings.SplitN(raw, "HEAD", 2) - if len(parts) == 2 { - raw = parts[1] - } - params := strings.Split(raw, " ") for _, p := range params { s := strings.SplitN(p, "=", 2) @@ -234,12 +229,10 @@ func (r *GitUploadPackInfo) read(d *pktline.Decoder) error { continue } - if len(r.Capabilities.o) == 0 { - r.decodeHeaderLine(line) - continue + if err := r.readLine(line); err != nil { + return err } - r.readLine(line) isEmpty = false } @@ -250,29 +243,31 @@ func (r *GitUploadPackInfo) read(d *pktline.Decoder) error { return nil } -func (r *GitUploadPackInfo) decodeHeaderLine(line string) { - r.Capabilities.Decode(line) - - name := r.Capabilities.SymbolicReference("HEAD") - if len(name) == 0 { - return - } - - refName := core.ReferenceName(name) - r.Refs.Set(core.NewSymbolicReference(core.HEAD, refName)) -} - func (r *GitUploadPackInfo) isValidLine(line string) bool { return line[0] != '#' } -func (r *GitUploadPackInfo) readLine(line string) { - parts := strings.Split(strings.Trim(line, " \n"), " ") - if len(parts) != 2 { - return +func (r *GitUploadPackInfo) readLine(line string) error { + hashEnd := strings.Index(line, " ") + hash := line[:hashEnd] + + zeroID := strings.Index(line, string([]byte{0})) + if zeroID == -1 { + name := line[hashEnd+1 : len(line)-1] + ref := core.NewReferenceFromStrings(name, hash) + return r.Refs.Set(ref) + } + + name := line[hashEnd+1 : zeroID] + r.Capabilities.Decode(line[zeroID+1 : len(line)-1]) + if !r.Capabilities.Supports("symref") { + ref := core.NewReferenceFromStrings(name, hash) + return r.Refs.Set(ref) } - r.Refs.Set(core.NewReferenceFromStrings(parts[1], parts[0])) + target := r.Capabilities.SymbolicReference(name) + ref := core.NewSymbolicReference(core.ReferenceName(name), core.ReferenceName(target)) + return r.Refs.Set(ref) } func (r *GitUploadPackInfo) Head() *core.Reference { diff --git a/clients/common/common_test.go b/clients/common/common_test.go index d2d3696..cc6c6d1 100644 --- a/clients/common/common_test.go +++ b/clients/common/common_test.go @@ -64,6 +64,24 @@ func (s *SuiteCommon) TestGitUploadPackInfo(c *C) { c.Assert(ref.Target(), Equals, core.ReferenceName(name)) } +const GitUploadPackInfoNoHEADFixture = "MDAxZSMgc2VydmljZT1naXQtdXBsb2FkLXBhY2sKMDAwMDAwYmNkN2UxZmVlMjYxMjM0YmIzYTQzYzA5NmY1NTg3NDhhNTY5ZDc5ZWZmIHJlZnMvaGVhZHMvdjQAbXVsdGlfYWNrIHRoaW4tcGFjayBzaWRlLWJhbmQgc2lkZS1iYW5kLTY0ayBvZnMtZGVsdGEgc2hhbGxvdyBuby1wcm9ncmVzcyBpbmNsdWRlLXRhZyBtdWx0aV9hY2tfZGV0YWlsZWQgbm8tZG9uZSBhZ2VudD1naXQvMS45LjEKMDAwMA==" + +func (s *SuiteCommon) TestGitUploadPackInfoNoHEAD(c *C) { + b, _ := base64.StdEncoding.DecodeString(GitUploadPackInfoNoHEADFixture) + + i := NewGitUploadPackInfo() + err := i.Decode(pktline.NewDecoder(bytes.NewBuffer(b))) + c.Assert(err, IsNil) + + name := i.Capabilities.SymbolicReference("HEAD") + c.Assert(name, Equals, "") + c.Assert(i.Refs, HasLen, 1) + + ref := i.Refs["refs/heads/v4"] + c.Assert(ref, NotNil) + c.Assert(ref.Hash().String(), Equals, "d7e1fee261234bb3a43c096f558748a569d79eff") +} + func (s *SuiteCommon) TestGitUploadPackInfoEmpty(c *C) { b := bytes.NewBuffer(nil) |