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