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
16 if test_have_prereq POSIXPERM && test -x "$1/config"
17 then
18 echo "$1/config is executable?"
19 return 1
20 fi
21
22 bare=$(cd "$1" && git config --bool core.bare)
23 worktree=$(cd "$1" && git config core.worktree) ||
24 worktree=unset
25
26 test "$bare" = "$2" && test "$worktree" = "$3" || {
27 echo "expected bare=$2 worktree=$3"
28 echo " got bare=$bare worktree=$worktree"
29 return 1
30 }
31}
32
33test_expect_success 'plain' '
34 git init plain &&
35 check_config plain/.git false unset
36'
37
38test_expect_success 'plain nested in bare' '
39 (
40 git init --bare bare-ancestor.git &&
41 cd bare-ancestor.git &&
42 mkdir plain-nested &&
43 cd plain-nested &&
44 git init
45 ) &&
46 check_config bare-ancestor.git/plain-nested/.git false unset
47'
48
49test_expect_success 'plain through aliased command, outside any git repo' '
50 (
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_success 'plain nested through aliased command' '
67 (
68 git init plain-ancestor-aliased &&
69 cd plain-ancestor-aliased &&
70 echo "[alias] aliasedinit = init" >>.git/config &&
71 mkdir plain-nested &&
72 cd plain-nested &&
73 git aliasedinit
74 ) &&
75 check_config plain-ancestor-aliased/plain-nested/.git false unset
76'
77
78test_expect_success 'plain nested in bare through aliased command' '
79 (
80 git init --bare bare-ancestor-aliased.git &&
81 cd bare-ancestor-aliased.git &&
82 echo "[alias] aliasedinit = init" >>config &&
83 mkdir plain-nested &&
84 cd plain-nested &&
85 git aliasedinit
86 ) &&
87 check_config bare-ancestor-aliased.git/plain-nested/.git false unset
88'
89
90test_expect_success 'No extra GIT_* on alias scripts' '
91 write_script script <<-\EOF &&
92 env |
93 sed -n \
94 -e "/^GIT_PREFIX=/d" \
95 -e "/^GIT_TEXTDOMAINDIR=/d" \
96 -e "/^GIT_/s/=.*//p" |
97 sort
98 EOF
99 ./script >expected &&
100 git config alias.script \!./script &&
101 ( mkdir sub && cd sub && git script >../actual ) &&
102 test_cmp expected actual
103'
104
105test_expect_success 'plain with GIT_WORK_TREE' '
106 mkdir plain-wt &&
107 test_must_fail env GIT_WORK_TREE="$(pwd)/plain-wt" git init plain-wt
108'
109
110test_expect_success 'plain bare' '
111 git --bare init plain-bare-1 &&
112 check_config plain-bare-1 true unset
113'
114
115test_expect_success 'plain bare with GIT_WORK_TREE' '
116 mkdir plain-bare-2 &&
117 test_must_fail \
118 env GIT_WORK_TREE="$(pwd)/plain-bare-2" \
119 git --bare init plain-bare-2
120'
121
122test_expect_success 'GIT_DIR bare' '
123 mkdir git-dir-bare.git &&
124 GIT_DIR=git-dir-bare.git git init &&
125 check_config git-dir-bare.git true unset
126'
127
128test_expect_success 'init --bare' '
129 git init --bare init-bare.git &&
130 check_config init-bare.git true unset
131'
132
133test_expect_success 'GIT_DIR non-bare' '
134
135 (
136 mkdir non-bare &&
137 cd non-bare &&
138 GIT_DIR=.git git init
139 ) &&
140 check_config non-bare/.git false unset
141'
142
143test_expect_success 'GIT_DIR & GIT_WORK_TREE (1)' '
144
145 (
146 mkdir git-dir-wt-1.git &&
147 GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-1.git git init
148 ) &&
149 check_config git-dir-wt-1.git false "$(pwd)"
150'
151
152test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
153 mkdir git-dir-wt-2.git &&
154 test_must_fail env \
155 GIT_WORK_TREE="$(pwd)" \
156 GIT_DIR=git-dir-wt-2.git \
157 git --bare init
158'
159
160test_expect_success 'reinit' '
161
162 (
163 mkdir again &&
164 cd again &&
165 git init >out1 2>err1 &&
166 git init >out2 2>err2
167 ) &&
168 test_i18ngrep "Initialized empty" again/out1 &&
169 test_i18ngrep "Reinitialized existing" again/out2 &&
170 >again/empty &&
171 test_i18ncmp again/empty again/err1 &&
172 test_i18ncmp again/empty again/err2
173'
174
175test_expect_success 'init with --template' '
176 mkdir template-source &&
177 echo content >template-source/file &&
178 git init --template=../template-source template-custom &&
179 test_cmp template-source/file template-custom/.git/file
180'
181
182test_expect_success 'init with --template (blank)' '
183 git init template-plain &&
184 test_path_is_file template-plain/.git/info/exclude &&
185 git init --template= template-blank &&
186 test_path_is_missing template-blank/.git/info/exclude
187'
188
189test_expect_success 'init with init.templatedir set' '
190 mkdir templatedir-source &&
191 echo Content >templatedir-source/file &&
192 test_config_global init.templatedir "${HOME}/templatedir-source" &&
193 (
194 mkdir templatedir-set &&
195 cd templatedir-set &&
196 sane_unset GIT_TEMPLATE_DIR &&
197 NO_SET_GIT_TEMPLATE_DIR=t &&
198 export NO_SET_GIT_TEMPLATE_DIR &&
199 git init
200 ) &&
201 test_cmp templatedir-source/file templatedir-set/.git/file
202'
203
204test_expect_success 'init --bare/--shared overrides system/global config' '
205 test_config_global core.bare false &&
206 test_config_global core.sharedRepository 0640 &&
207 git init --bare --shared=0666 init-bare-shared-override &&
208 check_config init-bare-shared-override true unset &&
209 test x0666 = \
210 x$(git config -f init-bare-shared-override/config core.sharedRepository)
211'
212
213test_expect_success 'init honors global core.sharedRepository' '
214 test_config_global core.sharedRepository 0666 &&
215 git init shared-honor-global &&
216 test x0666 = \
217 x$(git config -f shared-honor-global/.git/config core.sharedRepository)
218'
219
220test_expect_success 'init allows insanely long --template' '
221 git init --template=$(printf "x%09999dx" 1) test
222'
223
224test_expect_success 'init creates a new directory' '
225 rm -fr newdir &&
226 git init newdir &&
227 test_path_is_dir newdir/.git/refs
228'
229
230test_expect_success 'init creates a new bare directory' '
231 rm -fr newdir &&
232 git init --bare newdir &&
233 test_path_is_dir newdir/refs
234'
235
236test_expect_success 'init recreates a directory' '
237 rm -fr newdir &&
238 mkdir newdir &&
239 git init newdir &&
240 test_path_is_dir newdir/.git/refs
241'
242
243test_expect_success 'init recreates a new bare directory' '
244 rm -fr newdir &&
245 mkdir newdir &&
246 git init --bare newdir &&
247 test_path_is_dir newdir/refs
248'
249
250test_expect_success 'init creates a new deep directory' '
251 rm -fr newdir &&
252 git init newdir/a/b/c &&
253 test_path_is_dir newdir/a/b/c/.git/refs
254'
255
256test_expect_success POSIXPERM 'init creates a new deep directory (umask vs. shared)' '
257 rm -fr newdir &&
258 (
259 # Leading directories should honor umask while
260 # the repository itself should follow "shared"
261 mkdir newdir &&
262 # Remove a default ACL if possible.
263 (setfacl -k newdir 2>/dev/null || true) &&
264 umask 002 &&
265 git init --bare --shared=0660 newdir/a/b/c &&
266 test_path_is_dir newdir/a/b/c/refs &&
267 ls -ld newdir/a newdir/a/b > lsab.out &&
268 ! grep -v "^drwxrw[sx]r-x" lsab.out &&
269 ls -ld newdir/a/b/c > lsc.out &&
270 ! grep -v "^drwxrw[sx]---" lsc.out
271 )
272'
273
274test_expect_success 'init notices EEXIST (1)' '
275 rm -fr newdir &&
276 >newdir &&
277 test_must_fail git init newdir &&
278 test_path_is_file newdir
279'
280
281test_expect_success 'init notices EEXIST (2)' '
282 rm -fr newdir &&
283 mkdir newdir &&
284 >newdir/a &&
285 test_must_fail git init newdir/a/b &&
286 test_path_is_file newdir/a
287'
288
289test_expect_success POSIXPERM,SANITY 'init notices EPERM' '
290 rm -fr newdir &&
291 mkdir newdir &&
292 chmod -w newdir &&
293 test_must_fail git init newdir/a/b
294'
295
296test_expect_success 'init creates a new bare directory with global --bare' '
297 rm -rf newdir &&
298 git --bare init newdir &&
299 test_path_is_dir newdir/refs
300'
301
302test_expect_success 'init prefers command line to GIT_DIR' '
303 rm -rf newdir &&
304 mkdir otherdir &&
305 GIT_DIR=otherdir git --bare init newdir &&
306 test_path_is_dir newdir/refs &&
307 test_path_is_missing otherdir/refs
308'
309
310test_expect_success 'init with separate gitdir' '
311 rm -rf newdir &&
312 git init --separate-git-dir realgitdir newdir &&
313 echo "gitdir: $(pwd)/realgitdir" >expected &&
314 test_cmp expected newdir/.git &&
315 test_path_is_dir realgitdir/refs
316'
317
318test_expect_success 'init in long base path' '
319 # exceed initial buffer size of strbuf_getcwd()
320 component=123456789abcdef &&
321 test_when_finished "chmod 0700 $component; rm -rf $component" &&
322 p31=$component/$component &&
323 p127=$p31/$p31/$p31/$p31 &&
324 mkdir -p $p127 &&
325 chmod 0111 $component &&
326 (
327 cd $p127 &&
328 git init newdir
329 )
330'
331
332test_expect_success 're-init on .git file' '
333 ( cd newdir && git init )
334'
335
336test_expect_success 're-init to update git link' '
337 (
338 cd newdir &&
339 git init --separate-git-dir ../surrealgitdir
340 ) &&
341 echo "gitdir: $(pwd)/surrealgitdir" >expected &&
342 test_cmp expected newdir/.git &&
343 test_path_is_dir surrealgitdir/refs &&
344 test_path_is_missing realgitdir/refs
345'
346
347test_expect_success 're-init to move gitdir' '
348 rm -rf newdir realgitdir surrealgitdir &&
349 git init newdir &&
350 (
351 cd newdir &&
352 git init --separate-git-dir ../realgitdir
353 ) &&
354 echo "gitdir: $(pwd)/realgitdir" >expected &&
355 test_cmp expected newdir/.git &&
356 test_path_is_dir realgitdir/refs
357'
358
359test_expect_success SYMLINKS 're-init to move gitdir symlink' '
360 rm -rf newdir realgitdir &&
361 git init newdir &&
362 (
363 cd newdir &&
364 mv .git here &&
365 ln -s here .git &&
366 git init --separate-git-dir ../realgitdir
367 ) &&
368 echo "gitdir: $(pwd)/realgitdir" >expected &&
369 test_cmp expected newdir/.git &&
370 test_cmp expected newdir/here &&
371 test_path_is_dir realgitdir/refs
372'
373
374# Tests for the hidden file attribute on windows
375is_hidden () {
376 # Use the output of `attrib`, ignore the absolute path
377 case "$(attrib "$1")" in *H*?:*) return 0;; esac
378 return 1
379}
380
381test_expect_success MINGW '.git hidden' '
382 rm -rf newdir &&
383 (
384 unset GIT_DIR GIT_WORK_TREE
385 mkdir newdir &&
386 cd newdir &&
387 git init &&
388 is_hidden .git
389 ) &&
390 check_config newdir/.git false unset
391'
392
393test_expect_success MINGW 'bare git dir not hidden' '
394 rm -rf newdir &&
395 (
396 unset GIT_DIR GIT_WORK_TREE GIT_CONFIG
397 mkdir newdir &&
398 cd newdir &&
399 git --bare init
400 ) &&
401 ! is_hidden newdir
402'
403
404test_expect_success 'remote init from does not use config from cwd' '
405 rm -rf newdir &&
406 test_config core.logallrefupdates true &&
407 git init newdir &&
408 echo true >expect &&
409 git -C newdir config --bool core.logallrefupdates >actual &&
410 test_cmp expect actual
411'
412
413test_expect_success 're-init from a linked worktree' '
414 git init main-worktree &&
415 (
416 cd main-worktree &&
417 test_commit first &&
418 git worktree add ../linked-worktree &&
419 mv .git/info/exclude expected-exclude &&
420 cp .git/config expected-config &&
421 find .git/worktrees -print | sort >expected &&
422 git -C ../linked-worktree init &&
423 test_cmp expected-exclude .git/info/exclude &&
424 test_cmp expected-config .git/config &&
425 find .git/worktrees -print | sort >actual &&
426 test_cmp expected actual
427 )
428'
429
430test_done