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"
15 bare=$(GIT_CONFIG="$1/config" git config --bool core.bare)
16 worktree=$(GIT_CONFIG="$1/config" git config core.worktree) ||
19 test "$bare" = "$2" && test "$worktree" = "$3" || {
20 echo "expected bare=$2 worktree=$3"
21 echo " got bare=$bare worktree=$worktree"
26 test_expect_success 'plain' '
28 sane_unset GIT_DIR GIT_WORK_TREE &&
33 check_config plain/.git false unset
36 test_expect_success 'plain nested in bare' '
38 sane_unset GIT_DIR GIT_WORK_TREE &&
39 git init --bare bare-ancestor.git &&
40 cd bare-ancestor.git &&
45 check_config bare-ancestor.git/plain-nested/.git false unset
48 test_expect_success 'plain through aliased command, outside any git repo' '
50 sane_unset GIT_DIR GIT_WORK_TREE &&
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_failure 'plain nested through aliased command' '
68 sane_unset GIT_DIR GIT_WORK_TREE &&
69 git init plain-ancestor-aliased &&
70 cd plain-ancestor-aliased &&
71 echo "[alias] aliasedinit = init" >>.git/config &&
76 check_config plain-ancestor-aliased/plain-nested/.git false unset
79 test_expect_failure 'plain nested in bare through aliased command' '
81 sane_unset GIT_DIR GIT_WORK_TREE &&
82 git init --bare bare-ancestor-aliased.git &&
83 cd bare-ancestor-aliased.git &&
84 echo "[alias] aliasedinit = init" >>config &&
89 check_config bare-ancestor-aliased.git/plain-nested/.git false unset
92 test_expect_success 'plain with GIT_WORK_TREE' '
97 GIT_WORK_TREE=$(pwd) git init
100 echo Should have failed -- GIT_WORK_TREE should not be used
105 test_expect_success 'plain bare' '
107 sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG &&
108 mkdir plain-bare-1 &&
112 check_config plain-bare-1 true unset
115 test_expect_success 'plain bare with GIT_WORK_TREE' '
117 sane_unset GIT_DIR GIT_CONFIG &&
118 mkdir plain-bare-2 &&
120 GIT_WORK_TREE=$(pwd) git --bare init
123 echo Should have failed -- GIT_WORK_TREE should not be used
128 test_expect_success 'GIT_DIR bare' '
131 sane_unset GIT_CONFIG &&
132 mkdir git-dir-bare.git &&
133 GIT_DIR=git-dir-bare.git git init
135 check_config git-dir-bare.git true unset
138 test_expect_success 'init --bare' '
141 sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG &&
142 mkdir init-bare.git &&
146 check_config init-bare.git true unset
149 test_expect_success 'GIT_DIR non-bare' '
152 sane_unset GIT_CONFIG &&
155 GIT_DIR=.git git init
157 check_config non-bare/.git false unset
160 test_expect_success 'GIT_DIR & GIT_WORK_TREE (1)' '
163 sane_unset GIT_CONFIG &&
164 mkdir git-dir-wt-1.git &&
165 GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-1.git git init
167 check_config git-dir-wt-1.git false "$(pwd)"
170 test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
173 sane_unset GIT_CONFIG &&
174 mkdir git-dir-wt-2.git &&
175 GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-2.git git --bare init
178 echo Should have failed -- --bare should not be used
183 test_expect_success 'reinit' '
186 sane_unset GIT_CONFIG GIT_WORK_TREE GIT_CONFIG &&
190 git init >out1 2>err1 &&
191 git init >out2 2>err2
193 test_i18ngrep "Initialized empty" again/out1 &&
194 test_i18ngrep "Reinitialized existing" again/out2 &&
196 test_i18ncmp again/empty again/err1 &&
197 test_i18ncmp again/empty again/err2
200 test_expect_success 'init with --template' '
201 mkdir template-source &&
202 echo content >template-source/file &&
204 mkdir template-custom &&
205 cd template-custom &&
206 git init --template=../template-source
208 test_cmp template-source/file template-custom/.git/file
211 test_expect_success 'init with --template (blank)' '
213 mkdir template-plain &&
217 test -f template-plain/.git/info/exclude &&
219 mkdir template-blank &&
223 ! test -f template-blank/.git/info/exclude
226 test_expect_success 'init with init.templatedir set' '
227 mkdir templatedir-source &&
228 echo Content >templatedir-source/file &&
230 test_config="${HOME}/.gitconfig" &&
231 git config -f "$test_config" init.templatedir "${HOME}/templatedir-source" &&
232 mkdir templatedir-set &&
233 cd templatedir-set &&
234 sane_unset GIT_TEMPLATE_DIR &&
235 NO_SET_GIT_TEMPLATE_DIR=t &&
236 export NO_SET_GIT_TEMPLATE_DIR &&
239 test_cmp templatedir-source/file templatedir-set/.git/file
242 test_expect_success 'init --bare/--shared overrides system/global config' '
244 test_config="$HOME"/.gitconfig &&
245 git config -f "$test_config" core.bare false &&
246 git config -f "$test_config" core.sharedRepository 0640 &&
247 mkdir init-bare-shared-override &&
248 cd init-bare-shared-override &&
249 git init --bare --shared=0666
251 check_config init-bare-shared-override true unset &&
253 x`git config -f init-bare-shared-override/config core.sharedRepository`
256 test_expect_success 'init honors global core.sharedRepository' '
258 test_config="$HOME"/.gitconfig &&
259 git config -f "$test_config" core.sharedRepository 0666 &&
260 mkdir shared-honor-global &&
261 cd shared-honor-global &&
265 x`git config -f shared-honor-global/.git/config core.sharedRepository`
268 test_expect_success 'init rejects insanely long --template' '
270 insane=$(printf "x%09999dx" 1) &&
273 test_must_fail git init --template=$insane
277 test_expect_success 'init creates a new directory' '
281 test -d newdir/.git/refs
285 test_expect_success 'init creates a new bare directory' '
288 git init --bare newdir &&
293 test_expect_success 'init recreates a directory' '
298 test -d newdir/.git/refs
302 test_expect_success 'init recreates a new bare directory' '
306 git init --bare newdir &&
311 test_expect_success 'init creates a new deep directory' '
313 git init newdir/a/b/c &&
314 test -d newdir/a/b/c/.git/refs
317 test_expect_success POSIXPERM 'init creates a new deep directory (umask vs. shared)' '
320 # Leading directories should honor umask while
321 # the repository itself should follow "shared"
323 git init --bare --shared=0660 newdir/a/b/c &&
324 test -d newdir/a/b/c/refs &&
325 ls -ld newdir/a newdir/a/b > lsab.out &&
326 ! grep -v "^drwxrw[sx]r-x" lsab.out &&
327 ls -ld newdir/a/b/c > lsc.out &&
328 ! grep -v "^drwxrw[sx]---" lsc.out
332 test_expect_success 'init notices EEXIST (1)' '
336 test_must_fail git init newdir &&
341 test_expect_success 'init notices EEXIST (2)' '
346 test_must_fail git init newdir/a/b &&
351 test_expect_success POSIXPERM,SANITY 'init notices EPERM' '
356 test_must_fail git init newdir/a/b
360 test_expect_success 'init creates a new bare directory with global --bare' '
362 git --bare init newdir &&
366 test_expect_success 'init prefers command line to GIT_DIR' '
369 GIT_DIR=otherdir git --bare init newdir &&
370 test -d newdir/refs &&
371 ! test -d otherdir/refs
374 test_expect_success 'init with separate gitdir' '
376 git init --separate-git-dir realgitdir newdir &&
377 echo "gitdir: `pwd`/realgitdir" >expected &&
378 test_cmp expected newdir/.git &&
379 test -d realgitdir/refs
382 test_expect_success 're-init to update git link' '
385 git init --separate-git-dir ../surrealgitdir
387 echo "gitdir: `pwd`/surrealgitdir" >expected &&
388 test_cmp expected newdir/.git &&
389 test -d surrealgitdir/refs &&
390 ! test -d realgitdir/refs
393 test_expect_success 're-init to move gitdir' '
394 rm -rf newdir realgitdir surrealgitdir &&
398 git init --separate-git-dir ../realgitdir
400 echo "gitdir: `pwd`/realgitdir" >expected &&
401 test_cmp expected newdir/.git &&
402 test -d realgitdir/refs
405 test_expect_success SYMLINKS 're-init to move gitdir symlink' '
406 rm -rf newdir realgitdir &&
412 git init --separate-git-dir ../realgitdir
414 echo "gitdir: `pwd`/realgitdir" >expected &&
415 test_cmp expected newdir/.git &&
416 test -d realgitdir/refs &&
417 ! test -d newdir/here