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 GIT_CONFIG_NOGLOBAL &&
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 grep "Initialized empty" again/out1 &&
194 grep "Reinitialized existing" again/out2 &&
196 test_cmp again/empty again/err1 &&
197 test_cmp 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_CONFIG_NOGLOBAL &&
235 sane_unset GIT_TEMPLATE_DIR &&
236 NO_SET_GIT_TEMPLATE_DIR=t &&
237 export NO_SET_GIT_TEMPLATE_DIR &&
240 test_cmp templatedir-source/file templatedir-set/.git/file
243 test_expect_success 'init --bare/--shared overrides system/global config' '
245 test_config="$HOME"/.gitconfig &&
246 sane_unset GIT_CONFIG_NOGLOBAL &&
247 git config -f "$test_config" core.bare false &&
248 git config -f "$test_config" core.sharedRepository 0640 &&
249 mkdir init-bare-shared-override &&
250 cd init-bare-shared-override &&
251 git init --bare --shared=0666
253 check_config init-bare-shared-override true unset &&
255 x`git config -f init-bare-shared-override/config core.sharedRepository`
258 test_expect_success 'init honors global core.sharedRepository' '
260 test_config="$HOME"/.gitconfig &&
261 sane_unset GIT_CONFIG_NOGLOBAL &&
262 git config -f "$test_config" core.sharedRepository 0666 &&
263 mkdir shared-honor-global &&
264 cd shared-honor-global &&
268 x`git config -f shared-honor-global/.git/config core.sharedRepository`
271 test_expect_success 'init rejects insanely long --template' '
273 insane=$(printf "x%09999dx" 1) &&
276 test_must_fail git init --template=$insane
280 test_expect_success 'init creates a new directory' '
284 test -d newdir/.git/refs
288 test_expect_success 'init creates a new bare directory' '
291 git init --bare newdir &&
296 test_expect_success 'init recreates a directory' '
301 test -d newdir/.git/refs
305 test_expect_success 'init recreates a new bare directory' '
309 git init --bare newdir &&
314 test_expect_success 'init creates a new deep directory' '
316 git init newdir/a/b/c &&
317 test -d newdir/a/b/c/.git/refs
320 test_expect_success POSIXPERM 'init creates a new deep directory (umask vs. shared)' '
323 # Leading directories should honor umask while
324 # the repository itself should follow "shared"
326 git init --bare --shared=0660 newdir/a/b/c &&
327 test -d newdir/a/b/c/refs &&
328 ls -ld newdir/a newdir/a/b > lsab.out &&
329 ! grep -v "^drwxrw[sx]r-x" lsab.out &&
330 ls -ld newdir/a/b/c > lsc.out &&
331 ! grep -v "^drwxrw[sx]---" lsc.out
335 test_expect_success 'init notices EEXIST (1)' '
339 test_must_fail git init newdir &&
344 test_expect_success 'init notices EEXIST (2)' '
349 test_must_fail git init newdir/a/b &&
354 test_expect_success POSIXPERM,SANITY 'init notices EPERM' '
359 test_must_fail git init newdir/a/b
363 test_expect_success 'init creates a new bare directory with global --bare' '
365 git --bare init newdir &&
369 test_expect_success 'init prefers command line to GIT_DIR' '
372 GIT_DIR=otherdir git --bare init newdir &&
373 test -d newdir/refs &&
374 ! test -d otherdir/refs