diff options
Diffstat (limited to 'clients')
-rw-r--r-- | clients/common/common.go | 42 | ||||
-rw-r--r-- | clients/common/common_test.go | 24 |
2 files changed, 50 insertions, 16 deletions
diff --git a/clients/common/common.go b/clients/common/common.go index 1c6e552..a09f2d8 100644 --- a/clients/common/common.go +++ b/clients/common/common.go @@ -180,8 +180,7 @@ func (c *Capabilities) String() string { type GitUploadPackInfo struct { Capabilities *Capabilities - Head core.Hash - Refs map[string]core.Hash + Refs map[core.ReferenceName]*core.Reference } func NewGitUploadPackInfo() *GitUploadPackInfo { @@ -207,7 +206,7 @@ func (r *GitUploadPackInfo) read(d *pktline.Decoder) error { } isEmpty := true - r.Refs = map[string]core.Hash{} + r.Refs = map[core.ReferenceName]*core.Reference{} for _, line := range lines { if !r.isValidLine(line) { continue @@ -230,9 +229,15 @@ func (r *GitUploadPackInfo) read(d *pktline.Decoder) error { } func (r *GitUploadPackInfo) decodeHeaderLine(line string) { - parts := strings.SplitN(line, " HEAD", 2) - r.Head = core.NewHash(parts[0]) r.Capabilities.Decode(line) + + name := r.Capabilities.SymbolicReference("HEAD") + if len(name) == 0 { + return + } + + refName := core.ReferenceName(name) + r.Refs[core.HEAD] = core.NewSymbolicReference(core.HEAD, refName) } func (r *GitUploadPackInfo) isValidLine(line string) bool { @@ -245,7 +250,22 @@ func (r *GitUploadPackInfo) readLine(line string) { return } - r.Refs[parts[1]] = core.NewHash(parts[0]) + ref := core.NewReferenceFromStrings(parts[1], parts[0]) + r.Refs[ref.Name()] = ref +} + +func (r *GitUploadPackInfo) Head() *core.Reference { + h, ok := r.Refs[core.HEAD] + if !ok { + return nil + } + + ref, ok := r.Refs[h.Target()] + if !ok { + return nil + } + + return ref } func (r *GitUploadPackInfo) String() string { @@ -256,10 +276,14 @@ func (r *GitUploadPackInfo) Bytes() []byte { e := pktline.NewEncoder() e.AddLine("# service=git-upload-pack") e.AddFlush() - e.AddLine(fmt.Sprintf("%s HEAD\x00%s", r.Head, r.Capabilities.String())) + e.AddLine(fmt.Sprintf("%s HEAD\x00%s", r.Head().Hash(), r.Capabilities.String())) + + for _, ref := range r.Refs { + if ref.Type() != core.HashReference { + continue + } - for name, id := range r.Refs { - e.AddLine(fmt.Sprintf("%s %s", id, name)) + e.AddLine(fmt.Sprintf("%s %s", ref.Hash(), ref.Name())) } e.AddFlush() diff --git a/clients/common/common_test.go b/clients/common/common_test.go index 791001e..5be6eba 100644 --- a/clients/common/common_test.go +++ b/clients/common/common_test.go @@ -50,10 +50,17 @@ func (s *SuiteCommon) TestGitUploadPackInfo(c *C) { err := i.Decode(pktline.NewDecoder(bytes.NewBuffer(b))) c.Assert(err, IsNil) - ref := i.Capabilities.SymbolicReference("HEAD") - c.Assert(ref, Equals, "refs/heads/master") - c.Assert(i.Refs[ref].String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") - c.Assert(i.Head.String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") + name := i.Capabilities.SymbolicReference("HEAD") + c.Assert(name, Equals, "refs/heads/master") + c.Assert(i.Refs, HasLen, 4) + + ref := i.Refs[core.ReferenceName(name)] + c.Assert(ref, NotNil) + c.Assert(ref.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") + + ref = i.Refs[core.HEAD] + c.Assert(ref, NotNil) + c.Assert(ref.Target(), Equals, core.ReferenceName(name)) } func (s *SuiteCommon) TestGitUploadPackInfoEmpty(c *C) { @@ -101,11 +108,14 @@ func (s *SuiteCommon) TestGitUploadPackEncode(c *C) { info := NewGitUploadPackInfo() info.Capabilities.Add("symref", "HEAD:refs/heads/master") - info.Head = core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5") - info.Refs = map[string]core.Hash{ - "refs/heads/master": info.Head, + ref := core.ReferenceName("refs/heads/master") + hash := core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5") + info.Refs = map[core.ReferenceName]*core.Reference{ + core.HEAD: core.NewSymbolicReference(core.HEAD, ref), + ref: core.NewHashReference(ref, hash), } + c.Assert(info.Head(), NotNil) c.Assert(info.String(), Equals, "001e# service=git-upload-pack\n"+ "000000506ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00symref=HEAD:refs/heads/master\n"+ |