MCPcopy Create free account
hub / github.com/go-git/go-git / TestFetchFastForwardForCustomRef

Function TestFetchFastForwardForCustomRef

remote_test.go:1677–1746  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1675}
1676
1677func TestFetchFastForwardForCustomRef(t *testing.T) {
1678 customRef := "refs/custom/branch"
1679 // 1. Set up a remote with a URL
1680 remoteURL := t.TempDir()
1681 remoteRepo, err := PlainInit(remoteURL, true)
1682 if err != nil {
1683 t.Fatal(err)
1684 }
1685
1686 // 2. Add a commit with an empty tree to master and custom ref, also set HEAD
1687 emptyTreeID := writeEmptyTree(t, remoteRepo)
1688 writeCommitToRef(t, remoteRepo, "refs/heads/master", emptyTreeID, time.Now())
1689 writeCommitToRef(t, remoteRepo, customRef, emptyTreeID, time.Now())
1690 if err := remoteRepo.Storer.SetReference(plumbing.NewSymbolicReference(plumbing.HEAD, "refs/heads/master")); err != nil {
1691 t.Fatal(err)
1692 }
1693
1694 // 3. Clone repo, then fetch the custom ref
1695 // Note that using custom ref in ReferenceName has an IsBranch issue
1696 localRepo, err := Clone(memory.NewStorage(), memfs.New(), &CloneOptions{
1697 URL: remoteURL,
1698 })
1699 if err != nil {
1700 t.Fatal(err)
1701 }
1702 if err := localRepo.Fetch(&FetchOptions{
1703 RefSpecs: []config.RefSpec{
1704 config.RefSpec(fmt.Sprintf("%s:%s", customRef, customRef)),
1705 },
1706 }); err != nil {
1707 t.Fatal(err)
1708 }
1709
1710 // 4. Make divergent changes
1711 remoteCommitID := writeCommitToRef(t, remoteRepo, customRef, emptyTreeID, time.Now())
1712 // Consecutive calls to writeCommitToRef with time.Now() might have the same
1713 // time value, explicitly set distinct ones to ensure the commit hashes
1714 // differ
1715 writeCommitToRef(t, localRepo, customRef, emptyTreeID, time.Now().Add(time.Second))
1716
1717 // 5. Try to fetch with fast-forward only mode
1718 remote, err := localRepo.Remote(DefaultRemoteName)
1719 if err != nil {
1720 t.Fatal(err)
1721 }
1722
1723 err = remote.Fetch(&FetchOptions{RefSpecs: []config.RefSpec{
1724 config.RefSpec(fmt.Sprintf("%s:%s", customRef, customRef)),
1725 }})
1726 if !errors.Is(err, ErrForceNeeded) {
1727 t.Errorf("expected %v, got %v", ErrForceNeeded, err)
1728 }
1729
1730 // 6. Fetch with force
1731 err = remote.Fetch(&FetchOptions{RefSpecs: []config.RefSpec{
1732 config.RefSpec(fmt.Sprintf("+%s:%s", customRef, customRef)),
1733 }})
1734 if err != nil {

Callers

nothing calls this directly

Calls 15

NewSymbolicReferenceFunction · 0.92
NewStorageFunction · 0.92
RefSpecTypeAlias · 0.92
ReferenceNameTypeAlias · 0.92
PlainInitFunction · 0.85
writeEmptyTreeFunction · 0.85
writeCommitToRefFunction · 0.85
CloneFunction · 0.85
RemoteMethod · 0.80
IsMethod · 0.80
SetReferenceMethod · 0.65
ReferenceMethod · 0.65

Tested by

no test coverage detected