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

remote: add support for ls-remote #609

Merged
merged 3 commits into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,9 @@ func (o *CommitOptions) Validate(r *Repository) error {

return nil
}

// ListOptions describes how a remote list should be performed.
type ListOptions struct {
// Auth credentials, if required, to use with the remote repository.
Auth transport.AuthMethod
}
33 changes: 33 additions & 0 deletions remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,39 @@ func (r *Remote) buildFetchedTags(refs memory.ReferenceStorage) (updated bool, e
return
}

// List the references on the remote repository.
func (r *Remote) List(o *ListOptions) ([]*plumbing.Reference, error) {
s, err := newUploadPackSession(r.c.URLs[0], o.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 {
Expand Down
37 changes: 37 additions & 0 deletions remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,3 +642,40 @@ func (s *RemoteSuite) TestGetHaves(c *C) {
c.Assert(err, IsNil)
c.Assert(l, HasLen, 2)
}

func (s *RemoteSuite) TestList(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{
Copy link
Contributor

@orirawlings orirawlings Oct 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Push and List will basically use the same code under the hood, we are sort of asserting this code's behavior against itself.

Maybe it makes for sense to explicitly define the expected references for this fixture repository? Also this gives us a chance to assert that symbolic references are handled properly rather than ignoring them.

func (s *RemoteSuite) TestList(c *C) {
        repo := fixtures.Basic().One()
        remote := newRemote(memory.NewStorage(), &config.RemoteConfig{
                Name: DefaultRemoteName,
                URLs: []string{repo.URL},
        })

        refs, err := remote.List(&ListOptions{})
        c.Assert(err, IsNil)

        expected := []*plumbing.Reference{
                plumbing.NewSymbolicReference("HEAD", "refs/heads/master"),
                plumbing.NewReferenceFromStrings("refs/heads/master", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5"),
                plumbing.NewReferenceFromStrings("refs/heads/branch", "e8d3ffab552895c19b9fcf7aa264d277cde33881"),
                plumbing.NewReferenceFromStrings("refs/pull/1/head", "b8e471f58bcbca63b07bda20e428190409c2db47"),
                plumbing.NewReferenceFromStrings("refs/pull/2/head", "9632f02833b2f9613afb5e75682132b0b22e4a31"),
                plumbing.NewReferenceFromStrings("refs/pull/2/merge", "c37f58a130ca555e42ff96a071cb9ccb3f437504"),
        }
        c.Assert(len(refs), Equals, len(expected))
        for _, e := range expected {
                found := false
                for _, r := range refs {
                        if r.Name() == e.Name() {
                                found = true
                                c.Assert(r, DeepEquals, e)
                        }
                }
                c.Assert(found, Equals, true)
        }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Makes sense. 🙂

RefSpecs: []config.RefSpec{rs},
})
c.Assert(err, IsNil)

listOptions := ListOptions{}
refs, err := remote.List(&listOptions)
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)
}