1#!/bin/sh
2
3test_description='git init'
4
5. ./test-lib.sh
6
7check_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 bare=$(GIT_CONFIG="$1/config" git config --bool core.bare)
16 worktree=$(GIT_CONFIG="$1/config" git config core.worktree) ||
17 worktree=unset
18
19 test "$bare" = "$2" && test "$worktree" = "$3" || {
20 echo "expected bare=$2 worktree=$3"
21 echo " got bare=$bare worktree=$worktree"
22 return 1
23 }
24}
25
26test_expect_success 'plain' '
27 (
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
34'
35
36test_expect_success 'plain nested in bare' '
37 (
38 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
46'
47
48test_expect_success 'plain through aliased command, outside any git repo' '
49 (
50 unset GIT_DIR GIT_WORK_TREE GIT_CONFIG_NOGLOBAL &&
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
66test_expect_failure 'plain nested through aliased command' '
67 (
68 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
77'
78
79test_expect_failure 'plain nested in bare through aliased command' '
80 (
81 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
90'
91
92test_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
98 )
99 then
100 echo Should have failed -- GIT_WORK_TREE should not be used
101 false
102 fi
103'
104
105test_expect_success 'plain bare' '
106 (
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
113'
114
115test_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
121 )
122 then
123 echo Should have failed -- GIT_WORK_TREE should not be used
124 false
125 fi
126'
127
128test_expect_success 'GIT_DIR bare' '
129
130 (
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
136'
137
138test_expect_success 'init --bare' '
139
140 (
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
147'
148
149test_expect_success 'GIT_DIR non-bare' '
150
151 (
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
158'
159
160test_expect_success 'GIT_DIR & GIT_WORK_TREE (1)' '
161
162 (
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)"
168'
169
170test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
171
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
176 )
177 then
178 echo Should have failed -- --bare should not be used
179 false
180 fi
181'
182
183test_expect_success 'reinit' '
184
185 (
186 sane_unset GIT_CONFIG GIT_WORK_TREE GIT_CONFIG &&
187
188 mkdir again &&
189 cd again &&
190 git init >out1 2>err1 &&
191 git init >out2 2>err2
192 ) &&
193 grep "Initialized empty" again/out1 &&
194 grep "Reinitialized existing" again/out2 &&
195 >again/empty &&
196 test_cmp again/empty again/err1 &&
197 test_cmp again/empty again/err2
198'
199
200test_expect_success 'init with --template' '
201 mkdir template-source &&
202 echo content >template-source/file &&
203 (
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
209'
210
211test_expect_success 'init with --template (blank)' '
212 (
213 mkdir template-plain &&
214 cd template-plain &&
215 git init
216 ) &&
217 test -f template-plain/.git/info/exclude &&
218 (
219 mkdir template-blank &&
220 cd template-blank &&
221 git init --template=
222 ) &&
223 ! test -f template-blank/.git/info/exclude
224'
225
226test_expect_success 'init with init.templatedir set' '
227 mkdir templatedir-source &&
228 echo Content >templatedir-source/file &&
229 (
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 &&
238 git init
239 ) &&
240 test_cmp templatedir-source/file templatedir-set/.git/file
241'
242
243test_expect_success 'init --bare/--shared overrides system/global config' '
244 (
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
252 ) &&
253 check_config init-bare-shared-override true unset &&
254 test x0666 = \
255 x`git config -f init-bare-shared-override/config core.sharedRepository`
256'
257
258test_expect_success 'init honors global core.sharedRepository' '
259 (
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 &&
265 git init
266 ) &&
267 test x0666 = \
268 x`git config -f shared-honor-global/.git/config core.sharedRepository`
269'
270
271test_expect_success 'init rejects insanely long --template' '
272 (
273 insane=$(printf "x%09999dx" 1) &&
274 mkdir test &&
275 cd test &&
276 test_must_fail git init --template=$insane
277 )
278'
279
280test_expect_success 'init creates a new directory' '
281 rm -fr newdir &&
282 (
283 git init newdir &&
284 test -d newdir/.git/refs
285 )
286'
287
288test_expect_success 'init creates a new bare directory' '
289 rm -fr newdir &&
290 (
291 git init --bare newdir &&
292 test -d newdir/refs
293 )
294'
295
296test_expect_success 'init recreates a directory' '
297 rm -fr newdir &&
298 (
299 mkdir newdir &&
300 git init newdir &&
301 test -d newdir/.git/refs
302 )
303'
304
305test_expect_success 'init recreates a new bare directory' '
306 rm -fr newdir &&
307 (
308 mkdir newdir &&
309 git init --bare newdir &&
310 test -d newdir/refs
311 )
312'
313
314test_expect_success 'init creates a new deep directory' '
315 rm -fr newdir &&
316 git init newdir/a/b/c &&
317 test -d newdir/a/b/c/.git/refs
318'
319
320test_expect_success POSIXPERM 'init creates a new deep directory (umask vs. shared)' '
321 rm -fr newdir &&
322 (
323 # Leading directories should honor umask while
324 # the repository itself should follow "shared"
325 umask 002 &&
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
332 )
333'
334
335test_expect_success 'init notices EEXIST (1)' '
336 rm -fr newdir &&
337 (
338 >newdir &&
339 test_must_fail git init newdir &&
340 test -f newdir
341 )
342'
343
344test_expect_success 'init notices EEXIST (2)' '
345 rm -fr newdir &&
346 (
347 mkdir newdir &&
348 >newdir/a
349 test_must_fail git init newdir/a/b &&
350 test -f newdir/a
351 )
352'
353
354test_expect_success POSIXPERM,SANITY 'init notices EPERM' '
355 rm -fr newdir &&
356 (
357 mkdir newdir &&
358 chmod -w newdir &&
359 test_must_fail git init newdir/a/b
360 )
361'
362
363test_expect_success 'init creates a new bare directory with global --bare' '
364 rm -rf newdir &&
365 git --bare init newdir &&
366 test -d newdir/refs
367'
368
369test_expect_success 'init prefers command line to GIT_DIR' '
370 rm -rf newdir &&
371 mkdir otherdir &&
372 GIT_DIR=otherdir git --bare init newdir &&
373 test -d newdir/refs &&
374 ! test -d otherdir/refs
375'
376
377test_done