aboutsummaryrefslogtreecommitdiffstats
path: root/repository_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'repository_test.go')
-rw-r--r--repository_test.go229
1 files changed, 213 insertions, 16 deletions
diff --git a/repository_test.go b/repository_test.go
index e284df8..9e000a3 100644
--- a/repository_test.go
+++ b/repository_test.go
@@ -6,9 +6,9 @@ import (
"errors"
"fmt"
"io"
- "io/ioutil"
"os"
"os/exec"
+ "os/user"
"path/filepath"
"regexp"
"strings"
@@ -52,6 +52,54 @@ func (s *RepositorySuite) TestInit(c *C) {
cfg, err := r.Config()
c.Assert(err, IsNil)
c.Assert(cfg.Core.IsBare, Equals, false)
+
+ // check the HEAD to see what the default branch is
+ createCommit(c, r)
+ ref, err := r.Head()
+ c.Assert(err, IsNil)
+ c.Assert(ref.Name().String(), Equals, plumbing.Master.String())
+}
+
+func (s *RepositorySuite) TestInitWithOptions(c *C) {
+ r, err := InitWithOptions(memory.NewStorage(), memfs.New(), InitOptions{
+ DefaultBranch: "refs/heads/foo",
+ })
+ c.Assert(err, IsNil)
+ c.Assert(r, NotNil)
+ createCommit(c, r)
+
+ ref, err := r.Head()
+ c.Assert(err, IsNil)
+ c.Assert(ref.Name().String(), Equals, "refs/heads/foo")
+
+}
+
+func createCommit(c *C, r *Repository) {
+ // Create a commit so there is a HEAD to check
+ wt, err := r.Worktree()
+ c.Assert(err, IsNil)
+
+ rm, err := wt.Filesystem.Create("foo.txt")
+ c.Assert(err, IsNil)
+
+ _, err = rm.Write([]byte("foo text"))
+ c.Assert(err, IsNil)
+
+ _, err = wt.Add("foo.txt")
+ c.Assert(err, IsNil)
+
+ author := object.Signature{
+ Name: "go-git",
+ Email: "go-git@fake.local",
+ When: time.Now(),
+ }
+ _, err = wt.Commit("test commit message", &CommitOptions{
+ All: true,
+ Author: &author,
+ Committer: &author,
+ })
+ c.Assert(err, IsNil)
+
}
func (s *RepositorySuite) TestInitNonStandardDotGit(c *C) {
@@ -60,17 +108,18 @@ func (s *RepositorySuite) TestInitNonStandardDotGit(c *C) {
fs := osfs.New(dir)
dot, _ := fs.Chroot("storage")
- storage := filesystem.NewStorage(dot, cache.NewObjectLRUDefault())
+ st := filesystem.NewStorage(dot, cache.NewObjectLRUDefault())
wt, _ := fs.Chroot("worktree")
- r, err := Init(storage, wt)
+ r, err := Init(st, wt)
c.Assert(err, IsNil)
c.Assert(r, NotNil)
f, err := fs.Open(fs.Join("worktree", ".git"))
c.Assert(err, IsNil)
+ defer func() { _ = f.Close() }()
- all, err := ioutil.ReadAll(f)
+ all, err := io.ReadAll(f)
c.Assert(err, IsNil)
c.Assert(string(all), Equals, fmt.Sprintf("gitdir: %s\n", filepath.Join("..", "storage")))
@@ -85,9 +134,9 @@ func (s *RepositorySuite) TestInitStandardDotGit(c *C) {
fs := osfs.New(dir)
dot, _ := fs.Chroot(".git")
- storage := filesystem.NewStorage(dot, cache.NewObjectLRUDefault())
+ st := filesystem.NewStorage(dot, cache.NewObjectLRUDefault())
- r, err := Init(storage, fs)
+ r, err := Init(st, fs)
c.Assert(err, IsNil)
c.Assert(r, NotNil)
@@ -189,6 +238,35 @@ func (s *RepositorySuite) TestCloneContext(c *C) {
c.Assert(err, Equals, context.Canceled)
}
+func (s *RepositorySuite) TestCloneMirror(c *C) {
+ r, err := Clone(memory.NewStorage(), nil, &CloneOptions{
+ URL: fixtures.Basic().One().URL,
+ Mirror: true,
+ })
+
+ c.Assert(err, IsNil)
+
+ refs, err := r.References()
+ var count int
+ refs.ForEach(func(r *plumbing.Reference) error { c.Log(r); count++; return nil })
+ c.Assert(err, IsNil)
+ // 6 refs total from github.com/git-fixtures/basic.git:
+ // - HEAD
+ // - refs/heads/master
+ // - refs/heads/branch
+ // - refs/pull/1/head
+ // - refs/pull/2/head
+ // - refs/pull/2/merge
+ c.Assert(count, Equals, 6)
+
+ cfg, err := r.Config()
+ c.Assert(err, IsNil)
+
+ c.Assert(cfg.Core.IsBare, Equals, true)
+ c.Assert(cfg.Remotes[DefaultRemoteName].Validate(), IsNil)
+ c.Assert(cfg.Remotes[DefaultRemoteName].Mirror, Equals, true)
+}
+
func (s *RepositorySuite) TestCloneWithTags(c *C) {
url := s.GetLocalRepositoryURL(
fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
@@ -466,6 +544,29 @@ func (s *RepositorySuite) TestPlainOpen(c *C) {
c.Assert(r, NotNil)
}
+func (s *RepositorySuite) TestPlainOpenTildePath(c *C) {
+ dir, clean := s.TemporalHomeDir()
+ defer clean()
+
+ r, err := PlainInit(dir, false)
+ c.Assert(err, IsNil)
+ c.Assert(r, NotNil)
+
+ currentUser, err := user.Current()
+ c.Assert(err, IsNil)
+ // remove domain for windows
+ username := currentUser.Username[strings.Index(currentUser.Username, "\\")+1:]
+
+ homes := []string{"~/", "~" + username + "/"}
+ for _, home := range homes {
+ path := strings.Replace(dir, strings.Split(dir, ".tmp")[0], home, 1)
+
+ r, err = PlainOpen(path)
+ c.Assert(err, IsNil)
+ c.Assert(r, NotNil)
+ }
+}
+
func (s *RepositorySuite) TestPlainOpenBare(c *C) {
dir, clean := s.TemporalDir()
defer clean()
@@ -855,6 +956,43 @@ func (s *RepositorySuite) TestPlainCloneWithRecurseSubmodules(c *C) {
c.Assert(cfg.Submodules, HasLen, 2)
}
+func (s *RepositorySuite) TestPlainCloneWithShallowSubmodules(c *C) {
+ if testing.Short() {
+ c.Skip("skipping test in short mode.")
+ }
+
+ dir, clean := s.TemporalDir()
+ defer clean()
+
+ path := fixtures.ByTag("submodule").One().Worktree().Root()
+ mainRepo, err := PlainClone(dir, false, &CloneOptions{
+ URL: path,
+ RecurseSubmodules: 1,
+ ShallowSubmodules: true,
+ })
+ c.Assert(err, IsNil)
+
+ mainWorktree, err := mainRepo.Worktree()
+ c.Assert(err, IsNil)
+
+ submodule, err := mainWorktree.Submodule("basic")
+ c.Assert(err, IsNil)
+
+ subRepo, err := submodule.Repository()
+ c.Assert(err, IsNil)
+
+ lr, err := subRepo.Log(&LogOptions{})
+ c.Assert(err, IsNil)
+
+ commitCount := 0
+ for _, err := lr.Next(); err == nil; _, err = lr.Next() {
+ commitCount++
+ }
+ c.Assert(err, IsNil)
+
+ c.Assert(commitCount, Equals, 1)
+}
+
func (s *RepositorySuite) TestPlainCloneNoCheckout(c *C) {
dir, clean := s.TemporalDir()
defer clean()
@@ -991,6 +1129,14 @@ func (s *RepositorySuite) TestCloneConfig(c *C) {
}
func (s *RepositorySuite) TestCloneSingleBranchAndNonHEAD(c *C) {
+ s.testCloneSingleBranchAndNonHEADReference(c, "refs/heads/branch")
+}
+
+func (s *RepositorySuite) TestCloneSingleBranchAndNonHEADAndNonFull(c *C) {
+ s.testCloneSingleBranchAndNonHEADReference(c, "branch")
+}
+
+func (s *RepositorySuite) testCloneSingleBranchAndNonHEADReference(c *C, ref string) {
r, _ := Init(memory.NewStorage(), nil)
head, err := r.Head()
@@ -999,7 +1145,7 @@ func (s *RepositorySuite) TestCloneSingleBranchAndNonHEAD(c *C) {
err = r.clone(context.Background(), &CloneOptions{
URL: s.GetBasicLocalRepositoryURL(),
- ReferenceName: plumbing.ReferenceName("refs/heads/branch"),
+ ReferenceName: plumbing.ReferenceName(ref),
SingleBranch: true,
})
@@ -1034,6 +1180,49 @@ func (s *RepositorySuite) TestCloneSingleBranchAndNonHEAD(c *C) {
c.Assert(branch.Hash().String(), Equals, "e8d3ffab552895c19b9fcf7aa264d277cde33881")
}
+func (s *RepositorySuite) TestCloneSingleBranchHEADMain(c *C) {
+ r, _ := Init(memory.NewStorage(), nil)
+
+ head, err := r.Head()
+ c.Assert(err, Equals, plumbing.ErrReferenceNotFound)
+ c.Assert(head, IsNil)
+
+ err = r.clone(context.Background(), &CloneOptions{
+ URL: s.GetLocalRepositoryURL(fixtures.ByTag("no-master-head").One()),
+ SingleBranch: true,
+ })
+
+ c.Assert(err, IsNil)
+
+ remotes, err := r.Remotes()
+ c.Assert(err, IsNil)
+ c.Assert(remotes, HasLen, 1)
+
+ cfg, err := r.Config()
+ c.Assert(err, IsNil)
+ c.Assert(cfg.Branches, HasLen, 1)
+ c.Assert(cfg.Branches["main"].Name, Equals, "main")
+ c.Assert(cfg.Branches["main"].Remote, Equals, "origin")
+ c.Assert(cfg.Branches["main"].Merge, Equals, plumbing.ReferenceName("refs/heads/main"))
+
+ head, err = r.Reference(plumbing.HEAD, false)
+ c.Assert(err, IsNil)
+ c.Assert(head, NotNil)
+ c.Assert(head.Type(), Equals, plumbing.SymbolicReference)
+ c.Assert(head.Target().String(), Equals, "refs/heads/main")
+
+ branch, err := r.Reference(head.Target(), false)
+ c.Assert(err, IsNil)
+ c.Assert(branch, NotNil)
+ c.Assert(branch.Hash().String(), Equals, "786dafbd351e587da1ae97e5fb9fbdf868b4a28f")
+
+ branch, err = r.Reference("refs/remotes/origin/HEAD", false)
+ c.Assert(err, IsNil)
+ c.Assert(branch, NotNil)
+ c.Assert(branch.Type(), Equals, plumbing.HashReference)
+ c.Assert(branch.Hash().String(), Equals, "786dafbd351e587da1ae97e5fb9fbdf868b4a28f")
+}
+
func (s *RepositorySuite) TestCloneSingleBranch(c *C) {
r, _ := Init(memory.NewStorage(), nil)
@@ -1069,12 +1258,6 @@ func (s *RepositorySuite) TestCloneSingleBranch(c *C) {
c.Assert(err, IsNil)
c.Assert(branch, NotNil)
c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
-
- branch, err = r.Reference("refs/remotes/origin/master", false)
- c.Assert(err, IsNil)
- c.Assert(branch, NotNil)
- c.Assert(branch.Type(), Equals, plumbing.HashReference)
- c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
}
func (s *RepositorySuite) TestCloneSingleTag(c *C) {
@@ -2948,6 +3131,20 @@ func (s *RepositorySuite) TestBrokenMultipleShallowFetch(c *C) {
c.Assert(err, IsNil)
}
+func (s *RepositorySuite) TestDotGitToOSFilesystemsInvalidPath(c *C) {
+ _, _, err := dotGitToOSFilesystems("\000", false)
+ c.Assert(err, NotNil)
+}
+
+func (s *RepositorySuite) TestIssue674(c *C) {
+ r, _ := Init(memory.NewStorage(), nil)
+ h, err := r.ResolveRevision(plumbing.Revision(""))
+
+ c.Assert(err, NotNil)
+ c.Assert(h, NotNil)
+ c.Check(h.IsZero(), Equals, true)
+}
+
func BenchmarkObjects(b *testing.B) {
defer fixtures.Clean()
@@ -2958,14 +3155,14 @@ func BenchmarkObjects(b *testing.B) {
b.Run(f.URL, func(b *testing.B) {
fs := f.DotGit()
- storer := filesystem.NewStorage(fs, cache.NewObjectLRUDefault())
+ st := filesystem.NewStorage(fs, cache.NewObjectLRUDefault())
worktree, err := fs.Chroot(filepath.Dir(fs.Root()))
if err != nil {
b.Fatal(err)
}
- repo, err := Open(storer, worktree)
+ repo, err := Open(st, worktree)
if err != nil {
b.Fatal(err)
}
@@ -2995,7 +3192,7 @@ func BenchmarkObjects(b *testing.B) {
func BenchmarkPlainClone(b *testing.B) {
for i := 0; i < b.N; i++ {
- t, err := ioutil.TempDir("", "")
+ t, err := os.MkdirTemp("", "")
if err != nil {
b.Fatal(err)
}