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