aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--_examples/remotes/main.go6
-rw-r--r--common_test.go11
-rw-r--r--remote.go57
-rw-r--r--remote_test.go190
-rw-r--r--repository.go171
-rw-r--r--repository_test.go228
-rw-r--r--worktree.go41
-rw-r--r--worktree_test.go159
8 files changed, 422 insertions, 441 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/common_test.go b/common_test.go
index 21b4481..a7cd755 100644
--- a/common_test.go
+++ b/common_test.go
@@ -169,3 +169,14 @@ func (s *SuiteCommon) TestCountLines(c *C) {
c.Assert(o, Equals, t.e, Commentf("subtest %d, input=%q", i, t.i))
}
}
+
+func AssertReferences(c *C, r *Repository, expected map[string]string) {
+ for name, target := range expected {
+ expected := plumbing.NewReferenceFromStrings(name, target)
+
+ obtained, err := r.Reference(expected.Name(), true)
+ c.Assert(err, IsNil)
+
+ c.Assert(obtained, DeepEquals, expected)
+ }
+}
diff --git a/remote.go b/remote.go
index e29a00a..64b12ed 100644
--- a/remote.go
+++ b/remote.go
@@ -49,23 +49,10 @@ func (r *Remote) String() string {
return fmt.Sprintf("%s\t%s (fetch)\n%[1]s\t%[3]s (push)", r.c.Name, fetch, push)
}
-// Fetch fetches references from the remote to the local repository.
-// Returns nil if the operation is successful, NoErrAlreadyUpToDate if there are
-// no changes to be fetched and no local references to update, or an error.
-func (r *Remote) Fetch(o *FetchOptions) error {
- _, err := r.fetch(o)
- return err
-}
-
// Push performs a push to the remote. Returns NoErrAlreadyUpToDate if the
// remote was already up-to-date.
-func (r *Remote) Push(o *PushOptions) (err error) {
+func (r *Remote) Push(o *PushOptions) error {
// TODO: Sideband support
-
- if o.RemoteName == "" {
- o.RemoteName = r.c.Name
- }
-
if err := o.Validate(); err != nil {
return err
}
@@ -142,7 +129,47 @@ func (r *Remote) Push(o *PushOptions) (err error) {
return err
}
- return rs.Error()
+ if err = rs.Error(); err != nil {
+ return err
+ }
+
+ return r.updateRemoteReferenceStorage(req, rs)
+}
+
+func (r *Remote) updateRemoteReferenceStorage(
+ req *packp.ReferenceUpdateRequest,
+ result *packp.ReportStatus,
+) error {
+
+ for _, spec := range r.c.Fetch {
+ for _, c := range req.Commands {
+ if !spec.Match(c.Name) {
+ continue
+ }
+
+ local := spec.Dst(c.Name)
+ ref := plumbing.NewHashReference(local, c.New)
+ switch c.Action() {
+ case packp.Create, packp.Update:
+ if err := r.s.SetReference(ref); err != nil {
+ return err
+ }
+ case packp.Delete:
+ if err := r.s.RemoveReference(local); err != nil {
+ return err
+ }
+ }
+ }
+ }
+
+ return nil
+}
+
+// Fetch fetches references from the remote to the local repository.
+// no changes to be fetched and no local references to update, or an error.
+func (r *Remote) Fetch(o *FetchOptions) error {
+ _, err := r.fetch(o)
+ return err
}
func (r *Remote) fetch(o *FetchOptions) (storer.ReferenceStorer, error) {
diff --git a/remote_test.go b/remote_test.go
index e8ddb21..43276c2 100644
--- a/remote_test.go
+++ b/remote_test.go
@@ -5,8 +5,6 @@ import (
"io"
"io/ioutil"
"os"
- "path/filepath"
- "strings"
"github.com/src-d/go-git-fixtures"
"gopkg.in/src-d/go-git.v4/config"
@@ -305,13 +303,14 @@ func (s *RemoteSuite) TestString(c *C) {
}
func (s *RemoteSuite) TestPushToEmptyRepository(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)
- dstFs := fixtures.ByTag("empty").One().DotGit()
- url := dstFs.Root()
-
r := newRemote(sto, &config.RemoteConfig{
Name: DefaultRemoteName,
URL: url,
@@ -323,140 +322,117 @@ func (s *RemoteSuite) TestPushToEmptyRepository(c *C) {
})
c.Assert(err, IsNil)
- dstSto, err := filesystem.NewStorage(dstFs)
- c.Assert(err, IsNil)
- dstRepo, err := Open(dstSto, nil)
+ iter, err := r.s.IterReferences()
c.Assert(err, IsNil)
- iter, err := sto.IterReferences()
- c.Assert(err, IsNil)
- err = iter.ForEach(func(ref *plumbing.Reference) error {
+ expected := make(map[string]string)
+ iter.ForEach(func(ref *plumbing.Reference) error {
if !ref.IsBranch() {
return nil
}
- dstRef, err := dstRepo.Reference(ref.Name(), true)
- c.Assert(err, IsNil, Commentf("ref: %s", ref.String()))
- c.Assert(dstRef, DeepEquals, ref)
-
+ expected[ref.Name().String()] = ref.Hash().String()
return nil
})
c.Assert(err, IsNil)
+
+ AssertReferences(c, server, expected)
+
}
func (s *RemoteSuite) TestPushTags(c *C) {
- srcFs := fixtures.ByURL("https://github.com/git-fixtures/tags.git").One().DotGit()
- sto, err := filesystem.NewStorage(srcFs)
+ url := c.MkDir()
+ server, err := PlainInit(url, true)
c.Assert(err, IsNil)
- dstFs := fixtures.ByTag("empty").One().DotGit()
- url := dstFs.Root()
+ fs := fixtures.ByURL("https://github.com/git-fixtures/tags.git").One().DotGit()
+ sto, err := filesystem.NewStorage(fs)
+ c.Assert(err, IsNil)
r := newRemote(sto, &config.RemoteConfig{
Name: DefaultRemoteName,
URL: url,
})
- rs := config.RefSpec("refs/tags/*:refs/tags/*")
err = r.Push(&PushOptions{
- RefSpecs: []config.RefSpec{rs},
+ RefSpecs: []config.RefSpec{"refs/tags/*:refs/tags/*"},
})
c.Assert(err, IsNil)
- dstSto, err := filesystem.NewStorage(dstFs)
- c.Assert(err, IsNil)
- dstRepo, err := Open(dstSto, nil)
- c.Assert(err, IsNil)
-
- ref, err := dstRepo.Storer.Reference(plumbing.ReferenceName("refs/tags/lightweight-tag"))
- c.Assert(err, IsNil)
- c.Assert(ref, DeepEquals, plumbing.NewReferenceFromStrings("refs/tags/lightweight-tag", "f7b877701fbf855b44c0a9e86f3fdce2c298b07f"))
-
- ref, err = dstRepo.Storer.Reference(plumbing.ReferenceName("refs/tags/annotated-tag"))
- c.Assert(err, IsNil)
- c.Assert(ref, DeepEquals, plumbing.NewReferenceFromStrings("refs/tags/annotated-tag", "b742a2a9fa0afcfa9a6fad080980fbc26b007c69"))
-
- ref, err = dstRepo.Storer.Reference(plumbing.ReferenceName("refs/tags/commit-tag"))
- c.Assert(err, IsNil)
- c.Assert(ref, DeepEquals, plumbing.NewReferenceFromStrings("refs/tags/commit-tag", "ad7897c0fb8e7d9a9ba41fa66072cf06095a6cfc"))
-
- ref, err = dstRepo.Storer.Reference(plumbing.ReferenceName("refs/tags/blob-tag"))
- c.Assert(err, IsNil)
- c.Assert(ref, DeepEquals, plumbing.NewReferenceFromStrings("refs/tags/blob-tag", "fe6cb94756faa81e5ed9240f9191b833db5f40ae"))
-
- ref, err = dstRepo.Storer.Reference(plumbing.ReferenceName("refs/tags/tree-tag"))
- c.Assert(err, IsNil)
- c.Assert(ref, DeepEquals, plumbing.NewReferenceFromStrings("refs/tags/tree-tag", "152175bf7e5580299fa1f0ba41ef6474cc043b70"))
+ AssertReferences(c, server, map[string]string{
+ "refs/tags/lightweight-tag": "f7b877701fbf855b44c0a9e86f3fdce2c298b07f",
+ "refs/tags/annotated-tag": "b742a2a9fa0afcfa9a6fad080980fbc26b007c69",
+ "refs/tags/commit-tag": "ad7897c0fb8e7d9a9ba41fa66072cf06095a6cfc",
+ "refs/tags/blob-tag": "fe6cb94756faa81e5ed9240f9191b833db5f40ae",
+ "refs/tags/tree-tag": "152175bf7e5580299fa1f0ba41ef6474cc043b70",
+ })
}
func (s *RemoteSuite) TestPushNoErrAlreadyUpToDate(c *C) {
- f := fixtures.Basic().One()
- sto, err := filesystem.NewStorage(f.DotGit())
+ fs := fixtures.Basic().One().DotGit()
+ sto, err := filesystem.NewStorage(fs)
c.Assert(err, IsNil)
- url := f.DotGit().Root()
+
r := newRemote(sto, &config.RemoteConfig{
Name: DefaultRemoteName,
- URL: url,
+ URL: fs.Root(),
})
- rs := config.RefSpec("refs/heads/*:refs/heads/*")
err = r.Push(&PushOptions{
- RefSpecs: []config.RefSpec{rs},
+ RefSpecs: []config.RefSpec{"refs/heads/*:refs/heads/*"},
})
c.Assert(err, Equals, NoErrAlreadyUpToDate)
}
func (s *RemoteSuite) TestPushDeleteReference(c *C) {
- f := fixtures.Basic().One()
- sto, err := filesystem.NewStorage(f.DotGit())
+ fs := fixtures.Basic().One().DotGit()
+ sto, err := filesystem.NewStorage(fs)
c.Assert(err, IsNil)
- dstFs := f.DotGit()
- dstSto, err := filesystem.NewStorage(dstFs)
+ r, err := PlainClone(c.MkDir(), true, &CloneOptions{
+ URL: fs.Root(),
+ })
c.Assert(err, IsNil)
- prepareRepo(c, dstFs.Root())
- url := dstFs.Root()
- r := newRemote(sto, &config.RemoteConfig{
- Name: DefaultRemoteName,
- URL: url,
- })
+ remote, err := r.Remote(DefaultRemoteName)
+ c.Assert(err, IsNil)
- rs := config.RefSpec(":refs/heads/branch")
- err = r.Push(&PushOptions{
- RefSpecs: []config.RefSpec{rs},
+ err = remote.Push(&PushOptions{
+ RefSpecs: []config.RefSpec{":refs/heads/branch"},
})
c.Assert(err, IsNil)
- _, err = dstSto.Reference(plumbing.ReferenceName("refs/heads/branch"))
+ _, err = sto.Reference(plumbing.ReferenceName("refs/heads/branch"))
+ c.Assert(err, Equals, plumbing.ErrReferenceNotFound)
+
+ _, err = r.Storer.Reference(plumbing.ReferenceName("refs/heads/branch"))
c.Assert(err, Equals, plumbing.ErrReferenceNotFound)
}
func (s *RemoteSuite) TestPushRejectNonFastForward(c *C) {
- f := fixtures.Basic().One()
- sto, err := filesystem.NewStorage(f.DotGit())
+ fs := fixtures.Basic().One().DotGit()
+ server, err := filesystem.NewStorage(fs)
c.Assert(err, IsNil)
- dstFs := f.DotGit()
- dstSto, err := filesystem.NewStorage(dstFs)
+ r, err := PlainClone(c.MkDir(), true, &CloneOptions{
+ URL: fs.Root(),
+ })
c.Assert(err, IsNil)
- url := dstFs.Root()
- r := newRemote(sto, &config.RemoteConfig{
- Name: DefaultRemoteName,
- URL: url,
- })
+ remote, err := r.Remote(DefaultRemoteName)
+ c.Assert(err, IsNil)
- oldRef, err := dstSto.Reference(plumbing.ReferenceName("refs/heads/branch"))
+ branch := plumbing.ReferenceName("refs/heads/branch")
+ oldRef, err := server.Reference(branch)
c.Assert(err, IsNil)
c.Assert(oldRef, NotNil)
- err = r.Push(&PushOptions{RefSpecs: []config.RefSpec{
- config.RefSpec("refs/heads/master:refs/heads/branch"),
+ err = remote.Push(&PushOptions{RefSpecs: []config.RefSpec{
+ "refs/heads/master:refs/heads/branch",
}})
c.Assert(err, ErrorMatches, "non-fast-forward update: refs/heads/branch")
- newRef, err := dstSto.Reference(plumbing.ReferenceName("refs/heads/branch"))
+ newRef, err := server.Reference(branch)
c.Assert(err, IsNil)
c.Assert(newRef, DeepEquals, oldRef)
}
@@ -491,32 +467,35 @@ func (s *RemoteSuite) TestPushForce(c *C) {
}
func (s *RemoteSuite) TestPushNewReference(c *C) {
- f := fixtures.Basic().One()
- sto, err := filesystem.NewStorage(f.DotGit())
- c.Assert(err, IsNil)
+ fs := fixtures.Basic().One().DotGit()
+ url := c.MkDir()
+ server, err := PlainClone(url, true, &CloneOptions{
+ URL: fs.Root(),
+ })
- dstFs := f.DotGit()
- dstSto, err := filesystem.NewStorage(dstFs)
+ r, err := PlainClone(c.MkDir(), true, &CloneOptions{
+ URL: url,
+ })
c.Assert(err, IsNil)
- url := dstFs.Root()
- r := newRemote(sto, &config.RemoteConfig{
- Name: DefaultRemoteName,
- URL: url,
- })
+ remote, err := r.Remote(DefaultRemoteName)
+ c.Assert(err, IsNil)
- oldRef, err := dstSto.Reference(plumbing.ReferenceName("refs/heads/branch"))
+ ref, err := r.Reference(plumbing.ReferenceName("refs/heads/master"), true)
c.Assert(err, IsNil)
- c.Assert(oldRef, NotNil)
- err = r.Push(&PushOptions{RefSpecs: []config.RefSpec{
- config.RefSpec("refs/heads/branch:refs/heads/branch2"),
+ err = remote.Push(&PushOptions{RefSpecs: []config.RefSpec{
+ "refs/heads/master:refs/heads/branch2",
}})
c.Assert(err, IsNil)
- newRef, err := dstSto.Reference(plumbing.ReferenceName("refs/heads/branch2"))
- c.Assert(err, IsNil)
- c.Assert(newRef.Hash(), Equals, oldRef.Hash())
+ AssertReferences(c, server, map[string]string{
+ "refs/heads/branch2": ref.Hash().String(),
+ })
+
+ AssertReferences(c, r, map[string]string{
+ "refs/remotes/origin/branch2": ref.Hash().String(),
+ })
}
func (s *RemoteSuite) TestPushInvalidEndpoint(c *C) {
@@ -532,7 +511,7 @@ func (s *RemoteSuite) TestPushNonExistentEndpoint(c *C) {
}
func (s *RemoteSuite) TestPushInvalidSchemaEndpoint(c *C) {
- r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: "qux://foo"})
+ r := newRemote(nil, &config.RemoteConfig{Name: "origin", URL: "qux://foo"})
err := r.Push(&PushOptions{})
c.Assert(err, ErrorMatches, ".*unsupported scheme.*")
}
@@ -587,22 +566,3 @@ func (s *RemoteSuite) TestGetHaves(c *C) {
c.Assert(err, IsNil)
c.Assert(l, HasLen, 2)
}
-
-const bareConfig = `[core]
-repositoryformatversion = 0
-filemode = true
-bare = true`
-
-func prepareRepo(c *C, path string) {
- // git-receive-pack refuses to update refs/heads/master on non-bare repo
- // so we ensure bare repo config.
- config := filepath.Join(path, "config")
- if _, err := os.Stat(config); err == nil {
- f, err := os.OpenFile(config, os.O_TRUNC|os.O_WRONLY, 0)
- c.Assert(err, IsNil)
- content := strings.NewReader(bareConfig)
- _, err = io.Copy(f, content)
- c.Assert(err, IsNil)
- c.Assert(f.Close(), IsNil)
- }
-}
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 f31cd1d..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,212 +588,43 @@ 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{})
+func (s *RepositorySuite) TestPush(c *C) {
+ url := c.MkDir()
+ server, err := PlainInit(url, true)
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,
+ _, err = s.Repository.CreateRemote(&config.RemoteConfig{
+ Name: "test",
+ URL: url,
})
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"),
+ err = s.Repository.Push(&PushOptions{
+ RemoteName: "test",
})
-
- 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) TestPushToEmptyRepository(c *C) {
- srcFs := fixtures.Basic().One().DotGit()
- sto, err := filesystem.NewStorage(srcFs)
c.Assert(err, IsNil)
- dstFs := fixtures.ByTag("empty").One().DotGit()
- url := dstFs.Root()
-
- r, err := Open(sto, srcFs)
- c.Assert(err, IsNil)
-
- _, err = r.CreateRemote(&config.RemoteConfig{
- Name: "myremote",
- URL: url,
+ AssertReferences(c, server, map[string]string{
+ "refs/heads/master": "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
+ "refs/heads/branch": "e8d3ffab552895c19b9fcf7aa264d277cde33881",
})
- c.Assert(err, IsNil)
-
- err = r.Push(&PushOptions{RemoteName: "myremote"})
- c.Assert(err, IsNil)
-
- sto, err = filesystem.NewStorage(dstFs)
- c.Assert(err, IsNil)
- dstRepo, err := Open(sto, nil)
- c.Assert(err, IsNil)
-
- iter, err := sto.IterReferences()
- c.Assert(err, IsNil)
- err = iter.ForEach(func(ref *plumbing.Reference) error {
- if !ref.IsBranch() {
- return nil
- }
-
- dstRef, err := dstRepo.Reference(ref.Name(), true)
- c.Assert(err, IsNil)
- c.Assert(dstRef, DeepEquals, ref)
- return nil
+ AssertReferences(c, s.Repository, map[string]string{
+ "refs/remotes/test/master": "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
+ "refs/remotes/test/branch": "e8d3ffab552895c19b9fcf7aa264d277cde33881",
})
- c.Assert(err, IsNil)
}
func (s *RepositorySuite) TestPushDepth(c *C) {
- dir, err := ioutil.TempDir("", "push-depth")
- defer os.RemoveAll(dir)
-
- origin, err := PlainClone(c.MkDir(), true, &CloneOptions{
+ url := c.MkDir()
+ server, err := PlainClone(url, true, &CloneOptions{
URL: fixtures.Basic().One().DotGit().Root(),
})
c.Assert(err, IsNil)
- fs := origin.Storer.(*filesystem.Storage).Filesystem()
r, err := Clone(memory.NewStorage(), memfs.New(), &CloneOptions{
- URL: fs.Root(),
+ URL: url,
Depth: 1,
})
c.Assert(err, IsNil)
@@ -803,13 +647,13 @@ func (s *RepositorySuite) TestPushDepth(c *C) {
err = r.Push(&PushOptions{})
c.Assert(err, IsNil)
- remote, err := origin.Head()
- c.Assert(err, IsNil)
- c.Assert(remote.Hash(), Equals, hash)
+ AssertReferences(c, server, map[string]string{
+ "refs/heads/master": hash.String(),
+ })
- local, err := r.Head()
- c.Assert(err, IsNil)
- c.Assert(local.Hash(), Equals, remote.Hash())
+ AssertReferences(c, r, map[string]string{
+ "refs/remotes/origin/master": hash.String(),
+ })
}
func (s *RepositorySuite) TestPushNonExistentRemote(c *C) {
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{