t0001: use test_must_fail
[git/git.git] / t / t0001-init.sh
CommitLineData
6adcca3f
JH
1#!/bin/sh
2
3test_description='git init'
4
5. ./test-lib.sh
6
7check_config () {
8 if test -d "$1" && test -f "$1/config" && test -d "$1/refs"
9 then
10 : happy
11 else
12 echo "expected a directory $1, a file $1/config and $1/refs"
13 return 1
14 fi
3cc6a6f0
JK
15 bare=$(cd "$1" && git config --bool core.bare)
16 worktree=$(cd "$1" && git config core.worktree) ||
6adcca3f
JH
17 worktree=unset
18
19 test "$bare" = "$2" && test "$worktree" = "$3" || {
20 echo "expected bare=$2 worktree=$3"
21 echo " got bare=$bare worktree=$worktree"
22 return 1
23 }
24}
25
26test_expect_success 'plain' '
27 (
6adcca3f
JH
28 mkdir plain &&
29 cd plain &&
30 git init
31 ) &&
32 check_config plain/.git false unset
33'
34
4ad8332e
JN
35test_expect_success 'plain nested in bare' '
36 (
4ad8332e
JN
37 git init --bare bare-ancestor.git &&
38 cd bare-ancestor.git &&
39 mkdir plain-nested &&
40 cd plain-nested &&
41 git init
42 ) &&
43 check_config bare-ancestor.git/plain-nested/.git false unset
44'
45
46test_expect_success 'plain through aliased command, outside any git repo' '
47 (
4ad8332e
JN
48 HOME=$(pwd)/alias-config &&
49 export HOME &&
50 mkdir alias-config &&
51 echo "[alias] aliasedinit = init" >alias-config/.gitconfig &&
52
53 GIT_CEILING_DIRECTORIES=$(pwd) &&
54 export GIT_CEILING_DIRECTORIES &&
55
56 mkdir plain-aliased &&
57 cd plain-aliased &&
58 git aliasedinit
59 ) &&
60 check_config plain-aliased/.git false unset
61'
62
63test_expect_failure 'plain nested through aliased command' '
64 (
4ad8332e
JN
65 git init plain-ancestor-aliased &&
66 cd plain-ancestor-aliased &&
67 echo "[alias] aliasedinit = init" >>.git/config &&
68 mkdir plain-nested &&
69 cd plain-nested &&
70 git aliasedinit
71 ) &&
72 check_config plain-ancestor-aliased/plain-nested/.git false unset
73'
74
75test_expect_failure 'plain nested in bare through aliased command' '
76 (
4ad8332e
JN
77 git init --bare bare-ancestor-aliased.git &&
78 cd bare-ancestor-aliased.git &&
79 echo "[alias] aliasedinit = init" >>config &&
80 mkdir plain-nested &&
81 cd plain-nested &&
82 git aliasedinit
83 ) &&
84 check_config bare-ancestor-aliased.git/plain-nested/.git false unset
6adcca3f
JH
85'
86
87test_expect_success 'plain with GIT_WORK_TREE' '
0981140f
JK
88 mkdir plain-wt &&
89 test_must_fail env GIT_WORK_TREE="$(pwd)/plain-wt" git init plain-wt
6adcca3f
JH
90'
91
92test_expect_success 'plain bare' '
93 (
6adcca3f
JH
94 mkdir plain-bare-1 &&
95 cd plain-bare-1 &&
96 git --bare init
97 ) &&
98 check_config plain-bare-1 true unset
99'
100
101test_expect_success 'plain bare with GIT_WORK_TREE' '
0981140f
JK
102 mkdir plain-bare-2 &&
103 test_must_fail \
104 env GIT_WORK_TREE="$(pwd)/plain-bare-2" \
105 git --bare init plain-bare-2
6adcca3f
JH
106'
107
108test_expect_success 'GIT_DIR bare' '
109
110 (
6adcca3f
JH
111 mkdir git-dir-bare.git &&
112 GIT_DIR=git-dir-bare.git git init
113 ) &&
114 check_config git-dir-bare.git true unset
115'
116
74d3b23f
LR
117test_expect_success 'init --bare' '
118
119 (
b6138273
MV
120 mkdir init-bare.git &&
121 cd init-bare.git &&
74d3b23f
LR
122 git init --bare
123 ) &&
b6138273 124 check_config init-bare.git true unset
74d3b23f
LR
125'
126
6adcca3f
JH
127test_expect_success 'GIT_DIR non-bare' '
128
129 (
6adcca3f
JH
130 mkdir non-bare &&
131 cd non-bare &&
132 GIT_DIR=.git git init
133 ) &&
134 check_config non-bare/.git false unset
135'
136
137test_expect_success 'GIT_DIR & GIT_WORK_TREE (1)' '
138
139 (
6adcca3f
JH
140 mkdir git-dir-wt-1.git &&
141 GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-1.git git init
142 ) &&
143 check_config git-dir-wt-1.git false "$(pwd)"
144'
145
146test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
0981140f
JK
147 mkdir git-dir-wt-2.git &&
148 test_must_fail env \
149 GIT_WORK_TREE="$(pwd)" \
150 GIT_DIR=git-dir-wt-2.git \
151 git --bare init
6adcca3f
JH
152'
153
127df8c6 154test_expect_success 'reinit' '
5cc8f372
JS
155
156 (
5cc8f372
JS
157 mkdir again &&
158 cd again &&
159 git init >out1 2>err1 &&
160 git init >out2 2>err2
161 ) &&
127df8c6
JH
162 test_i18ngrep "Initialized empty" again/out1 &&
163 test_i18ngrep "Reinitialized existing" again/out2 &&
5cc8f372 164 >again/empty &&
127df8c6
JH
165 test_i18ncmp again/empty again/err1 &&
166 test_i18ncmp again/empty again/err2
5cc8f372
JS
167'
168
172035f0
JK
169test_expect_success 'init with --template' '
170 mkdir template-source &&
171 echo content >template-source/file &&
172 (
173 mkdir template-custom &&
174 cd template-custom &&
175 git init --template=../template-source
176 ) &&
177 test_cmp template-source/file template-custom/.git/file
178'
179
180test_expect_success 'init with --template (blank)' '
181 (
182 mkdir template-plain &&
183 cd template-plain &&
184 git init
185 ) &&
633734d4 186 test_path_is_file template-plain/.git/info/exclude &&
172035f0
JK
187 (
188 mkdir template-blank &&
189 cd template-blank &&
190 git init --template=
191 ) &&
633734d4 192 test_path_is_missing template-blank/.git/info/exclude
172035f0
JK
193'
194
a94d305b
SD
195test_expect_success 'init with init.templatedir set' '
196 mkdir templatedir-source &&
197 echo Content >templatedir-source/file &&
2a472410 198 test_config_global init.templatedir "${HOME}/templatedir-source" &&
a94d305b 199 (
a94d305b
SD
200 mkdir templatedir-set &&
201 cd templatedir-set &&
00648ba0 202 sane_unset GIT_TEMPLATE_DIR &&
a94d305b
SD
203 NO_SET_GIT_TEMPLATE_DIR=t &&
204 export NO_SET_GIT_TEMPLATE_DIR &&
205 git init
206 ) &&
207 test_cmp templatedir-source/file templatedir-set/.git/file
208'
209
0a2c7eea 210test_expect_success 'init --bare/--shared overrides system/global config' '
2a472410
JK
211 test_config_global core.bare false &&
212 test_config_global core.sharedRepository 0640 &&
0a2c7eea 213 (
0a2c7eea
DM
214 mkdir init-bare-shared-override &&
215 cd init-bare-shared-override &&
216 git init --bare --shared=0666
217 ) &&
218 check_config init-bare-shared-override true unset &&
219 test x0666 = \
220 x`git config -f init-bare-shared-override/config core.sharedRepository`
221'
222
223test_expect_success 'init honors global core.sharedRepository' '
2a472410 224 test_config_global core.sharedRepository 0666 &&
0a2c7eea 225 (
0a2c7eea
DM
226 mkdir shared-honor-global &&
227 cd shared-honor-global &&
228 git init
229 ) &&
230 test x0666 = \
231 x`git config -f shared-honor-global/.git/config core.sharedRepository`
232'
233
32d1776b
FL
234test_expect_success 'init rejects insanely long --template' '
235 (
236 insane=$(printf "x%09999dx" 1) &&
237 mkdir test &&
238 cd test &&
239 test_must_fail git init --template=$insane
240 )
241'
242
53d48885
NS
243test_expect_success 'init creates a new directory' '
244 rm -fr newdir &&
245 (
246 git init newdir &&
633734d4 247 test_path_is_dir newdir/.git/refs
53d48885
NS
248 )
249'
250
251test_expect_success 'init creates a new bare directory' '
252 rm -fr newdir &&
253 (
254 git init --bare newdir &&
633734d4 255 test_path_is_dir newdir/refs
53d48885
NS
256 )
257'
258
259test_expect_success 'init recreates a directory' '
260 rm -fr newdir &&
261 (
262 mkdir newdir &&
263 git init newdir &&
633734d4 264 test_path_is_dir newdir/.git/refs
53d48885
NS
265 )
266'
267
268test_expect_success 'init recreates a new bare directory' '
269 rm -fr newdir &&
270 (
271 mkdir newdir &&
272 git init --bare newdir &&
633734d4 273 test_path_is_dir newdir/refs
53d48885
NS
274 )
275'
276
277test_expect_success 'init creates a new deep directory' '
d82e75e8
JS
278 rm -fr newdir &&
279 git init newdir/a/b/c &&
633734d4 280 test_path_is_dir newdir/a/b/c/.git/refs
d82e75e8
JS
281'
282
283test_expect_success POSIXPERM 'init creates a new deep directory (umask vs. shared)' '
53d48885
NS
284 rm -fr newdir &&
285 (
286 # Leading directories should honor umask while
287 # the repository itself should follow "shared"
288 umask 002 &&
289 git init --bare --shared=0660 newdir/a/b/c &&
633734d4 290 test_path_is_dir newdir/a/b/c/refs &&
53d48885 291 ls -ld newdir/a newdir/a/b > lsab.out &&
7d53a07a 292 ! grep -v "^drwxrw[sx]r-x" lsab.out &&
53d48885
NS
293 ls -ld newdir/a/b/c > lsc.out &&
294 ! grep -v "^drwxrw[sx]---" lsc.out
295 )
296'
297
298test_expect_success 'init notices EEXIST (1)' '
299 rm -fr newdir &&
300 (
301 >newdir &&
302 test_must_fail git init newdir &&
633734d4 303 test_path_is_file newdir
53d48885
NS
304 )
305'
306
307test_expect_success 'init notices EEXIST (2)' '
308 rm -fr newdir &&
309 (
310 mkdir newdir &&
311 >newdir/a
312 test_must_fail git init newdir/a/b &&
633734d4 313 test_path_is_file newdir/a
53d48885
NS
314 )
315'
316
c91cfd19 317test_expect_success POSIXPERM,SANITY 'init notices EPERM' '
53d48885
NS
318 rm -fr newdir &&
319 (
320 mkdir newdir &&
321 chmod -w newdir &&
322 test_must_fail git init newdir/a/b
323 )
324'
325
87a074df
JK
326test_expect_success 'init creates a new bare directory with global --bare' '
327 rm -rf newdir &&
328 git --bare init newdir &&
633734d4 329 test_path_is_dir newdir/refs
87a074df
JK
330'
331
332test_expect_success 'init prefers command line to GIT_DIR' '
333 rm -rf newdir &&
334 mkdir otherdir &&
335 GIT_DIR=otherdir git --bare init newdir &&
633734d4
JK
336 test_path_is_dir newdir/refs &&
337 test_path_is_missing otherdir/refs
87a074df
JK
338'
339
b57fb80a
NTND
340test_expect_success 'init with separate gitdir' '
341 rm -rf newdir &&
342 git init --separate-git-dir realgitdir newdir &&
343 echo "gitdir: `pwd`/realgitdir" >expected &&
344 test_cmp expected newdir/.git &&
633734d4 345 test_path_is_dir realgitdir/refs
b57fb80a
NTND
346'
347
487a2b73
NTND
348test_expect_success 're-init on .git file' '
349 ( cd newdir && git init )
350'
351
b57fb80a
NTND
352test_expect_success 're-init to update git link' '
353 (
354 cd newdir &&
355 git init --separate-git-dir ../surrealgitdir
356 ) &&
357 echo "gitdir: `pwd`/surrealgitdir" >expected &&
358 test_cmp expected newdir/.git &&
633734d4
JK
359 test_path_is_dir surrealgitdir/refs &&
360 test_path_is_missing realgitdir/refs
b57fb80a
NTND
361'
362
363test_expect_success 're-init to move gitdir' '
364 rm -rf newdir realgitdir surrealgitdir &&
365 git init newdir &&
366 (
367 cd newdir &&
368 git init --separate-git-dir ../realgitdir
369 ) &&
370 echo "gitdir: `pwd`/realgitdir" >expected &&
371 test_cmp expected newdir/.git &&
633734d4 372 test_path_is_dir realgitdir/refs
b57fb80a
NTND
373'
374
4e95fb6c 375test_expect_success SYMLINKS 're-init to move gitdir symlink' '
b57fb80a
NTND
376 rm -rf newdir realgitdir &&
377 git init newdir &&
378 (
379 cd newdir &&
380 mv .git here &&
381 ln -s here .git &&
09ffc706 382 git init --separate-git-dir ../realgitdir
b57fb80a
NTND
383 ) &&
384 echo "gitdir: `pwd`/realgitdir" >expected &&
385 test_cmp expected newdir/.git &&
3d06c5f1 386 test_cmp expected newdir/here &&
633734d4 387 test_path_is_dir realgitdir/refs
b57fb80a
NTND
388'
389
6adcca3f 390test_done