diff options
author | Paulo Gomes <pjbgf@linux.com> | 2023-05-24 20:12:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-24 20:12:55 +0100 |
commit | b98b813a17d32f4fa29a3ef2e9f4c38c5a97b440 (patch) | |
tree | 034e45271df32054b7994356d8358e9ac2f33f36 /remote_test.go | |
parent | 84d9be20ca15d29bebc629e5b6f29dab78cc69ba (diff) | |
parent | cdba5330cd205312443e8553f434829c4c5e66ee (diff) | |
download | go-git-b98b813a17d32f4fa29a3ef2e9f4c38c5a97b440.tar.gz |
Merge pull request #778 from AriehSchneier/fix-fetch-after-shallow-v2
git: Fix fetching after shallow clone. Fixes #305
Diffstat (limited to 'remote_test.go')
-rw-r--r-- | remote_test.go | 109 |
1 files changed, 68 insertions, 41 deletions
diff --git a/remote_test.go b/remote_test.go index 1bcdb0f..f8a0bdb 100644 --- a/remote_test.go +++ b/remote_test.go @@ -14,7 +14,6 @@ import ( "github.com/go-git/go-git/v5/config" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/cache" - "github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/protocol/packp" "github.com/go-git/go-git/v5/plumbing/protocol/packp/capability" "github.com/go-git/go-git/v5/plumbing/storer" @@ -1175,7 +1174,7 @@ func (s *RemoteSuite) TestGetHaves(c *C) { ), } - l, err := getHaves(localRefs, memory.NewStorage(), sto) + l, err := getHaves(localRefs, memory.NewStorage(), sto, 0) c.Assert(err, IsNil) c.Assert(l, HasLen, 2) } @@ -1404,45 +1403,7 @@ func (s *RemoteSuite) TestCanPushShasToReference(c *C) { c.Assert(err, IsNil) c.Assert(repo, NotNil) - fd, err := os.Create(filepath.Join(d, "repo", "README.md")) - c.Assert(err, IsNil) - if err != nil { - return - } - _, err = fd.WriteString("# test repo") - c.Assert(err, IsNil) - if err != nil { - return - } - err = fd.Close() - c.Assert(err, IsNil) - if err != nil { - return - } - - wt, err := repo.Worktree() - c.Assert(err, IsNil) - if err != nil { - return - } - - wt.Add("README.md") - sha, err := wt.Commit("test commit", &CommitOptions{ - Author: &object.Signature{ - Name: "test", - Email: "test@example.com", - When: time.Now(), - }, - Committer: &object.Signature{ - Name: "test", - Email: "test@example.com", - When: time.Now(), - }, - }) - c.Assert(err, IsNil) - if err != nil { - return - } + sha := CommitNewFile(c, repo, "README.md") gitremote, err := repo.CreateRemote(&config.RemoteConfig{ Name: "local", @@ -1472,3 +1433,69 @@ func (s *RemoteSuite) TestCanPushShasToReference(c *C) { } c.Assert(ref.Hash().String(), Equals, sha.String()) } + +func (s *RemoteSuite) TestFetchAfterShallowClone(c *C) { + tempDir, clean := s.TemporalDir() + defer clean() + remoteUrl := filepath.Join(tempDir, "remote") + repoDir := filepath.Join(tempDir, "repo") + + // Create a new repo and add more than 1 commit (so we can have a shallow commit) + remote, err := PlainInit(remoteUrl, false) + c.Assert(err, IsNil) + c.Assert(remote, NotNil) + + _ = CommitNewFile(c, remote, "File1") + _ = CommitNewFile(c, remote, "File2") + + // Clone the repo with a depth of 1 + repo, err := PlainClone(repoDir, false, &CloneOptions{ + URL: remoteUrl, + Depth: 1, + Tags: NoTags, + SingleBranch: true, + ReferenceName: "master", + }) + c.Assert(err, IsNil) + + // Add new commits to the origin (more than 1 so that our next test hits a missing commit) + _ = CommitNewFile(c, remote, "File3") + sha4 := CommitNewFile(c, remote, "File4") + + // Try fetch with depth of 1 again (note, we need to ensure no remote branch remains pointing at the old commit) + r, err := repo.Remote(DefaultRemoteName) + c.Assert(err, IsNil) + s.testFetch(c, r, &FetchOptions{ + Depth: 2, + Tags: NoTags, + + RefSpecs: []config.RefSpec{ + "+refs/heads/master:refs/heads/master", + "+refs/heads/master:refs/remotes/origin/master", + }, + }, []*plumbing.Reference{ + plumbing.NewReferenceFromStrings("refs/heads/master", sha4.String()), + plumbing.NewReferenceFromStrings("refs/remotes/origin/master", sha4.String()), + plumbing.NewSymbolicReference("HEAD", "refs/heads/master"), + }) + + // Add another commit to the origin + sha5 := CommitNewFile(c, remote, "File5") + + // Try fetch with depth of 2 this time (to reach a commit that we don't have locally) + r, err = repo.Remote(DefaultRemoteName) + c.Assert(err, IsNil) + s.testFetch(c, r, &FetchOptions{ + Depth: 1, + Tags: NoTags, + + RefSpecs: []config.RefSpec{ + "+refs/heads/master:refs/heads/master", + "+refs/heads/master:refs/remotes/origin/master", + }, + }, []*plumbing.Reference{ + plumbing.NewReferenceFromStrings("refs/heads/master", sha5.String()), + plumbing.NewReferenceFromStrings("refs/remotes/origin/master", sha5.String()), + plumbing.NewSymbolicReference("HEAD", "refs/heads/master"), + }) +} |