init: use strbufs to store paths
[git/git.git] / t / t0001-init.sh
CommitLineData
6adcca3f
JH
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
1f32ecff
MH
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
3cc6a6f0
JK
22 bare=$(cd "$1" && git config --bool core.bare)
23 worktree=$(cd "$1" && git config core.worktree) ||
6adcca3f
JH
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' '
410c3428 34 git init plain &&
6adcca3f
JH
35 check_config plain/.git false unset
36'
37
4ad8332e
JN
38test_expect_success 'plain nested in bare' '
39 (
4ad8332e
JN
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 (
4ad8332e
JN
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
c0562611 66test_expect_success 'plain nested through aliased command' '
4ad8332e 67 (
4ad8332e
JN
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
c0562611 78test_expect_success 'plain nested in bare through aliased command' '
4ad8332e 79 (
4ad8332e
JN
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
6adcca3f
JH
88'
89
90test_expect_success 'plain with GIT_WORK_TREE' '
0981140f
JK
91 mkdir plain-wt &&
92 test_must_fail env GIT_WORK_TREE="$(pwd)/plain-wt" git init plain-wt
6adcca3f
JH
93'
94
95test_expect_success 'plain bare' '
410c3428 96 git --bare init plain-bare-1 &&
6adcca3f
JH
97 check_config plain-bare-1 true unset
98'
99
100test_expect_success 'plain bare with GIT_WORK_TREE' '
0981140f
JK
101 mkdir plain-bare-2 &&
102 test_must_fail \
103 env GIT_WORK_TREE="$(pwd)/plain-bare-2" \
104 git --bare init plain-bare-2
6adcca3f
JH
105'
106
107test_expect_success 'GIT_DIR bare' '
99e1c736
JK
108 mkdir git-dir-bare.git &&
109 GIT_DIR=git-dir-bare.git git init &&
6adcca3f
JH
110 check_config git-dir-bare.git true unset
111'
112
74d3b23f 113test_expect_success 'init --bare' '
410c3428 114 git init --bare init-bare.git &&
b6138273 115 check_config init-bare.git true unset
74d3b23f
LR
116'
117
6adcca3f
JH
118test_expect_success 'GIT_DIR non-bare' '
119
120 (
6adcca3f
JH
121 mkdir non-bare &&
122 cd non-bare &&
123 GIT_DIR=.git git init
124 ) &&
125 check_config non-bare/.git false unset
126'
127
128test_expect_success 'GIT_DIR & GIT_WORK_TREE (1)' '
129
130 (
6adcca3f
JH
131 mkdir git-dir-wt-1.git &&
132 GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-1.git git init
133 ) &&
134 check_config git-dir-wt-1.git false "$(pwd)"
135'
136
137test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
0981140f
JK
138 mkdir git-dir-wt-2.git &&
139 test_must_fail env \
140 GIT_WORK_TREE="$(pwd)" \
141 GIT_DIR=git-dir-wt-2.git \
142 git --bare init
6adcca3f
JH
143'
144
127df8c6 145test_expect_success 'reinit' '
5cc8f372
JS
146
147 (
5cc8f372
JS
148 mkdir again &&
149 cd again &&
150 git init >out1 2>err1 &&
151 git init >out2 2>err2
152 ) &&
127df8c6
JH
153 test_i18ngrep "Initialized empty" again/out1 &&
154 test_i18ngrep "Reinitialized existing" again/out2 &&
5cc8f372 155 >again/empty &&
127df8c6
JH
156 test_i18ncmp again/empty again/err1 &&
157 test_i18ncmp again/empty again/err2
5cc8f372
JS
158'
159
172035f0
JK
160test_expect_success 'init with --template' '
161 mkdir template-source &&
162 echo content >template-source/file &&
410c3428 163 git init --template=../template-source template-custom &&
172035f0
JK
164 test_cmp template-source/file template-custom/.git/file
165'
166
167test_expect_success 'init with --template (blank)' '
410c3428 168 git init template-plain &&
633734d4 169 test_path_is_file template-plain/.git/info/exclude &&
410c3428 170 git init --template= template-blank &&
633734d4 171 test_path_is_missing template-blank/.git/info/exclude
172035f0
JK
172'
173
a94d305b
SD
174test_expect_success 'init with init.templatedir set' '
175 mkdir templatedir-source &&
176 echo Content >templatedir-source/file &&
2a472410 177 test_config_global init.templatedir "${HOME}/templatedir-source" &&
a94d305b 178 (
a94d305b
SD
179 mkdir templatedir-set &&
180 cd templatedir-set &&
00648ba0 181 sane_unset GIT_TEMPLATE_DIR &&
a94d305b
SD
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
0a2c7eea 189test_expect_success 'init --bare/--shared overrides system/global config' '
2a472410
JK
190 test_config_global core.bare false &&
191 test_config_global core.sharedRepository 0640 &&
410c3428 192 git init --bare --shared=0666 init-bare-shared-override &&
0a2c7eea
DM
193 check_config init-bare-shared-override true unset &&
194 test x0666 = \
88619b3e 195 x$(git config -f init-bare-shared-override/config core.sharedRepository)
0a2c7eea
DM
196'
197
198test_expect_success 'init honors global core.sharedRepository' '
2a472410 199 test_config_global core.sharedRepository 0666 &&
410c3428 200 git init shared-honor-global &&
0a2c7eea 201 test x0666 = \
88619b3e 202 x$(git config -f shared-honor-global/.git/config core.sharedRepository)
0a2c7eea
DM
203'
204
9c28390b
JK
205test_expect_success 'init allows insanely long --template' '
206 git init --template=$(printf "x%09999dx" 1) test
32d1776b
FL
207'
208
53d48885
NS
209test_expect_success 'init creates a new directory' '
210 rm -fr newdir &&
99e1c736
JK
211 git init newdir &&
212 test_path_is_dir newdir/.git/refs
53d48885
NS
213'
214
215test_expect_success 'init creates a new bare directory' '
216 rm -fr newdir &&
99e1c736
JK
217 git init --bare newdir &&
218 test_path_is_dir newdir/refs
53d48885
NS
219'
220
221test_expect_success 'init recreates a directory' '
222 rm -fr newdir &&
99e1c736
JK
223 mkdir newdir &&
224 git init newdir &&
225 test_path_is_dir newdir/.git/refs
53d48885
NS
226'
227
228test_expect_success 'init recreates a new bare directory' '
229 rm -fr newdir &&
99e1c736
JK
230 mkdir newdir &&
231 git init --bare newdir &&
232 test_path_is_dir newdir/refs
53d48885
NS
233'
234
235test_expect_success 'init creates a new deep directory' '
d82e75e8
JS
236 rm -fr newdir &&
237 git init newdir/a/b/c &&
633734d4 238 test_path_is_dir newdir/a/b/c/.git/refs
d82e75e8
JS
239'
240
241test_expect_success POSIXPERM 'init creates a new deep directory (umask vs. shared)' '
53d48885
NS
242 rm -fr newdir &&
243 (
244 # Leading directories should honor umask while
245 # the repository itself should follow "shared"
246 umask 002 &&
247 git init --bare --shared=0660 newdir/a/b/c &&
633734d4 248 test_path_is_dir newdir/a/b/c/refs &&
53d48885 249 ls -ld newdir/a newdir/a/b > lsab.out &&
7d53a07a 250 ! grep -v "^drwxrw[sx]r-x" lsab.out &&
53d48885
NS
251 ls -ld newdir/a/b/c > lsc.out &&
252 ! grep -v "^drwxrw[sx]---" lsc.out
253 )
254'
255
256test_expect_success 'init notices EEXIST (1)' '
257 rm -fr newdir &&
99e1c736
JK
258 >newdir &&
259 test_must_fail git init newdir &&
260 test_path_is_file newdir
53d48885
NS
261'
262
263test_expect_success 'init notices EEXIST (2)' '
264 rm -fr newdir &&
99e1c736
JK
265 mkdir newdir &&
266 >newdir/a &&
267 test_must_fail git init newdir/a/b &&
268 test_path_is_file newdir/a
53d48885
NS
269'
270
c91cfd19 271test_expect_success POSIXPERM,SANITY 'init notices EPERM' '
53d48885 272 rm -fr newdir &&
99e1c736
JK
273 mkdir newdir &&
274 chmod -w newdir &&
275 test_must_fail git init newdir/a/b
53d48885
NS
276'
277
87a074df
JK
278test_expect_success 'init creates a new bare directory with global --bare' '
279 rm -rf newdir &&
280 git --bare init newdir &&
633734d4 281 test_path_is_dir newdir/refs
87a074df
JK
282'
283
284test_expect_success 'init prefers command line to GIT_DIR' '
285 rm -rf newdir &&
286 mkdir otherdir &&
287 GIT_DIR=otherdir git --bare init newdir &&
633734d4
JK
288 test_path_is_dir newdir/refs &&
289 test_path_is_missing otherdir/refs
87a074df
JK
290'
291
b57fb80a
NTND
292test_expect_success 'init with separate gitdir' '
293 rm -rf newdir &&
294 git init --separate-git-dir realgitdir newdir &&
88619b3e 295 echo "gitdir: $(pwd)/realgitdir" >expected &&
b57fb80a 296 test_cmp expected newdir/.git &&
633734d4 297 test_path_is_dir realgitdir/refs
b57fb80a
NTND
298'
299
487a2b73
NTND
300test_expect_success 're-init on .git file' '
301 ( cd newdir && git init )
302'
303
b57fb80a
NTND
304test_expect_success 're-init to update git link' '
305 (
306 cd newdir &&
307 git init --separate-git-dir ../surrealgitdir
308 ) &&
88619b3e 309 echo "gitdir: $(pwd)/surrealgitdir" >expected &&
b57fb80a 310 test_cmp expected newdir/.git &&
633734d4
JK
311 test_path_is_dir surrealgitdir/refs &&
312 test_path_is_missing realgitdir/refs
b57fb80a
NTND
313'
314
315test_expect_success 're-init to move gitdir' '
316 rm -rf newdir realgitdir surrealgitdir &&
317 git init newdir &&
318 (
319 cd newdir &&
320 git init --separate-git-dir ../realgitdir
321 ) &&
88619b3e 322 echo "gitdir: $(pwd)/realgitdir" >expected &&
b57fb80a 323 test_cmp expected newdir/.git &&
633734d4 324 test_path_is_dir realgitdir/refs
b57fb80a
NTND
325'
326
4e95fb6c 327test_expect_success SYMLINKS 're-init to move gitdir symlink' '
b57fb80a
NTND
328 rm -rf newdir realgitdir &&
329 git init newdir &&
330 (
331 cd newdir &&
332 mv .git here &&
333 ln -s here .git &&
09ffc706 334 git init --separate-git-dir ../realgitdir
b57fb80a 335 ) &&
88619b3e 336 echo "gitdir: $(pwd)/realgitdir" >expected &&
b57fb80a 337 test_cmp expected newdir/.git &&
3d06c5f1 338 test_cmp expected newdir/here &&
633734d4 339 test_path_is_dir realgitdir/refs
b57fb80a
NTND
340'
341
6adcca3f 342test_done