| 49 | } |
| 50 | |
| 51 | func TestCreateWithBaseBranchesFromRef(t *testing.T) { |
| 52 | t.Parallel() |
| 53 | dir := bootstrapRepo(t) |
| 54 | |
| 55 | // Add a second commit on a side branch; the default HEAD (main) stays at |
| 56 | // the first commit. |
| 57 | runGit(t, dir, "branch", "feature") |
| 58 | runGit(t, dir, "checkout", "feature") |
| 59 | require.NoError(t, os.WriteFile(filepath.Join(dir, "b.txt"), []byte("B"), 0o644)) |
| 60 | runGit(t, dir, "add", ".") |
| 61 | runGit(t, dir, "commit", "-m", "feature work") |
| 62 | featureHead := gitOut(t, dir, "rev-parse", "feature") |
| 63 | runGit(t, dir, "checkout", "-") |
| 64 | |
| 65 | wt, err := Create(t.Context(), dir, "from-feature", WithRoot(t.TempDir()), WithBase("feature")) |
| 66 | require.NoError(t, err) |
| 67 | |
| 68 | // The worktree branched from feature, so it carries feature's commit and |
| 69 | // its file. |
| 70 | assert.Equal(t, featureHead, gitOut(t, wt.Dir, "rev-parse", "HEAD")) |
| 71 | assert.FileExists(t, filepath.Join(wt.Dir, "b.txt")) |
| 72 | assert.Equal(t, featureHead, wt.BaseCommit) |
| 73 | } |
| 74 | |
| 75 | func TestCreateWithEmptyBaseUsesHead(t *testing.T) { |
| 76 | t.Parallel() |