|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/entireio/cli/cmd/entire/cli/testutil" |
| 7 | + "github.com/go-git/go-git/v6" |
| 8 | + "github.com/go-git/go-git/v6/plumbing" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +// TestResolveImportLinkCommitSHA_LocalDefaultBranchNoOrigin proves that when |
| 13 | +// there is no origin remote, the resolver resolves via the local default |
| 14 | +// branch arm (testutil.InitRepo checks out master, so GetDefaultBranchName |
| 15 | +// returns "master" and the local-branch lookup succeeds). The true HEAD |
| 16 | +// fallback is covered by TestResolveImportLinkCommitSHA_HEADWhenNoDefaultBranch. |
| 17 | +func TestResolveImportLinkCommitSHA_LocalDefaultBranchNoOrigin(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + repoDir := t.TempDir() |
| 21 | + testutil.InitRepo(t, repoDir) |
| 22 | + testutil.WriteFile(t, repoDir, "f.txt", "init") |
| 23 | + testutil.GitAdd(t, repoDir, "f.txt") |
| 24 | + testutil.GitCommit(t, repoDir, "init") |
| 25 | + |
| 26 | + repo, err := git.PlainOpen(repoDir) |
| 27 | + require.NoError(t, err) |
| 28 | + t.Cleanup(func() { _ = repo.Close() }) |
| 29 | + |
| 30 | + head, err := repo.Head() |
| 31 | + require.NoError(t, err) |
| 32 | + |
| 33 | + got := resolveImportLinkCommitSHA(repo) |
| 34 | + require.Equal(t, head.Hash().String(), got) |
| 35 | +} |
| 36 | + |
| 37 | +// TestResolveImportLinkCommitSHA_PrefersOriginDefaultBranch proves that when |
| 38 | +// origin's default branch tip differs from the local branch tip, the |
| 39 | +// resolver prefers origin's tip — that's the commit the server already |
| 40 | +// knows about. |
| 41 | +func TestResolveImportLinkCommitSHA_PrefersOriginDefaultBranch(t *testing.T) { |
| 42 | + t.Parallel() |
| 43 | + |
| 44 | + repoDir := t.TempDir() |
| 45 | + testutil.InitRepo(t, repoDir) |
| 46 | + testutil.WriteFile(t, repoDir, "f.txt", "one") |
| 47 | + testutil.GitAdd(t, repoDir, "f.txt") |
| 48 | + testutil.GitCommit(t, repoDir, "first") |
| 49 | + firstSHA := testutil.GetHeadHash(t, repoDir) |
| 50 | + |
| 51 | + testutil.WriteFile(t, repoDir, "f.txt", "two") |
| 52 | + testutil.GitAdd(t, repoDir, "f.txt") |
| 53 | + testutil.GitCommit(t, repoDir, "second") |
| 54 | + secondSHA := testutil.GetHeadHash(t, repoDir) |
| 55 | + |
| 56 | + repo, err := git.PlainOpen(repoDir) |
| 57 | + require.NoError(t, err) |
| 58 | + t.Cleanup(func() { _ = repo.Close() }) |
| 59 | + |
| 60 | + // Manually create refs/remotes/origin/main -> first commit, and |
| 61 | + // refs/remotes/origin/HEAD as a symbolic ref pointing at it. |
| 62 | + firstHash := plumbing.NewHash(firstSHA) |
| 63 | + originMainRef := plumbing.NewHashReference(plumbing.NewRemoteReferenceName("origin", "main"), firstHash) |
| 64 | + require.NoError(t, repo.Storer.SetReference(originMainRef)) |
| 65 | + originHeadRef := plumbing.NewSymbolicReference( |
| 66 | + plumbing.NewRemoteReferenceName("origin", "HEAD"), |
| 67 | + plumbing.NewRemoteReferenceName("origin", "main"), |
| 68 | + ) |
| 69 | + require.NoError(t, repo.Storer.SetReference(originHeadRef)) |
| 70 | + |
| 71 | + // Also create a local main -> second commit, so origin/main and local |
| 72 | + // main genuinely diverge. testutil.InitRepo defaults to `master`, so |
| 73 | + // without this the resolver's local-branch arm is never exercised and |
| 74 | + // the assertion below can't pin the origin-over-local preference order. |
| 75 | + secondHash := plumbing.NewHash(secondSHA) |
| 76 | + localMainRef := plumbing.NewHashReference(plumbing.NewBranchReferenceName("main"), secondHash) |
| 77 | + require.NoError(t, repo.Storer.SetReference(localMainRef)) |
| 78 | + |
| 79 | + got := resolveImportLinkCommitSHA(repo) |
| 80 | + require.Equal(t, firstSHA, got) |
| 81 | +} |
| 82 | + |
| 83 | +// TestResolveImportLinkCommitSHA_HEADWhenNoDefaultBranch proves the HEAD |
| 84 | +// fallback: when the default branch name cannot be resolved at all (no |
| 85 | +// remotes, and the checked-out branch is neither main nor master), the |
| 86 | +// resolver still returns HEAD's commit instead of "". |
| 87 | +func TestResolveImportLinkCommitSHA_HEADWhenNoDefaultBranch(t *testing.T) { |
| 88 | + t.Parallel() |
| 89 | + |
| 90 | + repoDir := t.TempDir() |
| 91 | + testutil.InitRepo(t, repoDir) |
| 92 | + testutil.WriteFile(t, repoDir, "f.txt", "init") |
| 93 | + testutil.GitAdd(t, repoDir, "f.txt") |
| 94 | + testutil.GitCommit(t, repoDir, "init") |
| 95 | + sha := testutil.GetHeadHash(t, repoDir) |
| 96 | + |
| 97 | + repo, err := git.PlainOpen(repoDir) |
| 98 | + require.NoError(t, err) |
| 99 | + t.Cleanup(func() { _ = repo.Close() }) |
| 100 | + |
| 101 | + // Rename the branch away from main/master so GetDefaultBranchName |
| 102 | + // returns "" (no origin, and no local main/master to fall back to). |
| 103 | + hash := plumbing.NewHash(sha) |
| 104 | + trunkRef := plumbing.NewHashReference(plumbing.NewBranchReferenceName("trunk"), hash) |
| 105 | + require.NoError(t, repo.Storer.SetReference(trunkRef)) |
| 106 | + require.NoError(t, repo.Storer.SetReference( |
| 107 | + plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.NewBranchReferenceName("trunk")), |
| 108 | + )) |
| 109 | + require.NoError(t, repo.Storer.RemoveReference(plumbing.NewBranchReferenceName("master"))) |
| 110 | + |
| 111 | + got := resolveImportLinkCommitSHA(repo) |
| 112 | + require.Equal(t, sha, got) |
| 113 | +} |
| 114 | + |
| 115 | +// TestResolveImportLinkCommitSHA_EmptyRepo proves the resolver returns "" and |
| 116 | +// does not panic on a repo with no commits. |
| 117 | +func TestResolveImportLinkCommitSHA_EmptyRepo(t *testing.T) { |
| 118 | + t.Parallel() |
| 119 | + |
| 120 | + repoDir := t.TempDir() |
| 121 | + // git.PlainInit deliberately (not testutil.InitRepo): the repo must stay |
| 122 | + // commit-free, so the helper's user/GPG config is irrelevant here. |
| 123 | + repo, err := git.PlainInit(repoDir, false) |
| 124 | + require.NoError(t, err) |
| 125 | + t.Cleanup(func() { _ = repo.Close() }) |
| 126 | + |
| 127 | + got := resolveImportLinkCommitSHA(repo) |
| 128 | + require.Empty(t, got) |
| 129 | +} |
0 commit comments