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 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 with GIT_WORK_TREE' '
37 if (
38 unset GIT_DIR
39 mkdir plain-wt &&
40 cd plain-wt &&
41 GIT_WORK_TREE=$(pwd) git init
42 )
43 then
44 echo Should have failed -- GIT_WORK_TREE should not be used
45 false
46 fi
47'
48
49test_expect_success 'plain bare' '
50 (
51 unset GIT_DIR GIT_WORK_TREE GIT_CONFIG
52 mkdir plain-bare-1 &&
53 cd plain-bare-1 &&
54 git --bare init
55 ) &&
56 check_config plain-bare-1 true unset
57'
58
59test_expect_success 'plain bare with GIT_WORK_TREE' '
60 if (
61 unset GIT_DIR GIT_CONFIG
62 mkdir plain-bare-2 &&
63 cd plain-bare-2 &&
64 GIT_WORK_TREE=$(pwd) git --bare init
65 )
66 then
67 echo Should have failed -- GIT_WORK_TREE should not be used
68 false
69 fi
70'
71
72test_expect_success 'GIT_DIR bare' '
73
74 (
75 unset GIT_CONFIG
76 mkdir git-dir-bare.git &&
77 GIT_DIR=git-dir-bare.git git init
78 ) &&
79 check_config git-dir-bare.git true unset
80'
81
82test_expect_success 'init --bare' '
83
84 (
85 unset GIT_DIR GIT_WORK_TREE GIT_CONFIG
86 mkdir init-bare.git &&
87 cd init-bare.git &&
88 git init --bare
89 ) &&
90 check_config init-bare.git true unset
91'
92
93test_expect_success 'GIT_DIR non-bare' '
94
95 (
96 unset GIT_CONFIG
97 mkdir non-bare &&
98 cd non-bare &&
99 GIT_DIR=.git git init
100 ) &&
101 check_config non-bare/.git false unset
102'
103
104test_expect_success 'GIT_DIR & GIT_WORK_TREE (1)' '
105
106 (
107 unset GIT_CONFIG
108 mkdir git-dir-wt-1.git &&
109 GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-1.git git init
110 ) &&
111 check_config git-dir-wt-1.git false "$(pwd)"
112'
113
114test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
115
116 if (
117 unset GIT_CONFIG
118 mkdir git-dir-wt-2.git &&
119 GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-2.git git --bare init
120 )
121 then
122 echo Should have failed -- --bare should not be used
123 false
124 fi
125'
126
127test_expect_success 'reinit' '
128
129 (
130 unset GIT_CONFIG GIT_WORK_TREE GIT_CONFIG
131
132 mkdir again &&
133 cd again &&
134 git init >out1 2>err1 &&
135 git init >out2 2>err2
136 ) &&
137 grep "Initialized empty" again/out1 &&
138 grep "Reinitialized existing" again/out2 &&
139 >again/empty &&
140 test_cmp again/empty again/err1 &&
141 test_cmp again/empty again/err2
142'
143
144test_expect_success 'init with --template' '
145 mkdir template-source &&
146 echo content >template-source/file &&
147 (
148 mkdir template-custom &&
149 cd template-custom &&
150 git init --template=../template-source
151 ) &&
152 test_cmp template-source/file template-custom/.git/file
153'
154
155test_expect_success 'init with --template (blank)' '
156 (
157 mkdir template-plain &&
158 cd template-plain &&
159 git init
160 ) &&
161 test -f template-plain/.git/info/exclude &&
162 (
163 mkdir template-blank &&
164 cd template-blank &&
165 git init --template=
166 ) &&
167 ! test -f template-blank/.git/info/exclude
168'
169
170test_expect_success 'init with init.templatedir set' '
171 mkdir templatedir-source &&
172 echo Content >templatedir-source/file &&
173 (
174 HOME="`pwd`" &&
175 export HOME &&
176 test_config="${HOME}/.gitconfig" &&
177 git config -f "$test_config" init.templatedir "${HOME}/templatedir-source" &&
178 mkdir templatedir-set &&
179 cd templatedir-set &&
180 unset GIT_CONFIG_NOGLOBAL &&
181 unset GIT_TEMPLATE_DIR &&
182 NO_SET_GIT_TEMPLATE_DIR=t &&
183 export NO_SET_GIT_TEMPLATE_DIR &&
184 git init
185 ) &&
186 test_cmp templatedir-source/file templatedir-set/.git/file
187'
188
189test_expect_success 'init --bare/--shared overrides system/global config' '
190 (
191 HOME="`pwd`" &&
192 export HOME &&
193 test_config="$HOME"/.gitconfig &&
194 unset GIT_CONFIG_NOGLOBAL &&
195 git config -f "$test_config" core.bare false &&
196 git config -f "$test_config" core.sharedRepository 0640 &&
197 mkdir init-bare-shared-override &&
198 cd init-bare-shared-override &&
199 git init --bare --shared=0666
200 ) &&
201 check_config init-bare-shared-override true unset &&
202 test x0666 = \
203 x`git config -f init-bare-shared-override/config core.sharedRepository`
204'
205
206test_expect_success 'init honors global core.sharedRepository' '
207 (
208 HOME="`pwd`" &&
209 export HOME &&
210 test_config="$HOME"/.gitconfig &&
211 unset GIT_CONFIG_NOGLOBAL &&
212 git config -f "$test_config" core.sharedRepository 0666 &&
213 mkdir shared-honor-global &&
214 cd shared-honor-global &&
215 git init
216 ) &&
217 test x0666 = \
218 x`git config -f shared-honor-global/.git/config core.sharedRepository`
219'
220
221test_expect_success 'init rejects insanely long --template' '
222 (
223 insane=$(printf "x%09999dx" 1) &&
224 mkdir test &&
225 cd test &&
226 test_must_fail git init --template=$insane
227 )
228'
229
230test_expect_success 'init creates a new directory' '
231 rm -fr newdir &&
232 (
233 git init newdir &&
234 test -d newdir/.git/refs
235 )
236'
237
238test_expect_success 'init creates a new bare directory' '
239 rm -fr newdir &&
240 (
241 git init --bare newdir &&
242 test -d newdir/refs
243 )
244'
245
246test_expect_success 'init recreates a directory' '
247 rm -fr newdir &&
248 (
249 mkdir newdir &&
250 git init newdir &&
251 test -d newdir/.git/refs
252 )
253'
254
255test_expect_success 'init recreates a new bare directory' '
256 rm -fr newdir &&
257 (
258 mkdir newdir &&
259 git init --bare newdir &&
260 test -d newdir/refs
261 )
262'
263
264test_expect_success 'init creates a new deep directory' '
265 rm -fr newdir &&
266 git init newdir/a/b/c &&
267 test -d newdir/a/b/c/.git/refs
268'
269
270test_expect_success POSIXPERM 'init creates a new deep directory (umask vs. shared)' '
271 rm -fr newdir &&
272 (
273 # Leading directories should honor umask while
274 # the repository itself should follow "shared"
275 umask 002 &&
276 git init --bare --shared=0660 newdir/a/b/c &&
277 test -d newdir/a/b/c/refs &&
278 ls -ld newdir/a newdir/a/b > lsab.out &&
279 ! grep -v "^drwxrw[sx]r-x" lsab.out &&
280 ls -ld newdir/a/b/c > lsc.out &&
281 ! grep -v "^drwxrw[sx]---" lsc.out
282 )
283'
284
285test_expect_success 'init notices EEXIST (1)' '
286 rm -fr newdir &&
287 (
288 >newdir &&
289 test_must_fail git init newdir &&
290 test -f newdir
291 )
292'
293
294test_expect_success 'init notices EEXIST (2)' '
295 rm -fr newdir &&
296 (
297 mkdir newdir &&
298 >newdir/a
299 test_must_fail git init newdir/a/b &&
300 test -f newdir/a
301 )
302'
303
304test_expect_success POSIXPERM,SANITY 'init notices EPERM' '
305 rm -fr newdir &&
306 (
307 mkdir newdir &&
308 chmod -w newdir &&
309 test_must_fail git init newdir/a/b
310 )
311'
312
313test_expect_success 'init creates a new bare directory with global --bare' '
314 rm -rf newdir &&
315 git --bare init newdir &&
316 test -d newdir/refs
317'
318
319test_expect_success 'init prefers command line to GIT_DIR' '
320 rm -rf newdir &&
321 mkdir otherdir &&
322 GIT_DIR=otherdir git --bare init newdir &&
323 test -d newdir/refs &&
324 ! test -d otherdir/refs
325'
326
327test_done