commit-graph: when incompatible with graphs, indicate why
[git] / t / t0001-init.sh
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
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
22         bare=$(cd "$1" && git config --bool core.bare)
23         worktree=$(cd "$1" && git config core.worktree) ||
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' '
34         git init plain &&
35         check_config plain/.git false unset
36 '
37
38 test_expect_success 'plain nested in bare' '
39         (
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         (
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
66 test_expect_success 'plain nested through aliased command' '
67         (
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
78 test_expect_success 'plain nested in bare through aliased command' '
79         (
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
88 '
89
90 test_expect_success 'No extra GIT_* on alias scripts' '
91         write_script script <<-\EOF &&
92         env |
93                 sed -n \
94                         -e "/^GIT_PREFIX=/d" \
95                         -e "/^GIT_TEXTDOMAINDIR=/d" \
96                         -e "/^GIT_TRACE2_PARENT/d" \
97                         -e "/^GIT_/s/=.*//p" |
98                 sort
99         EOF
100         ./script >expected &&
101         git config alias.script \!./script &&
102         ( mkdir sub && cd sub && git script >../actual ) &&
103         test_cmp expected actual
104 '
105
106 test_expect_success 'plain with GIT_WORK_TREE' '
107         mkdir plain-wt &&
108         test_must_fail env GIT_WORK_TREE="$(pwd)/plain-wt" git init plain-wt
109 '
110
111 test_expect_success 'plain bare' '
112         git --bare init plain-bare-1 &&
113         check_config plain-bare-1 true unset
114 '
115
116 test_expect_success 'plain bare with GIT_WORK_TREE' '
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
121 '
122
123 test_expect_success 'GIT_DIR bare' '
124         mkdir git-dir-bare.git &&
125         GIT_DIR=git-dir-bare.git git init &&
126         check_config git-dir-bare.git true unset
127 '
128
129 test_expect_success 'init --bare' '
130         git init --bare init-bare.git &&
131         check_config init-bare.git true unset
132 '
133
134 test_expect_success 'GIT_DIR non-bare' '
135
136         (
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         (
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)' '
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
159 '
160
161 test_expect_success 'reinit' '
162
163         (
164                 mkdir again &&
165                 cd again &&
166                 git -c init.defaultBranch=initial init >out1 2>err1 &&
167                 git init >out2 2>err2
168         ) &&
169         test_i18ngrep "Initialized empty" again/out1 &&
170         test_i18ngrep "Reinitialized existing" again/out2 &&
171         test_must_be_empty again/err1 &&
172         test_must_be_empty again/err2
173 '
174
175 test_expect_success 'init with --template' '
176         mkdir template-source &&
177         echo content >template-source/file &&
178         git init --template=template-source template-custom &&
179         test_cmp template-source/file template-custom/.git/file
180 '
181
182 test_expect_success 'init with --template (blank)' '
183         git init template-plain &&
184         test_path_is_file template-plain/.git/info/exclude &&
185         git init --template= template-blank &&
186         test_path_is_missing template-blank/.git/info/exclude
187 '
188
189 test_expect_success 'init with init.templatedir set' '
190         mkdir templatedir-source &&
191         echo Content >templatedir-source/file &&
192         test_config_global init.templatedir "${HOME}/templatedir-source" &&
193         (
194                 mkdir templatedir-set &&
195                 cd templatedir-set &&
196                 sane_unset GIT_TEMPLATE_DIR &&
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
204 test_expect_success 'init --bare/--shared overrides system/global config' '
205         test_config_global core.bare false &&
206         test_config_global core.sharedRepository 0640 &&
207         git init --bare --shared=0666 init-bare-shared-override &&
208         check_config init-bare-shared-override true unset &&
209         test x0666 = \
210         x$(git config -f init-bare-shared-override/config core.sharedRepository)
211 '
212
213 test_expect_success 'init honors global core.sharedRepository' '
214         test_config_global core.sharedRepository 0666 &&
215         git init shared-honor-global &&
216         test x0666 = \
217         x$(git config -f shared-honor-global/.git/config core.sharedRepository)
218 '
219
220 test_expect_success 'init allows insanely long --template' '
221         git init --template=$(printf "x%09999dx" 1) test
222 '
223
224 test_expect_success 'init creates a new directory' '
225         rm -fr newdir &&
226         git init newdir &&
227         test_path_is_dir newdir/.git/refs
228 '
229
230 test_expect_success 'init creates a new bare directory' '
231         rm -fr newdir &&
232         git init --bare newdir &&
233         test_path_is_dir newdir/refs
234 '
235
236 test_expect_success 'init recreates a directory' '
237         rm -fr newdir &&
238         mkdir newdir &&
239         git init newdir &&
240         test_path_is_dir newdir/.git/refs
241 '
242
243 test_expect_success 'init recreates a new bare directory' '
244         rm -fr newdir &&
245         mkdir newdir &&
246         git init --bare newdir &&
247         test_path_is_dir newdir/refs
248 '
249
250 test_expect_success 'init creates a new deep directory' '
251         rm -fr newdir &&
252         git init newdir/a/b/c &&
253         test_path_is_dir newdir/a/b/c/.git/refs
254 '
255
256 test_expect_success POSIXPERM 'init creates a new deep directory (umask vs. shared)' '
257         rm -fr newdir &&
258         (
259                 # Leading directories should honor umask while
260                 # the repository itself should follow "shared"
261                 mkdir newdir &&
262                 # Remove a default ACL if possible.
263                 (setfacl -k newdir 2>/dev/null || true) &&
264                 umask 002 &&
265                 git init --bare --shared=0660 newdir/a/b/c &&
266                 test_path_is_dir newdir/a/b/c/refs &&
267                 ls -ld newdir/a newdir/a/b > lsab.out &&
268                 ! grep -v "^drwxrw[sx]r-x" lsab.out &&
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 &&
276         >newdir &&
277         test_must_fail git init newdir &&
278         test_path_is_file newdir
279 '
280
281 test_expect_success 'init notices EEXIST (2)' '
282         rm -fr newdir &&
283         mkdir newdir &&
284         >newdir/a &&
285         test_must_fail git init newdir/a/b &&
286         test_path_is_file newdir/a
287 '
288
289 test_expect_success POSIXPERM,SANITY 'init notices EPERM' '
290         test_when_finished "chmod +w newdir" &&
291         rm -fr newdir &&
292         mkdir newdir &&
293         chmod -w newdir &&
294         test_must_fail git init newdir/a/b
295 '
296
297 test_expect_success 'init creates a new bare directory with global --bare' '
298         rm -rf newdir &&
299         git --bare init newdir &&
300         test_path_is_dir newdir/refs
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 &&
307         test_path_is_dir newdir/refs &&
308         test_path_is_missing otherdir/refs
309 '
310
311 test_expect_success 'init with separate gitdir' '
312         rm -rf newdir &&
313         git init --separate-git-dir realgitdir newdir &&
314         newdir_git="$(cat newdir/.git)" &&
315         test_cmp_fspath "$(pwd)/realgitdir" "${newdir_git#gitdir: }" &&
316         test_path_is_dir realgitdir/refs
317 '
318
319 test_expect_success 'explicit bare & --separate-git-dir incompatible' '
320         test_must_fail git init --bare --separate-git-dir goop.git bare.git 2>err &&
321         test_i18ngrep "mutually exclusive" err
322 '
323
324 test_expect_success 'implicit bare & --separate-git-dir incompatible' '
325         test_when_finished "rm -rf bare.git" &&
326         mkdir -p bare.git &&
327         test_must_fail env GIT_DIR=. \
328                 git -C bare.git init --separate-git-dir goop.git 2>err &&
329         test_i18ngrep "incompatible" err
330 '
331
332 test_expect_success 'bare & --separate-git-dir incompatible within worktree' '
333         test_when_finished "rm -rf bare.git linkwt seprepo" &&
334         test_commit gumby &&
335         git clone --bare . bare.git &&
336         git -C bare.git worktree add --detach ../linkwt &&
337         test_must_fail git -C linkwt init --separate-git-dir seprepo 2>err &&
338         test_i18ngrep "incompatible" err
339 '
340
341 test_lazy_prereq GETCWD_IGNORES_PERMS '
342         base=GETCWD_TEST_BASE_DIR &&
343         mkdir -p $base/dir &&
344         chmod 100 $base ||
345         BUG "cannot prepare $base"
346
347         (cd $base/dir && /bin/pwd -P)
348         status=$?
349
350         chmod 700 $base &&
351         rm -rf $base ||
352         BUG "cannot clean $base"
353         return $status
354 '
355
356 check_long_base_path () {
357         # exceed initial buffer size of strbuf_getcwd()
358         component=123456789abcdef &&
359         test_when_finished "chmod 0700 $component; rm -rf $component" &&
360         p31=$component/$component &&
361         p127=$p31/$p31/$p31/$p31 &&
362         mkdir -p $p127 &&
363         if test $# = 1
364         then
365                 chmod $1 $component
366         fi &&
367         (
368                 cd $p127 &&
369                 git init newdir
370         )
371 }
372
373 test_expect_success 'init in long base path' '
374         check_long_base_path
375 '
376
377 test_expect_success GETCWD_IGNORES_PERMS 'init in long restricted base path' '
378         check_long_base_path 0111
379 '
380
381 test_expect_success 're-init on .git file' '
382         ( cd newdir && git init )
383 '
384
385 test_expect_success 're-init to update git link' '
386         git -C newdir init --separate-git-dir ../surrealgitdir &&
387         newdir_git="$(cat newdir/.git)" &&
388         test_cmp_fspath "$(pwd)/surrealgitdir" "${newdir_git#gitdir: }" &&
389         test_path_is_dir surrealgitdir/refs &&
390         test_path_is_missing realgitdir/refs
391 '
392
393 test_expect_success 're-init to move gitdir' '
394         rm -rf newdir realgitdir surrealgitdir &&
395         git init newdir &&
396         git -C newdir init --separate-git-dir ../realgitdir &&
397         newdir_git="$(cat newdir/.git)" &&
398         test_cmp_fspath "$(pwd)/realgitdir" "${newdir_git#gitdir: }" &&
399         test_path_is_dir realgitdir/refs
400 '
401
402 test_expect_success SYMLINKS 're-init to move gitdir symlink' '
403         rm -rf newdir realgitdir &&
404         git init newdir &&
405         (
406         cd newdir &&
407         mv .git here &&
408         ln -s here .git &&
409         git init --separate-git-dir ../realgitdir
410         ) &&
411         echo "gitdir: $(pwd)/realgitdir" >expected &&
412         test_cmp expected newdir/.git &&
413         test_cmp expected newdir/here &&
414         test_path_is_dir realgitdir/refs
415 '
416
417 sep_git_dir_worktree ()  {
418         test_when_finished "rm -rf mainwt linkwt seprepo" &&
419         git init mainwt &&
420         test_commit -C mainwt gumby &&
421         git -C mainwt worktree add --detach ../linkwt &&
422         git -C "$1" init --separate-git-dir ../seprepo &&
423         git -C mainwt rev-parse --git-common-dir >expect &&
424         git -C linkwt rev-parse --git-common-dir >actual &&
425         test_cmp expect actual
426 }
427
428 test_expect_success 're-init to move gitdir with linked worktrees' '
429         sep_git_dir_worktree mainwt
430 '
431
432 test_expect_success 're-init to move gitdir within linked worktree' '
433         sep_git_dir_worktree linkwt
434 '
435
436 test_expect_success MINGW '.git hidden' '
437         rm -rf newdir &&
438         (
439                 sane_unset GIT_DIR GIT_WORK_TREE &&
440                 mkdir newdir &&
441                 cd newdir &&
442                 git init &&
443                 test_path_is_hidden .git
444         ) &&
445         check_config newdir/.git false unset
446 '
447
448 test_expect_success MINGW 'bare git dir not hidden' '
449         rm -rf newdir &&
450         (
451                 sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG &&
452                 mkdir newdir &&
453                 cd newdir &&
454                 git --bare init
455         ) &&
456         ! is_hidden newdir
457 '
458
459 test_expect_success 'remote init from does not use config from cwd' '
460         rm -rf newdir &&
461         test_config core.logallrefupdates true &&
462         git init newdir &&
463         echo true >expect &&
464         git -C newdir config --bool core.logallrefupdates >actual &&
465         test_cmp expect actual
466 '
467
468 test_expect_success 're-init from a linked worktree' '
469         git init main-worktree &&
470         (
471                 cd main-worktree &&
472                 test_commit first &&
473                 git worktree add ../linked-worktree &&
474                 mv .git/info/exclude expected-exclude &&
475                 cp .git/config expected-config &&
476                 find .git/worktrees -print | sort >expected &&
477                 git -C ../linked-worktree init &&
478                 test_cmp expected-exclude .git/info/exclude &&
479                 test_cmp expected-config .git/config &&
480                 find .git/worktrees -print | sort >actual &&
481                 test_cmp expected actual
482         )
483 '
484
485 test_expect_success 'init honors GIT_DEFAULT_HASH' '
486         GIT_DEFAULT_HASH=sha1 git init sha1 &&
487         git -C sha1 rev-parse --show-object-format >actual &&
488         echo sha1 >expected &&
489         test_cmp expected actual &&
490         GIT_DEFAULT_HASH=sha256 git init sha256 &&
491         git -C sha256 rev-parse --show-object-format >actual &&
492         echo sha256 >expected &&
493         test_cmp expected actual
494 '
495
496 test_expect_success 'init honors --object-format' '
497         git init --object-format=sha1 explicit-sha1 &&
498         git -C explicit-sha1 rev-parse --show-object-format >actual &&
499         echo sha1 >expected &&
500         test_cmp expected actual &&
501         git init --object-format=sha256 explicit-sha256 &&
502         git -C explicit-sha256 rev-parse --show-object-format >actual &&
503         echo sha256 >expected &&
504         test_cmp expected actual
505 '
506
507 test_expect_success 'extensions.objectFormat is not allowed with repo version 0' '
508         git init --object-format=sha256 explicit-v0 &&
509         git -C explicit-v0 config core.repositoryformatversion 0 &&
510         test_must_fail git -C explicit-v0 rev-parse --show-object-format
511 '
512
513 test_expect_success 'init rejects attempts to initialize with different hash' '
514         test_must_fail git -C sha1 init --object-format=sha256 &&
515         test_must_fail git -C sha256 init --object-format=sha1
516 '
517
518 test_expect_success MINGW 'core.hidedotfiles = false' '
519         git config --global core.hidedotfiles false &&
520         rm -rf newdir &&
521         mkdir newdir &&
522         (
523                 sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG &&
524                 git -C newdir init
525         ) &&
526         ! is_hidden newdir/.git
527 '
528
529 test_expect_success MINGW 'redirect std handles' '
530         GIT_REDIRECT_STDOUT=output.txt git rev-parse --git-dir &&
531         test .git = "$(cat output.txt)" &&
532         test -z "$(GIT_REDIRECT_STDOUT=off git rev-parse --git-dir)" &&
533         test_must_fail env \
534                 GIT_REDIRECT_STDOUT=output.txt \
535                 GIT_REDIRECT_STDERR="2>&1" \
536                 git rev-parse --git-dir --verify refs/invalid &&
537         grep "^\\.git\$" output.txt &&
538         grep "Needed a single revision" output.txt
539 '
540
541 test_expect_success '--initial-branch' '
542         git init --initial-branch=hello initial-branch-option &&
543         git -C initial-branch-option symbolic-ref HEAD >actual &&
544         echo refs/heads/hello >expect &&
545         test_cmp expect actual &&
546
547         : re-initializing should not change the branch name &&
548         git init --initial-branch=ignore initial-branch-option 2>err &&
549         test_i18ngrep "ignored --initial-branch" err &&
550         git -C initial-branch-option symbolic-ref HEAD >actual &&
551         grep hello actual
552 '
553
554 test_expect_success 'overridden default initial branch name (config)' '
555         test_config_global init.defaultBranch nmb &&
556         GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= git init initial-branch-config &&
557         git -C initial-branch-config symbolic-ref HEAD >actual &&
558         grep nmb actual
559 '
560
561 test_expect_success 'advice on unconfigured init.defaultBranch' '
562         GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= git -c color.advice=always \
563                 init unconfigured-default-branch-name 2>err &&
564         test_decode_color <err >decoded &&
565         test_i18ngrep "<YELLOW>hint: " decoded
566 '
567
568 test_expect_success 'overridden default main branch name (env)' '
569         test_config_global init.defaultBranch nmb &&
570         GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=env git init main-branch-env &&
571         git -C main-branch-env symbolic-ref HEAD >actual &&
572         grep env actual
573 '
574
575 test_expect_success 'invalid default branch name' '
576         test_must_fail env GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME="with space" \
577                 git init initial-branch-invalid 2>err &&
578         test_i18ngrep "invalid branch name" err
579 '
580
581 test_expect_success 'branch -m with the initial branch' '
582         git init rename-initial &&
583         git -C rename-initial branch -m renamed &&
584         test renamed = $(git -C rename-initial symbolic-ref --short HEAD) &&
585         git -C rename-initial branch -m renamed again &&
586         test again = $(git -C rename-initial symbolic-ref --short HEAD)
587 '
588
589 test_done