| 93 | } |
| 94 | |
| 95 | func TestCreateWithRemoteBaseFetches(t *testing.T) { |
| 96 | t.Parallel() |
| 97 | // A bare "remote" repo with one extra commit beyond what the clone has. |
| 98 | remote := bootstrapRepo(t) |
| 99 | require.NoError(t, os.WriteFile(filepath.Join(remote, "remote-only.txt"), []byte("R"), 0o644)) |
| 100 | runGit(t, remote, "add", ".") |
| 101 | runGit(t, remote, "commit", "-m", "remote work") |
| 102 | remoteBranch := gitOut(t, remote, "rev-parse", "--abbrev-ref", "HEAD") |
| 103 | |
| 104 | // A clone that is behind: it lacks the remote's latest commit until |
| 105 | // WithBase triggers a fetch. |
| 106 | clone, err := filepath.EvalSymlinks(t.TempDir()) |
| 107 | require.NoError(t, err) |
| 108 | runGit(t, clone, "clone", remote, ".") |
| 109 | runGit(t, clone, "config", "user.email", "test@example.com") |
| 110 | runGit(t, clone, "config", "user.name", "Test User") |
| 111 | runGit(t, clone, "config", "commit.gpgsign", "false") |
| 112 | // Move the remote forward again so the clone's remote-tracking ref is |
| 113 | // stale; only an explicit fetch (done by WithBase) brings it up to date. |
| 114 | require.NoError(t, os.WriteFile(filepath.Join(remote, "newer.txt"), []byte("N"), 0o644)) |
| 115 | runGit(t, remote, "add", ".") |
| 116 | runGit(t, remote, "commit", "-m", "newer work") |
| 117 | newerHead := gitOut(t, remote, "rev-parse", "HEAD") |
| 118 | |
| 119 | wt, err := Create(t.Context(), clone, "from-remote", WithRoot(t.TempDir()), WithBase("origin/"+remoteBranch)) |
| 120 | require.NoError(t, err) |
| 121 | |
| 122 | // The fetch pulled the latest remote commit, so the worktree starts there. |
| 123 | assert.Equal(t, newerHead, gitOut(t, wt.Dir, "rev-parse", "HEAD")) |
| 124 | assert.FileExists(t, filepath.Join(wt.Dir, "newer.txt")) |
| 125 | } |
| 126 | |
| 127 | func TestCreateWithRemoteBaseUpdatesTrackingRefWithoutFetchConfig(t *testing.T) { |
| 128 | t.Parallel() |