diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-07-24 13:34:40 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2017-07-24 13:34:40 +0200 |
commit | 460a7278e6fff2d58300e1855ef8429fec374fa0 (patch) | |
tree | 1ad85aa138fb9e3d9e1eefc3b3de40f5f2e16265 /repository.go | |
parent | 4ee12ab91dc23ef1f6f04a6326ead904f21baf7b (diff) | |
download | go-git-460a7278e6fff2d58300e1855ef8429fec374fa0.tar.gz |
move Repository.Pull to Worktree.Pull
Diffstat (limited to 'repository.go')
-rw-r--r-- | repository.go | 171 |
1 files changed, 55 insertions, 116 deletions
diff --git a/repository.go b/repository.go index ec9f254..014b92d 100644 --- a/repository.go +++ b/repository.go @@ -26,7 +26,7 @@ var ( ErrRepositoryNotExists = errors.New("repository not exists") ErrRepositoryAlreadyExists = errors.New("repository already exists") ErrRemoteNotFound = errors.New("remote not found") - ErrRemoteExists = errors.New("remote already exists") + ErrRemoteExists = errors.New("remote already exists ") ErrWorktreeNotProvided = errors.New("worktree should be provided") ErrIsBareRepository = errors.New("worktree not available in a bare repository") ) @@ -382,58 +382,38 @@ func (r *Repository) clone(o *CloneOptions) error { URL: o.URL, } - remote, err := r.CreateRemote(c) - if err != nil { + if _, err := r.CreateRemote(c); err != nil { return err } - remoteRefs, err := remote.fetch(&FetchOptions{ + head, err := r.fetchAndUpdateReferences(&FetchOptions{ RefSpecs: r.cloneRefSpec(o, c), Depth: o.Depth, Auth: o.Auth, Progress: o.Progress, - }) + }, o.ReferenceName) if err != nil { return err } - head, err := storer.ResolveReference(remoteRefs, o.ReferenceName) - if err != nil { - return err - } - - if _, err := r.updateReferences(c.Fetch, head); err != nil { - return err - } - - if err := r.updateWorktree(head.Name()); err != nil { - return err - } - - if o.RecurseSubmodules != NoRecurseSubmodules && r.wt != nil { - if err := r.updateSubmodules(o.RecurseSubmodules); err != nil { + if r.wt != nil { + w, err := r.Worktree() + if err != nil { return err } - } - - return r.updateRemoteConfig(remote, o, c, head) -} -func (r *Repository) updateSubmodules(recursion SubmoduleRescursivity) error { - w, err := r.Worktree() - if err != nil { - return err - } + if err := w.Reset(&ResetOptions{Commit: head.Hash()}); err != nil { + return err + } - s, err := w.Submodules() - if err != nil { - return err + if o.RecurseSubmodules != NoRecurseSubmodules { + if err := w.updateSubmodules(o.RecurseSubmodules); err != nil { + return err + } + } } - return s.Update(&SubmoduleUpdateOptions{ - Init: true, - RecurseSubmodules: recursion, - }) + return r.updateRemoteConfigIfNeeded(o, c, head) } func (r *Repository) cloneRefSpec(o *CloneOptions, @@ -470,9 +450,7 @@ const ( refspecSingleBranchHEAD = "+HEAD:refs/remotes/%s/HEAD" ) -func (r *Repository) updateRemoteConfig(remote *Remote, o *CloneOptions, - c *config.RemoteConfig, head *plumbing.Reference) error { - +func (r *Repository) updateRemoteConfigIfNeeded(o *CloneOptions, c *config.RemoteConfig, head *plumbing.Reference) error { if !o.SingleBranch { return nil } @@ -490,6 +468,44 @@ func (r *Repository) updateRemoteConfig(remote *Remote, o *CloneOptions, return r.Storer.SetConfig(cfg) } +func (r *Repository) fetchAndUpdateReferences( + o *FetchOptions, ref plumbing.ReferenceName, +) (*plumbing.Reference, error) { + + if err := o.Validate(); err != nil { + return nil, err + } + + remote, err := r.Remote(o.RemoteName) + if err != nil { + return nil, err + } + + objsUpdated := true + remoteRefs, err := remote.fetch(o) + if err == NoErrAlreadyUpToDate { + objsUpdated = false + } else if err != nil { + return nil, err + } + + head, err := storer.ResolveReference(remoteRefs, ref) + if err != nil { + return nil, err + } + + refsUpdated, err := r.updateReferences(remote.c.Fetch, head) + if err != nil { + return nil, err + } + + if !objsUpdated && !refsUpdated { + return nil, NoErrAlreadyUpToDate + } + + return head, nil +} + func (r *Repository) updateReferences(spec []config.RefSpec, resolvedHead *plumbing.Reference) (updated bool, err error) { @@ -565,83 +581,6 @@ func updateReferenceStorerIfNeeded( return false, nil } -// Pull incorporates changes from a remote repository into the current branch. -// Returns nil if the operation is successful, NoErrAlreadyUpToDate if there are -// no changes to be fetched, or an error. -func (r *Repository) Pull(o *PullOptions) error { - if err := o.Validate(); err != nil { - return err - } - - remote, err := r.Remote(o.RemoteName) - if err != nil { - return err - } - - remoteRefs, err := remote.fetch(&FetchOptions{ - Depth: o.Depth, - Auth: o.Auth, - Progress: o.Progress, - }) - - updated := true - if err == NoErrAlreadyUpToDate { - updated = false - } else if err != nil { - return err - } - - head, err := storer.ResolveReference(remoteRefs, o.ReferenceName) - if err != nil { - return err - } - - refsUpdated, err := r.updateReferences(remote.c.Fetch, head) - if err != nil { - return err - } - - if refsUpdated { - updated = refsUpdated - } - - if !updated { - return NoErrAlreadyUpToDate - } - - if err := r.updateWorktree(head.Name()); err != nil { - return err - } - - if o.RecurseSubmodules != NoRecurseSubmodules && r.wt != nil { - if err := r.updateSubmodules(o.RecurseSubmodules); err != nil { - return err - } - } - - return nil -} - -func (r *Repository) updateWorktree(branch plumbing.ReferenceName) error { - if r.wt == nil { - return nil - } - - b, err := r.Reference(branch, true) - if err != nil { - return err - } - - w, err := r.Worktree() - if err != nil { - return err - } - - return w.Reset(&ResetOptions{ - Commit: b.Hash(), - }) -} - // Fetch fetches changes from a remote repository. // Returns nil if the operation is successful, NoErrAlreadyUpToDate if there are // no changes to be fetched, or an error. |