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 on re-checking out current branch' '
83 (
84 cd there &&
85 git checkout newmaster
86 )
87'
88
89test_expect_success 'checkout --to from a bare repo' '
90 (
91 git clone --bare . bare &&
92 cd bare &&
93 git checkout --to ../there2 -b bare-master master
94 )
95'
96
97test_expect_success 'checkout from a bare repo without --to' '
98 (
99 cd bare &&
100 test_must_fail git checkout master
101 )
102'
103
104test_expect_success 'checkout with grafts' '
105 test_when_finished rm .git/info/grafts &&
106 test_commit abc &&
107 SHA1=`git rev-parse HEAD` &&
108 test_commit def &&
109 test_commit xyz &&
110 echo "`git rev-parse HEAD` $SHA1" >.git/info/grafts &&
111 cat >expected <<-\EOF &&
112 xyz
113 abc
114 EOF
115 git log --format=%s -2 >actual &&
116 test_cmp expected actual &&
117 git checkout --detach --to grafted master &&
118 git --git-dir=grafted/.git log --format=%s -2 >actual &&
119 test_cmp expected actual
120'
121
122test_done