]> git.ipfire.org Git - thirdparty/git.git/blame - t/t0001-init.sh
The sixth batch
[thirdparty/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 () {
d4fe066e
SY
8 if test_path_is_dir "$1" &&
9 test_path_is_file "$1/config" && test_path_is_dir "$1/refs"
6adcca3f
JH
10 then
11 : happy
12 else
13 echo "expected a directory $1, a file $1/config and $1/refs"
14 return 1
15 fi
1f32ecff
MH
16
17 if test_have_prereq POSIXPERM && test -x "$1/config"
18 then
19 echo "$1/config is executable?"
20 return 1
21 fi
22
3cc6a6f0
JK
23 bare=$(cd "$1" && git config --bool core.bare)
24 worktree=$(cd "$1" && git config core.worktree) ||
6adcca3f
JH
25 worktree=unset
26
27 test "$bare" = "$2" && test "$worktree" = "$3" || {
28 echo "expected bare=$2 worktree=$3"
29 echo " got bare=$bare worktree=$worktree"
30 return 1
31 }
32}
33
34test_expect_success 'plain' '
410c3428 35 git init plain &&
6adcca3f
JH
36 check_config plain/.git false unset
37'
38
4ad8332e
JN
39test_expect_success 'plain nested in bare' '
40 (
4ad8332e
JN
41 git init --bare bare-ancestor.git &&
42 cd bare-ancestor.git &&
43 mkdir plain-nested &&
44 cd plain-nested &&
45 git init
46 ) &&
47 check_config bare-ancestor.git/plain-nested/.git false unset
48'
49
50test_expect_success 'plain through aliased command, outside any git repo' '
51 (
4ad8332e
JN
52 HOME=$(pwd)/alias-config &&
53 export HOME &&
54 mkdir alias-config &&
55 echo "[alias] aliasedinit = init" >alias-config/.gitconfig &&
56
57 GIT_CEILING_DIRECTORIES=$(pwd) &&
58 export GIT_CEILING_DIRECTORIES &&
59
60 mkdir plain-aliased &&
61 cd plain-aliased &&
62 git aliasedinit
63 ) &&
64 check_config plain-aliased/.git false unset
65'
66
c0562611 67test_expect_success 'plain nested through aliased command' '
4ad8332e 68 (
4ad8332e
JN
69 git init plain-ancestor-aliased &&
70 cd plain-ancestor-aliased &&
71 echo "[alias] aliasedinit = init" >>.git/config &&
72 mkdir plain-nested &&
73 cd plain-nested &&
74 git aliasedinit
75 ) &&
76 check_config plain-ancestor-aliased/plain-nested/.git false unset
77'
78
c0562611 79test_expect_success 'plain nested in bare through aliased command' '
4ad8332e 80 (
4ad8332e
JN
81 git init --bare bare-ancestor-aliased.git &&
82 cd bare-ancestor-aliased.git &&
83 echo "[alias] aliasedinit = init" >>config &&
84 mkdir plain-nested &&
85 cd plain-nested &&
86 git aliasedinit
87 ) &&
88 check_config bare-ancestor-aliased.git/plain-nested/.git false unset
6adcca3f
JH
89'
90
57ea7123 91test_expect_success 'No extra GIT_* on alias scripts' '
f3858f8e
JS
92 write_script script <<-\EOF &&
93 env |
94 sed -n \
95 -e "/^GIT_PREFIX=/d" \
96 -e "/^GIT_TEXTDOMAINDIR=/d" \
e4b75d6a 97 -e "/^GIT_TRACE2_PARENT/d" \
f3858f8e
JS
98 -e "/^GIT_/s/=.*//p" |
99 sort
57ea7123 100 EOF
f3858f8e 101 ./script >expected &&
57ea7123 102 git config alias.script \!./script &&
f3858f8e 103 ( mkdir sub && cd sub && git script >../actual ) &&
57ea7123
NTND
104 test_cmp expected actual
105'
106
6adcca3f 107test_expect_success 'plain with GIT_WORK_TREE' '
0981140f
JK
108 mkdir plain-wt &&
109 test_must_fail env GIT_WORK_TREE="$(pwd)/plain-wt" git init plain-wt
6adcca3f
JH
110'
111
112test_expect_success 'plain bare' '
410c3428 113 git --bare init plain-bare-1 &&
6adcca3f
JH
114 check_config plain-bare-1 true unset
115'
116
117test_expect_success 'plain bare with GIT_WORK_TREE' '
0981140f
JK
118 mkdir plain-bare-2 &&
119 test_must_fail \
120 env GIT_WORK_TREE="$(pwd)/plain-bare-2" \
121 git --bare init plain-bare-2
6adcca3f
JH
122'
123
124test_expect_success 'GIT_DIR bare' '
99e1c736
JK
125 mkdir git-dir-bare.git &&
126 GIT_DIR=git-dir-bare.git git init &&
6adcca3f
JH
127 check_config git-dir-bare.git true unset
128'
129
74d3b23f 130test_expect_success 'init --bare' '
410c3428 131 git init --bare init-bare.git &&
b6138273 132 check_config init-bare.git true unset
74d3b23f
LR
133'
134
6adcca3f
JH
135test_expect_success 'GIT_DIR non-bare' '
136
137 (
6adcca3f
JH
138 mkdir non-bare &&
139 cd non-bare &&
140 GIT_DIR=.git git init
141 ) &&
142 check_config non-bare/.git false unset
143'
144
145test_expect_success 'GIT_DIR & GIT_WORK_TREE (1)' '
146
147 (
6adcca3f
JH
148 mkdir git-dir-wt-1.git &&
149 GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-1.git git init
150 ) &&
151 check_config git-dir-wt-1.git false "$(pwd)"
152'
153
154test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
0981140f
JK
155 mkdir git-dir-wt-2.git &&
156 test_must_fail env \
157 GIT_WORK_TREE="$(pwd)" \
158 GIT_DIR=git-dir-wt-2.git \
159 git --bare init
6adcca3f
JH
160'
161
127df8c6 162test_expect_success 'reinit' '
5cc8f372
JS
163
164 (
5cc8f372
JS
165 mkdir again &&
166 cd again &&
675704c7 167 git -c init.defaultBranch=initial init >out1 2>err1 &&
5cc8f372
JS
168 git init >out2 2>err2
169 ) &&
6789275d
JH
170 test_grep "Initialized empty" again/out1 &&
171 test_grep "Reinitialized existing" again/out2 &&
1c5e94f4
SG
172 test_must_be_empty again/err1 &&
173 test_must_be_empty again/err2
5cc8f372
JS
174'
175
172035f0
JK
176test_expect_success 'init with --template' '
177 mkdir template-source &&
178 echo content >template-source/file &&
e1df7fe4 179 git init --template=template-source template-custom &&
172035f0
JK
180 test_cmp template-source/file template-custom/.git/file
181'
182
183test_expect_success 'init with --template (blank)' '
410c3428 184 git init template-plain &&
633734d4 185 test_path_is_file template-plain/.git/info/exclude &&
410c3428 186 git init --template= template-blank &&
633734d4 187 test_path_is_missing template-blank/.git/info/exclude
172035f0
JK
188'
189
a185dd58 190init_no_templatedir_env () {
a94d305b 191 (
00648ba0 192 sane_unset GIT_TEMPLATE_DIR &&
a94d305b
SD
193 NO_SET_GIT_TEMPLATE_DIR=t &&
194 export NO_SET_GIT_TEMPLATE_DIR &&
a185dd58
MT
195 git init "$1"
196 )
197}
198
199test_expect_success 'init with init.templatedir set' '
200 mkdir templatedir-source &&
201 echo Content >templatedir-source/file &&
202 test_config_global init.templatedir "${HOME}/templatedir-source" &&
203
204 init_no_templatedir_env templatedir-set &&
a94d305b
SD
205 test_cmp templatedir-source/file templatedir-set/.git/file
206'
207
a185dd58
MT
208test_expect_success 'init with init.templatedir using ~ expansion' '
209 mkdir -p templatedir-source &&
210 echo Content >templatedir-source/file &&
211 test_config_global init.templatedir "~/templatedir-source" &&
212
213 init_no_templatedir_env templatedir-expansion &&
214 test_cmp templatedir-source/file templatedir-expansion/.git/file
215'
216
0a2c7eea 217test_expect_success 'init --bare/--shared overrides system/global config' '
2a472410
JK
218 test_config_global core.bare false &&
219 test_config_global core.sharedRepository 0640 &&
410c3428 220 git init --bare --shared=0666 init-bare-shared-override &&
0a2c7eea
DM
221 check_config init-bare-shared-override true unset &&
222 test x0666 = \
88619b3e 223 x$(git config -f init-bare-shared-override/config core.sharedRepository)
0a2c7eea
DM
224'
225
226test_expect_success 'init honors global core.sharedRepository' '
2a472410 227 test_config_global core.sharedRepository 0666 &&
410c3428 228 git init shared-honor-global &&
0a2c7eea 229 test x0666 = \
88619b3e 230 x$(git config -f shared-honor-global/.git/config core.sharedRepository)
0a2c7eea
DM
231'
232
9c28390b
JK
233test_expect_success 'init allows insanely long --template' '
234 git init --template=$(printf "x%09999dx" 1) test
32d1776b
FL
235'
236
53d48885
NS
237test_expect_success 'init creates a new directory' '
238 rm -fr newdir &&
99e1c736
JK
239 git init newdir &&
240 test_path_is_dir newdir/.git/refs
53d48885
NS
241'
242
243test_expect_success 'init creates a new bare directory' '
244 rm -fr newdir &&
99e1c736
JK
245 git init --bare newdir &&
246 test_path_is_dir newdir/refs
53d48885
NS
247'
248
249test_expect_success 'init recreates a directory' '
250 rm -fr newdir &&
99e1c736
JK
251 mkdir newdir &&
252 git init newdir &&
253 test_path_is_dir newdir/.git/refs
53d48885
NS
254'
255
256test_expect_success 'init recreates a new bare directory' '
257 rm -fr newdir &&
99e1c736
JK
258 mkdir newdir &&
259 git init --bare newdir &&
260 test_path_is_dir newdir/refs
53d48885
NS
261'
262
263test_expect_success 'init creates a new deep directory' '
d82e75e8
JS
264 rm -fr newdir &&
265 git init newdir/a/b/c &&
633734d4 266 test_path_is_dir newdir/a/b/c/.git/refs
d82e75e8
JS
267'
268
269test_expect_success POSIXPERM 'init creates a new deep directory (umask vs. shared)' '
53d48885
NS
270 rm -fr newdir &&
271 (
272 # Leading directories should honor umask while
273 # the repository itself should follow "shared"
d549d213
MM
274 mkdir newdir &&
275 # Remove a default ACL if possible.
276 (setfacl -k newdir 2>/dev/null || true) &&
53d48885
NS
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' '
03771425 303 test_when_finished "chmod +w newdir" &&
53d48885 304 rm -fr newdir &&
99e1c736
JK
305 mkdir newdir &&
306 chmod -w newdir &&
307 test_must_fail git init newdir/a/b
53d48885
NS
308'
309
87a074df
JK
310test_expect_success 'init creates a new bare directory with global --bare' '
311 rm -rf newdir &&
312 git --bare init newdir &&
633734d4 313 test_path_is_dir newdir/refs
87a074df
JK
314'
315
316test_expect_success 'init prefers command line to GIT_DIR' '
317 rm -rf newdir &&
318 mkdir otherdir &&
319 GIT_DIR=otherdir git --bare init newdir &&
633734d4
JK
320 test_path_is_dir newdir/refs &&
321 test_path_is_missing otherdir/refs
87a074df
JK
322'
323
b57fb80a
NTND
324test_expect_success 'init with separate gitdir' '
325 rm -rf newdir &&
326 git init --separate-git-dir realgitdir newdir &&
ed33bd8f
JS
327 newdir_git="$(cat newdir/.git)" &&
328 test_cmp_fspath "$(pwd)/realgitdir" "${newdir_git#gitdir: }" &&
633734d4 329 test_path_is_dir realgitdir/refs
b57fb80a
NTND
330'
331
ccf236a2
ES
332test_expect_success 'explicit bare & --separate-git-dir incompatible' '
333 test_must_fail git init --bare --separate-git-dir goop.git bare.git 2>err &&
6789275d 334 test_grep "cannot be used together" err
ccf236a2
ES
335'
336
337test_expect_success 'implicit bare & --separate-git-dir incompatible' '
338 test_when_finished "rm -rf bare.git" &&
339 mkdir -p bare.git &&
340 test_must_fail env GIT_DIR=. \
341 git -C bare.git init --separate-git-dir goop.git 2>err &&
6789275d 342 test_grep "incompatible" err
ccf236a2
ES
343'
344
59d876cc
ES
345test_expect_success 'bare & --separate-git-dir incompatible within worktree' '
346 test_when_finished "rm -rf bare.git linkwt seprepo" &&
347 test_commit gumby &&
348 git clone --bare . bare.git &&
349 git -C bare.git worktree add --detach ../linkwt &&
350 test_must_fail git -C linkwt init --separate-git-dir seprepo 2>err &&
6789275d 351 test_grep "incompatible" err
59d876cc
ES
352'
353
bed67874
RS
354test_lazy_prereq GETCWD_IGNORES_PERMS '
355 base=GETCWD_TEST_BASE_DIR &&
356 mkdir -p $base/dir &&
357 chmod 100 $base ||
165293af 358 BUG "cannot prepare $base"
bed67874 359
482e1488
ÆAB
360 (
361 cd $base/dir &&
362 test-tool getcwd
363 )
bed67874
RS
364 status=$?
365
366 chmod 700 $base &&
367 rm -rf $base ||
165293af 368 BUG "cannot clean $base"
bed67874
RS
369 return $status
370'
371
372check_long_base_path () {
a54e938e
RS
373 # exceed initial buffer size of strbuf_getcwd()
374 component=123456789abcdef &&
375 test_when_finished "chmod 0700 $component; rm -rf $component" &&
376 p31=$component/$component &&
377 p127=$p31/$p31/$p31/$p31 &&
378 mkdir -p $p127 &&
bed67874
RS
379 if test $# = 1
380 then
381 chmod $1 $component
382 fi &&
a54e938e
RS
383 (
384 cd $p127 &&
385 git init newdir
386 )
bed67874
RS
387}
388
389test_expect_success 'init in long base path' '
390 check_long_base_path
391'
392
393test_expect_success GETCWD_IGNORES_PERMS 'init in long restricted base path' '
394 check_long_base_path 0111
a54e938e
RS
395'
396
487a2b73
NTND
397test_expect_success 're-init on .git file' '
398 ( cd newdir && git init )
399'
400
b57fb80a 401test_expect_success 're-init to update git link' '
ed33bd8f
JS
402 git -C newdir init --separate-git-dir ../surrealgitdir &&
403 newdir_git="$(cat newdir/.git)" &&
404 test_cmp_fspath "$(pwd)/surrealgitdir" "${newdir_git#gitdir: }" &&
633734d4
JK
405 test_path_is_dir surrealgitdir/refs &&
406 test_path_is_missing realgitdir/refs
b57fb80a
NTND
407'
408
409test_expect_success 're-init to move gitdir' '
410 rm -rf newdir realgitdir surrealgitdir &&
411 git init newdir &&
ed33bd8f
JS
412 git -C newdir init --separate-git-dir ../realgitdir &&
413 newdir_git="$(cat newdir/.git)" &&
414 test_cmp_fspath "$(pwd)/realgitdir" "${newdir_git#gitdir: }" &&
633734d4 415 test_path_is_dir realgitdir/refs
b57fb80a
NTND
416'
417
4e95fb6c 418test_expect_success SYMLINKS 're-init to move gitdir symlink' '
b57fb80a
NTND
419 rm -rf newdir realgitdir &&
420 git init newdir &&
421 (
422 cd newdir &&
423 mv .git here &&
424 ln -s here .git &&
09ffc706 425 git init --separate-git-dir ../realgitdir
b57fb80a 426 ) &&
88619b3e 427 echo "gitdir: $(pwd)/realgitdir" >expected &&
b57fb80a 428 test_cmp expected newdir/.git &&
3d06c5f1 429 test_cmp expected newdir/here &&
633734d4 430 test_path_is_dir realgitdir/refs
b57fb80a
NTND
431'
432
59d876cc 433sep_git_dir_worktree () {
42264bc8
ES
434 test_when_finished "rm -rf mainwt linkwt seprepo" &&
435 git init mainwt &&
2037ca85
CW
436 if test "relative" = $2
437 then
438 test_config -C mainwt worktree.useRelativePaths true
439 else
440 test_config -C mainwt worktree.useRelativePaths false
441 fi
42264bc8
ES
442 test_commit -C mainwt gumby &&
443 git -C mainwt worktree add --detach ../linkwt &&
59d876cc 444 git -C "$1" init --separate-git-dir ../seprepo &&
42264bc8
ES
445 git -C mainwt rev-parse --git-common-dir >expect &&
446 git -C linkwt rev-parse --git-common-dir >actual &&
447 test_cmp expect actual
59d876cc
ES
448}
449
2037ca85
CW
450test_expect_success 're-init to move gitdir with linked worktrees (absolute)' '
451 sep_git_dir_worktree mainwt absolute
452'
453
454test_expect_success 're-init to move gitdir within linked worktree (absolute)' '
455 sep_git_dir_worktree linkwt absolute
456'
457
458test_expect_success 're-init to move gitdir with linked worktrees (relative)' '
459 sep_git_dir_worktree mainwt relative
59d876cc
ES
460'
461
2037ca85
CW
462test_expect_success 're-init to move gitdir within linked worktree (relative)' '
463 sep_git_dir_worktree linkwt relative
42264bc8
ES
464'
465
f30afdab
JS
466test_expect_success MINGW '.git hidden' '
467 rm -rf newdir &&
468 (
ed6c994a 469 sane_unset GIT_DIR GIT_WORK_TREE &&
f30afdab
JS
470 mkdir newdir &&
471 cd newdir &&
472 git init &&
176a66a7 473 test_path_is_hidden .git
f30afdab
JS
474 ) &&
475 check_config newdir/.git false unset
476'
477
478test_expect_success MINGW 'bare git dir not hidden' '
479 rm -rf newdir &&
480 (
ed6c994a 481 sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG &&
f30afdab
JS
482 mkdir newdir &&
483 cd newdir &&
484 git --bare init
485 ) &&
486 ! is_hidden newdir
487'
488
b9605bc4
JK
489test_expect_success 'remote init from does not use config from cwd' '
490 rm -rf newdir &&
491 test_config core.logallrefupdates true &&
492 git init newdir &&
493 echo true >expect &&
494 git -C newdir config --bool core.logallrefupdates >actual &&
495 test_cmp expect actual
496'
497
fe9aa0b2
NTND
498test_expect_success 're-init from a linked worktree' '
499 git init main-worktree &&
500 (
501 cd main-worktree &&
502 test_commit first &&
503 git worktree add ../linked-worktree &&
504 mv .git/info/exclude expected-exclude &&
6311cfaf 505 cp .git/config expected-config &&
fe9aa0b2
NTND
506 find .git/worktrees -print | sort >expected &&
507 git -C ../linked-worktree init &&
508 test_cmp expected-exclude .git/info/exclude &&
6311cfaf 509 test_cmp expected-config .git/config &&
fe9aa0b2
NTND
510 find .git/worktrees -print | sort >actual &&
511 test_cmp expected actual
512 )
513'
514
eff45daa 515test_expect_success 'init honors GIT_DEFAULT_HASH' '
7689f6cb 516 test_when_finished "rm -rf sha1 sha256" &&
eff45daa 517 GIT_DEFAULT_HASH=sha1 git init sha1 &&
518 git -C sha1 rev-parse --show-object-format >actual &&
519 echo sha1 >expected &&
520 test_cmp expected actual &&
521 GIT_DEFAULT_HASH=sha256 git init sha256 &&
522 git -C sha256 rev-parse --show-object-format >actual &&
523 echo sha256 >expected &&
524 test_cmp expected actual
525'
526
527test_expect_success 'init honors --object-format' '
7689f6cb 528 test_when_finished "rm -rf explicit-sha1 explicit-sha256" &&
eff45daa 529 git init --object-format=sha1 explicit-sha1 &&
530 git -C explicit-sha1 rev-parse --show-object-format >actual &&
531 echo sha1 >expected &&
532 test_cmp expected actual &&
533 git init --object-format=sha256 explicit-sha256 &&
534 git -C explicit-sha256 rev-parse --show-object-format >actual &&
535 echo sha256 >expected &&
536 test_cmp expected actual
537'
538
0c22e09b
PS
539test_expect_success 'init honors init.defaultObjectFormat' '
540 test_when_finished "rm -rf sha1 sha256" &&
541
542 test_config_global init.defaultObjectFormat sha1 &&
543 (
544 sane_unset GIT_DEFAULT_HASH &&
545 git init sha1 &&
546 git -C sha1 rev-parse --show-object-format >actual &&
547 echo sha1 >expected &&
548 test_cmp expected actual
549 ) &&
550
551 test_config_global init.defaultObjectFormat sha256 &&
552 (
553 sane_unset GIT_DEFAULT_HASH &&
554 git init sha256 &&
555 git -C sha256 rev-parse --show-object-format >actual &&
556 echo sha256 >expected &&
557 test_cmp expected actual
558 )
559'
560
561test_expect_success 'init warns about invalid init.defaultObjectFormat' '
562 test_when_finished "rm -rf repo" &&
563 test_config_global init.defaultObjectFormat garbage &&
564
565 echo "warning: unknown hash algorithm ${SQ}garbage${SQ}" >expect &&
566 git init repo 2>err &&
567 test_cmp expect err &&
568
569 git -C repo rev-parse --show-object-format >actual &&
570 echo $GIT_DEFAULT_HASH >expected &&
571 test_cmp expected actual
572'
573
574test_expect_success '--object-format overrides GIT_DEFAULT_HASH' '
575 test_when_finished "rm -rf repo" &&
576 GIT_DEFAULT_HASH=sha1 git init --object-format=sha256 repo &&
577 git -C repo rev-parse --show-object-format >actual &&
578 echo sha256 >expected
579'
580
581test_expect_success 'GIT_DEFAULT_HASH overrides init.defaultObjectFormat' '
582 test_when_finished "rm -rf repo" &&
583 test_config_global init.defaultObjectFormat sha1 &&
584 GIT_DEFAULT_HASH=sha256 git init repo &&
585 git -C repo rev-parse --show-object-format >actual &&
586 echo sha256 >expected
587'
588
7e88640c
PS
589for hash in sha1 sha256
590do
591 test_expect_success "reinit repository with GIT_DEFAULT_HASH=$hash does not change format" '
592 test_when_finished "rm -rf repo" &&
593 git init repo &&
594 git -C repo rev-parse --show-object-format >expect &&
595 GIT_DEFAULT_HASH=$hash git init repo &&
596 git -C repo rev-parse --show-object-format >actual &&
597 test_cmp expect actual
598 '
599done
600
eff45daa 601test_expect_success 'extensions.objectFormat is not allowed with repo version 0' '
7689f6cb 602 test_when_finished "rm -rf explicit-v0" &&
eff45daa 603 git init --object-format=sha256 explicit-v0 &&
604 git -C explicit-v0 config core.repositoryformatversion 0 &&
605 test_must_fail git -C explicit-v0 rev-parse --show-object-format
606'
607
608test_expect_success 'init rejects attempts to initialize with different hash' '
609 test_must_fail git -C sha1 init --object-format=sha256 &&
610 test_must_fail git -C sha256 init --object-format=sha1
611'
612
d7497a42
PS
613test_expect_success DEFAULT_REPO_FORMAT 'extensions.refStorage is not allowed with repo version 0' '
614 test_when_finished "rm -rf refstorage" &&
615 git init refstorage &&
616 git -C refstorage config extensions.refStorage files &&
617 test_must_fail git -C refstorage rev-parse 2>err &&
618 grep "repo version is 0, but v1-only extension found" err
619'
620
621test_expect_success DEFAULT_REPO_FORMAT 'extensions.refStorage with files backend' '
622 test_when_finished "rm -rf refstorage" &&
623 git init refstorage &&
624 git -C refstorage config core.repositoryformatversion 1 &&
625 git -C refstorage config extensions.refStorage files &&
626 test_commit -C refstorage A &&
627 git -C refstorage rev-parse --verify HEAD
628'
629
630test_expect_success DEFAULT_REPO_FORMAT 'extensions.refStorage with unknown backend' '
631 test_when_finished "rm -rf refstorage" &&
632 git init refstorage &&
633 git -C refstorage config core.repositoryformatversion 1 &&
634 git -C refstorage config extensions.refStorage garbage &&
635 test_must_fail git -C refstorage rev-parse 2>err &&
636 grep "invalid value for ${SQ}extensions.refstorage${SQ}: ${SQ}garbage${SQ}" err
637'
638
aa19619a
PS
639test_expect_success 'init with GIT_DEFAULT_REF_FORMAT=garbage' '
640 test_when_finished "rm -rf refformat" &&
641 cat >expect <<-EOF &&
642 fatal: unknown ref storage format ${SQ}garbage${SQ}
643 EOF
644 test_must_fail env GIT_DEFAULT_REF_FORMAT=garbage git init refformat 2>err &&
645 test_cmp expect err
646'
647
d2511eea
PS
648test_expect_success 'init warns about invalid init.defaultRefFormat' '
649 test_when_finished "rm -rf repo" &&
650 test_config_global init.defaultRefFormat garbage &&
651
652 echo "warning: unknown ref storage format ${SQ}garbage${SQ}" >expect &&
653 git init repo 2>err &&
654 test_cmp expect err &&
655
656 git -C repo rev-parse --show-ref-format >actual &&
657 echo $GIT_DEFAULT_REF_FORMAT >expected &&
658 test_cmp expected actual
659'
660
05d20915
PS
661backends="files reftable"
662for format in $backends
663do
664 test_expect_success DEFAULT_REPO_FORMAT "init with GIT_DEFAULT_REF_FORMAT=$format" '
665 test_when_finished "rm -rf refformat" &&
666 GIT_DEFAULT_REF_FORMAT=$format git init refformat &&
667
668 if test $format = files
669 then
670 test_must_fail git -C refformat config extensions.refstorage &&
671 echo 0 >expect
672 else
673 git -C refformat config extensions.refstorage &&
674 echo 1 >expect
675 fi &&
676 git -C refformat config core.repositoryformatversion >actual &&
677 test_cmp expect actual &&
678
679 echo $format >expect &&
680 git -C refformat rev-parse --show-ref-format >actual &&
681 test_cmp expect actual
682 '
683
684 test_expect_success "init with --ref-format=$format" '
685 test_when_finished "rm -rf refformat" &&
686 git init --ref-format=$format refformat &&
687 echo $format >expect &&
688 git -C refformat rev-parse --show-ref-format >actual &&
689 test_cmp expect actual
690 '
d2511eea
PS
691
692 test_expect_success "init with init.defaultRefFormat=$format" '
693 test_when_finished "rm -rf refformat" &&
694 test_config_global init.defaultRefFormat $format &&
695 (
696 sane_unset GIT_DEFAULT_REF_FORMAT &&
697 git init refformat
698 ) &&
699
700 echo $format >expect &&
701 git -C refformat rev-parse --show-ref-format >actual &&
702 test_cmp expect actual
703 '
704
705 test_expect_success "--ref-format=$format overrides GIT_DEFAULT_REF_FORMAT" '
706 test_when_finished "rm -rf refformat" &&
707 GIT_DEFAULT_REF_FORMAT=garbage git init --ref-format=$format refformat &&
708 echo $format >expect &&
709 git -C refformat rev-parse --show-ref-format >actual &&
796fda3f
PS
710 test_cmp expect actual
711 '
712
713 test_expect_success "reinit repository with GIT_DEFAULT_REF_FORMAT=$format does not change format" '
714 test_when_finished "rm -rf refformat" &&
715 git init refformat &&
716 git -C refformat rev-parse --show-ref-format >expect &&
717 GIT_DEFAULT_REF_FORMAT=$format git init refformat &&
718 git -C refformat rev-parse --show-ref-format >actual &&
d2511eea
PS
719 test_cmp expect actual
720 '
05d20915
PS
721done
722
723test_expect_success "--ref-format= overrides GIT_DEFAULT_REF_FORMAT" '
48fa45f5 724 test_when_finished "rm -rf refformat" &&
05d20915
PS
725 GIT_DEFAULT_REF_FORMAT=files git init --ref-format=reftable refformat &&
726 echo reftable >expect &&
48fa45f5
PS
727 git -C refformat rev-parse --show-ref-format >actual &&
728 test_cmp expect actual
729'
730
d2511eea
PS
731test_expect_success "GIT_DEFAULT_REF_FORMAT= overrides init.defaultRefFormat" '
732 test_when_finished "rm -rf refformat" &&
733 test_config_global init.defaultRefFormat files &&
734
735 GIT_DEFAULT_REF_FORMAT=reftable git init refformat &&
736 echo reftable >expect &&
737 git -C refformat rev-parse --show-ref-format >actual &&
738 test_cmp expect actual
739'
740
407997c1
PS
741for from_format in $backends
742do
743 test_expect_success "re-init with same format ($from_format)" '
744 test_when_finished "rm -rf refformat" &&
745 git init --ref-format=$from_format refformat &&
746 git init --ref-format=$from_format refformat &&
747 echo $from_format >expect &&
748 git -C refformat rev-parse --show-ref-format >actual &&
749 test_cmp expect actual
750 '
751
752 for to_format in $backends
753 do
754 if test "$from_format" = "$to_format"
755 then
756 continue
757 fi
758
759 test_expect_success "re-init with different format fails ($from_format -> $to_format)" '
760 test_when_finished "rm -rf refformat" &&
761 git init --ref-format=$from_format refformat &&
762 cat >expect <<-EOF &&
763 fatal: attempt to reinitialize repository with different reference storage format
764 EOF
765 test_must_fail git init --ref-format=$to_format refformat 2>err &&
766 test_cmp expect err &&
767 echo $from_format >expect &&
768 git -C refformat rev-parse --show-ref-format >actual &&
769 test_cmp expect actual
770 '
771 done
772done
48fa45f5
PS
773
774test_expect_success 'init with --ref-format=garbage' '
775 test_when_finished "rm -rf refformat" &&
776 cat >expect <<-EOF &&
777 fatal: unknown ref storage format ${SQ}garbage${SQ}
778 EOF
779 test_must_fail git init --ref-format=garbage refformat 2>err &&
780 test_cmp expect err
781'
782
28785339
JS
783test_expect_success MINGW 'core.hidedotfiles = false' '
784 git config --global core.hidedotfiles false &&
785 rm -rf newdir &&
786 mkdir newdir &&
787 (
788 sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG &&
789 git -C newdir init
790 ) &&
791 ! is_hidden newdir/.git
792'
793
3f944424
JS
794test_expect_success MINGW 'redirect std handles' '
795 GIT_REDIRECT_STDOUT=output.txt git rev-parse --git-dir &&
796 test .git = "$(cat output.txt)" &&
1a172e4a
JS
797 test -z "$(GIT_REDIRECT_STDOUT=off git rev-parse --git-dir)" &&
798 test_must_fail env \
799 GIT_REDIRECT_STDOUT=output.txt \
800 GIT_REDIRECT_STDERR="2>&1" \
801 git rev-parse --git-dir --verify refs/invalid &&
fdda1ac6
JS
802 grep "^\\.git\$" output.txt &&
803 grep "Needed a single revision" output.txt
3f944424
JS
804'
805
32ba12da
JS
806test_expect_success '--initial-branch' '
807 git init --initial-branch=hello initial-branch-option &&
808 git -C initial-branch-option symbolic-ref HEAD >actual &&
809 echo refs/heads/hello >expect &&
810 test_cmp expect actual &&
811
812 : re-initializing should not change the branch name &&
813 git init --initial-branch=ignore initial-branch-option 2>err &&
6789275d 814 test_grep "ignored --initial-branch" err &&
32ba12da
JS
815 git -C initial-branch-option symbolic-ref HEAD >actual &&
816 grep hello actual
817'
818
8747ebb7
DGW
819test_expect_success 'overridden default initial branch name (config)' '
820 test_config_global init.defaultBranch nmb &&
704fed9e 821 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= git init initial-branch-config &&
8747ebb7
DGW
822 git -C initial-branch-config symbolic-ref HEAD >actual &&
823 grep nmb actual
824'
825
675704c7
JS
826test_expect_success 'advice on unconfigured init.defaultBranch' '
827 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= git -c color.advice=always \
828 init unconfigured-default-branch-name 2>err &&
829 test_decode_color <err >decoded &&
6789275d 830 test_grep "<YELLOW>hint: " decoded
675704c7
JS
831'
832
ec0f362e
JT
833test_expect_success 'advice on unconfigured init.defaultBranch disabled' '
834 test_when_finished "rm -rf no-advice" &&
835
836 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
837 git -c advice.defaultBranchName=false init no-advice 2>err &&
838 test_grep ! "hint: " err
839'
840
704fed9e
JS
841test_expect_success 'overridden default main branch name (env)' '
842 test_config_global init.defaultBranch nmb &&
843 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=env git init main-branch-env &&
844 git -C main-branch-env symbolic-ref HEAD >actual &&
845 grep env actual
846'
847
8747ebb7 848test_expect_success 'invalid default branch name' '
704fed9e
JS
849 test_must_fail env GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME="with space" \
850 git init initial-branch-invalid 2>err &&
6789275d 851 test_grep "invalid branch name" err
8747ebb7
DGW
852'
853
cfaff3aa
JS
854test_expect_success 'branch -m with the initial branch' '
855 git init rename-initial &&
856 git -C rename-initial branch -m renamed &&
4bd0785d
ÆAB
857 echo renamed >expect &&
858 git -C rename-initial symbolic-ref --short HEAD >actual &&
859 test_cmp expect actual &&
860
cfaff3aa 861 git -C rename-initial branch -m renamed again &&
4bd0785d
ÆAB
862 echo again >expect &&
863 git -C rename-initial symbolic-ref --short HEAD >actual &&
864 test_cmp expect actual
cfaff3aa
JS
865'
866
407997c1
PS
867test_expect_success 'init with includeIf.onbranch condition' '
868 test_when_finished "rm -rf repo" &&
869 git -c includeIf.onbranch:main.path=nonexistent init repo &&
870 echo $GIT_DEFAULT_REF_FORMAT >expect &&
871 git -C repo rev-parse --show-ref-format >actual &&
872 test_cmp expect actual
873'
874
875test_expect_success 'init with includeIf.onbranch condition with existing directory' '
876 test_when_finished "rm -rf repo" &&
877 mkdir repo &&
878 git -c includeIf.onbranch:nonexistent.path=/does/not/exist init repo &&
879 echo $GIT_DEFAULT_REF_FORMAT >expect &&
880 git -C repo rev-parse --show-ref-format >actual &&
881 test_cmp expect actual
882'
407997c1
PS
883
884test_expect_success 're-init with includeIf.onbranch condition' '
885 test_when_finished "rm -rf repo" &&
886 git init repo &&
887 git -c includeIf.onbranch:nonexistent.path=/does/not/exist init repo &&
888 echo $GIT_DEFAULT_REF_FORMAT >expect &&
889 git -C repo rev-parse --show-ref-format >actual &&
890 test_cmp expect actual
891'
892
893test_expect_success 're-init skips non-matching includeIf.onbranch' '
894 test_when_finished "rm -rf repo config" &&
895 cat >config <<-EOF &&
896 [
897 garbage
898 EOF
899 git init repo &&
900 git -c includeIf.onbranch:nonexistent.path="$(test-tool path-utils absolute_path config)" init repo
901'
902
903test_expect_success 're-init reads matching includeIf.onbranch' '
904 test_when_finished "rm -rf repo config" &&
905 cat >config <<-EOF &&
906 [
907 garbage
908 EOF
909 path="$(test-tool path-utils absolute_path config)" &&
910 git init --initial-branch=branch repo &&
911 cat >expect <<-EOF &&
912 fatal: bad config line 1 in file $path
913 EOF
914 test_must_fail git -c includeIf.onbranch:branch.path="$path" init repo 2>err &&
915 test_cmp expect err
916'
917
6adcca3f 918test_done