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