Raw
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Shawn Pearce
4 #
5
6 test_description='git checkout-index --temp test.
7
8 With --temp flag, git checkout-index writes to temporary merge files
9 rather than the tracked path.'
10
11 . ./test-lib.sh
12
13 test_expect_success 'setup' '
14 mkdir asubdir &&
15 echo tree1path0 >path0 &&
16 echo tree1path1 >path1 &&
17 echo tree1path3 >path3 &&
18 echo tree1path4 >path4 &&
19 echo tree1asubdir/path5 >asubdir/path5 &&
20 git update-index --add path0 path1 path3 path4 asubdir/path5 &&
21 t1=$(git write-tree) &&
22 rm -f path* .merge_* actual .git/index &&
23 echo tree2path0 >path0 &&
24 echo tree2path1 >path1 &&
25 echo tree2path2 >path2 &&
26 echo tree2path4 >path4 &&
27 git update-index --add path0 path1 path2 path4 &&
28 t2=$(git write-tree) &&
29 rm -f path* .merge_* actual .git/index &&
30 echo tree2path0 >path0 &&
31 echo tree3path1 >path1 &&
32 echo tree3path2 >path2 &&
33 echo tree3path3 >path3 &&
34 git update-index --add path0 path1 path2 path3 &&
35 t3=$(git write-tree)
36 '
37
38 test_expect_success 'checkout one stage 0 to temporary file' '
39 rm -f path* .merge_* actual .git/index &&
40 git read-tree $t1 &&
41 git checkout-index --temp -- path1 >actual &&
42 test_line_count = 1 actual &&
43 test $(cut "-d " -f2 actual) = path1 &&
44 p=$(cut "-d " -f1 actual) &&
45 test_path_is_file $p &&
46 test $(cat $p) = tree1path1
47 '
48
49 test_expect_success 'checkout all stage 0 to temporary files' '
50 rm -f path* .merge_* actual .git/index &&
51 git read-tree $t1 &&
52 git checkout-index -a --temp >actual &&
53 test_line_count = 5 actual &&
54 for f in path0 path1 path3 path4 asubdir/path5
55 do
56 test $(grep $f actual | cut "-d " -f2) = $f &&
57 p=$(grep $f actual | cut "-d " -f1) &&
58 test_path_is_file $p &&
59 test $(cat $p) = tree1$f || return 1
60 done
61 '
62
63 test_expect_success 'setup 3-way merge' '
64 rm -f path* .merge_* actual .git/index &&
65 git read-tree -m $t1 $t2 $t3
66 '
67
68 test_expect_success 'checkout one stage 2 to temporary file' '
69 rm -f path* .merge_* actual &&
70 git checkout-index --stage=2 --temp -- path1 >actual &&
71 test_line_count = 1 actual &&
72 test $(cut "-d " -f2 actual) = path1 &&
73 p=$(cut "-d " -f1 actual) &&
74 test_path_is_file $p &&
75 test $(cat $p) = tree2path1
76 '
77
78 test_expect_success 'checkout all stage 2 to temporary files' '
79 rm -f path* .merge_* actual &&
80 git checkout-index --all --stage=2 --temp >actual &&
81 test_line_count = 3 actual &&
82 for f in path1 path2 path4
83 do
84 test $(grep $f actual | cut "-d " -f2) = $f &&
85 p=$(grep $f actual | cut "-d " -f1) &&
86 test_path_is_file $p &&
87 test $(cat $p) = tree2$f || return 1
88 done
89 '
90
91 test_expect_success 'checkout all stages of unknown path' '
92 rm -f path* .merge_* actual &&
93 test_must_fail git checkout-index --stage=all --temp \
94 -- does-not-exist 2>stderr &&
95 test_grep not.in.the.cache stderr
96 '
97
98 test_expect_success 'checkout all stages/one file to nothing' '
99 rm -f path* .merge_* actual &&
100 git checkout-index --stage=all --temp -- path0 >actual 2>stderr &&
101 test_must_be_empty stderr &&
102 test_line_count = 0 actual
103 '
104
105 test_expect_success 'checkout all stages/one file to temporary files' '
106 rm -f path* .merge_* actual &&
107 git checkout-index --stage=all --temp -- path1 >actual &&
108 test_line_count = 1 actual &&
109 test $(cut "-d " -f2 actual) = path1 &&
110 cut "-d " -f1 actual | (read s1 s2 s3 &&
111 test_path_is_file $s1 &&
112 test_path_is_file $s2 &&
113 test_path_is_file $s3 &&
114 test $(cat $s1) = tree1path1 &&
115 test $(cat $s2) = tree2path1 &&
116 test $(cat $s3) = tree3path1)
117 '
118
119 test_expect_success '--stage=all implies --temp' '
120 rm -f path* .merge_* actual &&
121 git checkout-index --stage=all -- path1 &&
122 test_path_is_missing path1
123 '
124
125 test_expect_success 'overriding --stage=all resets implied --temp' '
126 rm -f path* .merge_* actual &&
127 git checkout-index --stage=all --stage=2 -- path1 &&
128 echo tree2path1 >expect &&
129 test_cmp expect path1
130 '
131
132 test_expect_success '--stage=all --no-temp is rejected' '
133 rm -f path* .merge_* actual &&
134 test_must_fail git checkout-index --stage=all --no-temp -- path1 2>err &&
135 grep -v "already exists" err &&
136 grep "options .--stage=all. and .--no-temp. cannot be used together" err
137 '
138
139 test_expect_success 'checkout some stages/one file to temporary files' '
140 rm -f path* .merge_* actual &&
141 git checkout-index --stage=all --temp -- path2 >actual &&
142 test_line_count = 1 actual &&
143 test $(cut "-d " -f2 actual) = path2 &&
144 cut "-d " -f1 actual | (read s1 s2 s3 &&
145 test $s1 = . &&
146 test_path_is_file $s2 &&
147 test_path_is_file $s3 &&
148 test $(cat $s2) = tree2path2 &&
149 test $(cat $s3) = tree3path2)
150 '
151
152 test_expect_success 'checkout all stages/all files to temporary files' '
153 rm -f path* .merge_* actual &&
154 git checkout-index -a --stage=all --temp >actual &&
155 test_line_count = 5 actual
156 '
157
158 test_expect_success '-- path0: no entry' '
159 test x$(grep path0 actual | cut "-d " -f2) = x
160 '
161
162 test_expect_success '-- path1: all 3 stages' '
163 test $(grep path1 actual | cut "-d " -f2) = path1 &&
164 grep path1 actual | cut "-d " -f1 | (read s1 s2 s3 &&
165 test_path_is_file $s1 &&
166 test_path_is_file $s2 &&
167 test_path_is_file $s3 &&
168 test $(cat $s1) = tree1path1 &&
169 test $(cat $s2) = tree2path1 &&
170 test $(cat $s3) = tree3path1)
171 '
172
173 test_expect_success '-- path2: no stage 1, have stage 2 and 3' '
174 test $(grep path2 actual | cut "-d " -f2) = path2 &&
175 grep path2 actual | cut "-d " -f1 | (read s1 s2 s3 &&
176 test $s1 = . &&
177 test_path_is_file $s2 &&
178 test_path_is_file $s3 &&
179 test $(cat $s2) = tree2path2 &&
180 test $(cat $s3) = tree3path2)
181 '
182
183 test_expect_success '-- path3: no stage 2, have stage 1 and 3' '
184 test $(grep path3 actual | cut "-d " -f2) = path3 &&
185 grep path3 actual | cut "-d " -f1 | (read s1 s2 s3 &&
186 test_path_is_file $s1 &&
187 test $s2 = . &&
188 test_path_is_file $s3 &&
189 test $(cat $s1) = tree1path3 &&
190 test $(cat $s3) = tree3path3)
191 '
192
193 test_expect_success '-- path4: no stage 3, have stage 1 and 3' '
194 test $(grep path4 actual | cut "-d " -f2) = path4 &&
195 grep path4 actual | cut "-d " -f1 | (read s1 s2 s3 &&
196 test_path_is_file $s1 &&
197 test_path_is_file $s2 &&
198 test $s3 = . &&
199 test $(cat $s1) = tree1path4 &&
200 test $(cat $s2) = tree2path4)
201 '
202
203 test_expect_success '-- asubdir/path5: no stage 2 and 3 have stage 1' '
204 test $(grep asubdir/path5 actual | cut "-d " -f2) = asubdir/path5 &&
205 grep asubdir/path5 actual | cut "-d " -f1 | (read s1 s2 s3 &&
206 test_path_is_file $s1 &&
207 test $s2 = . &&
208 test $s3 = . &&
209 test $(cat $s1) = tree1asubdir/path5)
210 '
211
212 test_expect_success 'checkout --temp within subdir' '
213 (
214 cd asubdir &&
215 git checkout-index -a --stage=all >actual &&
216 test_line_count = 1 actual &&
217 test $(grep path5 actual | cut "-d " -f2) = path5 &&
218 grep path5 actual | cut "-d " -f1 | (read s1 s2 s3 &&
219 test_path_is_file ../$s1 &&
220 test $s2 = . &&
221 test $s3 = . &&
222 test $(cat ../$s1) = tree1asubdir/path5)
223 )
224 '
225
226 test_expect_success 'checkout --temp symlink' '
227 rm -f path* .merge_* actual .git/index &&
228 test_ln_s_add path7 path6 &&
229 git checkout-index --temp -a >actual &&
230 test_line_count = 1 actual &&
231 test $(cut "-d " -f2 actual) = path6 &&
232 p=$(cut "-d " -f1 actual) &&
233 test_path_is_file $p &&
234 test $(cat $p) = path7
235 '
236
237 test_expect_success 'emit well-formed relative path' '
238 rm -f path* .merge_* actual .git/index &&
239 >path0123456789 &&
240 git update-index --add path0123456789 &&
241 (
242 cd asubdir &&
243 git checkout-index --temp -- ../path0123456789 >actual &&
244 test_line_count = 1 actual &&
245 test $(cut "-d " -f2 actual) = ../path0123456789
246 )
247 '
248
249 test_done