| 12 | ) |
| 13 | |
| 14 | func TestResolveWorktreeTarget(t *testing.T) { |
| 15 | dir := t.TempDir() |
| 16 | nonEmptyLinkedWorktree := t.TempDir() |
| 17 | require.NoError(t, os.WriteFile(filepath.Join(nonEmptyLinkedWorktree, "file"), []byte("x"), 0o644)) |
| 18 | |
| 19 | tests := []struct { |
| 20 | name string |
| 21 | path string |
| 22 | stubs func(*run.CommandStubber) |
| 23 | wantReuse bool |
| 24 | wantErr string |
| 25 | }{ |
| 26 | { |
| 27 | name: "path is the current worktree", |
| 28 | stubs: func(cs *run.CommandStubber) { |
| 29 | cs.Register(`git rev-parse --path-format=absolute --show-toplevel --git-common-dir`, 0, "/repo/main\n/repo/.git\n") |
| 30 | cs.Register(`git -C .+rev-parse --path-format=absolute --show-toplevel --show-prefix --git-common-dir`, 0, "/repo/main\n\n/repo/.git\n") |
| 31 | }, |
| 32 | wantErr: "--worktree path points to the repository you're already in; omit --worktree to check out here", |
| 33 | }, |
| 34 | { |
| 35 | name: "path is a subdirectory of the current worktree", |
| 36 | stubs: func(cs *run.CommandStubber) { |
| 37 | cs.Register(`git rev-parse --path-format=absolute --show-toplevel --git-common-dir`, 0, "/repo/main\n/repo/.git\n") |
| 38 | cs.Register(`git -C .+ rev-parse --path-format=absolute --show-toplevel --show-prefix --git-common-dir`, 0, "/repo/main\nsub/\n/repo/.git\n") |
| 39 | }, |
| 40 | wantErr: "--worktree path points to the repository you're already in; omit --worktree to check out here", |
| 41 | }, |
| 42 | { |
| 43 | name: "path is a different worktree of this repo", |
| 44 | path: nonEmptyLinkedWorktree, |
| 45 | stubs: func(cs *run.CommandStubber) { |
| 46 | cs.Register(`git rev-parse --path-format=absolute --show-toplevel --git-common-dir`, 0, "/repo/main\n/repo/.git\n") |
| 47 | cs.Register(`git -C .+ rev-parse --path-format=absolute --show-toplevel --show-prefix --git-common-dir`, 0, "/path/to/wt\n\n/repo/.git\n") |
| 48 | }, |
| 49 | wantReuse: true, |
| 50 | }, |
| 51 | { |
| 52 | name: "path is a subdirectory of another worktree", |
| 53 | stubs: func(cs *run.CommandStubber) { |
| 54 | cs.Register(`git rev-parse --path-format=absolute --show-toplevel --git-common-dir`, 0, "/repo/main\n/repo/.git\n") |
| 55 | cs.Register(`git -C .+ rev-parse --path-format=absolute --show-toplevel --show-prefix --git-common-dir`, 0, "/path/to/wt\nsub/\n/repo/.git\n") |
| 56 | }, |
| 57 | wantErr: "--worktree path is inside an existing worktree", |
| 58 | }, |
| 59 | { |
| 60 | name: "path is inside a different repository", |
| 61 | stubs: func(cs *run.CommandStubber) { |
| 62 | cs.Register(`git rev-parse --path-format=absolute --show-toplevel --git-common-dir`, 0, "/repo/main\n/repo/.git\n") |
| 63 | cs.Register(`git -C .+ rev-parse --path-format=absolute --show-toplevel --show-prefix --git-common-dir`, 0, "/other/wt\n\n/other/.git\n") |
| 64 | }, |
| 65 | wantErr: "--worktree path is inside a different repository", |
| 66 | }, |
| 67 | { |
| 68 | name: "target is non-git or non-existent", |
| 69 | stubs: func(cs *run.CommandStubber) { |
| 70 | cs.Register(`git rev-parse --path-format=absolute --show-toplevel --git-common-dir`, 0, "/repo/main\n/repo/.git\n") |
| 71 | cs.Register(`git -C .+ rev-parse --path-format=absolute --show-toplevel --show-prefix --git-common-dir`, 128, "") |