aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-07-24 13:34:40 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2017-07-24 13:34:40 +0200
commit460a7278e6fff2d58300e1855ef8429fec374fa0 (patch)
tree1ad85aa138fb9e3d9e1eefc3b3de40f5f2e16265
parent4ee12ab91dc23ef1f6f04a6326ead904f21baf7b (diff)
downloadgo-git-460a7278e6fff2d58300e1855ef8429fec374fa0.tar.gz
move Repository.Pull to Worktree.Pull
-rw-r--r--_examples/remotes/main.go6
-rw-r--r--repository.go171
-rw-r--r--repository_test.go165
-rw-r--r--worktree.go41
-rw-r--r--worktree_test.go159
5 files changed, 271 insertions, 271 deletions
diff --git a/_examples/remotes/main.go b/_examples/remotes/main.go
index 31c5cc5..90817dc 100644
--- a/_examples/remotes/main.go
+++ b/_examples/remotes/main.go
@@ -42,9 +42,9 @@ func main() {
fmt.Println(r)
}
- // Pull using the create repository
- Info("git pull example")
- err = r.Pull(&git.PullOptions{
+ // Fetch using the new remote
+ Info("git fetch example")
+ err = r.Fetch(&git.FetchOptions{
RemoteName: "example",
})
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.
diff --git a/repository_test.go b/repository_test.go
index d95b65e..3cc8d48 100644
--- a/repository_test.go
+++ b/repository_test.go
@@ -426,6 +426,19 @@ func (s *RepositorySuite) TestFetch(c *C) {
c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
}
+func (s *RepositorySuite) TestCloneWithProgress(c *C) {
+ fs := memfs.New()
+
+ buf := bytes.NewBuffer(nil)
+ _, err := Clone(memory.NewStorage(), fs, &CloneOptions{
+ URL: s.GetBasicLocalRepositoryURL(),
+ Progress: buf,
+ })
+
+ c.Assert(err, IsNil)
+ c.Assert(buf.Len(), Not(Equals), 0)
+}
+
func (s *RepositorySuite) TestCloneDeep(c *C) {
fs := memfs.New()
r, _ := Init(memory.NewStorage(), fs)
@@ -575,158 +588,6 @@ func (s *RepositorySuite) TestCloneDetachedHEAD(c *C) {
c.Assert(head.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
}
-func (s *RepositorySuite) TestPullCheckout(c *C) {
- fs := memfs.New()
- r, _ := Init(memory.NewStorage(), fs)
- r.CreateRemote(&config.RemoteConfig{
- Name: DefaultRemoteName,
- URL: s.GetBasicLocalRepositoryURL(),
- })
-
- err := r.Pull(&PullOptions{})
- c.Assert(err, IsNil)
-
- fi, err := fs.ReadDir("")
- c.Assert(err, IsNil)
- c.Assert(fi, HasLen, 8)
-}
-
-func (s *RepositorySuite) TestCloneWithProgress(c *C) {
- fs := memfs.New()
-
- buf := bytes.NewBuffer(nil)
- _, err := Clone(memory.NewStorage(), fs, &CloneOptions{
- URL: s.GetBasicLocalRepositoryURL(),
- Progress: buf,
- })
-
- c.Assert(err, IsNil)
- c.Assert(buf.Len(), Not(Equals), 0)
-}
-
-func (s *RepositorySuite) TestPullUpdateReferencesIfNeeded(c *C) {
- r, _ := Init(memory.NewStorage(), nil)
- r.CreateRemote(&config.RemoteConfig{
- Name: DefaultRemoteName,
- URL: s.GetBasicLocalRepositoryURL(),
- })
-
- err := r.Fetch(&FetchOptions{})
- c.Assert(err, IsNil)
-
- _, err = r.Reference("refs/heads/master", false)
- c.Assert(err, NotNil)
-
- err = r.Pull(&PullOptions{})
- c.Assert(err, IsNil)
-
- head, err := r.Reference(plumbing.HEAD, true)
- c.Assert(err, IsNil)
- c.Assert(head.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
-
- branch, err := r.Reference("refs/heads/master", false)
- c.Assert(err, IsNil)
- c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
-
- err = r.Pull(&PullOptions{})
- c.Assert(err, Equals, NoErrAlreadyUpToDate)
-}
-
-func (s *RepositorySuite) TestPullSingleBranch(c *C) {
- r, _ := Init(memory.NewStorage(), nil)
- err := r.clone(&CloneOptions{
- URL: s.GetBasicLocalRepositoryURL(),
- SingleBranch: true,
- })
-
- c.Assert(err, IsNil)
-
- err = r.Pull(&PullOptions{})
- c.Assert(err, Equals, NoErrAlreadyUpToDate)
-
- branch, err := r.Reference("refs/heads/master", false)
- c.Assert(err, IsNil)
- c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
-
- branch, err = r.Reference("refs/remotes/foo/branch", false)
- c.Assert(err, NotNil)
-
- storage := r.Storer.(*memory.Storage)
- c.Assert(storage.Objects, HasLen, 28)
-}
-
-func (s *RepositorySuite) TestPullProgress(c *C) {
- r, _ := Init(memory.NewStorage(), nil)
-
- r.CreateRemote(&config.RemoteConfig{
- Name: DefaultRemoteName,
- URL: s.GetBasicLocalRepositoryURL(),
- })
-
- buf := bytes.NewBuffer(nil)
- err := r.Pull(&PullOptions{
- Progress: buf,
- })
-
- c.Assert(err, IsNil)
- c.Assert(buf.Len(), Not(Equals), 0)
-}
-
-func (s *RepositorySuite) TestPullProgressWithRecursion(c *C) {
- path := fixtures.ByTag("submodule").One().Worktree().Root()
-
- dir, err := ioutil.TempDir("", "plain-clone-submodule")
- c.Assert(err, IsNil)
- defer os.RemoveAll(dir)
-
- r, _ := PlainInit(dir, false)
- r.CreateRemote(&config.RemoteConfig{
- Name: DefaultRemoteName,
- URL: path,
- })
-
- err = r.Pull(&PullOptions{
- RecurseSubmodules: DefaultSubmoduleRecursionDepth,
- })
- c.Assert(err, IsNil)
-
- cfg, err := r.Config()
- c.Assert(cfg.Submodules, HasLen, 2)
-}
-
-func (s *RepositorySuite) TestPullAdd(c *C) {
- path := fixtures.Basic().ByTag("worktree").One().Worktree().Root()
-
- r, err := Clone(memory.NewStorage(), nil, &CloneOptions{
- URL: filepath.Join(path, ".git"),
- })
-
- c.Assert(err, IsNil)
-
- storage := r.Storer.(*memory.Storage)
- c.Assert(storage.Objects, HasLen, 28)
-
- branch, err := r.Reference("refs/heads/master", false)
- c.Assert(err, IsNil)
- c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
-
- ExecuteOnPath(c, path,
- "touch foo",
- "git add foo",
- "git commit -m foo foo",
- )
-
- err = r.Pull(&PullOptions{RemoteName: "origin"})
- c.Assert(err, IsNil)
-
- // the commit command has introduced a new commit, tree and blob
- c.Assert(storage.Objects, HasLen, 31)
-
- branch, err = r.Reference("refs/heads/master", false)
- c.Assert(err, IsNil)
- c.Assert(branch.Hash().String(), Not(Equals), "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
-}
-
func (s *RepositorySuite) TestPush(c *C) {
url := c.MkDir()
server, err := PlainInit(url, true)
diff --git a/worktree.go b/worktree.go
index 13b2497..0c15d4c 100644
--- a/worktree.go
+++ b/worktree.go
@@ -32,6 +32,47 @@ type Worktree struct {
fs billy.Filesystem
}
+// 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 (w *Worktree) Pull(o *PullOptions) error {
+ if err := o.Validate(); err != nil {
+ return err
+ }
+
+ head, err := w.r.fetchAndUpdateReferences(&FetchOptions{
+ RemoteName: o.RemoteName,
+ Depth: o.Depth,
+ Auth: o.Auth,
+ Progress: o.Progress,
+ }, o.ReferenceName)
+ if err != nil {
+ return err
+ }
+
+ if err := w.Reset(&ResetOptions{Commit: head.Hash()}); err != nil {
+ return err
+ }
+
+ if o.RecurseSubmodules != NoRecurseSubmodules {
+ return w.updateSubmodules(o.RecurseSubmodules)
+ }
+
+ return nil
+}
+
+func (w *Worktree) updateSubmodules(recursion SubmoduleRescursivity) error {
+ s, err := w.Submodules()
+ if err != nil {
+ return err
+ }
+
+ return s.Update(&SubmoduleUpdateOptions{
+ Init: true,
+ RecurseSubmodules: recursion,
+ })
+}
+
// Checkout switch branches or restore working tree files.
func (w *Worktree) Checkout(opts *CheckoutOptions) error {
if err := opts.Validate(); err != nil {
diff --git a/worktree_test.go b/worktree_test.go
index a6c7b06..97c4055 100644
--- a/worktree_test.go
+++ b/worktree_test.go
@@ -1,10 +1,12 @@
package git
import (
+ "bytes"
"io/ioutil"
"os"
"path/filepath"
+ "gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
"gopkg.in/src-d/go-git.v4/plumbing/format/index"
@@ -29,6 +31,163 @@ func (s *WorktreeSuite) SetUpTest(c *C) {
s.Repository = s.NewRepositoryWithEmptyWorktree(f)
}
+func (s *WorktreeSuite) TestPullCheckout(c *C) {
+ fs := memfs.New()
+ r, _ := Init(memory.NewStorage(), fs)
+ r.CreateRemote(&config.RemoteConfig{
+ Name: DefaultRemoteName,
+ URL: s.GetBasicLocalRepositoryURL(),
+ })
+
+ w, err := r.Worktree()
+ c.Assert(err, IsNil)
+
+ err = w.Pull(&PullOptions{})
+ c.Assert(err, IsNil)
+
+ fi, err := fs.ReadDir("")
+ c.Assert(err, IsNil)
+ c.Assert(fi, HasLen, 8)
+}
+
+func (s *WorktreeSuite) TestPullUpdateReferencesIfNeeded(c *C) {
+ r, _ := Init(memory.NewStorage(), memfs.New())
+ r.CreateRemote(&config.RemoteConfig{
+ Name: DefaultRemoteName,
+ URL: s.GetBasicLocalRepositoryURL(),
+ })
+
+ err := r.Fetch(&FetchOptions{})
+ c.Assert(err, IsNil)
+
+ _, err = r.Reference("refs/heads/master", false)
+ c.Assert(err, NotNil)
+
+ w, err := r.Worktree()
+ c.Assert(err, IsNil)
+
+ err = w.Pull(&PullOptions{})
+ c.Assert(err, IsNil)
+
+ head, err := r.Reference(plumbing.HEAD, true)
+ c.Assert(err, IsNil)
+ c.Assert(head.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
+
+ branch, err := r.Reference("refs/heads/master", false)
+ c.Assert(err, IsNil)
+ c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
+
+ err = w.Pull(&PullOptions{})
+ c.Assert(err, Equals, NoErrAlreadyUpToDate)
+}
+
+func (s *WorktreeSuite) TestPullInSingleBranch(c *C) {
+ r, _ := Init(memory.NewStorage(), memfs.New())
+ err := r.clone(&CloneOptions{
+ URL: s.GetBasicLocalRepositoryURL(),
+ SingleBranch: true,
+ })
+
+ c.Assert(err, IsNil)
+
+ w, err := r.Worktree()
+ c.Assert(err, IsNil)
+
+ err = w.Pull(&PullOptions{})
+ c.Assert(err, Equals, NoErrAlreadyUpToDate)
+
+ branch, err := r.Reference("refs/heads/master", false)
+ c.Assert(err, IsNil)
+ c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
+
+ branch, err = r.Reference("refs/remotes/foo/branch", false)
+ c.Assert(err, NotNil)
+
+ storage := r.Storer.(*memory.Storage)
+ c.Assert(storage.Objects, HasLen, 28)
+}
+
+func (s *WorktreeSuite) TestPullProgress(c *C) {
+ r, _ := Init(memory.NewStorage(), memfs.New())
+
+ r.CreateRemote(&config.RemoteConfig{
+ Name: DefaultRemoteName,
+ URL: s.GetBasicLocalRepositoryURL(),
+ })
+
+ w, err := r.Worktree()
+ c.Assert(err, IsNil)
+
+ buf := bytes.NewBuffer(nil)
+ err = w.Pull(&PullOptions{
+ Progress: buf,
+ })
+
+ c.Assert(err, IsNil)
+ c.Assert(buf.Len(), Not(Equals), 0)
+}
+
+func (s *WorktreeSuite) TestPullProgressWithRecursion(c *C) {
+ path := fixtures.ByTag("submodule").One().Worktree().Root()
+
+ dir, err := ioutil.TempDir("", "plain-clone-submodule")
+ c.Assert(err, IsNil)
+ defer os.RemoveAll(dir)
+
+ r, _ := PlainInit(dir, false)
+ r.CreateRemote(&config.RemoteConfig{
+ Name: DefaultRemoteName,
+ URL: path,
+ })
+
+ w, err := r.Worktree()
+ c.Assert(err, IsNil)
+
+ err = w.Pull(&PullOptions{
+ RecurseSubmodules: DefaultSubmoduleRecursionDepth,
+ })
+ c.Assert(err, IsNil)
+
+ cfg, err := r.Config()
+ c.Assert(cfg.Submodules, HasLen, 2)
+}
+
+func (s *RepositorySuite) TestPullAdd(c *C) {
+ path := fixtures.Basic().ByTag("worktree").One().Worktree().Root()
+
+ r, err := Clone(memory.NewStorage(), memfs.New(), &CloneOptions{
+ URL: filepath.Join(path, ".git"),
+ })
+
+ c.Assert(err, IsNil)
+
+ storage := r.Storer.(*memory.Storage)
+ c.Assert(storage.Objects, HasLen, 28)
+
+ branch, err := r.Reference("refs/heads/master", false)
+ c.Assert(err, IsNil)
+ c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
+
+ ExecuteOnPath(c, path,
+ "touch foo",
+ "git add foo",
+ "git commit -m foo foo",
+ )
+
+ w, err := r.Worktree()
+ c.Assert(err, IsNil)
+
+ err = w.Pull(&PullOptions{RemoteName: "origin"})
+ c.Assert(err, IsNil)
+
+ // the commit command has introduced a new commit, tree and blob
+ c.Assert(storage.Objects, HasLen, 31)
+
+ branch, err = r.Reference("refs/heads/master", false)
+ c.Assert(err, IsNil)
+ c.Assert(branch.Hash().String(), Not(Equals), "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
+}
+
func (s *WorktreeSuite) TestCheckout(c *C) {
fs := memfs.New()
w := &Worktree{