git stash: make sure that .git/logs/refs/ exists
[git/mingw/4msysgit.git] / t / t0001-init.sh
blob9fb582b192e7d44856d3687234de922217e511f4
1 #!/bin/sh
3 test_description='git init'
5 . ./test-lib.sh
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
15 bare=$(GIT_CONFIG="$1/config" git config --bool core.bare)
16 worktree=$(GIT_CONFIG="$1/config" git config core.worktree) ||
17 worktree=unset
19 test "$bare" = "$2" && test "$worktree" = "$3" || {
20 echo "expected bare=$2 worktree=$3"
21 echo " got bare=$bare worktree=$worktree"
22 return 1
26 test_expect_success 'plain' '
28 sane_unset GIT_DIR GIT_WORK_TREE &&
29 mkdir plain &&
30 cd plain &&
31 git init
32 ) &&
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 &&
41 mkdir plain-nested &&
42 cd plain-nested &&
43 git init
44 ) &&
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 &&
52 export HOME &&
53 mkdir alias-config &&
54 echo "[alias] aliasedinit = init" >alias-config/.gitconfig &&
56 GIT_CEILING_DIRECTORIES=$(pwd) &&
57 export GIT_CEILING_DIRECTORIES &&
59 mkdir plain-aliased &&
60 cd plain-aliased &&
61 git aliasedinit
62 ) &&
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 &&
72 mkdir plain-nested &&
73 cd plain-nested &&
74 git aliasedinit
75 ) &&
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 &&
85 mkdir plain-nested &&
86 cd plain-nested &&
87 git aliasedinit
88 ) &&
89 check_config bare-ancestor-aliased.git/plain-nested/.git false unset
92 test_expect_success 'plain with GIT_WORK_TREE' '
93 if (
94 sane_unset GIT_DIR &&
95 mkdir plain-wt &&
96 cd plain-wt &&
97 GIT_WORK_TREE=$(pwd) git init
99 then
100 echo Should have failed -- GIT_WORK_TREE should not be used
101 false
105 test_expect_success 'plain bare' '
107 sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG &&
108 mkdir plain-bare-1 &&
109 cd plain-bare-1 &&
110 git --bare init
111 ) &&
112 check_config plain-bare-1 true unset
115 test_expect_success 'plain bare with GIT_WORK_TREE' '
116 if (
117 sane_unset GIT_DIR GIT_CONFIG &&
118 mkdir plain-bare-2 &&
119 cd plain-bare-2 &&
120 GIT_WORK_TREE=$(pwd) git --bare init
122 then
123 echo Should have failed -- GIT_WORK_TREE should not be used
124 false
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
134 ) &&
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 &&
143 cd init-bare.git &&
144 git init --bare
145 ) &&
146 check_config init-bare.git true unset
149 test_expect_success 'GIT_DIR non-bare' '
152 sane_unset GIT_CONFIG &&
153 mkdir non-bare &&
154 cd non-bare &&
155 GIT_DIR=.git git init
156 ) &&
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
166 ) &&
167 check_config git-dir-wt-1.git false "$(pwd)"
170 test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
172 if (
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
177 then
178 echo Should have failed -- --bare should not be used
179 false
183 test_expect_success 'reinit' '
186 sane_unset GIT_CONFIG GIT_WORK_TREE GIT_CONFIG &&
188 mkdir again &&
189 cd again &&
190 git init >out1 2>err1 &&
191 git init >out2 2>err2
192 ) &&
193 test_i18ngrep "Initialized empty" again/out1 &&
194 test_i18ngrep "Reinitialized existing" again/out2 &&
195 >again/empty &&
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
207 ) &&
208 test_cmp template-source/file template-custom/.git/file
211 test_expect_success 'init with --template (blank)' '
213 mkdir template-plain &&
214 cd template-plain &&
215 git init
216 ) &&
217 test -f template-plain/.git/info/exclude &&
219 mkdir template-blank &&
220 cd template-blank &&
221 git init --template=
222 ) &&
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 &&
237 git init
238 ) &&
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
250 ) &&
251 check_config init-bare-shared-override true unset &&
252 test x0666 = \
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 &&
262 git init
263 ) &&
264 test x0666 = \
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) &&
271 mkdir test &&
272 cd test &&
273 test_must_fail git init --template=$insane
277 test_expect_success 'init creates a new directory' '
278 rm -fr newdir &&
280 git init newdir &&
281 test -d newdir/.git/refs
285 test_expect_success 'init creates a new bare directory' '
286 rm -fr newdir &&
288 git init --bare newdir &&
289 test -d newdir/refs
293 test_expect_success 'init recreates a directory' '
294 rm -fr newdir &&
296 mkdir newdir &&
297 git init newdir &&
298 test -d newdir/.git/refs
302 test_expect_success 'init recreates a new bare directory' '
303 rm -fr newdir &&
305 mkdir newdir &&
306 git init --bare newdir &&
307 test -d newdir/refs
311 test_expect_success 'init creates a new deep directory' '
312 rm -fr newdir &&
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)' '
318 rm -fr newdir &&
320 # Leading directories should honor umask while
321 # the repository itself should follow "shared"
322 umask 002 &&
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)' '
333 rm -fr newdir &&
335 >newdir &&
336 test_must_fail git init newdir &&
337 test -f newdir
341 test_expect_success 'init notices EEXIST (2)' '
342 rm -fr newdir &&
344 mkdir newdir &&
345 >newdir/a
346 test_must_fail git init newdir/a/b &&
347 test -f newdir/a
351 test_expect_success POSIXPERM,SANITY 'init notices EPERM' '
352 rm -fr newdir &&
354 mkdir newdir &&
355 chmod -w newdir &&
356 test_must_fail git init newdir/a/b
360 test_expect_success 'init creates a new bare directory with global --bare' '
361 rm -rf newdir &&
362 git --bare init newdir &&
363 test -d newdir/refs
366 test_expect_success 'init prefers command line to GIT_DIR' '
367 rm -rf newdir &&
368 mkdir otherdir &&
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' '
375 rm -rf newdir &&
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 on .git file' '
383 ( cd newdir && git init )
386 test_expect_success 're-init to update git link' '
388 cd newdir &&
389 git init --separate-git-dir ../surrealgitdir
390 ) &&
391 echo "gitdir: `pwd`/surrealgitdir" >expected &&
392 test_cmp expected newdir/.git &&
393 test -d surrealgitdir/refs &&
394 ! test -d realgitdir/refs
397 test_expect_success 're-init to move gitdir' '
398 rm -rf newdir realgitdir surrealgitdir &&
399 git init newdir &&
401 cd newdir &&
402 git init --separate-git-dir ../realgitdir
403 ) &&
404 echo "gitdir: `pwd`/realgitdir" >expected &&
405 test_cmp expected newdir/.git &&
406 test -d realgitdir/refs
409 test_expect_success SYMLINKS 're-init to move gitdir symlink' '
410 rm -rf newdir realgitdir &&
411 git init newdir &&
413 cd newdir &&
414 mv .git here &&
415 ln -s here .git &&
416 git init --separate-git-dir ../realgitdir
417 ) &&
418 echo "gitdir: `pwd`/realgitdir" >expected &&
419 test_cmp expected newdir/.git &&
420 test -d realgitdir/refs &&
421 ! test -d newdir/here
424 test_done