diff options
author | Sunny <me@darkowlzz.space> | 2017-10-02 14:42:08 +0530 |
---|---|---|
committer | Sunny <me@darkowlzz.space> | 2017-10-02 14:42:08 +0530 |
commit | 7172e04fe7e000d8826f0402dd7f70d8b3d55072 (patch) | |
tree | 0af4b7ed70ae40812f855d9c556d0171579761e6 | |
parent | f9879dd043f84936a1f8acb8a53b74332a7ae135 (diff) | |
download | go-git-7172e04fe7e000d8826f0402dd7f70d8b3d55072.tar.gz |
remote: add support for ls-remote
-rw-r--r-- | remote.go | 33 | ||||
-rw-r--r-- | remote_test.go | 39 |
2 files changed, 72 insertions, 0 deletions
@@ -728,6 +728,39 @@ func (r *Remote) buildFetchedTags(refs memory.ReferenceStorage) (updated bool, e return } +// LSRemote performs ls-remote on the remote. +func (r *Remote) LSRemote(auth transport.AuthMethod) ([]*plumbing.Reference, error) { + s, err := newUploadPackSession(r.c.URLs[0], auth) + if err != nil { + return nil, err + } + + defer ioutil.CheckClose(s, &err) + + ar, err := s.AdvertisedReferences() + if err != nil { + return nil, err + } + + allRefs, err := ar.AllReferences() + if err != nil { + return nil, err + } + + refs, err := allRefs.IterReferences() + if err != nil { + return nil, err + } + + var resultRefs []*plumbing.Reference + refs.ForEach(func(ref *plumbing.Reference) error { + resultRefs = append(resultRefs, ref) + return nil + }) + + return resultRefs, nil +} + func objectsToPush(commands []*packp.Command) ([]plumbing.Hash, error) { var objects []plumbing.Hash for _, cmd := range commands { diff --git a/remote_test.go b/remote_test.go index 10f6708..4bb2037 100644 --- a/remote_test.go +++ b/remote_test.go @@ -11,6 +11,7 @@ import ( "gopkg.in/src-d/go-git.v4/config" "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/storer" + "gopkg.in/src-d/go-git.v4/plumbing/transport" "gopkg.in/src-d/go-git.v4/storage" "gopkg.in/src-d/go-git.v4/storage/filesystem" "gopkg.in/src-d/go-git.v4/storage/memory" @@ -642,3 +643,41 @@ func (s *RemoteSuite) TestGetHaves(c *C) { c.Assert(err, IsNil) c.Assert(l, HasLen, 2) } + +func (s *RemoteSuite) TestLSRemote(c *C) { + url := c.MkDir() + server, err := PlainInit(url, true) + c.Assert(err, IsNil) + + srcFs := fixtures.Basic().One().DotGit() + sto, err := filesystem.NewStorage(srcFs) + c.Assert(err, IsNil) + + remote := newRemote(sto, &config.RemoteConfig{ + Name: DefaultRemoteName, + URLs: []string{url}, + }) + + rs := config.RefSpec("refs/heads/*:refs/heads/*") + err = remote.Push(&PushOptions{ + RefSpecs: []config.RefSpec{rs}, + }) + c.Assert(err, IsNil) + + // Perform ls-remote. + var authMethod transport.AuthMethod + refs, err := remote.LSRemote(authMethod) + c.Assert(err, IsNil) + + // Create a map of remote name and their hash. + refsMap := map[string]string{} + for _, rf := range refs { + // Skip the symbolically linked HEAD. + if string(rf.Name()) == "HEAD" { + continue + } + refsMap[string(rf.Name())] = rf.Hash().String() + } + + AssertReferences(c, server, refsMap) +} |