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

Commit

Permalink
Merge pull request #1142 from EmrysMyrddin/feature/export-new-remote
Browse files Browse the repository at this point in the history
git : allows to create a Remote without a Repository
  • Loading branch information
mcuadros authored Jun 18, 2019
2 parents a35ce6e + b4fba7e commit f9a3019
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 37 deletions.
42 changes: 42 additions & 0 deletions _examples/ls-remote/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"log"

"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/storage/memory"
)

// Retrieve remote tags without cloning repository
func main() {

// Create the remote with repository URL
rem := git.NewRemote(memory.NewStorage(), &config.RemoteConfig{
Name: "origin",
URLs: []string{"https://github.com/Zenika/MARCEL"},
})

log.Print("Fetching tags...")

// We can then use every Remote functions to retrieve wanted informations
refs, err := rem.List(&git.ListOptions{})
if err != nil {
log.Fatal(err)
}

// Filters the references list and only keeps tags
var tags []string
for _, ref := range refs {
if ref.Name().IsTag() {
tags = append(tags, ref.Name().Short())
}
}

if len(tags) == 0 {
log.Println("No tags!")
return
}

log.Printf("Tags found: %v", tags)
}
5 changes: 4 additions & 1 deletion remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ type Remote struct {
s storage.Storer
}

func newRemote(s storage.Storer, c *config.RemoteConfig) *Remote {
// NewRemote creates a new Remote.
// The intended purpose is to use the Remote for tasks such as listing remote references (like using git ls-remote).
// Otherwise Remotes should be created via the use of a Repository.
func NewRemote(s storage.Storer, c *config.RemoteConfig) *Remote {
return &Remote{s: s, c: c}
}

Expand Down
64 changes: 32 additions & 32 deletions remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,32 @@ type RemoteSuite struct {
var _ = Suite(&RemoteSuite{})

func (s *RemoteSuite) TestFetchInvalidEndpoint(c *C) {
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URLs: []string{"http://\\"}})
r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URLs: []string{"http://\\"}})
err := r.Fetch(&FetchOptions{RemoteName: "foo"})
c.Assert(err, ErrorMatches, ".*invalid character.*")
}

func (s *RemoteSuite) TestFetchNonExistentEndpoint(c *C) {
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URLs: []string{"ssh://non-existent/foo.git"}})
r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URLs: []string{"ssh://non-existent/foo.git"}})
err := r.Fetch(&FetchOptions{})
c.Assert(err, NotNil)
}

func (s *RemoteSuite) TestFetchInvalidSchemaEndpoint(c *C) {
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URLs: []string{"qux://foo"}})
r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URLs: []string{"qux://foo"}})
err := r.Fetch(&FetchOptions{})
c.Assert(err, ErrorMatches, ".*unsupported scheme.*")
}

func (s *RemoteSuite) TestFetchInvalidFetchOptions(c *C) {
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URLs: []string{"qux://foo"}})
r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URLs: []string{"qux://foo"}})
invalid := config.RefSpec("^*$ñ")
err := r.Fetch(&FetchOptions{RefSpecs: []config.RefSpec{invalid}})
c.Assert(err, Equals, config.ErrRefSpecMalformedSeparator)
}

func (s *RemoteSuite) TestFetchWildcard(c *C) {
r := newRemote(memory.NewStorage(), &config.RemoteConfig{
r := NewRemote(memory.NewStorage(), &config.RemoteConfig{
URLs: []string{s.GetBasicLocalRepositoryURL()},
})

Expand All @@ -72,7 +72,7 @@ func (s *RemoteSuite) TestFetchWildcard(c *C) {
}

func (s *RemoteSuite) TestFetchWildcardTags(c *C) {
r := newRemote(memory.NewStorage(), &config.RemoteConfig{
r := NewRemote(memory.NewStorage(), &config.RemoteConfig{
URLs: []string{s.GetLocalRepositoryURL(fixtures.ByTag("tags").One())},
})

Expand All @@ -91,7 +91,7 @@ func (s *RemoteSuite) TestFetchWildcardTags(c *C) {
}

func (s *RemoteSuite) TestFetch(c *C) {
r := newRemote(memory.NewStorage(), &config.RemoteConfig{
r := NewRemote(memory.NewStorage(), &config.RemoteConfig{
URLs: []string{s.GetLocalRepositoryURL(fixtures.ByTag("tags").One())},
})

Expand All @@ -105,7 +105,7 @@ func (s *RemoteSuite) TestFetch(c *C) {
}

func (s *RemoteSuite) TestFetchNonExistantReference(c *C) {
r := newRemote(memory.NewStorage(), &config.RemoteConfig{
r := NewRemote(memory.NewStorage(), &config.RemoteConfig{
URLs: []string{s.GetLocalRepositoryURL(fixtures.ByTag("tags").One())},
})

Expand All @@ -119,7 +119,7 @@ func (s *RemoteSuite) TestFetchNonExistantReference(c *C) {
}

func (s *RemoteSuite) TestFetchContext(c *C) {
r := newRemote(memory.NewStorage(), &config.RemoteConfig{
r := NewRemote(memory.NewStorage(), &config.RemoteConfig{
URLs: []string{s.GetLocalRepositoryURL(fixtures.ByTag("tags").One())},
})

Expand All @@ -135,7 +135,7 @@ func (s *RemoteSuite) TestFetchContext(c *C) {
}

func (s *RemoteSuite) TestFetchWithAllTags(c *C) {
r := newRemote(memory.NewStorage(), &config.RemoteConfig{
r := NewRemote(memory.NewStorage(), &config.RemoteConfig{
URLs: []string{s.GetLocalRepositoryURL(fixtures.ByTag("tags").One())},
})

Expand All @@ -155,7 +155,7 @@ func (s *RemoteSuite) TestFetchWithAllTags(c *C) {
}

func (s *RemoteSuite) TestFetchWithNoTags(c *C) {
r := newRemote(memory.NewStorage(), &config.RemoteConfig{
r := NewRemote(memory.NewStorage(), &config.RemoteConfig{
URLs: []string{s.GetLocalRepositoryURL(fixtures.ByTag("tags").One())},
})

Expand All @@ -171,7 +171,7 @@ func (s *RemoteSuite) TestFetchWithNoTags(c *C) {
}

func (s *RemoteSuite) TestFetchWithDepth(c *C) {
r := newRemote(memory.NewStorage(), &config.RemoteConfig{
r := NewRemote(memory.NewStorage(), &config.RemoteConfig{
URLs: []string{s.GetBasicLocalRepositoryURL()},
})

Expand Down Expand Up @@ -212,7 +212,7 @@ func (s *RemoteSuite) TestFetchWithProgress(c *C) {
sto := memory.NewStorage()
buf := bytes.NewBuffer(nil)

r := newRemote(sto, &config.RemoteConfig{Name: "foo", URLs: []string{url}})
r := NewRemote(sto, &config.RemoteConfig{Name: "foo", URLs: []string{url}})

refspec := config.RefSpec("+refs/heads/*:refs/remotes/origin/*")
err := r.Fetch(&FetchOptions{
Expand Down Expand Up @@ -248,7 +248,7 @@ func (s *RemoteSuite) TestFetchWithPackfileWriter(c *C) {
mock := &mockPackfileWriter{Storer: fss}

url := s.GetBasicLocalRepositoryURL()
r := newRemote(mock, &config.RemoteConfig{Name: "foo", URLs: []string{url}})
r := NewRemote(mock, &config.RemoteConfig{Name: "foo", URLs: []string{url}})

refspec := config.RefSpec("+refs/heads/*:refs/remotes/origin/*")
err = r.Fetch(&FetchOptions{
Expand Down Expand Up @@ -276,7 +276,7 @@ func (s *RemoteSuite) TestFetchNoErrAlreadyUpToDate(c *C) {
}

func (s *RemoteSuite) TestFetchNoErrAlreadyUpToDateButStillUpdateLocalRemoteRefs(c *C) {
r := newRemote(memory.NewStorage(), &config.RemoteConfig{
r := NewRemote(memory.NewStorage(), &config.RemoteConfig{
URLs: []string{s.GetBasicLocalRepositoryURL()},
})

Expand Down Expand Up @@ -313,7 +313,7 @@ func (s *RemoteSuite) TestFetchNoErrAlreadyUpToDateWithNonCommitObjects(c *C) {
}

func (s *RemoteSuite) doTestFetchNoErrAlreadyUpToDate(c *C, url string) {
r := newRemote(memory.NewStorage(), &config.RemoteConfig{URLs: []string{url}})
r := NewRemote(memory.NewStorage(), &config.RemoteConfig{URLs: []string{url}})

o := &FetchOptions{
RefSpecs: []config.RefSpec{
Expand All @@ -328,7 +328,7 @@ func (s *RemoteSuite) doTestFetchNoErrAlreadyUpToDate(c *C, url string) {
}

func (s *RemoteSuite) testFetchFastForward(c *C, sto storage.Storer) {
r := newRemote(sto, &config.RemoteConfig{
r := NewRemote(sto, &config.RemoteConfig{
URLs: []string{s.GetBasicLocalRepositoryURL()},
})

Expand Down Expand Up @@ -386,7 +386,7 @@ func (s *RemoteSuite) TestFetchFastForwardFS(c *C) {
}

func (s *RemoteSuite) TestString(c *C) {
r := newRemote(nil, &config.RemoteConfig{
r := NewRemote(nil, &config.RemoteConfig{
Name: "foo",
URLs: []string{"https://github.com/git-fixtures/basic.git"},
})
Expand All @@ -405,7 +405,7 @@ func (s *RemoteSuite) TestPushToEmptyRepository(c *C) {
srcFs := fixtures.Basic().One().DotGit()
sto := filesystem.NewStorage(srcFs, cache.NewObjectLRUDefault())

r := newRemote(sto, &config.RemoteConfig{
r := NewRemote(sto, &config.RemoteConfig{
Name: DefaultRemoteName,
URLs: []string{url},
})
Expand Down Expand Up @@ -442,7 +442,7 @@ func (s *RemoteSuite) TestPushContext(c *C) {
fs := fixtures.ByURL("https://github.com/git-fixtures/tags.git").One().DotGit()
sto := filesystem.NewStorage(fs, cache.NewObjectLRUDefault())

r := newRemote(sto, &config.RemoteConfig{
r := NewRemote(sto, &config.RemoteConfig{
Name: DefaultRemoteName,
URLs: []string{url},
})
Expand Down Expand Up @@ -471,7 +471,7 @@ func (s *RemoteSuite) TestPushTags(c *C) {
fs := fixtures.ByURL("https://github.com/git-fixtures/tags.git").One().DotGit()
sto := filesystem.NewStorage(fs, cache.NewObjectLRUDefault())

r := newRemote(sto, &config.RemoteConfig{
r := NewRemote(sto, &config.RemoteConfig{
Name: DefaultRemoteName,
URLs: []string{url},
})
Expand All @@ -494,7 +494,7 @@ func (s *RemoteSuite) TestPushNoErrAlreadyUpToDate(c *C) {
fs := fixtures.Basic().One().DotGit()
sto := filesystem.NewStorage(fs, cache.NewObjectLRUDefault())

r := newRemote(sto, &config.RemoteConfig{
r := NewRemote(sto, &config.RemoteConfig{
Name: DefaultRemoteName,
URLs: []string{fs.Root()},
})
Expand Down Expand Up @@ -564,7 +564,7 @@ func (s *RemoteSuite) TestPushForce(c *C) {
dstSto := filesystem.NewStorage(dstFs, cache.NewObjectLRUDefault())

url := dstFs.Root()
r := newRemote(sto, &config.RemoteConfig{
r := NewRemote(sto, &config.RemoteConfig{
Name: DefaultRemoteName,
URLs: []string{url},
})
Expand Down Expand Up @@ -654,32 +654,32 @@ func (s *RemoteSuite) TestPushNewReferenceAndDeleteInBatch(c *C) {
}

func (s *RemoteSuite) TestPushInvalidEndpoint(c *C) {
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URLs: []string{"http://\\"}})
r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URLs: []string{"http://\\"}})
err := r.Push(&PushOptions{RemoteName: "foo"})
c.Assert(err, ErrorMatches, ".*invalid character.*")
}

func (s *RemoteSuite) TestPushNonExistentEndpoint(c *C) {
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URLs: []string{"ssh://non-existent/foo.git"}})
r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URLs: []string{"ssh://non-existent/foo.git"}})
err := r.Push(&PushOptions{})
c.Assert(err, NotNil)
}

func (s *RemoteSuite) TestPushInvalidSchemaEndpoint(c *C) {
r := newRemote(nil, &config.RemoteConfig{Name: "origin", URLs: []string{"qux://foo"}})
r := NewRemote(nil, &config.RemoteConfig{Name: "origin", URLs: []string{"qux://foo"}})
err := r.Push(&PushOptions{})
c.Assert(err, ErrorMatches, ".*unsupported scheme.*")
}

func (s *RemoteSuite) TestPushInvalidFetchOptions(c *C) {
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URLs: []string{"qux://foo"}})
r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URLs: []string{"qux://foo"}})
invalid := config.RefSpec("^*$ñ")
err := r.Push(&PushOptions{RefSpecs: []config.RefSpec{invalid}})
c.Assert(err, Equals, config.ErrRefSpecMalformedSeparator)
}

func (s *RemoteSuite) TestPushInvalidRefSpec(c *C) {
r := newRemote(nil, &config.RemoteConfig{
r := NewRemote(nil, &config.RemoteConfig{
Name: DefaultRemoteName,
URLs: []string{"some-url"},
})
Expand All @@ -692,7 +692,7 @@ func (s *RemoteSuite) TestPushInvalidRefSpec(c *C) {
}

func (s *RemoteSuite) TestPushWrongRemoteName(c *C) {
r := newRemote(nil, &config.RemoteConfig{
r := NewRemote(nil, &config.RemoteConfig{
Name: DefaultRemoteName,
URLs: []string{"some-url"},
})
Expand Down Expand Up @@ -729,7 +729,7 @@ func (s *RemoteSuite) TestGetHaves(c *C) {

func (s *RemoteSuite) TestList(c *C) {
repo := fixtures.Basic().One()
remote := newRemote(memory.NewStorage(), &config.RemoteConfig{
remote := NewRemote(memory.NewStorage(), &config.RemoteConfig{
Name: DefaultRemoteName,
URLs: []string{repo.URL},
})
Expand Down Expand Up @@ -784,7 +784,7 @@ func (s *RemoteSuite) TestUpdateShallows(c *C) {
{nil, hashes[0:6]},
}

remote := newRemote(memory.NewStorage(), &config.RemoteConfig{
remote := NewRemote(memory.NewStorage(), &config.RemoteConfig{
Name: DefaultRemoteName,
})

Expand Down Expand Up @@ -817,7 +817,7 @@ func (s *RemoteSuite) TestUseRefDeltas(c *C) {
fs := fixtures.ByURL("https://github.com/git-fixtures/tags.git").One().DotGit()
sto := filesystem.NewStorage(fs, cache.NewObjectLRUDefault())

r := newRemote(sto, &config.RemoteConfig{
r := NewRemote(sto, &config.RemoteConfig{
Name: DefaultRemoteName,
URLs: []string{url},
})
Expand Down
8 changes: 4 additions & 4 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ func (r *Repository) Remote(name string) (*Remote, error) {
return nil, ErrRemoteNotFound
}

return newRemote(r.Storer, c), nil
return NewRemote(r.Storer, c), nil
}

// Remotes returns a list with all the remotes
Expand All @@ -465,7 +465,7 @@ func (r *Repository) Remotes() ([]*Remote, error) {

var i int
for _, c := range cfg.Remotes {
remotes[i] = newRemote(r.Storer, c)
remotes[i] = NewRemote(r.Storer, c)
i++
}

Expand All @@ -478,7 +478,7 @@ func (r *Repository) CreateRemote(c *config.RemoteConfig) (*Remote, error) {
return nil, err
}

remote := newRemote(r.Storer, c)
remote := NewRemote(r.Storer, c)

cfg, err := r.Storer.Config()
if err != nil {
Expand All @@ -504,7 +504,7 @@ func (r *Repository) CreateRemoteAnonymous(c *config.RemoteConfig) (*Remote, err
return nil, ErrAnonymousRemoteName
}

remote := newRemote(r.Storer, c)
remote := NewRemote(r.Storer, c)

return remote, nil
}
Expand Down

0 comments on commit f9a3019

Please sign in to comment.