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

Commit 3ad1e56

Browse files
committed
general: usage of the new Reference objects
1 parent f0b12f7 commit 3ad1e56

File tree

8 files changed

+105
-85
lines changed

8 files changed

+105
-85
lines changed

clients/common/common.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ func (c *Capabilities) String() string {
180180

181181
type GitUploadPackInfo struct {
182182
Capabilities *Capabilities
183-
Head core.Hash
184-
Refs map[string]core.Hash
183+
Refs map[core.ReferenceName]*core.Reference
185184
}
186185

187186
func NewGitUploadPackInfo() *GitUploadPackInfo {
@@ -207,7 +206,7 @@ func (r *GitUploadPackInfo) read(d *pktline.Decoder) error {
207206
}
208207

209208
isEmpty := true
210-
r.Refs = map[string]core.Hash{}
209+
r.Refs = map[core.ReferenceName]*core.Reference{}
211210
for _, line := range lines {
212211
if !r.isValidLine(line) {
213212
continue
@@ -230,9 +229,15 @@ func (r *GitUploadPackInfo) read(d *pktline.Decoder) error {
230229
}
231230

232231
func (r *GitUploadPackInfo) decodeHeaderLine(line string) {
233-
parts := strings.SplitN(line, " HEAD", 2)
234-
r.Head = core.NewHash(parts[0])
235232
r.Capabilities.Decode(line)
233+
234+
name := r.Capabilities.SymbolicReference("HEAD")
235+
if len(name) == 0 {
236+
return
237+
}
238+
239+
refName := core.ReferenceName(name)
240+
r.Refs[core.HEAD] = core.NewSymbolicReference(core.HEAD, refName)
236241
}
237242

238243
func (r *GitUploadPackInfo) isValidLine(line string) bool {
@@ -245,7 +250,22 @@ func (r *GitUploadPackInfo) readLine(line string) {
245250
return
246251
}
247252

248-
r.Refs[parts[1]] = core.NewHash(parts[0])
253+
ref := core.NewReferenceFromStrings(parts[1], parts[0])
254+
r.Refs[ref.Name()] = ref
255+
}
256+
257+
func (r *GitUploadPackInfo) Head() *core.Reference {
258+
h, ok := r.Refs[core.HEAD]
259+
if !ok {
260+
return nil
261+
}
262+
263+
ref, ok := r.Refs[h.Target()]
264+
if !ok {
265+
return nil
266+
}
267+
268+
return ref
249269
}
250270

251271
func (r *GitUploadPackInfo) String() string {
@@ -256,10 +276,14 @@ func (r *GitUploadPackInfo) Bytes() []byte {
256276
e := pktline.NewEncoder()
257277
e.AddLine("# service=git-upload-pack")
258278
e.AddFlush()
259-
e.AddLine(fmt.Sprintf("%s HEAD\x00%s", r.Head, r.Capabilities.String()))
279+
e.AddLine(fmt.Sprintf("%s HEAD\x00%s", r.Head().Hash(), r.Capabilities.String()))
280+
281+
for _, ref := range r.Refs {
282+
if ref.Type() != core.HashReference {
283+
continue
284+
}
260285

261-
for name, id := range r.Refs {
262-
e.AddLine(fmt.Sprintf("%s %s", id, name))
286+
e.AddLine(fmt.Sprintf("%s %s", ref.Hash(), ref.Name()))
263287
}
264288

265289
e.AddFlush()

clients/common/common_test.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,17 @@ func (s *SuiteCommon) TestGitUploadPackInfo(c *C) {
5050
err := i.Decode(pktline.NewDecoder(bytes.NewBuffer(b)))
5151
c.Assert(err, IsNil)
5252

53-
ref := i.Capabilities.SymbolicReference("HEAD")
54-
c.Assert(ref, Equals, "refs/heads/master")
55-
c.Assert(i.Refs[ref].String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
56-
c.Assert(i.Head.String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
53+
name := i.Capabilities.SymbolicReference("HEAD")
54+
c.Assert(name, Equals, "refs/heads/master")
55+
c.Assert(i.Refs, HasLen, 4)
56+
57+
ref := i.Refs[core.ReferenceName(name)]
58+
c.Assert(ref, NotNil)
59+
c.Assert(ref.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
60+
61+
ref = i.Refs[core.HEAD]
62+
c.Assert(ref, NotNil)
63+
c.Assert(ref.Target(), Equals, core.ReferenceName(name))
5764
}
5865

5966
func (s *SuiteCommon) TestGitUploadPackInfoEmpty(c *C) {
@@ -101,11 +108,14 @@ func (s *SuiteCommon) TestGitUploadPackEncode(c *C) {
101108
info := NewGitUploadPackInfo()
102109
info.Capabilities.Add("symref", "HEAD:refs/heads/master")
103110

104-
info.Head = core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
105-
info.Refs = map[string]core.Hash{
106-
"refs/heads/master": info.Head,
111+
ref := core.ReferenceName("refs/heads/master")
112+
hash := core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
113+
info.Refs = map[core.ReferenceName]*core.Reference{
114+
core.HEAD: core.NewSymbolicReference(core.HEAD, ref),
115+
ref: core.NewHashReference(ref, hash),
107116
}
108117

118+
c.Assert(info.Head(), NotNil)
109119
c.Assert(info.String(), Equals,
110120
"001e# service=git-upload-pack\n"+
111121
"000000506ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00symref=HEAD:refs/heads/master\n"+

common_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@ func (s *MockGitUploadPackService) Info() (*common.GitUploadPackInfo, error) {
3636
c := common.NewCapabilities()
3737
c.Decode("6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEADmulti_ack thin-pack side-band side-band-64k ofs-delta shallow no-progress include-tag multi_ack_detailed no-done symref=HEAD:refs/heads/master agent=git/2:2.4.8~dbussink-fix-enterprise-tokens-compilation-1167-gc7006cf")
3838

39+
ref := core.ReferenceName("refs/heads/master")
3940
return &common.GitUploadPackInfo{
4041
Capabilities: c,
41-
Head: h,
42-
Refs: map[string]core.Hash{"refs/heads/master": h},
42+
Refs: map[core.ReferenceName]*core.Reference{
43+
core.HEAD: core.NewSymbolicReference(core.HEAD, ref),
44+
ref: core.NewHashReference(ref, h),
45+
},
4346
}, nil
4447
}
4548

core/object.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type ObjectStorage interface {
5151
type ObjectType int8
5252

5353
const (
54+
InvalidObject ObjectType = 0
5455
CommitObject ObjectType = 1
5556
TreeObject ObjectType = 2
5657
BlobObject ObjectType = 3

examples/latest/latest.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@ func main() {
1818
panic(err)
1919
}
2020

21-
hash, err := r.Remotes[git.DefaultRemoteName].Head()
22-
if err != nil {
23-
panic(err)
24-
}
25-
26-
commit, err := r.Commit(hash)
21+
head := r.Remotes[git.DefaultRemoteName].Head()
22+
commit, err := r.Commit(head.Hash())
2723
if err != nil {
2824
panic(err)
2925
}

remote.go

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -77,45 +77,27 @@ func (r *Remote) Capabilities() *common.Capabilities {
7777
return r.upInfo.Capabilities
7878
}
7979

80-
// DefaultBranch returns the name of the remote's default branch
81-
func (r *Remote) DefaultBranch() string {
82-
return r.upInfo.Capabilities.SymbolicReference("HEAD")
83-
}
84-
85-
// Head returns the Hash of the HEAD
86-
func (r *Remote) Head() (core.Hash, error) {
87-
return r.Ref(r.DefaultBranch())
88-
}
89-
9080
// Fetch returns a reader using the request
9181
func (r *Remote) Fetch(req *common.GitUploadPackRequest) (io.ReadCloser, error) {
9282
return r.upSrv.Fetch(req)
9383
}
9484

95-
// FetchDefaultBranch returns a reader for the default branch
96-
func (r *Remote) FetchDefaultBranch() (io.ReadCloser, error) {
97-
ref, err := r.Ref(r.DefaultBranch())
98-
if err != nil {
99-
return nil, err
100-
}
101-
102-
req := &common.GitUploadPackRequest{}
103-
req.Want(ref)
104-
105-
return r.Fetch(req)
85+
// Head returns the Hash of the HEAD
86+
func (r *Remote) Head() *core.Reference {
87+
return r.upInfo.Head()
10688
}
10789

10890
// Ref returns the Hash pointing the given refName
109-
func (r *Remote) Ref(refName string) (core.Hash, error) {
110-
ref, ok := r.upInfo.Refs[refName]
91+
func (r *Remote) Ref(name core.ReferenceName) (*core.Reference, error) {
92+
ref, ok := r.upInfo.Refs[name]
11193
if !ok {
112-
return core.NewHash(""), fmt.Errorf("unable to find ref %q", refName)
94+
return nil, fmt.Errorf("unable to find ref %q", name)
11395
}
11496

11597
return ref, nil
11698
}
11799

118-
// Refs returns the Hash pointing the given refName
119-
func (r *Remote) Refs() map[string]core.Hash {
100+
// Refs returns a map with all the References
101+
func (r *Remote) Refs() map[core.ReferenceName]*core.Reference {
120102
return r.upInfo.Refs
121103
}

remote_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package git
22

33
import (
4+
"gopkg.in/src-d/go-git.v4/clients/common"
45
"gopkg.in/src-d/go-git.v4/clients/http"
56
"gopkg.in/src-d/go-git.v4/core"
67
"gopkg.in/src-d/go-git.v4/formats/packfile"
@@ -34,7 +35,7 @@ func (s *SuiteRemote) TestDefaultBranch(c *C) {
3435

3536
c.Assert(err, IsNil)
3637
c.Assert(r.Connect(), IsNil)
37-
c.Assert(r.DefaultBranch(), Equals, "refs/heads/master")
38+
c.Assert(r.Head().Name(), Equals, core.ReferenceName("refs/heads/master"))
3839
}
3940

4041
func (s *SuiteRemote) TestCapabilities(c *C) {
@@ -46,14 +47,17 @@ func (s *SuiteRemote) TestCapabilities(c *C) {
4647
c.Assert(r.Capabilities().Get("agent").Values, HasLen, 1)
4748
}
4849

49-
func (s *SuiteRemote) TestFetchDefaultBranch(c *C) {
50+
func (s *SuiteRemote) TestFetch(c *C) {
5051
r, err := NewRemote(RepositoryFixture)
5152
r.upSrv = &MockGitUploadPackService{}
5253

5354
c.Assert(err, IsNil)
5455
c.Assert(r.Connect(), IsNil)
5556

56-
reader, err := r.FetchDefaultBranch()
57+
req := &common.GitUploadPackRequest{}
58+
req.Want(r.Head().Hash())
59+
60+
reader, err := r.Fetch(req)
5761
c.Assert(err, IsNil)
5862

5963
packfileReader := packfile.NewStream(reader)
@@ -74,7 +78,6 @@ func (s *SuiteRemote) TestHead(c *C) {
7478
err = r.Connect()
7579
c.Assert(err, IsNil)
7680

77-
hash, err := r.Head()
7881
c.Assert(err, IsNil)
79-
c.Assert(hash, Equals, core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"))
82+
c.Assert(r.Head().Hash(), Equals, core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"))
8083
}

repository.go

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,50 @@ func NewPlainRepository() *Repository {
6969
}
7070
}
7171

72+
func (r *Repository) Clone(url string, auth common.AuthMethod) error {
73+
remote, err := r.createDefaultRemote(url, auth)
74+
if err != nil {
75+
return err
76+
}
77+
78+
if err = remote.Connect(); err != nil {
79+
return err
80+
}
81+
82+
return nil
83+
}
84+
85+
func (r *Repository) createDefaultRemote(url string, auth common.AuthMethod) (*Remote, error) {
86+
remote, err := NewAuthenticatedRemote(url, auth)
87+
if err != nil {
88+
return nil, err
89+
}
90+
91+
r.Remotes[DefaultRemoteName] = remote
92+
93+
return remote, nil
94+
}
95+
7296
// Pull connect and fetch the given branch from the given remote, the branch
7397
// should be provided with the full path not only the abbreviation, eg.:
7498
// "refs/heads/master"
75-
func (r *Repository) Pull(remoteName, branch string) (err error) {
99+
func (r *Repository) Pull(remoteName, branch string) error {
76100
remote, ok := r.Remotes[remoteName]
77101
if !ok {
78102
return fmt.Errorf("unable to find remote %q", remoteName)
79103
}
80104

81-
if err = remote.Connect(); err != nil {
105+
if err := remote.Connect(); err != nil {
82106
return err
83107
}
84108

85-
if branch == "" {
86-
branch = remote.DefaultBranch()
87-
}
88-
89-
ref, err := remote.Ref(branch)
90-
if err != nil {
91-
return err
109+
head := remote.Head()
110+
if head.Hash().IsZero() {
111+
return errors.New("HEAD is missing")
92112
}
93113

94114
req := &common.GitUploadPackRequest{}
95-
req.Want(ref)
115+
req.Want(head.Hash())
96116

97117
// TODO: Provide "haves" for what's already in the repository's storage
98118

@@ -104,9 +124,7 @@ func (r *Repository) Pull(remoteName, branch string) (err error) {
104124
stream := packfile.NewStream(reader)
105125

106126
d := packfile.NewDecoder(stream)
107-
err = d.Decode(r.Storage)
108-
109-
return err
127+
return d.Decode(r.Storage)
110128
}
111129

112130
// PullDefault like Pull but retrieve the default branch from the default remote
@@ -222,23 +240,6 @@ func (r *Repository) Object(h core.Hash) (Object, error) {
222240
// Head returns the hash of the HEAD of the repository or the head of a
223241
// remote, if one is passed.
224242
func (r *Repository) Head(remote string) (core.Hash, error) {
225-
if remote == "" {
226-
return r.localHead()
227-
}
228-
229-
return r.remoteHead(remote)
230-
}
231-
232-
func (r *Repository) remoteHead(remote string) (core.Hash, error) {
233-
rem, ok := r.Remotes[remote]
234-
if !ok {
235-
return core.ZeroHash, fmt.Errorf("unable to find remote %q", remote)
236-
}
237-
238-
return rem.Head()
239-
}
240-
241-
func (r *Repository) localHead() (core.Hash, error) {
242243
storage, ok := r.Storage.(*filesystem.ObjectStorage)
243244
if !ok {
244245
return core.ZeroHash,

0 commit comments

Comments
 (0)