]>
Commit | Line | Data |
---|---|---|
6adcca3f JH |
1 | #!/bin/sh |
2 | ||
3 | test_description='git init' | |
4 | ||
5 | . ./test-lib.sh | |
6 | ||
7 | check_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 | |
1f32ecff MH |
15 | |
16 | if test_have_prereq POSIXPERM && test -x "$1/config" | |
17 | then | |
18 | echo "$1/config is executable?" | |
19 | return 1 | |
20 | fi | |
21 | ||
3cc6a6f0 JK |
22 | bare=$(cd "$1" && git config --bool core.bare) |
23 | worktree=$(cd "$1" && git config core.worktree) || | |
6adcca3f JH |
24 | worktree=unset |
25 | ||
26 | test "$bare" = "$2" && test "$worktree" = "$3" || { | |
27 | echo "expected bare=$2 worktree=$3" | |
28 | echo " got bare=$bare worktree=$worktree" | |
29 | return 1 | |
30 | } | |
31 | } | |
32 | ||
33 | test_expect_success 'plain' ' | |
410c3428 | 34 | git init plain && |
6adcca3f JH |
35 | check_config plain/.git false unset |
36 | ' | |
37 | ||
4ad8332e JN |
38 | test_expect_success 'plain nested in bare' ' |
39 | ( | |
4ad8332e JN |
40 | git init --bare bare-ancestor.git && |
41 | cd bare-ancestor.git && | |
42 | mkdir plain-nested && | |
43 | cd plain-nested && | |
44 | git init | |
45 | ) && | |
46 | check_config bare-ancestor.git/plain-nested/.git false unset | |
47 | ' | |
48 | ||
49 | test_expect_success 'plain through aliased command, outside any git repo' ' | |
50 | ( | |
4ad8332e JN |
51 | HOME=$(pwd)/alias-config && |
52 | export HOME && | |
53 | mkdir alias-config && | |
54 | echo "[alias] aliasedinit = init" >alias-config/.gitconfig && | |
55 | ||
56 | GIT_CEILING_DIRECTORIES=$(pwd) && | |
57 | export GIT_CEILING_DIRECTORIES && | |
58 | ||
59 | mkdir plain-aliased && | |
60 | cd plain-aliased && | |
61 | git aliasedinit | |
62 | ) && | |
63 | check_config plain-aliased/.git false unset | |
64 | ' | |
65 | ||
c0562611 | 66 | test_expect_success 'plain nested through aliased command' ' |
4ad8332e | 67 | ( |
4ad8332e JN |
68 | git init plain-ancestor-aliased && |
69 | cd plain-ancestor-aliased && | |
70 | echo "[alias] aliasedinit = init" >>.git/config && | |
71 | mkdir plain-nested && | |
72 | cd plain-nested && | |
73 | git aliasedinit | |
74 | ) && | |
75 | check_config plain-ancestor-aliased/plain-nested/.git false unset | |
76 | ' | |
77 | ||
c0562611 | 78 | test_expect_success 'plain nested in bare through aliased command' ' |
4ad8332e | 79 | ( |
4ad8332e JN |
80 | git init --bare bare-ancestor-aliased.git && |
81 | cd bare-ancestor-aliased.git && | |
82 | echo "[alias] aliasedinit = init" >>config && | |
83 | mkdir plain-nested && | |
84 | cd plain-nested && | |
85 | git aliasedinit | |
86 | ) && | |
87 | check_config bare-ancestor-aliased.git/plain-nested/.git false unset | |
6adcca3f JH |
88 | ' |
89 | ||
57ea7123 | 90 | test_expect_success 'No extra GIT_* on alias scripts' ' |
f3858f8e JS |
91 | write_script script <<-\EOF && |
92 | env | | |
93 | sed -n \ | |
94 | -e "/^GIT_PREFIX=/d" \ | |
95 | -e "/^GIT_TEXTDOMAINDIR=/d" \ | |
e4b75d6a | 96 | -e "/^GIT_TRACE2_PARENT/d" \ |
f3858f8e JS |
97 | -e "/^GIT_/s/=.*//p" | |
98 | sort | |
57ea7123 | 99 | EOF |
f3858f8e | 100 | ./script >expected && |
57ea7123 | 101 | git config alias.script \!./script && |
f3858f8e | 102 | ( mkdir sub && cd sub && git script >../actual ) && |
57ea7123 NTND |
103 | test_cmp expected actual |
104 | ' | |
105 | ||
6adcca3f | 106 | test_expect_success 'plain with GIT_WORK_TREE' ' |
0981140f JK |
107 | mkdir plain-wt && |
108 | test_must_fail env GIT_WORK_TREE="$(pwd)/plain-wt" git init plain-wt | |
6adcca3f JH |
109 | ' |
110 | ||
111 | test_expect_success 'plain bare' ' | |
410c3428 | 112 | git --bare init plain-bare-1 && |
6adcca3f JH |
113 | check_config plain-bare-1 true unset |
114 | ' | |
115 | ||
116 | test_expect_success 'plain bare with GIT_WORK_TREE' ' | |
0981140f JK |
117 | mkdir plain-bare-2 && |
118 | test_must_fail \ | |
119 | env GIT_WORK_TREE="$(pwd)/plain-bare-2" \ | |
120 | git --bare init plain-bare-2 | |
6adcca3f JH |
121 | ' |
122 | ||
123 | test_expect_success 'GIT_DIR bare' ' | |
99e1c736 JK |
124 | mkdir git-dir-bare.git && |
125 | GIT_DIR=git-dir-bare.git git init && | |
6adcca3f JH |
126 | check_config git-dir-bare.git true unset |
127 | ' | |
128 | ||
74d3b23f | 129 | test_expect_success 'init --bare' ' |
410c3428 | 130 | git init --bare init-bare.git && |
b6138273 | 131 | check_config init-bare.git true unset |
74d3b23f LR |
132 | ' |
133 | ||
6adcca3f JH |
134 | test_expect_success 'GIT_DIR non-bare' ' |
135 | ||
136 | ( | |
6adcca3f JH |
137 | mkdir non-bare && |
138 | cd non-bare && | |
139 | GIT_DIR=.git git init | |
140 | ) && | |
141 | check_config non-bare/.git false unset | |
142 | ' | |
143 | ||
144 | test_expect_success 'GIT_DIR & GIT_WORK_TREE (1)' ' | |
145 | ||
146 | ( | |
6adcca3f JH |
147 | mkdir git-dir-wt-1.git && |
148 | GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-1.git git init | |
149 | ) && | |
150 | check_config git-dir-wt-1.git false "$(pwd)" | |
151 | ' | |
152 | ||
153 | test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' ' | |
0981140f JK |
154 | mkdir git-dir-wt-2.git && |
155 | test_must_fail env \ | |
156 | GIT_WORK_TREE="$(pwd)" \ | |
157 | GIT_DIR=git-dir-wt-2.git \ | |
158 | git --bare init | |
6adcca3f JH |
159 | ' |
160 | ||
127df8c6 | 161 | test_expect_success 'reinit' ' |
5cc8f372 JS |
162 | |
163 | ( | |
5cc8f372 JS |
164 | mkdir again && |
165 | cd again && | |
166 | git init >out1 2>err1 && | |
167 | git init >out2 2>err2 | |
168 | ) && | |
127df8c6 JH |
169 | test_i18ngrep "Initialized empty" again/out1 && |
170 | test_i18ngrep "Reinitialized existing" again/out2 && | |
1c5e94f4 SG |
171 | test_must_be_empty again/err1 && |
172 | test_must_be_empty again/err2 | |
5cc8f372 JS |
173 | ' |
174 | ||
172035f0 JK |
175 | test_expect_success 'init with --template' ' |
176 | mkdir template-source && | |
177 | echo content >template-source/file && | |
e1df7fe4 | 178 | git init --template=template-source template-custom && |
172035f0 JK |
179 | test_cmp template-source/file template-custom/.git/file |
180 | ' | |
181 | ||
182 | test_expect_success 'init with --template (blank)' ' | |
410c3428 | 183 | git init template-plain && |
633734d4 | 184 | test_path_is_file template-plain/.git/info/exclude && |
410c3428 | 185 | git init --template= template-blank && |
633734d4 | 186 | test_path_is_missing template-blank/.git/info/exclude |
172035f0 JK |
187 | ' |
188 | ||
a94d305b SD |
189 | test_expect_success 'init with init.templatedir set' ' |
190 | mkdir templatedir-source && | |
191 | echo Content >templatedir-source/file && | |
2a472410 | 192 | test_config_global init.templatedir "${HOME}/templatedir-source" && |
a94d305b | 193 | ( |
a94d305b SD |
194 | mkdir templatedir-set && |
195 | cd templatedir-set && | |
00648ba0 | 196 | sane_unset GIT_TEMPLATE_DIR && |
a94d305b SD |
197 | NO_SET_GIT_TEMPLATE_DIR=t && |
198 | export NO_SET_GIT_TEMPLATE_DIR && | |
199 | git init | |
200 | ) && | |
201 | test_cmp templatedir-source/file templatedir-set/.git/file | |
202 | ' | |
203 | ||
0a2c7eea | 204 | test_expect_success 'init --bare/--shared overrides system/global config' ' |
2a472410 JK |
205 | test_config_global core.bare false && |
206 | test_config_global core.sharedRepository 0640 && | |
410c3428 | 207 | git init --bare --shared=0666 init-bare-shared-override && |
0a2c7eea DM |
208 | check_config init-bare-shared-override true unset && |
209 | test x0666 = \ | |
88619b3e | 210 | x$(git config -f init-bare-shared-override/config core.sharedRepository) |
0a2c7eea DM |
211 | ' |
212 | ||
213 | test_expect_success 'init honors global core.sharedRepository' ' | |
2a472410 | 214 | test_config_global core.sharedRepository 0666 && |
410c3428 | 215 | git init shared-honor-global && |
0a2c7eea | 216 | test x0666 = \ |
88619b3e | 217 | x$(git config -f shared-honor-global/.git/config core.sharedRepository) |
0a2c7eea DM |
218 | ' |
219 | ||
9c28390b JK |
220 | test_expect_success 'init allows insanely long --template' ' |
221 | git init --template=$(printf "x%09999dx" 1) test | |
32d1776b FL |
222 | ' |
223 | ||
53d48885 NS |
224 | test_expect_success 'init creates a new directory' ' |
225 | rm -fr newdir && | |
99e1c736 JK |
226 | git init newdir && |
227 | test_path_is_dir newdir/.git/refs | |
53d48885 NS |
228 | ' |
229 | ||
230 | test_expect_success 'init creates a new bare directory' ' | |
231 | rm -fr newdir && | |
99e1c736 JK |
232 | git init --bare newdir && |
233 | test_path_is_dir newdir/refs | |
53d48885 NS |
234 | ' |
235 | ||
236 | test_expect_success 'init recreates a directory' ' | |
237 | rm -fr newdir && | |
99e1c736 JK |
238 | mkdir newdir && |
239 | git init newdir && | |
240 | test_path_is_dir newdir/.git/refs | |
53d48885 NS |
241 | ' |
242 | ||
243 | test_expect_success 'init recreates a new bare directory' ' | |
244 | rm -fr newdir && | |
99e1c736 JK |
245 | mkdir newdir && |
246 | git init --bare newdir && | |
247 | test_path_is_dir newdir/refs | |
53d48885 NS |
248 | ' |
249 | ||
250 | test_expect_success 'init creates a new deep directory' ' | |
d82e75e8 JS |
251 | rm -fr newdir && |
252 | git init newdir/a/b/c && | |
633734d4 | 253 | test_path_is_dir newdir/a/b/c/.git/refs |
d82e75e8 JS |
254 | ' |
255 | ||
256 | test_expect_success POSIXPERM 'init creates a new deep directory (umask vs. shared)' ' | |
53d48885 NS |
257 | rm -fr newdir && |
258 | ( | |
259 | # Leading directories should honor umask while | |
260 | # the repository itself should follow "shared" | |
d549d213 MM |
261 | mkdir newdir && |
262 | # Remove a default ACL if possible. | |
263 | (setfacl -k newdir 2>/dev/null || true) && | |
53d48885 NS |
264 | umask 002 && |
265 | git init --bare --shared=0660 newdir/a/b/c && | |
633734d4 | 266 | test_path_is_dir newdir/a/b/c/refs && |
53d48885 | 267 | ls -ld newdir/a newdir/a/b > lsab.out && |
7d53a07a | 268 | ! grep -v "^drwxrw[sx]r-x" lsab.out && |
53d48885 NS |
269 | ls -ld newdir/a/b/c > lsc.out && |
270 | ! grep -v "^drwxrw[sx]---" lsc.out | |
271 | ) | |
272 | ' | |
273 | ||
274 | test_expect_success 'init notices EEXIST (1)' ' | |
275 | rm -fr newdir && | |
99e1c736 JK |
276 | >newdir && |
277 | test_must_fail git init newdir && | |
278 | test_path_is_file newdir | |
53d48885 NS |
279 | ' |
280 | ||
281 | test_expect_success 'init notices EEXIST (2)' ' | |
282 | rm -fr newdir && | |
99e1c736 JK |
283 | mkdir newdir && |
284 | >newdir/a && | |
285 | test_must_fail git init newdir/a/b && | |
286 | test_path_is_file newdir/a | |
53d48885 NS |
287 | ' |
288 | ||
c91cfd19 | 289 | test_expect_success POSIXPERM,SANITY 'init notices EPERM' ' |
03771425 | 290 | test_when_finished "chmod +w newdir" && |
53d48885 | 291 | rm -fr newdir && |
99e1c736 JK |
292 | mkdir newdir && |
293 | chmod -w newdir && | |
294 | test_must_fail git init newdir/a/b | |
53d48885 NS |
295 | ' |
296 | ||
87a074df JK |
297 | test_expect_success 'init creates a new bare directory with global --bare' ' |
298 | rm -rf newdir && | |
299 | git --bare init newdir && | |
633734d4 | 300 | test_path_is_dir newdir/refs |
87a074df JK |
301 | ' |
302 | ||
303 | test_expect_success 'init prefers command line to GIT_DIR' ' | |
304 | rm -rf newdir && | |
305 | mkdir otherdir && | |
306 | GIT_DIR=otherdir git --bare init newdir && | |
633734d4 JK |
307 | test_path_is_dir newdir/refs && |
308 | test_path_is_missing otherdir/refs | |
87a074df JK |
309 | ' |
310 | ||
b57fb80a NTND |
311 | test_expect_success 'init with separate gitdir' ' |
312 | rm -rf newdir && | |
313 | git init --separate-git-dir realgitdir newdir && | |
ed33bd8f JS |
314 | newdir_git="$(cat newdir/.git)" && |
315 | test_cmp_fspath "$(pwd)/realgitdir" "${newdir_git#gitdir: }" && | |
633734d4 | 316 | test_path_is_dir realgitdir/refs |
b57fb80a NTND |
317 | ' |
318 | ||
bed67874 RS |
319 | test_lazy_prereq GETCWD_IGNORES_PERMS ' |
320 | base=GETCWD_TEST_BASE_DIR && | |
321 | mkdir -p $base/dir && | |
322 | chmod 100 $base || | |
165293af | 323 | BUG "cannot prepare $base" |
bed67874 RS |
324 | |
325 | (cd $base/dir && /bin/pwd -P) | |
326 | status=$? | |
327 | ||
328 | chmod 700 $base && | |
329 | rm -rf $base || | |
165293af | 330 | BUG "cannot clean $base" |
bed67874 RS |
331 | return $status |
332 | ' | |
333 | ||
334 | check_long_base_path () { | |
a54e938e RS |
335 | # exceed initial buffer size of strbuf_getcwd() |
336 | component=123456789abcdef && | |
337 | test_when_finished "chmod 0700 $component; rm -rf $component" && | |
338 | p31=$component/$component && | |
339 | p127=$p31/$p31/$p31/$p31 && | |
340 | mkdir -p $p127 && | |
bed67874 RS |
341 | if test $# = 1 |
342 | then | |
343 | chmod $1 $component | |
344 | fi && | |
a54e938e RS |
345 | ( |
346 | cd $p127 && | |
347 | git init newdir | |
348 | ) | |
bed67874 RS |
349 | } |
350 | ||
351 | test_expect_success 'init in long base path' ' | |
352 | check_long_base_path | |
353 | ' | |
354 | ||
355 | test_expect_success GETCWD_IGNORES_PERMS 'init in long restricted base path' ' | |
356 | check_long_base_path 0111 | |
a54e938e RS |
357 | ' |
358 | ||
487a2b73 NTND |
359 | test_expect_success 're-init on .git file' ' |
360 | ( cd newdir && git init ) | |
361 | ' | |
362 | ||
b57fb80a | 363 | test_expect_success 're-init to update git link' ' |
ed33bd8f JS |
364 | git -C newdir init --separate-git-dir ../surrealgitdir && |
365 | newdir_git="$(cat newdir/.git)" && | |
366 | test_cmp_fspath "$(pwd)/surrealgitdir" "${newdir_git#gitdir: }" && | |
633734d4 JK |
367 | test_path_is_dir surrealgitdir/refs && |
368 | test_path_is_missing realgitdir/refs | |
b57fb80a NTND |
369 | ' |
370 | ||
371 | test_expect_success 're-init to move gitdir' ' | |
372 | rm -rf newdir realgitdir surrealgitdir && | |
373 | git init newdir && | |
ed33bd8f JS |
374 | git -C newdir init --separate-git-dir ../realgitdir && |
375 | newdir_git="$(cat newdir/.git)" && | |
376 | test_cmp_fspath "$(pwd)/realgitdir" "${newdir_git#gitdir: }" && | |
633734d4 | 377 | test_path_is_dir realgitdir/refs |
b57fb80a NTND |
378 | ' |
379 | ||
4e95fb6c | 380 | test_expect_success SYMLINKS 're-init to move gitdir symlink' ' |
b57fb80a NTND |
381 | rm -rf newdir realgitdir && |
382 | git init newdir && | |
383 | ( | |
384 | cd newdir && | |
385 | mv .git here && | |
386 | ln -s here .git && | |
09ffc706 | 387 | git init --separate-git-dir ../realgitdir |
b57fb80a | 388 | ) && |
88619b3e | 389 | echo "gitdir: $(pwd)/realgitdir" >expected && |
b57fb80a | 390 | test_cmp expected newdir/.git && |
3d06c5f1 | 391 | test_cmp expected newdir/here && |
633734d4 | 392 | test_path_is_dir realgitdir/refs |
b57fb80a NTND |
393 | ' |
394 | ||
f30afdab JS |
395 | # Tests for the hidden file attribute on windows |
396 | is_hidden () { | |
397 | # Use the output of `attrib`, ignore the absolute path | |
398 | case "$(attrib "$1")" in *H*?:*) return 0;; esac | |
399 | return 1 | |
400 | } | |
401 | ||
402 | test_expect_success MINGW '.git hidden' ' | |
403 | rm -rf newdir && | |
404 | ( | |
ed6c994a | 405 | sane_unset GIT_DIR GIT_WORK_TREE && |
f30afdab JS |
406 | mkdir newdir && |
407 | cd newdir && | |
408 | git init && | |
409 | is_hidden .git | |
410 | ) && | |
411 | check_config newdir/.git false unset | |
412 | ' | |
413 | ||
414 | test_expect_success MINGW 'bare git dir not hidden' ' | |
415 | rm -rf newdir && | |
416 | ( | |
ed6c994a | 417 | sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG && |
f30afdab JS |
418 | mkdir newdir && |
419 | cd newdir && | |
420 | git --bare init | |
421 | ) && | |
422 | ! is_hidden newdir | |
423 | ' | |
424 | ||
b9605bc4 JK |
425 | test_expect_success 'remote init from does not use config from cwd' ' |
426 | rm -rf newdir && | |
427 | test_config core.logallrefupdates true && | |
428 | git init newdir && | |
429 | echo true >expect && | |
430 | git -C newdir config --bool core.logallrefupdates >actual && | |
431 | test_cmp expect actual | |
432 | ' | |
433 | ||
fe9aa0b2 NTND |
434 | test_expect_success 're-init from a linked worktree' ' |
435 | git init main-worktree && | |
436 | ( | |
437 | cd main-worktree && | |
438 | test_commit first && | |
439 | git worktree add ../linked-worktree && | |
440 | mv .git/info/exclude expected-exclude && | |
6311cfaf | 441 | cp .git/config expected-config && |
fe9aa0b2 NTND |
442 | find .git/worktrees -print | sort >expected && |
443 | git -C ../linked-worktree init && | |
444 | test_cmp expected-exclude .git/info/exclude && | |
6311cfaf | 445 | test_cmp expected-config .git/config && |
fe9aa0b2 NTND |
446 | find .git/worktrees -print | sort >actual && |
447 | test_cmp expected actual | |
448 | ) | |
449 | ' | |
450 | ||
28785339 JS |
451 | test_expect_success MINGW 'core.hidedotfiles = false' ' |
452 | git config --global core.hidedotfiles false && | |
453 | rm -rf newdir && | |
454 | mkdir newdir && | |
455 | ( | |
456 | sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG && | |
457 | git -C newdir init | |
458 | ) && | |
459 | ! is_hidden newdir/.git | |
460 | ' | |
461 | ||
3f944424 JS |
462 | test_expect_success MINGW 'redirect std handles' ' |
463 | GIT_REDIRECT_STDOUT=output.txt git rev-parse --git-dir && | |
464 | test .git = "$(cat output.txt)" && | |
1a172e4a JS |
465 | test -z "$(GIT_REDIRECT_STDOUT=off git rev-parse --git-dir)" && |
466 | test_must_fail env \ | |
467 | GIT_REDIRECT_STDOUT=output.txt \ | |
468 | GIT_REDIRECT_STDERR="2>&1" \ | |
469 | git rev-parse --git-dir --verify refs/invalid && | |
fdda1ac6 JS |
470 | grep "^\\.git\$" output.txt && |
471 | grep "Needed a single revision" output.txt | |
3f944424 JS |
472 | ' |
473 | ||
6adcca3f | 474 | test_done |