Merge branch 'sg/t7501-thinkofix'
[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_/s/=.*//p" |
97                 sort
98         EOF
99         ./script >expected &&
100         git config alias.script \!./script &&
101         ( mkdir sub && cd sub && git script >../actual ) &&
102         test_cmp expected actual
103 '
104
105 test_expect_success 'plain with GIT_WORK_TREE' '
106         mkdir plain-wt &&
107         test_must_fail env GIT_WORK_TREE="$(pwd)/plain-wt" git init plain-wt
108 '
109
110 test_expect_success 'plain bare' '
111         git --bare init plain-bare-1 &&
112         check_config plain-bare-1 true unset
113 '
114
115 test_expect_success 'plain bare with GIT_WORK_TREE' '
116         mkdir plain-bare-2 &&
117         test_must_fail \
118                 env GIT_WORK_TREE="$(pwd)/plain-bare-2" \
119                 git --bare init plain-bare-2
120 '
121
122 test_expect_success 'GIT_DIR bare' '
123         mkdir git-dir-bare.git &&
124         GIT_DIR=git-dir-bare.git git init &&
125         check_config git-dir-bare.git true unset
126 '
127
128 test_expect_success 'init --bare' '
129         git init --bare init-bare.git &&
130         check_config init-bare.git true unset
131 '
132
133 test_expect_success 'GIT_DIR non-bare' '
134
135         (
136                 mkdir non-bare &&
137                 cd non-bare &&
138                 GIT_DIR=.git git init
139         ) &&
140         check_config non-bare/.git false unset
141 '
142
143 test_expect_success 'GIT_DIR & GIT_WORK_TREE (1)' '
144
145         (
146                 mkdir git-dir-wt-1.git &&
147                 GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-1.git git init
148         ) &&
149         check_config git-dir-wt-1.git false "$(pwd)"
150 '
151
152 test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
153         mkdir git-dir-wt-2.git &&
154         test_must_fail env \
155                 GIT_WORK_TREE="$(pwd)" \
156                 GIT_DIR=git-dir-wt-2.git \
157                 git --bare init
158 '
159
160 test_expect_success 'reinit' '
161
162         (
163                 mkdir again &&
164                 cd again &&
165                 git init >out1 2>err1 &&
166                 git init >out2 2>err2
167         ) &&
168         test_i18ngrep "Initialized empty" again/out1 &&
169         test_i18ngrep "Reinitialized existing" again/out2 &&
170         test_must_be_empty again/err1 &&
171         test_must_be_empty again/err2
172 '
173
174 test_expect_success 'init with --template' '
175         mkdir template-source &&
176         echo content >template-source/file &&
177         git init --template=../template-source template-custom &&
178         test_cmp template-source/file template-custom/.git/file
179 '
180
181 test_expect_success 'init with --template (blank)' '
182         git init template-plain &&
183         test_path_is_file template-plain/.git/info/exclude &&
184         git init --template= template-blank &&
185         test_path_is_missing template-blank/.git/info/exclude
186 '
187
188 test_expect_success 'init with init.templatedir set' '
189         mkdir templatedir-source &&
190         echo Content >templatedir-source/file &&
191         test_config_global init.templatedir "${HOME}/templatedir-source" &&
192         (
193                 mkdir templatedir-set &&
194                 cd templatedir-set &&
195                 sane_unset GIT_TEMPLATE_DIR &&
196                 NO_SET_GIT_TEMPLATE_DIR=t &&
197                 export NO_SET_GIT_TEMPLATE_DIR &&
198                 git init
199         ) &&
200         test_cmp templatedir-source/file templatedir-set/.git/file
201 '
202
203 test_expect_success 'init --bare/--shared overrides system/global config' '
204         test_config_global core.bare false &&
205         test_config_global core.sharedRepository 0640 &&
206         git init --bare --shared=0666 init-bare-shared-override &&
207         check_config init-bare-shared-override true unset &&
208         test x0666 = \
209         x$(git config -f init-bare-shared-override/config core.sharedRepository)
210 '
211
212 test_expect_success 'init honors global core.sharedRepository' '
213         test_config_global core.sharedRepository 0666 &&
214         git init shared-honor-global &&
215         test x0666 = \
216         x$(git config -f shared-honor-global/.git/config core.sharedRepository)
217 '
218
219 test_expect_success 'init allows insanely long --template' '
220         git init --template=$(printf "x%09999dx" 1) test
221 '
222
223 test_expect_success 'init creates a new directory' '
224         rm -fr newdir &&
225         git init newdir &&
226         test_path_is_dir newdir/.git/refs
227 '
228
229 test_expect_success 'init creates a new bare directory' '
230         rm -fr newdir &&
231         git init --bare newdir &&
232         test_path_is_dir newdir/refs
233 '
234
235 test_expect_success 'init recreates a directory' '
236         rm -fr newdir &&
237         mkdir newdir &&
238         git init newdir &&
239         test_path_is_dir newdir/.git/refs
240 '
241
242 test_expect_success 'init recreates a new bare directory' '
243         rm -fr newdir &&
244         mkdir newdir &&
245         git init --bare newdir &&
246         test_path_is_dir newdir/refs
247 '
248
249 test_expect_success 'init creates a new deep directory' '
250         rm -fr newdir &&
251         git init newdir/a/b/c &&
252         test_path_is_dir newdir/a/b/c/.git/refs
253 '
254
255 test_expect_success POSIXPERM 'init creates a new deep directory (umask vs. shared)' '
256         rm -fr newdir &&
257         (
258                 # Leading directories should honor umask while
259                 # the repository itself should follow "shared"
260                 mkdir newdir &&
261                 # Remove a default ACL if possible.
262                 (setfacl -k newdir 2>/dev/null || true) &&
263                 umask 002 &&
264                 git init --bare --shared=0660 newdir/a/b/c &&
265                 test_path_is_dir newdir/a/b/c/refs &&
266                 ls -ld newdir/a newdir/a/b > lsab.out &&
267                 ! grep -v "^drwxrw[sx]r-x" lsab.out &&
268                 ls -ld newdir/a/b/c > lsc.out &&
269                 ! grep -v "^drwxrw[sx]---" lsc.out
270         )
271 '
272
273 test_expect_success 'init notices EEXIST (1)' '
274         rm -fr newdir &&
275         >newdir &&
276         test_must_fail git init newdir &&
277         test_path_is_file newdir
278 '
279
280 test_expect_success 'init notices EEXIST (2)' '
281         rm -fr newdir &&
282         mkdir newdir &&
283         >newdir/a &&
284         test_must_fail git init newdir/a/b &&
285         test_path_is_file newdir/a
286 '
287
288 test_expect_success POSIXPERM,SANITY 'init notices EPERM' '
289         test_when_finished "chmod +w newdir" &&
290         rm -fr newdir &&
291         mkdir newdir &&
292         chmod -w newdir &&
293         test_must_fail git init newdir/a/b
294 '
295
296 test_expect_success 'init creates a new bare directory with global --bare' '
297         rm -rf newdir &&
298         git --bare init newdir &&
299         test_path_is_dir newdir/refs
300 '
301
302 test_expect_success 'init prefers command line to GIT_DIR' '
303         rm -rf newdir &&
304         mkdir otherdir &&
305         GIT_DIR=otherdir git --bare init newdir &&
306         test_path_is_dir newdir/refs &&
307         test_path_is_missing otherdir/refs
308 '
309
310 test_expect_success 'init with separate gitdir' '
311         rm -rf newdir &&
312         git init --separate-git-dir realgitdir newdir &&
313         echo "gitdir: $(pwd)/realgitdir" >expected &&
314         test_cmp expected newdir/.git &&
315         test_path_is_dir realgitdir/refs
316 '
317
318 test_lazy_prereq GETCWD_IGNORES_PERMS '
319         base=GETCWD_TEST_BASE_DIR &&
320         mkdir -p $base/dir &&
321         chmod 100 $base ||
322         error "bug in test script: cannot prepare $base"
323
324         (cd $base/dir && /bin/pwd -P)
325         status=$?
326
327         chmod 700 $base &&
328         rm -rf $base ||
329         error "bug in test script: cannot clean $base"
330         return $status
331 '
332
333 check_long_base_path () {
334         # exceed initial buffer size of strbuf_getcwd()
335         component=123456789abcdef &&
336         test_when_finished "chmod 0700 $component; rm -rf $component" &&
337         p31=$component/$component &&
338         p127=$p31/$p31/$p31/$p31 &&
339         mkdir -p $p127 &&
340         if test $# = 1
341         then
342                 chmod $1 $component
343         fi &&
344         (
345                 cd $p127 &&
346                 git init newdir
347         )
348 }
349
350 test_expect_success 'init in long base path' '
351         check_long_base_path
352 '
353
354 test_expect_success GETCWD_IGNORES_PERMS 'init in long restricted base path' '
355         check_long_base_path 0111
356 '
357
358 test_expect_success 're-init on .git file' '
359         ( cd newdir && git init )
360 '
361
362 test_expect_success 're-init to update git link' '
363         (
364         cd newdir &&
365         git init --separate-git-dir ../surrealgitdir
366         ) &&
367         echo "gitdir: $(pwd)/surrealgitdir" >expected &&
368         test_cmp expected newdir/.git &&
369         test_path_is_dir surrealgitdir/refs &&
370         test_path_is_missing realgitdir/refs
371 '
372
373 test_expect_success 're-init to move gitdir' '
374         rm -rf newdir realgitdir surrealgitdir &&
375         git init newdir &&
376         (
377         cd newdir &&
378         git init --separate-git-dir ../realgitdir
379         ) &&
380         echo "gitdir: $(pwd)/realgitdir" >expected &&
381         test_cmp expected newdir/.git &&
382         test_path_is_dir realgitdir/refs
383 '
384
385 test_expect_success SYMLINKS 're-init to move gitdir symlink' '
386         rm -rf newdir realgitdir &&
387         git init newdir &&
388         (
389         cd newdir &&
390         mv .git here &&
391         ln -s here .git &&
392         git init --separate-git-dir ../realgitdir
393         ) &&
394         echo "gitdir: $(pwd)/realgitdir" >expected &&
395         test_cmp expected newdir/.git &&
396         test_cmp expected newdir/here &&
397         test_path_is_dir realgitdir/refs
398 '
399
400 # Tests for the hidden file attribute on windows
401 is_hidden () {
402         # Use the output of `attrib`, ignore the absolute path
403         case "$(attrib "$1")" in *H*?:*) return 0;; esac
404         return 1
405 }
406
407 test_expect_success MINGW '.git hidden' '
408         rm -rf newdir &&
409         (
410                 sane_unset GIT_DIR GIT_WORK_TREE &&
411                 mkdir newdir &&
412                 cd newdir &&
413                 git init &&
414                 is_hidden .git
415         ) &&
416         check_config newdir/.git false unset
417 '
418
419 test_expect_success MINGW 'bare git dir not hidden' '
420         rm -rf newdir &&
421         (
422                 sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG &&
423                 mkdir newdir &&
424                 cd newdir &&
425                 git --bare init
426         ) &&
427         ! is_hidden newdir
428 '
429
430 test_expect_success 'remote init from does not use config from cwd' '
431         rm -rf newdir &&
432         test_config core.logallrefupdates true &&
433         git init newdir &&
434         echo true >expect &&
435         git -C newdir config --bool core.logallrefupdates >actual &&
436         test_cmp expect actual
437 '
438
439 test_expect_success 're-init from a linked worktree' '
440         git init main-worktree &&
441         (
442                 cd main-worktree &&
443                 test_commit first &&
444                 git worktree add ../linked-worktree &&
445                 mv .git/info/exclude expected-exclude &&
446                 cp .git/config expected-config &&
447                 find .git/worktrees -print | sort >expected &&
448                 git -C ../linked-worktree init &&
449                 test_cmp expected-exclude .git/info/exclude &&
450                 test_cmp expected-config .git/config &&
451                 find .git/worktrees -print | sort >actual &&
452                 test_cmp expected actual
453         )
454 '
455
456 test_expect_success MINGW 'redirect std handles' '
457         GIT_REDIRECT_STDOUT=output.txt git rev-parse --git-dir &&
458         test .git = "$(cat output.txt)" &&
459         test -z "$(GIT_REDIRECT_STDOUT=off git rev-parse --git-dir)" &&
460         test_must_fail env \
461                 GIT_REDIRECT_STDOUT=output.txt \
462                 GIT_REDIRECT_STDERR="2>&1" \
463                 git rev-parse --git-dir --verify refs/invalid &&
464         printf ".git\nfatal: Needed a single revision\n" >expect &&
465         test_cmp expect output.txt
466 '
467
468 test_done