diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-22 00:18:02 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-22 00:18:02 +0200 |
commit | f42d82364c5d159f65a48e720433ad2bc97f0b7f (patch) | |
tree | 82623f4163625786f4e8e41028dcd1c7abaf209f /repository.go | |
parent | a045606fc9c5cbf30b409384cbdad4804f01c61d (diff) | |
download | go-git-f42d82364c5d159f65a48e720433ad2bc97f0b7f.tar.gz |
Remote.Fetch multiple RefSpec support
Diffstat (limited to 'repository.go')
-rw-r--r-- | repository.go | 52 |
1 files changed, 40 insertions, 12 deletions
diff --git a/repository.go b/repository.go index da4aada..64107f1 100644 --- a/repository.go +++ b/repository.go @@ -2,6 +2,7 @@ package git import ( "errors" + "fmt" "gopkg.in/src-d/go-git.v4/config" "gopkg.in/src-d/go-git.v4/core" @@ -22,8 +23,9 @@ type Repository struct { } // NewMemoryRepository creates a new repository, backed by a memory.Storage -func NewMemoryRepository() (*Repository, error) { - return NewRepository(memory.NewStorage()) +func NewMemoryRepository() *Repository { + r, _ := NewRepository(memory.NewStorage()) + return r } // NewFilesystemRepository creates a new repository, backed by a filesystem.Storage @@ -73,6 +75,10 @@ func (r *Repository) Remotes() ([]*Remote, error) { // CreateRemote creates a new remote func (r *Repository) CreateRemote(c *config.RemoteConfig) (*Remote, error) { + if err := c.Validate(); err != nil { + return nil, err + } + remote := newRemote(r.s, c) if err := r.s.ConfigStorage().SetRemote(c); err != nil { return nil, err @@ -92,11 +98,12 @@ func (r *Repository) Clone(o *RepositoryCloneOptions) error { return err } - remote, err := r.CreateRemote(&config.RemoteConfig{ + c := &config.RemoteConfig{ Name: o.RemoteName, URL: o.URL, - }) + } + remote, err := r.CreateRemote(c) if err != nil { return err } @@ -107,17 +114,11 @@ func (r *Repository) Clone(o *RepositoryCloneOptions) error { defer remote.Disconnect() - spec, err := o.refSpec(remote.Info().Refs) - if err != nil { + if err := r.updateRemoteConfig(remote, o, c); err != nil { return err } - err = remote.Fetch(&RemoteFetchOptions{ - RefSpec: spec, - Depth: o.Depth, - }) - - if err != nil { + if err = remote.Fetch(&RemoteFetchOptions{Depth: o.Depth}); err != nil { return err } @@ -129,6 +130,27 @@ func (r *Repository) Clone(o *RepositoryCloneOptions) error { return r.createReferences(head) } +const refspecSingleBranch = "+refs/heads/%s:refs/remotes/%s/%[1]s" + +func (r *Repository) updateRemoteConfig( + remote *Remote, o *RepositoryCloneOptions, c *config.RemoteConfig, +) error { + if o.SingleBranch { + head, err := core.ResolveReference(remote.Info().Refs, o.ReferenceName) + if err != nil { + return err + } + + c.Fetch = []config.RefSpec{ + config.RefSpec(fmt.Sprintf(refspecSingleBranch, head.Name().Short(), c.Name)), + } + + return r.s.ConfigStorage().SetRemote(c) + } + + return nil +} + func (r *Repository) createReferences(ref *core.Reference) error { if !ref.IsBranch() { // detached HEAD mode @@ -155,6 +177,12 @@ func (r *Repository) Pull(o *RepositoryPullOptions) error { return err } + if err = remote.Connect(); err != nil { + return err + } + + defer remote.Disconnect() + head, err := remote.Ref(o.ReferenceName, true) if err != nil { return err |