t0001: drop useless subshells
[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' '
99e1c736
JK
109 mkdir git-dir-bare.git &&
110 GIT_DIR=git-dir-bare.git git init &&
6adcca3f
JH
111 check_config git-dir-bare.git true unset
112'
113
74d3b23f
LR
114test_expect_success 'init --bare' '
115
116 (
b6138273
MV
117 mkdir init-bare.git &&
118 cd init-bare.git &&
74d3b23f
LR
119 git init --bare
120 ) &&
b6138273 121 check_config init-bare.git true unset
74d3b23f
LR
122'
123
6adcca3f
JH
124test_expect_success 'GIT_DIR non-bare' '
125
126 (
6adcca3f
JH
127 mkdir non-bare &&
128 cd non-bare &&
129 GIT_DIR=.git git init
130 ) &&
131 check_config non-bare/.git false unset
132'
133
134test_expect_success 'GIT_DIR & GIT_WORK_TREE (1)' '
135
136 (
6adcca3f
JH
137 mkdir git-dir-wt-1.git &&
138 GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-1.git git init
139 ) &&
140 check_config git-dir-wt-1.git false "$(pwd)"
141'
142
143test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
0981140f
JK
144 mkdir git-dir-wt-2.git &&
145 test_must_fail env \
146 GIT_WORK_TREE="$(pwd)" \
147 GIT_DIR=git-dir-wt-2.git \
148 git --bare init
6adcca3f
JH
149'
150
127df8c6 151test_expect_success 'reinit' '
5cc8f372
JS
152
153 (
5cc8f372
JS
154 mkdir again &&
155 cd again &&
156 git init >out1 2>err1 &&
157 git init >out2 2>err2
158 ) &&
127df8c6
JH
159 test_i18ngrep "Initialized empty" again/out1 &&
160 test_i18ngrep "Reinitialized existing" again/out2 &&
5cc8f372 161 >again/empty &&
127df8c6
JH
162 test_i18ncmp again/empty again/err1 &&
163 test_i18ncmp again/empty again/err2
5cc8f372
JS
164'
165
172035f0
JK
166test_expect_success 'init with --template' '
167 mkdir template-source &&
168 echo content >template-source/file &&
169 (
170 mkdir template-custom &&
171 cd template-custom &&
172 git init --template=../template-source
173 ) &&
174 test_cmp template-source/file template-custom/.git/file
175'
176
177test_expect_success 'init with --template (blank)' '
178 (
179 mkdir template-plain &&
180 cd template-plain &&
181 git init
182 ) &&
633734d4 183 test_path_is_file template-plain/.git/info/exclude &&
172035f0
JK
184 (
185 mkdir template-blank &&
186 cd template-blank &&
187 git init --template=
188 ) &&
633734d4 189 test_path_is_missing template-blank/.git/info/exclude
172035f0
JK
190'
191
a94d305b
SD
192test_expect_success 'init with init.templatedir set' '
193 mkdir templatedir-source &&
194 echo Content >templatedir-source/file &&
2a472410 195 test_config_global init.templatedir "${HOME}/templatedir-source" &&
a94d305b 196 (
a94d305b
SD
197 mkdir templatedir-set &&
198 cd templatedir-set &&
00648ba0 199 sane_unset GIT_TEMPLATE_DIR &&
a94d305b
SD
200 NO_SET_GIT_TEMPLATE_DIR=t &&
201 export NO_SET_GIT_TEMPLATE_DIR &&
202 git init
203 ) &&
204 test_cmp templatedir-source/file templatedir-set/.git/file
205'
206
0a2c7eea 207test_expect_success 'init --bare/--shared overrides system/global config' '
2a472410
JK
208 test_config_global core.bare false &&
209 test_config_global core.sharedRepository 0640 &&
0a2c7eea 210 (
0a2c7eea
DM
211 mkdir init-bare-shared-override &&
212 cd init-bare-shared-override &&
213 git init --bare --shared=0666
214 ) &&
215 check_config init-bare-shared-override true unset &&
216 test x0666 = \
217 x`git config -f init-bare-shared-override/config core.sharedRepository`
218'
219
220test_expect_success 'init honors global core.sharedRepository' '
2a472410 221 test_config_global core.sharedRepository 0666 &&
0a2c7eea 222 (
0a2c7eea
DM
223 mkdir shared-honor-global &&
224 cd shared-honor-global &&
225 git init
226 ) &&
227 test x0666 = \
228 x`git config -f shared-honor-global/.git/config core.sharedRepository`
229'
230
32d1776b
FL
231test_expect_success 'init rejects insanely long --template' '
232 (
233 insane=$(printf "x%09999dx" 1) &&
234 mkdir test &&
235 cd test &&
236 test_must_fail git init --template=$insane
237 )
238'
239
53d48885
NS
240test_expect_success 'init creates a new directory' '
241 rm -fr newdir &&
99e1c736
JK
242 git init newdir &&
243 test_path_is_dir newdir/.git/refs
53d48885
NS
244'
245
246test_expect_success 'init creates a new bare directory' '
247 rm -fr newdir &&
99e1c736
JK
248 git init --bare newdir &&
249 test_path_is_dir newdir/refs
53d48885
NS
250'
251
252test_expect_success 'init recreates a directory' '
253 rm -fr newdir &&
99e1c736
JK
254 mkdir newdir &&
255 git init newdir &&
256 test_path_is_dir newdir/.git/refs
53d48885
NS
257'
258
259test_expect_success 'init recreates a new bare directory' '
260 rm -fr newdir &&
99e1c736
JK
261 mkdir newdir &&
262 git init --bare newdir &&
263 test_path_is_dir newdir/refs
53d48885
NS
264'
265
266test_expect_success 'init creates a new deep directory' '
d82e75e8
JS
267 rm -fr newdir &&
268 git init newdir/a/b/c &&
633734d4 269 test_path_is_dir newdir/a/b/c/.git/refs
d82e75e8
JS
270'
271
272test_expect_success POSIXPERM 'init creates a new deep directory (umask vs. shared)' '
53d48885
NS
273 rm -fr newdir &&
274 (
275 # Leading directories should honor umask while
276 # the repository itself should follow "shared"
277 umask 002 &&
278 git init --bare --shared=0660 newdir/a/b/c &&
633734d4 279 test_path_is_dir newdir/a/b/c/refs &&
53d48885 280 ls -ld newdir/a newdir/a/b > lsab.out &&
7d53a07a 281 ! grep -v "^drwxrw[sx]r-x" lsab.out &&
53d48885
NS
282 ls -ld newdir/a/b/c > lsc.out &&
283 ! grep -v "^drwxrw[sx]---" lsc.out
284 )
285'
286
287test_expect_success 'init notices EEXIST (1)' '
288 rm -fr newdir &&
99e1c736
JK
289 >newdir &&
290 test_must_fail git init newdir &&
291 test_path_is_file newdir
53d48885
NS
292'
293
294test_expect_success 'init notices EEXIST (2)' '
295 rm -fr newdir &&
99e1c736
JK
296 mkdir newdir &&
297 >newdir/a &&
298 test_must_fail git init newdir/a/b &&
299 test_path_is_file newdir/a
53d48885
NS
300'
301
c91cfd19 302test_expect_success POSIXPERM,SANITY 'init notices EPERM' '
53d48885 303 rm -fr newdir &&
99e1c736
JK
304 mkdir newdir &&
305 chmod -w newdir &&
306 test_must_fail git init newdir/a/b
53d48885
NS
307'
308
87a074df
JK
309test_expect_success 'init creates a new bare directory with global --bare' '
310 rm -rf newdir &&
311 git --bare init newdir &&
633734d4 312 test_path_is_dir newdir/refs
87a074df
JK
313'
314
315test_expect_success 'init prefers command line to GIT_DIR' '
316 rm -rf newdir &&
317 mkdir otherdir &&
318 GIT_DIR=otherdir git --bare init newdir &&
633734d4
JK
319 test_path_is_dir newdir/refs &&
320 test_path_is_missing otherdir/refs
87a074df
JK
321'
322
b57fb80a
NTND
323test_expect_success 'init with separate gitdir' '
324 rm -rf newdir &&
325 git init --separate-git-dir realgitdir newdir &&
326 echo "gitdir: `pwd`/realgitdir" >expected &&
327 test_cmp expected newdir/.git &&
633734d4 328 test_path_is_dir realgitdir/refs
b57fb80a
NTND
329'
330
487a2b73
NTND
331test_expect_success 're-init on .git file' '
332 ( cd newdir && git init )
333'
334
b57fb80a
NTND
335test_expect_success 're-init to update git link' '
336 (
337 cd newdir &&
338 git init --separate-git-dir ../surrealgitdir
339 ) &&
340 echo "gitdir: `pwd`/surrealgitdir" >expected &&
341 test_cmp expected newdir/.git &&
633734d4
JK
342 test_path_is_dir surrealgitdir/refs &&
343 test_path_is_missing realgitdir/refs
b57fb80a
NTND
344'
345
346test_expect_success 're-init to move gitdir' '
347 rm -rf newdir realgitdir surrealgitdir &&
348 git init newdir &&
349 (
350 cd newdir &&
351 git init --separate-git-dir ../realgitdir
352 ) &&
353 echo "gitdir: `pwd`/realgitdir" >expected &&
354 test_cmp expected newdir/.git &&
633734d4 355 test_path_is_dir realgitdir/refs
b57fb80a
NTND
356'
357
4e95fb6c 358test_expect_success SYMLINKS 're-init to move gitdir symlink' '
b57fb80a
NTND
359 rm -rf newdir realgitdir &&
360 git init newdir &&
361 (
362 cd newdir &&
363 mv .git here &&
364 ln -s here .git &&
09ffc706 365 git init --separate-git-dir ../realgitdir
b57fb80a
NTND
366 ) &&
367 echo "gitdir: `pwd`/realgitdir" >expected &&
368 test_cmp expected newdir/.git &&
3d06c5f1 369 test_cmp expected newdir/here &&
633734d4 370 test_path_is_dir realgitdir/refs
b57fb80a
NTND
371'
372
6adcca3f 373test_done