3 test_description='git init'
8 if test -d "$1" && test -f "$1/config" && test -d "$1/refs"
12 echo "expected a directory $1, a file $1/config and $1/refs"
16 if test_have_prereq POSIXPERM && test -x "$1/config"
18 echo "$1/config is executable?"
22 bare=$(cd "$1" && git config --bool core.bare)
23 worktree=$(cd "$1" && git config core.worktree) ||
26 test "$bare" = "$2" && test "$worktree" = "$3" || {
27 echo "expected bare=$2 worktree=$3"
28 echo " got bare=$bare worktree=$worktree"
33 test_expect_success 'plain' '
35 check_config plain/.git false unset
38 test_expect_success 'plain nested in bare' '
40 git init --bare bare-ancestor.git &&
41 cd bare-ancestor.git &&
46 check_config bare-ancestor.git/plain-nested/.git false unset
49 test_expect_success 'plain through aliased command, outside any git repo' '
51 HOME=$(pwd)/alias-config &&
54 echo "[alias] aliasedinit = init" >alias-config/.gitconfig &&
56 GIT_CEILING_DIRECTORIES=$(pwd) &&
57 export GIT_CEILING_DIRECTORIES &&
59 mkdir plain-aliased &&
63 check_config plain-aliased/.git false unset
66 test_expect_success 'plain nested through aliased command' '
68 git init plain-ancestor-aliased &&
69 cd plain-ancestor-aliased &&
70 echo "[alias] aliasedinit = init" >>.git/config &&
75 check_config plain-ancestor-aliased/plain-nested/.git false unset
78 test_expect_success 'plain nested in bare through aliased command' '
80 git init --bare bare-ancestor-aliased.git &&
81 cd bare-ancestor-aliased.git &&
82 echo "[alias] aliasedinit = init" >>config &&
87 check_config bare-ancestor-aliased.git/plain-nested/.git false unset
90 test_expect_success 'No extra GIT_* on alias scripts' '
91 write_script script <<-\EOF &&
94 -e "/^GIT_PREFIX=/d" \
95 -e "/^GIT_TEXTDOMAINDIR=/d" \
96 -e "/^GIT_TRACE2_PARENT/d" \
97 -e "/^GIT_/s/=.*//p" |
100 ./script >expected &&
101 git config alias.script \!./script &&
102 ( mkdir sub && cd sub && git script >../actual ) &&
103 test_cmp expected actual
106 test_expect_success 'plain with GIT_WORK_TREE' '
108 test_must_fail env GIT_WORK_TREE="$(pwd)/plain-wt" git init plain-wt
111 test_expect_success 'plain bare' '
112 git --bare init plain-bare-1 &&
113 check_config plain-bare-1 true unset
116 test_expect_success 'plain bare with GIT_WORK_TREE' '
117 mkdir plain-bare-2 &&
119 env GIT_WORK_TREE="$(pwd)/plain-bare-2" \
120 git --bare init plain-bare-2
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
129 test_expect_success 'init --bare' '
130 git init --bare init-bare.git &&
131 check_config init-bare.git true unset
134 test_expect_success 'GIT_DIR non-bare' '
139 GIT_DIR=.git git init
141 check_config non-bare/.git false unset
144 test_expect_success 'GIT_DIR & GIT_WORK_TREE (1)' '
147 mkdir git-dir-wt-1.git &&
148 GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-1.git git init
150 check_config git-dir-wt-1.git false "$(pwd)"
153 test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
154 mkdir git-dir-wt-2.git &&
156 GIT_WORK_TREE="$(pwd)" \
157 GIT_DIR=git-dir-wt-2.git \
161 test_expect_success 'reinit' '
166 git init >out1 2>err1 &&
167 git init >out2 2>err2
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
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
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
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" &&
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 &&
201 test_cmp templatedir-source/file templatedir-set/.git/file
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 &&
210 x$(git config -f init-bare-shared-override/config core.sharedRepository)
213 test_expect_success 'init honors global core.sharedRepository' '
214 test_config_global core.sharedRepository 0666 &&
215 git init shared-honor-global &&
217 x$(git config -f shared-honor-global/.git/config core.sharedRepository)
220 test_expect_success 'init allows insanely long --template' '
221 git init --template=$(printf "x%09999dx" 1) test
224 test_expect_success 'init creates a new directory' '
227 test_path_is_dir newdir/.git/refs
230 test_expect_success 'init creates a new bare directory' '
232 git init --bare newdir &&
233 test_path_is_dir newdir/refs
236 test_expect_success 'init recreates a directory' '
240 test_path_is_dir newdir/.git/refs
243 test_expect_success 'init recreates a new bare directory' '
246 git init --bare newdir &&
247 test_path_is_dir newdir/refs
250 test_expect_success 'init creates a new deep directory' '
252 git init newdir/a/b/c &&
253 test_path_is_dir newdir/a/b/c/.git/refs
256 test_expect_success POSIXPERM 'init creates a new deep directory (umask vs. shared)' '
259 # Leading directories should honor umask while
260 # the repository itself should follow "shared"
262 # Remove a default ACL if possible.
263 (setfacl -k newdir 2>/dev/null || true) &&
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
274 test_expect_success 'init notices EEXIST (1)' '
277 test_must_fail git init newdir &&
278 test_path_is_file newdir
281 test_expect_success 'init notices EEXIST (2)' '
285 test_must_fail git init newdir/a/b &&
286 test_path_is_file newdir/a
289 test_expect_success POSIXPERM,SANITY 'init notices EPERM' '
290 test_when_finished "chmod +w newdir" &&
294 test_must_fail git init newdir/a/b
297 test_expect_success 'init creates a new bare directory with global --bare' '
299 git --bare init newdir &&
300 test_path_is_dir newdir/refs
303 test_expect_success 'init prefers command line to GIT_DIR' '
306 GIT_DIR=otherdir git --bare init newdir &&
307 test_path_is_dir newdir/refs &&
308 test_path_is_missing otherdir/refs
311 test_expect_success 'init with separate gitdir' '
313 git init --separate-git-dir realgitdir newdir &&
314 echo "gitdir: $(pwd)/realgitdir" >expected &&
315 test_cmp expected newdir/.git &&
316 test_path_is_dir realgitdir/refs
319 test_lazy_prereq GETCWD_IGNORES_PERMS '
320 base=GETCWD_TEST_BASE_DIR &&
321 mkdir -p $base/dir &&
323 BUG "cannot prepare $base"
325 (cd $base/dir && /bin/pwd -P)
330 BUG "cannot clean $base"
334 check_long_base_path () {
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 &&
351 test_expect_success 'init in long base path' '
355 test_expect_success GETCWD_IGNORES_PERMS 'init in long restricted base path' '
356 check_long_base_path 0111
359 test_expect_success 're-init on .git file' '
360 ( cd newdir && git init )
363 test_expect_success 're-init to update git link' '
366 git init --separate-git-dir ../surrealgitdir
368 echo "gitdir: $(pwd)/surrealgitdir" >expected &&
369 test_cmp expected newdir/.git &&
370 test_path_is_dir surrealgitdir/refs &&
371 test_path_is_missing realgitdir/refs
374 test_expect_success 're-init to move gitdir' '
375 rm -rf newdir realgitdir surrealgitdir &&
379 git init --separate-git-dir ../realgitdir
381 echo "gitdir: $(pwd)/realgitdir" >expected &&
382 test_cmp expected newdir/.git &&
383 test_path_is_dir realgitdir/refs
386 test_expect_success SYMLINKS 're-init to move gitdir symlink' '
387 rm -rf newdir realgitdir &&
393 git init --separate-git-dir ../realgitdir
395 echo "gitdir: $(pwd)/realgitdir" >expected &&
396 test_cmp expected newdir/.git &&
397 test_cmp expected newdir/here &&
398 test_path_is_dir realgitdir/refs
401 # Tests for the hidden file attribute on windows
403 # Use the output of `attrib`, ignore the absolute path
404 case "$(attrib "$1")" in *H*?:*) return 0;; esac
408 test_expect_success MINGW '.git hidden' '
411 sane_unset GIT_DIR GIT_WORK_TREE &&
417 check_config newdir/.git false unset
420 test_expect_success MINGW 'bare git dir not hidden' '
423 sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG &&
431 test_expect_success 'remote init from does not use config from cwd' '
433 test_config core.logallrefupdates true &&
436 git -C newdir config --bool core.logallrefupdates >actual &&
437 test_cmp expect actual
440 test_expect_success 're-init from a linked worktree' '
441 git init main-worktree &&
445 git worktree add ../linked-worktree &&
446 mv .git/info/exclude expected-exclude &&
447 cp .git/config expected-config &&
448 find .git/worktrees -print | sort >expected &&
449 git -C ../linked-worktree init &&
450 test_cmp expected-exclude .git/info/exclude &&
451 test_cmp expected-config .git/config &&
452 find .git/worktrees -print | sort >actual &&
453 test_cmp expected actual
457 test_expect_success MINGW 'core.hidedotfiles = false' '
458 git config --global core.hidedotfiles false &&
462 sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG &&
465 ! is_hidden newdir/.git
468 test_expect_success MINGW 'redirect std handles' '
469 GIT_REDIRECT_STDOUT=output.txt git rev-parse --git-dir &&
470 test .git = "$(cat output.txt)" &&
471 test -z "$(GIT_REDIRECT_STDOUT=off git rev-parse --git-dir)" &&
473 GIT_REDIRECT_STDOUT=output.txt \
474 GIT_REDIRECT_STDERR="2>&1" \
475 git rev-parse --git-dir --verify refs/invalid &&
476 grep "^\\.git\$" output.txt &&
477 grep "Needed a single revision" output.txt