diff options
Diffstat (limited to 'repository_test.go')
-rw-r--r-- | repository_test.go | 337 |
1 files changed, 260 insertions, 77 deletions
diff --git a/repository_test.go b/repository_test.go index 336582e..1c43c6e 100644 --- a/repository_test.go +++ b/repository_test.go @@ -3,6 +3,7 @@ package git import ( "bytes" "fmt" + "io/ioutil" "os" "os/exec" "path/filepath" @@ -12,10 +13,11 @@ import ( "gopkg.in/src-d/go-git.v4/fixtures" "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/object" + "gopkg.in/src-d/go-git.v4/storage/filesystem" "gopkg.in/src-d/go-git.v4/storage/memory" . "gopkg.in/check.v1" - "gopkg.in/src-d/go-git.v4/storage/filesystem" + memoryfs "srcd.works/go-billy.v1/memory" ) type RepositorySuite struct { @@ -24,13 +26,95 @@ type RepositorySuite struct { var _ = Suite(&RepositorySuite{}) -func (s *RepositorySuite) TestNewRepository(c *C) { - r := NewMemoryRepository() +func (s *RepositorySuite) TestInit(c *C) { + r, err := Init(memory.NewStorage(), memoryfs.New()) + c.Assert(err, IsNil) + c.Assert(r, NotNil) + + cfg, err := r.Config() + c.Assert(err, IsNil) + c.Assert(cfg.Core.IsBare, Equals, false) +} + +func (s *RepositorySuite) TestInitBare(c *C) { + r, err := Init(memory.NewStorage(), nil) + c.Assert(err, IsNil) + c.Assert(r, NotNil) + + cfg, err := r.Config() + c.Assert(err, IsNil) + c.Assert(cfg.Core.IsBare, Equals, true) + +} + +func (s *RepositorySuite) TestInitAlreadyExists(c *C) { + st := memory.NewStorage() + + r, err := Init(st, nil) + c.Assert(err, IsNil) + c.Assert(r, NotNil) + + r, err = Init(st, nil) + c.Assert(err, Equals, ErrRepositoryAlreadyExists) + c.Assert(r, IsNil) +} + +func (s *RepositorySuite) TestOpen(c *C) { + st := memory.NewStorage() + + r, err := Init(st, memoryfs.New()) + c.Assert(err, IsNil) + c.Assert(r, NotNil) + + r, err = Open(st, memoryfs.New()) + c.Assert(err, IsNil) + c.Assert(r, NotNil) +} + +func (s *RepositorySuite) TestOpenBare(c *C) { + st := memory.NewStorage() + + r, err := Init(st, nil) + c.Assert(err, IsNil) + c.Assert(r, NotNil) + + r, err = Open(st, nil) + c.Assert(err, IsNil) c.Assert(r, NotNil) } +func (s *RepositorySuite) TestOpenMissingWorktree(c *C) { + st := memory.NewStorage() + + r, err := Init(st, memoryfs.New()) + c.Assert(err, IsNil) + c.Assert(r, NotNil) + + r, err = Open(st, nil) + c.Assert(err, Equals, ErrWorktreeNotProvided) + c.Assert(r, IsNil) +} + +func (s *RepositorySuite) TestOpenNotExists(c *C) { + r, err := Open(memory.NewStorage(), nil) + c.Assert(err, Equals, ErrRepositoryNotExists) + c.Assert(r, IsNil) +} + +func (s *RepositorySuite) TestClone(c *C) { + r, err := Clone(memory.NewStorage(), nil, &CloneOptions{ + URL: s.GetBasicLocalRepositoryURL(), + }) + + c.Assert(err, IsNil) + + remotes, err := r.Remotes() + c.Assert(err, IsNil) + c.Assert(remotes, HasLen, 1) +} + func (s *RepositorySuite) TestCreateRemoteAndRemote(c *C) { - r := NewMemoryRepository() + r, _ := Init(memory.NewStorage(), nil) remote, err := r.CreateRemote(&config.RemoteConfig{ Name: "foo", URL: "http://foo/foo.git", @@ -46,7 +130,7 @@ func (s *RepositorySuite) TestCreateRemoteAndRemote(c *C) { } func (s *RepositorySuite) TestCreateRemoteInvalid(c *C) { - r := NewMemoryRepository() + r, _ := Init(memory.NewStorage(), nil) remote, err := r.CreateRemote(&config.RemoteConfig{}) c.Assert(err, Equals, config.ErrRemoteConfigEmptyName) @@ -54,7 +138,7 @@ func (s *RepositorySuite) TestCreateRemoteInvalid(c *C) { } func (s *RepositorySuite) TestDeleteRemote(c *C) { - r := NewMemoryRepository() + r, _ := Init(memory.NewStorage(), nil) _, err := r.CreateRemote(&config.RemoteConfig{ Name: "foo", URL: "http://foo/foo.git", @@ -70,8 +154,100 @@ func (s *RepositorySuite) TestDeleteRemote(c *C) { c.Assert(alt, IsNil) } +func (s *RepositorySuite) TestPlainInit(c *C) { + dir, err := ioutil.TempDir("", "plain-init") + c.Assert(err, IsNil) + defer os.RemoveAll(dir) + + r, err := PlainInit(dir, true) + c.Assert(err, IsNil) + c.Assert(r, NotNil) + + cfg, err := r.Config() + c.Assert(err, IsNil) + c.Assert(cfg.Core.IsBare, Equals, true) +} + +func (s *RepositorySuite) TestPlainInitAlreadyExists(c *C) { + dir, err := ioutil.TempDir("", "plain-init") + c.Assert(err, IsNil) + defer os.RemoveAll(dir) + + r, err := PlainInit(dir, true) + c.Assert(err, IsNil) + c.Assert(r, NotNil) + + r, err = PlainInit(dir, true) + c.Assert(err, Equals, ErrRepositoryAlreadyExists) + c.Assert(r, IsNil) +} + +func (s *RepositorySuite) TestPlainOpen(c *C) { + dir, err := ioutil.TempDir("", "plain-open") + c.Assert(err, IsNil) + defer os.RemoveAll(dir) + + r, err := PlainInit(dir, false) + c.Assert(err, IsNil) + c.Assert(r, NotNil) + + r, err = PlainOpen(dir) + c.Assert(err, IsNil) + c.Assert(r, NotNil) +} + +func (s *RepositorySuite) TestPlainOpenBare(c *C) { + dir, err := ioutil.TempDir("", "plain-open") + c.Assert(err, IsNil) + defer os.RemoveAll(dir) + + r, err := PlainInit(dir, true) + c.Assert(err, IsNil) + c.Assert(r, NotNil) + + r, err = PlainOpen(dir) + c.Assert(err, IsNil) + c.Assert(r, NotNil) +} + +func (s *RepositorySuite) TestPlainOpenNotBare(c *C) { + dir, err := ioutil.TempDir("", "plain-open") + c.Assert(err, IsNil) + defer os.RemoveAll(dir) + + r, err := PlainInit(dir, false) + c.Assert(err, IsNil) + c.Assert(r, NotNil) + + r, err = PlainOpen(filepath.Join(dir, ".git")) + c.Assert(err, Equals, ErrWorktreeNotProvided) + c.Assert(r, IsNil) +} + +func (s *RepositorySuite) TestPlainOpenNotExists(c *C) { + r, err := PlainOpen("/not-exists/") + c.Assert(err, Equals, ErrRepositoryNotExists) + c.Assert(r, IsNil) +} + +func (s *RepositorySuite) TestPlainClone(c *C) { + dir, err := ioutil.TempDir("", "plain-clone") + c.Assert(err, IsNil) + defer os.RemoveAll(dir) + + r, err := PlainClone(dir, false, &CloneOptions{ + URL: s.GetBasicLocalRepositoryURL(), + }) + + c.Assert(err, IsNil) + + remotes, err := r.Remotes() + c.Assert(err, IsNil) + c.Assert(remotes, HasLen, 1) +} + func (s *RepositorySuite) TestFetch(c *C) { - r := NewMemoryRepository() + r, _ := Init(memory.NewStorage(), nil) _, err := r.CreateRemote(&config.RemoteConfig{ Name: DefaultRemoteName, URL: s.GetBasicLocalRepositoryURL(), @@ -83,7 +259,7 @@ func (s *RepositorySuite) TestFetch(c *C) { c.Assert(err, IsNil) c.Assert(remotes, HasLen, 1) - _, err = r.Reference(plumbing.HEAD, false) + _, err = r.Head() c.Assert(err, Equals, plumbing.ErrReferenceNotFound) branch, err := r.Reference("refs/remotes/origin/master", false) @@ -93,14 +269,15 @@ func (s *RepositorySuite) TestFetch(c *C) { c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") } -func (s *RepositorySuite) TestClone(c *C) { - r := NewMemoryRepository() +func (s *RepositorySuite) TestCloneDeep(c *C) { + fs := memoryfs.New() + r, _ := Init(memory.NewStorage(), fs) head, err := r.Head() c.Assert(err, Equals, plumbing.ErrReferenceNotFound) c.Assert(head, IsNil) - err = r.Clone(&CloneOptions{ + err = r.clone(&CloneOptions{ URL: s.GetBasicLocalRepositoryURL(), }) @@ -126,16 +303,20 @@ func (s *RepositorySuite) TestClone(c *C) { c.Assert(branch, NotNil) c.Assert(branch.Type(), Equals, plumbing.HashReference) c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") + + fi, err := fs.ReadDir("") + c.Assert(err, IsNil) + c.Assert(fi, HasLen, 8) } func (s *RepositorySuite) TestCloneConfig(c *C) { - r := NewMemoryRepository() + r, _ := Init(memory.NewStorage(), nil) head, err := r.Head() c.Assert(err, Equals, plumbing.ErrReferenceNotFound) c.Assert(head, IsNil) - err = r.Clone(&CloneOptions{ + err = r.clone(&CloneOptions{ URL: s.GetBasicLocalRepositoryURL(), }) @@ -149,29 +330,15 @@ func (s *RepositorySuite) TestCloneConfig(c *C) { c.Assert(cfg.Remotes["origin"].Name, Equals, "origin") c.Assert(cfg.Remotes["origin"].URL, Not(Equals), "") } -func (s *RepositorySuite) TestCloneNonEmpty(c *C) { - r := NewMemoryRepository() - - head, err := r.Head() - c.Assert(err, Equals, plumbing.ErrReferenceNotFound) - c.Assert(head, IsNil) - - o := &CloneOptions{URL: s.GetBasicLocalRepositoryURL()} - err = r.Clone(o) - c.Assert(err, IsNil) - - err = r.Clone(o) - c.Assert(err, Equals, ErrRepositoryNonEmpty) -} func (s *RepositorySuite) TestCloneSingleBranchAndNonHEAD(c *C) { - r := NewMemoryRepository() + r, _ := Init(memory.NewStorage(), nil) head, err := r.Head() c.Assert(err, Equals, plumbing.ErrReferenceNotFound) c.Assert(head, IsNil) - err = r.Clone(&CloneOptions{ + err = r.clone(&CloneOptions{ URL: s.GetBasicLocalRepositoryURL(), ReferenceName: plumbing.ReferenceName("refs/heads/branch"), SingleBranch: true, @@ -202,13 +369,13 @@ func (s *RepositorySuite) TestCloneSingleBranchAndNonHEAD(c *C) { } func (s *RepositorySuite) TestCloneSingleBranch(c *C) { - r := NewMemoryRepository() + r, _ := Init(memory.NewStorage(), nil) head, err := r.Head() c.Assert(err, Equals, plumbing.ErrReferenceNotFound) c.Assert(head, IsNil) - err = r.Clone(&CloneOptions{ + err = r.clone(&CloneOptions{ URL: s.GetBasicLocalRepositoryURL(), SingleBranch: true, }) @@ -238,8 +405,8 @@ func (s *RepositorySuite) TestCloneSingleBranch(c *C) { } func (s *RepositorySuite) TestCloneDetachedHEAD(c *C) { - r := NewMemoryRepository() - err := r.Clone(&CloneOptions{ + r, _ := Init(memory.NewStorage(), nil) + err := r.clone(&CloneOptions{ URL: s.GetBasicLocalRepositoryURL(), ReferenceName: plumbing.ReferenceName("refs/tags/v1.0.0"), }) @@ -251,11 +418,27 @@ func (s *RepositorySuite) TestCloneDetachedHEAD(c *C) { c.Assert(head.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") } +func (s *RepositorySuite) TestPullCheckout(c *C) { + fs := memoryfs.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) { - r := NewMemoryRepository() + fs := memoryfs.New() buf := bytes.NewBuffer(nil) - err := r.Clone(&CloneOptions{ + _, err := Clone(memory.NewStorage(), fs, &CloneOptions{ URL: s.GetBasicLocalRepositoryURL(), Progress: buf, }) @@ -265,7 +448,7 @@ func (s *RepositorySuite) TestCloneWithProgress(c *C) { } func (s *RepositorySuite) TestPullUpdateReferencesIfNeeded(c *C) { - r := NewMemoryRepository() + r, _ := Init(memory.NewStorage(), nil) r.CreateRemote(&config.RemoteConfig{ Name: DefaultRemoteName, URL: s.GetBasicLocalRepositoryURL(), @@ -293,8 +476,8 @@ func (s *RepositorySuite) TestPullUpdateReferencesIfNeeded(c *C) { } func (s *RepositorySuite) TestPullSingleBranch(c *C) { - r := NewMemoryRepository() - err := r.Clone(&CloneOptions{ + r, _ := Init(memory.NewStorage(), nil) + err := r.clone(&CloneOptions{ URL: s.GetBasicLocalRepositoryURL(), SingleBranch: true, }) @@ -316,7 +499,7 @@ func (s *RepositorySuite) TestPullSingleBranch(c *C) { } func (s *RepositorySuite) TestPullProgress(c *C) { - r := NewMemoryRepository() + r, _ := Init(memory.NewStorage(), nil) r.CreateRemote(&config.RemoteConfig{ Name: DefaultRemoteName, @@ -335,8 +518,8 @@ func (s *RepositorySuite) TestPullProgress(c *C) { func (s *RepositorySuite) TestPullAdd(c *C) { path := fixtures.Basic().One().Worktree().Base() - r := NewMemoryRepository() - err := r.Clone(&CloneOptions{ + r, _ := Init(memory.NewStorage(), nil) + err := r.clone(&CloneOptions{ URL: fmt.Sprintf("file://%s", filepath.Join(path, ".git")), }) @@ -383,7 +566,7 @@ func (s *RepositorySuite) TestPushToEmptyRepository(c *C) { dstFs := fixtures.ByTag("empty").One().DotGit() url := fmt.Sprintf("file://%s", dstFs.Base()) - r, err := NewRepository(sto) + r, err := Open(sto, srcFs) c.Assert(err, IsNil) _, err = r.CreateRemote(&config.RemoteConfig{ @@ -397,7 +580,7 @@ func (s *RepositorySuite) TestPushToEmptyRepository(c *C) { sto, err = filesystem.NewStorage(dstFs) c.Assert(err, IsNil) - dstRepo, err := NewRepository(sto) + dstRepo, err := Open(sto, nil) c.Assert(err, IsNil) iter, err := sto.IterReferences() @@ -421,31 +604,16 @@ func (s *RepositorySuite) TestPushNonExistentRemote(c *C) { sto, err := filesystem.NewStorage(srcFs) c.Assert(err, IsNil) - r, err := NewRepository(sto) + r, err := Open(sto, srcFs) c.Assert(err, IsNil) err = r.Push(&PushOptions{RemoteName: "myremote"}) c.Assert(err, ErrorMatches, ".*remote not found.*") } -func (s *RepositorySuite) TestIsEmpty(c *C) { - r := NewMemoryRepository() - - empty, err := r.IsEmpty() - c.Assert(err, IsNil) - c.Assert(empty, Equals, true) - - err = r.Clone(&CloneOptions{URL: s.GetBasicLocalRepositoryURL()}) - c.Assert(err, IsNil) - - empty, err = r.IsEmpty() - c.Assert(err, IsNil) - c.Assert(empty, Equals, false) -} - func (s *RepositorySuite) TestCommit(c *C) { - r := NewMemoryRepository() - err := r.Clone(&CloneOptions{ + r, _ := Init(memory.NewStorage(), nil) + err := r.clone(&CloneOptions{ URL: s.GetBasicLocalRepositoryURL(), }) @@ -468,8 +636,8 @@ func (s *RepositorySuite) TestCommit(c *C) { } func (s *RepositorySuite) TestCommits(c *C) { - r := NewMemoryRepository() - err := r.Clone(&CloneOptions{URL: s.GetBasicLocalRepositoryURL()}) + r, _ := Init(memory.NewStorage(), nil) + err := r.clone(&CloneOptions{URL: s.GetBasicLocalRepositoryURL()}) c.Assert(err, IsNil) count := 0 @@ -495,8 +663,8 @@ func (s *RepositorySuite) TestTag(c *C) { fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(), ) - r := NewMemoryRepository() - err := r.Clone(&CloneOptions{URL: url}) + r, _ := Init(memory.NewStorage(), nil) + err := r.clone(&CloneOptions{URL: url}) c.Assert(err, IsNil) hash := plumbing.NewHash("ad7897c0fb8e7d9a9ba41fa66072cf06095a6cfc") @@ -513,8 +681,8 @@ func (s *RepositorySuite) TestTags(c *C) { fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(), ) - r := NewMemoryRepository() - err := r.Clone(&CloneOptions{URL: url}) + r, _ := Init(memory.NewStorage(), nil) + err := r.clone(&CloneOptions{URL: url}) c.Assert(err, IsNil) count := 0 @@ -538,8 +706,8 @@ func (s *RepositorySuite) TestTags(c *C) { } func (s *RepositorySuite) TestCommitIterClosePanic(c *C) { - r := NewMemoryRepository() - err := r.Clone(&CloneOptions{URL: s.GetBasicLocalRepositoryURL()}) + r, _ := Init(memory.NewStorage(), nil) + err := r.clone(&CloneOptions{URL: s.GetBasicLocalRepositoryURL()}) c.Assert(err, IsNil) commits, err := r.Commits() @@ -548,8 +716,8 @@ func (s *RepositorySuite) TestCommitIterClosePanic(c *C) { } func (s *RepositorySuite) TestRef(c *C) { - r := NewMemoryRepository() - err := r.Clone(&CloneOptions{URL: s.GetBasicLocalRepositoryURL()}) + r, _ := Init(memory.NewStorage(), nil) + err := r.clone(&CloneOptions{URL: s.GetBasicLocalRepositoryURL()}) c.Assert(err, IsNil) ref, err := r.Reference(plumbing.HEAD, false) @@ -562,8 +730,8 @@ func (s *RepositorySuite) TestRef(c *C) { } func (s *RepositorySuite) TestRefs(c *C) { - r := NewMemoryRepository() - err := r.Clone(&CloneOptions{URL: s.GetBasicLocalRepositoryURL()}) + r, _ := Init(memory.NewStorage(), nil) + err := r.clone(&CloneOptions{URL: s.GetBasicLocalRepositoryURL()}) c.Assert(err, IsNil) c.Assert(err, IsNil) @@ -574,8 +742,8 @@ func (s *RepositorySuite) TestRefs(c *C) { } func (s *RepositorySuite) TestObject(c *C) { - r := NewMemoryRepository() - err := r.Clone(&CloneOptions{URL: s.GetBasicLocalRepositoryURL()}) + r, _ := Init(memory.NewStorage(), nil) + err := r.clone(&CloneOptions{URL: s.GetBasicLocalRepositoryURL()}) c.Assert(err, IsNil) hash := plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5") @@ -587,8 +755,8 @@ func (s *RepositorySuite) TestObject(c *C) { } func (s *RepositorySuite) TestObjectNotFound(c *C) { - r := NewMemoryRepository() - err := r.Clone(&CloneOptions{URL: s.GetBasicLocalRepositoryURL()}) + r, _ := Init(memory.NewStorage(), nil) + err := r.clone(&CloneOptions{URL: s.GetBasicLocalRepositoryURL()}) c.Assert(err, IsNil) hash := plumbing.NewHash("0a3fb06ff80156fb153bcdcc58b5e16c2d27625c") @@ -597,6 +765,21 @@ func (s *RepositorySuite) TestObjectNotFound(c *C) { c.Assert(tag, IsNil) } +func (s *RepositorySuite) TestWorktree(c *C) { + def := memoryfs.New() + r, _ := Init(memory.NewStorage(), def) + w, err := r.Worktree() + c.Assert(err, IsNil) + c.Assert(w.fs, Equals, def) +} + +func (s *RepositorySuite) TestWorktreeBare(c *C) { + r, _ := Init(memory.NewStorage(), nil) + w, err := r.Worktree() + c.Assert(err, Equals, ErrIsBareRepository) + c.Assert(w, IsNil) +} + func ExecuteOnPath(c *C, path string, cmds ...string) error { for _, cmd := range cmds { err := executeOnPath(path, cmd) |