1#!/bin/sh
2
3test_description='test git checkout --to'
4
5. ./test-lib.sh
6
7test_expect_success 'setup' '
8 test_commit init
9'
10
11test_expect_success 'checkout --to not updating paths' '
12 test_must_fail git checkout --to -- init.t
13'
14
15test_expect_success 'checkout --to an existing worktree' '
16 mkdir -p existing/subtree &&
17 test_must_fail git checkout --detach --to existing master
18'
19
20test_expect_success 'checkout --to an existing empty worktree' '
21 mkdir existing_empty &&
22 git checkout --detach --to existing_empty master
23'
24
25test_expect_success 'checkout --to refuses to checkout locked branch' '
26 test_must_fail git checkout --to zere master &&
27 ! test -d zere &&
28 ! test -d .git/worktrees/zere
29'
30
31test_expect_success 'checkout --to a new worktree' '
32 git rev-parse HEAD >expect &&
33 git checkout --detach --to here master &&
34 (
35 cd here &&
36 test_cmp ../init.t init.t &&
37 test_must_fail git symbolic-ref HEAD &&
38 git rev-parse HEAD >actual &&
39 test_cmp ../expect actual &&
40 git fsck
41 )
42'
43
44test_expect_success 'checkout --to a new worktree from a subdir' '
45 (
46 mkdir sub &&
47 cd sub &&
48 git checkout --detach --to here master &&
49 cd here &&
50 test_cmp ../../init.t init.t
51 )
52'
53
54test_expect_success 'checkout --to from a linked checkout' '
55 (
56 cd here &&
57 git checkout --detach --to nested-here master &&
58 cd nested-here &&
59 git fsck
60 )
61'
62
63test_expect_success 'checkout --to a new worktree creating new branch' '
64 git checkout --to there -b newmaster master &&
65 (
66 cd there &&
67 test_cmp ../init.t init.t &&
68 git symbolic-ref HEAD >actual &&
69 echo refs/heads/newmaster >expect &&
70 test_cmp expect actual &&
71 git fsck
72 )
73'
74
75test_expect_success 'die the same branch is already checked out' '
76 (
77 cd here &&
78 test_must_fail git checkout newmaster
79 )
80'
81
82test_expect_success 'not die the same branch is already checked out' '
83 (
84 cd here &&
85 git checkout --ignore-other-worktrees --to anothernewmaster newmaster
86 )
87'
88
89test_expect_success 'not die on re-checking out current branch' '
90 (
91 cd there &&
92 git checkout newmaster
93 )
94'
95
96test_expect_success 'checkout --to from a bare repo' '
97 (
98 git clone --bare . bare &&
99 cd bare &&
100 git checkout --to ../there2 -b bare-master master
101 )
102'
103
104test_expect_success 'checkout from a bare repo without --to' '
105 (
106 cd bare &&
107 test_must_fail git checkout master
108 )
109'
110
111test_expect_success 'checkout with grafts' '
112 test_when_finished rm .git/info/grafts &&
113 test_commit abc &&
114 SHA1=`git rev-parse HEAD` &&
115 test_commit def &&
116 test_commit xyz &&
117 echo "`git rev-parse HEAD` $SHA1" >.git/info/grafts &&
118 cat >expected <<-\EOF &&
119 xyz
120 abc
121 EOF
122 git log --format=%s -2 >actual &&
123 test_cmp expected actual &&
124 git checkout --detach --to grafted master &&
125 git --git-dir=grafted/.git log --format=%s -2 >actual &&
126 test_cmp expected actual
127'
128
129test_done