blob: 49e9bf77c6037541a9bdd8d98d482b3d40f6e070 [file] [log] [blame]
Junio C Hamano6adcca32007-08-27 00:58:06 -07001#!/bin/sh
2
3test_description='git init'
4
Ævar Arnfjörð Bjarmasonc1500642021-10-12 15:56:37 +02005TEST_PASSES_SANITIZE_LEAK=true
Junio C Hamano6adcca32007-08-27 00:58:06 -07006. ./test-lib.sh
7
8check_config () {
Shaoxuan Yuand4fe0662022-01-21 18:21:09 +08009 if test_path_is_dir "$1" &&
10 test_path_is_file "$1/config" && test_path_is_dir "$1/refs"
Junio C Hamano6adcca32007-08-27 00:58:06 -070011 then
12 : happy
13 else
14 echo "expected a directory $1, a file $1/config and $1/refs"
15 return 1
16 fi
Michael Haggerty1f32ecf2014-11-18 14:50:24 +010017
18 if test_have_prereq POSIXPERM && test -x "$1/config"
19 then
20 echo "$1/config is executable?"
21 return 1
22 fi
23
Jeff King3cc6a6f2014-03-20 19:15:24 -040024 bare=$(cd "$1" && git config --bool core.bare)
25 worktree=$(cd "$1" && git config core.worktree) ||
Junio C Hamano6adcca32007-08-27 00:58:06 -070026 worktree=unset
27
28 test "$bare" = "$2" && test "$worktree" = "$3" || {
29 echo "expected bare=$2 worktree=$3"
30 echo " got bare=$bare worktree=$worktree"
31 return 1
32 }
33}
34
35test_expect_success 'plain' '
Jeff King410c3422014-03-20 19:23:06 -040036 git init plain &&
Junio C Hamano6adcca32007-08-27 00:58:06 -070037 check_config plain/.git false unset
38'
39
Jonathan Nieder4ad83322010-11-26 22:32:41 +070040test_expect_success 'plain nested in bare' '
41 (
Jonathan Nieder4ad83322010-11-26 22:32:41 +070042 git init --bare bare-ancestor.git &&
43 cd bare-ancestor.git &&
44 mkdir plain-nested &&
45 cd plain-nested &&
46 git init
47 ) &&
48 check_config bare-ancestor.git/plain-nested/.git false unset
49'
50
51test_expect_success 'plain through aliased command, outside any git repo' '
52 (
Jonathan Nieder4ad83322010-11-26 22:32:41 +070053 HOME=$(pwd)/alias-config &&
54 export HOME &&
55 mkdir alias-config &&
56 echo "[alias] aliasedinit = init" >alias-config/.gitconfig &&
57
58 GIT_CEILING_DIRECTORIES=$(pwd) &&
59 export GIT_CEILING_DIRECTORIES &&
60
61 mkdir plain-aliased &&
62 cd plain-aliased &&
63 git aliasedinit
64 ) &&
65 check_config plain-aliased/.git false unset
66'
67
Nguyễn Thái Ngọc Duyc0562612014-06-08 16:37:10 +070068test_expect_success 'plain nested through aliased command' '
Jonathan Nieder4ad83322010-11-26 22:32:41 +070069 (
Jonathan Nieder4ad83322010-11-26 22:32:41 +070070 git init plain-ancestor-aliased &&
71 cd plain-ancestor-aliased &&
72 echo "[alias] aliasedinit = init" >>.git/config &&
73 mkdir plain-nested &&
74 cd plain-nested &&
75 git aliasedinit
76 ) &&
77 check_config plain-ancestor-aliased/plain-nested/.git false unset
78'
79
Nguyễn Thái Ngọc Duyc0562612014-06-08 16:37:10 +070080test_expect_success 'plain nested in bare through aliased command' '
Jonathan Nieder4ad83322010-11-26 22:32:41 +070081 (
Jonathan Nieder4ad83322010-11-26 22:32:41 +070082 git init --bare bare-ancestor-aliased.git &&
83 cd bare-ancestor-aliased.git &&
84 echo "[alias] aliasedinit = init" >>config &&
85 mkdir plain-nested &&
86 cd plain-nested &&
87 git aliasedinit
88 ) &&
89 check_config bare-ancestor-aliased.git/plain-nested/.git false unset
90'
91
Nguyễn Thái Ngọc Duy57ea7122015-12-20 14:50:19 +070092test_expect_success 'No extra GIT_* on alias scripts' '
Johannes Sixtf3858f82016-03-03 07:55:17 +010093 write_script script <<-\EOF &&
94 env |
95 sed -n \
96 -e "/^GIT_PREFIX=/d" \
97 -e "/^GIT_TEXTDOMAINDIR=/d" \
SZEDER Gábore4b75d62019-05-19 16:43:08 +020098 -e "/^GIT_TRACE2_PARENT/d" \
Johannes Sixtf3858f82016-03-03 07:55:17 +010099 -e "/^GIT_/s/=.*//p" |
100 sort
Nguyễn Thái Ngọc Duy57ea7122015-12-20 14:50:19 +0700101 EOF
Johannes Sixtf3858f82016-03-03 07:55:17 +0100102 ./script >expected &&
Nguyễn Thái Ngọc Duy57ea7122015-12-20 14:50:19 +0700103 git config alias.script \!./script &&
Johannes Sixtf3858f82016-03-03 07:55:17 +0100104 ( mkdir sub && cd sub && git script >../actual ) &&
Nguyễn Thái Ngọc Duy57ea7122015-12-20 14:50:19 +0700105 test_cmp expected actual
106'
107
Junio C Hamano6adcca32007-08-27 00:58:06 -0700108test_expect_success 'plain with GIT_WORK_TREE' '
Jeff King09811402014-03-20 19:19:50 -0400109 mkdir plain-wt &&
110 test_must_fail env GIT_WORK_TREE="$(pwd)/plain-wt" git init plain-wt
Junio C Hamano6adcca32007-08-27 00:58:06 -0700111'
112
113test_expect_success 'plain bare' '
Jeff King410c3422014-03-20 19:23:06 -0400114 git --bare init plain-bare-1 &&
Junio C Hamano6adcca32007-08-27 00:58:06 -0700115 check_config plain-bare-1 true unset
116'
117
118test_expect_success 'plain bare with GIT_WORK_TREE' '
Jeff King09811402014-03-20 19:19:50 -0400119 mkdir plain-bare-2 &&
120 test_must_fail \
121 env GIT_WORK_TREE="$(pwd)/plain-bare-2" \
122 git --bare init plain-bare-2
Junio C Hamano6adcca32007-08-27 00:58:06 -0700123'
124
125test_expect_success 'GIT_DIR bare' '
Jeff King99e1c732014-03-20 19:21:25 -0400126 mkdir git-dir-bare.git &&
127 GIT_DIR=git-dir-bare.git git init &&
Junio C Hamano6adcca32007-08-27 00:58:06 -0700128 check_config git-dir-bare.git true unset
129'
130
Luciano Rocha74d3b232008-05-28 19:53:57 +0100131test_expect_success 'init --bare' '
Jeff King410c3422014-03-20 19:23:06 -0400132 git init --bare init-bare.git &&
Miklos Vajnab6138272008-07-11 02:12:03 +0200133 check_config init-bare.git true unset
Luciano Rocha74d3b232008-05-28 19:53:57 +0100134'
135
Junio C Hamano6adcca32007-08-27 00:58:06 -0700136test_expect_success 'GIT_DIR non-bare' '
137
138 (
Junio C Hamano6adcca32007-08-27 00:58:06 -0700139 mkdir non-bare &&
140 cd non-bare &&
141 GIT_DIR=.git git init
142 ) &&
143 check_config non-bare/.git false unset
144'
145
146test_expect_success 'GIT_DIR & GIT_WORK_TREE (1)' '
147
148 (
Junio C Hamano6adcca32007-08-27 00:58:06 -0700149 mkdir git-dir-wt-1.git &&
150 GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-1.git git init
151 ) &&
152 check_config git-dir-wt-1.git false "$(pwd)"
153'
154
155test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
Jeff King09811402014-03-20 19:19:50 -0400156 mkdir git-dir-wt-2.git &&
157 test_must_fail env \
158 GIT_WORK_TREE="$(pwd)" \
159 GIT_DIR=git-dir-wt-2.git \
160 git --bare init
Junio C Hamano6adcca32007-08-27 00:58:06 -0700161'
162
Junio C Hamano127df8c2011-04-12 15:57:08 -0700163test_expect_success 'reinit' '
Johannes Schindelin5cc8f372008-03-24 16:14:52 +0100164
165 (
Johannes Schindelin5cc8f372008-03-24 16:14:52 +0100166 mkdir again &&
167 cd again &&
Johannes Schindelin675704c2020-12-11 11:36:57 +0000168 git -c init.defaultBranch=initial init >out1 2>err1 &&
Johannes Schindelin5cc8f372008-03-24 16:14:52 +0100169 git init >out2 2>err2
170 ) &&
Junio C Hamano67892752023-10-31 14:23:30 +0900171 test_grep "Initialized empty" again/out1 &&
172 test_grep "Reinitialized existing" again/out2 &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +0200173 test_must_be_empty again/err1 &&
174 test_must_be_empty again/err2
Johannes Schindelin5cc8f372008-03-24 16:14:52 +0100175'
176
Jeff King172035f2008-07-28 02:02:04 -0400177test_expect_success 'init with --template' '
178 mkdir template-source &&
179 echo content >template-source/file &&
Nguyễn Thái Ngọc Duye1df7fe2019-05-10 17:46:57 +0700180 git init --template=template-source template-custom &&
Jeff King172035f2008-07-28 02:02:04 -0400181 test_cmp template-source/file template-custom/.git/file
182'
183
184test_expect_success 'init with --template (blank)' '
Jeff King410c3422014-03-20 19:23:06 -0400185 git init template-plain &&
Jeff King633734d2014-03-20 19:17:35 -0400186 test_path_is_file template-plain/.git/info/exclude &&
Jeff King410c3422014-03-20 19:23:06 -0400187 git init --template= template-blank &&
Jeff King633734d2014-03-20 19:17:35 -0400188 test_path_is_missing template-blank/.git/info/exclude
Jeff King172035f2008-07-28 02:02:04 -0400189'
190
Matheus Tavaresa185dd52021-05-25 00:41:01 -0300191init_no_templatedir_env () {
192 (
193 sane_unset GIT_TEMPLATE_DIR &&
194 NO_SET_GIT_TEMPLATE_DIR=t &&
195 export NO_SET_GIT_TEMPLATE_DIR &&
196 git init "$1"
197 )
198}
199
Steven Drakea94d3052010-02-26 17:00:21 +1300200test_expect_success 'init with init.templatedir set' '
201 mkdir templatedir-source &&
202 echo Content >templatedir-source/file &&
Jeff King2a472412014-03-20 19:18:12 -0400203 test_config_global init.templatedir "${HOME}/templatedir-source" &&
Matheus Tavaresa185dd52021-05-25 00:41:01 -0300204
205 init_no_templatedir_env templatedir-set &&
Steven Drakea94d3052010-02-26 17:00:21 +1300206 test_cmp templatedir-source/file templatedir-set/.git/file
207'
208
Matheus Tavaresa185dd52021-05-25 00:41:01 -0300209test_expect_success 'init with init.templatedir using ~ expansion' '
210 mkdir -p templatedir-source &&
211 echo Content >templatedir-source/file &&
212 test_config_global init.templatedir "~/templatedir-source" &&
213
214 init_no_templatedir_env templatedir-expansion &&
215 test_cmp templatedir-source/file templatedir-expansion/.git/file
216'
217
Deskin Miller0a2c7ee2008-10-07 01:37:48 -0400218test_expect_success 'init --bare/--shared overrides system/global config' '
Jeff King2a472412014-03-20 19:18:12 -0400219 test_config_global core.bare false &&
220 test_config_global core.sharedRepository 0640 &&
Jeff King410c3422014-03-20 19:23:06 -0400221 git init --bare --shared=0666 init-bare-shared-override &&
Deskin Miller0a2c7ee2008-10-07 01:37:48 -0400222 check_config init-bare-shared-override true unset &&
223 test x0666 = \
Elia Pinto88619b32014-04-28 05:57:24 -0700224 x$(git config -f init-bare-shared-override/config core.sharedRepository)
Deskin Miller0a2c7ee2008-10-07 01:37:48 -0400225'
226
227test_expect_success 'init honors global core.sharedRepository' '
Jeff King2a472412014-03-20 19:18:12 -0400228 test_config_global core.sharedRepository 0666 &&
Jeff King410c3422014-03-20 19:23:06 -0400229 git init shared-honor-global &&
Deskin Miller0a2c7ee2008-10-07 01:37:48 -0400230 test x0666 = \
Elia Pinto88619b32014-04-28 05:57:24 -0700231 x$(git config -f shared-honor-global/.git/config core.sharedRepository)
Deskin Miller0a2c7ee2008-10-07 01:37:48 -0400232'
233
Jeff King9c283902015-10-04 23:46:04 -0400234test_expect_success 'init allows insanely long --template' '
235 git init --template=$(printf "x%09999dx" 1) test
Frank Lichtenheld32d17762009-04-18 16:14:02 +0200236'
237
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900238test_expect_success 'init creates a new directory' '
239 rm -fr newdir &&
Jeff King99e1c732014-03-20 19:21:25 -0400240 git init newdir &&
241 test_path_is_dir newdir/.git/refs
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900242'
243
244test_expect_success 'init creates a new bare directory' '
245 rm -fr newdir &&
Jeff King99e1c732014-03-20 19:21:25 -0400246 git init --bare newdir &&
247 test_path_is_dir newdir/refs
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900248'
249
250test_expect_success 'init recreates a directory' '
251 rm -fr newdir &&
Jeff King99e1c732014-03-20 19:21:25 -0400252 mkdir newdir &&
253 git init newdir &&
254 test_path_is_dir newdir/.git/refs
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900255'
256
257test_expect_success 'init recreates a new bare directory' '
258 rm -fr newdir &&
Jeff King99e1c732014-03-20 19:21:25 -0400259 mkdir newdir &&
260 git init --bare newdir &&
261 test_path_is_dir newdir/refs
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900262'
263
264test_expect_success 'init creates a new deep directory' '
265 rm -fr newdir &&
Johannes Sixtd82e75e2009-08-09 18:02:55 +0200266 git init newdir/a/b/c &&
Jeff King633734d2014-03-20 19:17:35 -0400267 test_path_is_dir newdir/a/b/c/.git/refs
Johannes Sixtd82e75e2009-08-09 18:02:55 +0200268'
269
270test_expect_success POSIXPERM 'init creates a new deep directory (umask vs. shared)' '
271 rm -fr newdir &&
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900272 (
273 # Leading directories should honor umask while
274 # the repository itself should follow "shared"
Matt McCutchend549d212017-01-28 15:25:48 -0500275 mkdir newdir &&
276 # Remove a default ACL if possible.
277 (setfacl -k newdir 2>/dev/null || true) &&
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900278 umask 002 &&
279 git init --bare --shared=0660 newdir/a/b/c &&
Jeff King633734d2014-03-20 19:17:35 -0400280 test_path_is_dir newdir/a/b/c/refs &&
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900281 ls -ld newdir/a newdir/a/b > lsab.out &&
Johannes Sixt7d53a072009-08-09 17:38:04 +0200282 ! grep -v "^drwxrw[sx]r-x" lsab.out &&
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900283 ls -ld newdir/a/b/c > lsc.out &&
284 ! grep -v "^drwxrw[sx]---" lsc.out
285 )
286'
287
288test_expect_success 'init notices EEXIST (1)' '
289 rm -fr newdir &&
Jeff King99e1c732014-03-20 19:21:25 -0400290 >newdir &&
291 test_must_fail git init newdir &&
292 test_path_is_file newdir
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900293'
294
295test_expect_success 'init notices EEXIST (2)' '
296 rm -fr newdir &&
Jeff King99e1c732014-03-20 19:21:25 -0400297 mkdir newdir &&
298 >newdir/a &&
299 test_must_fail git init newdir/a/b &&
300 test_path_is_file newdir/a
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900301'
302
Ævar Arnfjörð Bjarmasonc91cfd12010-08-06 22:09:09 +0000303test_expect_success POSIXPERM,SANITY 'init notices EPERM' '
Junio C Hamano03771422018-06-15 11:13:39 -0700304 test_when_finished "chmod +w newdir" &&
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900305 rm -fr newdir &&
Jeff King99e1c732014-03-20 19:21:25 -0400306 mkdir newdir &&
307 chmod -w newdir &&
308 test_must_fail git init newdir/a/b
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900309'
310
Jeff King87a074d2010-05-10 05:42:06 -0400311test_expect_success 'init creates a new bare directory with global --bare' '
312 rm -rf newdir &&
313 git --bare init newdir &&
Jeff King633734d2014-03-20 19:17:35 -0400314 test_path_is_dir newdir/refs
Jeff King87a074d2010-05-10 05:42:06 -0400315'
316
317test_expect_success 'init prefers command line to GIT_DIR' '
318 rm -rf newdir &&
319 mkdir otherdir &&
320 GIT_DIR=otherdir git --bare init newdir &&
Jeff King633734d2014-03-20 19:17:35 -0400321 test_path_is_dir newdir/refs &&
322 test_path_is_missing otherdir/refs
Jeff King87a074d2010-05-10 05:42:06 -0400323'
324
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700325test_expect_success 'init with separate gitdir' '
326 rm -rf newdir &&
327 git init --separate-git-dir realgitdir newdir &&
Johannes Schindelined33bd82019-06-24 19:40:05 +0200328 newdir_git="$(cat newdir/.git)" &&
329 test_cmp_fspath "$(pwd)/realgitdir" "${newdir_git#gitdir: }" &&
Jeff King633734d2014-03-20 19:17:35 -0400330 test_path_is_dir realgitdir/refs
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700331'
332
Eric Sunshineccf236a2020-08-09 18:53:16 -0400333test_expect_success 'explicit bare & --separate-git-dir incompatible' '
334 test_must_fail git init --bare --separate-git-dir goop.git bare.git 2>err &&
Junio C Hamano67892752023-10-31 14:23:30 +0900335 test_grep "cannot be used together" err
Eric Sunshineccf236a2020-08-09 18:53:16 -0400336'
337
338test_expect_success 'implicit bare & --separate-git-dir incompatible' '
339 test_when_finished "rm -rf bare.git" &&
340 mkdir -p bare.git &&
341 test_must_fail env GIT_DIR=. \
342 git -C bare.git init --separate-git-dir goop.git 2>err &&
Junio C Hamano67892752023-10-31 14:23:30 +0900343 test_grep "incompatible" err
Eric Sunshineccf236a2020-08-09 18:53:16 -0400344'
345
Eric Sunshine59d876c2020-08-31 02:58:00 -0400346test_expect_success 'bare & --separate-git-dir incompatible within worktree' '
347 test_when_finished "rm -rf bare.git linkwt seprepo" &&
348 test_commit gumby &&
349 git clone --bare . bare.git &&
350 git -C bare.git worktree add --detach ../linkwt &&
351 test_must_fail git -C linkwt init --separate-git-dir seprepo 2>err &&
Junio C Hamano67892752023-10-31 14:23:30 +0900352 test_grep "incompatible" err
Eric Sunshine59d876c2020-08-31 02:58:00 -0400353'
354
René Scharfebed67872017-08-07 13:04:18 +0200355test_lazy_prereq GETCWD_IGNORES_PERMS '
356 base=GETCWD_TEST_BASE_DIR &&
357 mkdir -p $base/dir &&
358 chmod 100 $base ||
SZEDER Gábor165293a2018-11-19 14:13:26 +0100359 BUG "cannot prepare $base"
René Scharfebed67872017-08-07 13:04:18 +0200360
Ævar Arnfjörð Bjarmason482e1482021-07-30 18:18:14 +0200361 (
362 cd $base/dir &&
363 test-tool getcwd
364 )
René Scharfebed67872017-08-07 13:04:18 +0200365 status=$?
366
367 chmod 700 $base &&
368 rm -rf $base ||
SZEDER Gábor165293a2018-11-19 14:13:26 +0100369 BUG "cannot clean $base"
René Scharfebed67872017-08-07 13:04:18 +0200370 return $status
371'
372
373check_long_base_path () {
René Scharfea54e9382017-03-26 15:43:50 +0200374 # exceed initial buffer size of strbuf_getcwd()
375 component=123456789abcdef &&
376 test_when_finished "chmod 0700 $component; rm -rf $component" &&
377 p31=$component/$component &&
378 p127=$p31/$p31/$p31/$p31 &&
379 mkdir -p $p127 &&
René Scharfebed67872017-08-07 13:04:18 +0200380 if test $# = 1
381 then
382 chmod $1 $component
383 fi &&
René Scharfea54e9382017-03-26 15:43:50 +0200384 (
385 cd $p127 &&
386 git init newdir
387 )
René Scharfebed67872017-08-07 13:04:18 +0200388}
389
390test_expect_success 'init in long base path' '
391 check_long_base_path
392'
393
394test_expect_success GETCWD_IGNORES_PERMS 'init in long restricted base path' '
395 check_long_base_path 0111
René Scharfea54e9382017-03-26 15:43:50 +0200396'
397
Nguyễn Thái Ngọc Duy487a2b72013-08-31 08:04:14 +0700398test_expect_success 're-init on .git file' '
399 ( cd newdir && git init )
400'
401
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700402test_expect_success 're-init to update git link' '
Johannes Schindelined33bd82019-06-24 19:40:05 +0200403 git -C newdir init --separate-git-dir ../surrealgitdir &&
404 newdir_git="$(cat newdir/.git)" &&
405 test_cmp_fspath "$(pwd)/surrealgitdir" "${newdir_git#gitdir: }" &&
Jeff King633734d2014-03-20 19:17:35 -0400406 test_path_is_dir surrealgitdir/refs &&
407 test_path_is_missing realgitdir/refs
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700408'
409
410test_expect_success 're-init to move gitdir' '
411 rm -rf newdir realgitdir surrealgitdir &&
412 git init newdir &&
Johannes Schindelined33bd82019-06-24 19:40:05 +0200413 git -C newdir init --separate-git-dir ../realgitdir &&
414 newdir_git="$(cat newdir/.git)" &&
415 test_cmp_fspath "$(pwd)/realgitdir" "${newdir_git#gitdir: }" &&
Jeff King633734d2014-03-20 19:17:35 -0400416 test_path_is_dir realgitdir/refs
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700417'
418
Johannes Sixt4e95fb62011-04-12 08:30:49 +0200419test_expect_success SYMLINKS 're-init to move gitdir symlink' '
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700420 rm -rf newdir realgitdir &&
421 git init newdir &&
422 (
423 cd newdir &&
424 mv .git here &&
425 ln -s here .git &&
Nguyen Thai Ngoc Duy09ffc702011-05-24 23:40:32 +0700426 git init --separate-git-dir ../realgitdir
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700427 ) &&
Elia Pinto88619b32014-04-28 05:57:24 -0700428 echo "gitdir: $(pwd)/realgitdir" >expected &&
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700429 test_cmp expected newdir/.git &&
Jeff King3d06c5f2014-03-20 19:17:15 -0400430 test_cmp expected newdir/here &&
Jeff King633734d2014-03-20 19:17:35 -0400431 test_path_is_dir realgitdir/refs
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700432'
433
Eric Sunshine59d876c2020-08-31 02:58:00 -0400434sep_git_dir_worktree () {
Eric Sunshine42264bc2020-08-31 02:57:59 -0400435 test_when_finished "rm -rf mainwt linkwt seprepo" &&
436 git init mainwt &&
437 test_commit -C mainwt gumby &&
438 git -C mainwt worktree add --detach ../linkwt &&
Eric Sunshine59d876c2020-08-31 02:58:00 -0400439 git -C "$1" init --separate-git-dir ../seprepo &&
Eric Sunshine42264bc2020-08-31 02:57:59 -0400440 git -C mainwt rev-parse --git-common-dir >expect &&
441 git -C linkwt rev-parse --git-common-dir >actual &&
442 test_cmp expect actual
Eric Sunshine59d876c2020-08-31 02:58:00 -0400443}
444
445test_expect_success 're-init to move gitdir with linked worktrees' '
446 sep_git_dir_worktree mainwt
447'
448
449test_expect_success 're-init to move gitdir within linked worktree' '
450 sep_git_dir_worktree linkwt
Eric Sunshine42264bc2020-08-31 02:57:59 -0400451'
452
Johannes Schindelinf30afda2016-05-11 10:43:37 +0200453test_expect_success MINGW '.git hidden' '
454 rm -rf newdir &&
455 (
Eric Sunshineed6c9942018-07-01 20:23:43 -0400456 sane_unset GIT_DIR GIT_WORK_TREE &&
Johannes Schindelinf30afda2016-05-11 10:43:37 +0200457 mkdir newdir &&
458 cd newdir &&
459 git init &&
Johannes Schindelin176a66a2020-04-11 13:40:22 +0000460 test_path_is_hidden .git
Johannes Schindelinf30afda2016-05-11 10:43:37 +0200461 ) &&
462 check_config newdir/.git false unset
463'
464
465test_expect_success MINGW 'bare git dir not hidden' '
466 rm -rf newdir &&
467 (
Eric Sunshineed6c9942018-07-01 20:23:43 -0400468 sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG &&
Johannes Schindelinf30afda2016-05-11 10:43:37 +0200469 mkdir newdir &&
470 cd newdir &&
471 git --bare init
472 ) &&
473 ! is_hidden newdir
474'
475
Jeff Kingb9605bc2016-09-12 20:24:15 -0700476test_expect_success 'remote init from does not use config from cwd' '
477 rm -rf newdir &&
478 test_config core.logallrefupdates true &&
479 git init newdir &&
480 echo true >expect &&
481 git -C newdir config --bool core.logallrefupdates >actual &&
482 test_cmp expect actual
483'
484
Nguyễn Thái Ngọc Duyfe9aa0b2016-09-25 10:14:36 +0700485test_expect_success 're-init from a linked worktree' '
486 git init main-worktree &&
487 (
488 cd main-worktree &&
489 test_commit first &&
490 git worktree add ../linked-worktree &&
491 mv .git/info/exclude expected-exclude &&
Nguyễn Thái Ngọc Duy6311cfa2016-09-25 10:14:39 +0700492 cp .git/config expected-config &&
Nguyễn Thái Ngọc Duyfe9aa0b2016-09-25 10:14:36 +0700493 find .git/worktrees -print | sort >expected &&
494 git -C ../linked-worktree init &&
495 test_cmp expected-exclude .git/info/exclude &&
Nguyễn Thái Ngọc Duy6311cfa2016-09-25 10:14:39 +0700496 test_cmp expected-config .git/config &&
Nguyễn Thái Ngọc Duyfe9aa0b2016-09-25 10:14:36 +0700497 find .git/worktrees -print | sort >actual &&
498 test_cmp expected actual
499 )
500'
501
brian m. carlsoneff45da2020-07-29 23:14:22 +0000502test_expect_success 'init honors GIT_DEFAULT_HASH' '
503 GIT_DEFAULT_HASH=sha1 git init sha1 &&
504 git -C sha1 rev-parse --show-object-format >actual &&
505 echo sha1 >expected &&
506 test_cmp expected actual &&
507 GIT_DEFAULT_HASH=sha256 git init sha256 &&
508 git -C sha256 rev-parse --show-object-format >actual &&
509 echo sha256 >expected &&
510 test_cmp expected actual
511'
512
513test_expect_success 'init honors --object-format' '
514 git init --object-format=sha1 explicit-sha1 &&
515 git -C explicit-sha1 rev-parse --show-object-format >actual &&
516 echo sha1 >expected &&
517 test_cmp expected actual &&
518 git init --object-format=sha256 explicit-sha256 &&
519 git -C explicit-sha256 rev-parse --show-object-format >actual &&
520 echo sha256 >expected &&
521 test_cmp expected actual
522'
523
524test_expect_success 'extensions.objectFormat is not allowed with repo version 0' '
525 git init --object-format=sha256 explicit-v0 &&
526 git -C explicit-v0 config core.repositoryformatversion 0 &&
527 test_must_fail git -C explicit-v0 rev-parse --show-object-format
528'
529
530test_expect_success 'init rejects attempts to initialize with different hash' '
531 test_must_fail git -C sha1 init --object-format=sha256 &&
532 test_must_fail git -C sha256 init --object-format=sha1
533'
534
Patrick Steinhardtd7497a42023-12-29 08:26:47 +0100535test_expect_success DEFAULT_REPO_FORMAT 'extensions.refStorage is not allowed with repo version 0' '
536 test_when_finished "rm -rf refstorage" &&
537 git init refstorage &&
538 git -C refstorage config extensions.refStorage files &&
539 test_must_fail git -C refstorage rev-parse 2>err &&
540 grep "repo version is 0, but v1-only extension found" err
541'
542
543test_expect_success DEFAULT_REPO_FORMAT 'extensions.refStorage with files backend' '
544 test_when_finished "rm -rf refstorage" &&
545 git init refstorage &&
546 git -C refstorage config core.repositoryformatversion 1 &&
547 git -C refstorage config extensions.refStorage files &&
548 test_commit -C refstorage A &&
549 git -C refstorage rev-parse --verify HEAD
550'
551
552test_expect_success DEFAULT_REPO_FORMAT 'extensions.refStorage with unknown backend' '
553 test_when_finished "rm -rf refstorage" &&
554 git init refstorage &&
555 git -C refstorage config core.repositoryformatversion 1 &&
556 git -C refstorage config extensions.refStorage garbage &&
557 test_must_fail git -C refstorage rev-parse 2>err &&
558 grep "invalid value for ${SQ}extensions.refstorage${SQ}: ${SQ}garbage${SQ}" err
559'
560
Patrick Steinhardtaa196192023-12-29 08:26:52 +0100561test_expect_success DEFAULT_REPO_FORMAT 'init with GIT_DEFAULT_REF_FORMAT=files' '
562 test_when_finished "rm -rf refformat" &&
563 GIT_DEFAULT_REF_FORMAT=files git init refformat &&
564 echo 0 >expect &&
565 git -C refformat config core.repositoryformatversion >actual &&
566 test_cmp expect actual &&
567 test_must_fail git -C refformat config extensions.refstorage
568'
569
570test_expect_success 'init with GIT_DEFAULT_REF_FORMAT=garbage' '
571 test_when_finished "rm -rf refformat" &&
572 cat >expect <<-EOF &&
573 fatal: unknown ref storage format ${SQ}garbage${SQ}
574 EOF
575 test_must_fail env GIT_DEFAULT_REF_FORMAT=garbage git init refformat 2>err &&
576 test_cmp expect err
577'
578
Patrick Steinhardt48fa45f2023-12-29 08:27:04 +0100579test_expect_success 'init with --ref-format=files' '
580 test_when_finished "rm -rf refformat" &&
581 git init --ref-format=files refformat &&
582 echo files >expect &&
583 git -C refformat rev-parse --show-ref-format >actual &&
584 test_cmp expect actual
585'
586
Junio C Hamano5c7c0632024-06-03 13:11:11 -0700587backends="files reftable"
Patrick Steinhardt407997c2024-05-22 12:38:46 +0200588for from_format in $backends
589do
590 test_expect_success "re-init with same format ($from_format)" '
591 test_when_finished "rm -rf refformat" &&
592 git init --ref-format=$from_format refformat &&
593 git init --ref-format=$from_format refformat &&
594 echo $from_format >expect &&
595 git -C refformat rev-parse --show-ref-format >actual &&
596 test_cmp expect actual
597 '
598
599 for to_format in $backends
600 do
601 if test "$from_format" = "$to_format"
602 then
603 continue
604 fi
605
606 test_expect_success "re-init with different format fails ($from_format -> $to_format)" '
607 test_when_finished "rm -rf refformat" &&
608 git init --ref-format=$from_format refformat &&
609 cat >expect <<-EOF &&
610 fatal: attempt to reinitialize repository with different reference storage format
611 EOF
612 test_must_fail git init --ref-format=$to_format refformat 2>err &&
613 test_cmp expect err &&
614 echo $from_format >expect &&
615 git -C refformat rev-parse --show-ref-format >actual &&
616 test_cmp expect actual
617 '
618 done
619done
Patrick Steinhardt48fa45f2023-12-29 08:27:04 +0100620
621test_expect_success 'init with --ref-format=garbage' '
622 test_when_finished "rm -rf refformat" &&
623 cat >expect <<-EOF &&
624 fatal: unknown ref storage format ${SQ}garbage${SQ}
625 EOF
626 test_must_fail git init --ref-format=garbage refformat 2>err &&
627 test_cmp expect err
628'
629
Johannes Schindelin28785332019-03-11 13:10:58 -0700630test_expect_success MINGW 'core.hidedotfiles = false' '
631 git config --global core.hidedotfiles false &&
632 rm -rf newdir &&
633 mkdir newdir &&
634 (
635 sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG &&
636 git -C newdir init
637 ) &&
638 ! is_hidden newdir/.git
639'
640
Johannes Schindelin3f944422017-11-01 18:10:25 +0100641test_expect_success MINGW 'redirect std handles' '
642 GIT_REDIRECT_STDOUT=output.txt git rev-parse --git-dir &&
643 test .git = "$(cat output.txt)" &&
Johannes Schindelin1a172e42017-11-01 18:10:30 +0100644 test -z "$(GIT_REDIRECT_STDOUT=off git rev-parse --git-dir)" &&
645 test_must_fail env \
646 GIT_REDIRECT_STDOUT=output.txt \
647 GIT_REDIRECT_STDERR="2>&1" \
648 git rev-parse --git-dir --verify refs/invalid &&
Johannes Schindelinfdda1ac2019-06-19 14:05:57 -0700649 grep "^\\.git\$" output.txt &&
650 grep "Needed a single revision" output.txt
Johannes Schindelin3f944422017-11-01 18:10:25 +0100651'
652
Johannes Schindelin32ba12d2020-06-24 14:46:32 +0000653test_expect_success '--initial-branch' '
654 git init --initial-branch=hello initial-branch-option &&
655 git -C initial-branch-option symbolic-ref HEAD >actual &&
656 echo refs/heads/hello >expect &&
657 test_cmp expect actual &&
658
659 : re-initializing should not change the branch name &&
660 git init --initial-branch=ignore initial-branch-option 2>err &&
Junio C Hamano67892752023-10-31 14:23:30 +0900661 test_grep "ignored --initial-branch" err &&
Johannes Schindelin32ba12d2020-06-24 14:46:32 +0000662 git -C initial-branch-option symbolic-ref HEAD >actual &&
663 grep hello actual
664'
665
Don Goodman-Wilson8747ebb2020-06-24 14:46:33 +0000666test_expect_success 'overridden default initial branch name (config)' '
667 test_config_global init.defaultBranch nmb &&
Johannes Schindelin704fed92020-10-23 14:00:00 +0000668 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= git init initial-branch-config &&
Don Goodman-Wilson8747ebb2020-06-24 14:46:33 +0000669 git -C initial-branch-config symbolic-ref HEAD >actual &&
670 grep nmb actual
671'
672
Johannes Schindelin675704c2020-12-11 11:36:57 +0000673test_expect_success 'advice on unconfigured init.defaultBranch' '
674 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= git -c color.advice=always \
675 init unconfigured-default-branch-name 2>err &&
676 test_decode_color <err >decoded &&
Junio C Hamano67892752023-10-31 14:23:30 +0900677 test_grep "<YELLOW>hint: " decoded
Johannes Schindelin675704c2020-12-11 11:36:57 +0000678'
679
Johannes Schindelin704fed92020-10-23 14:00:00 +0000680test_expect_success 'overridden default main branch name (env)' '
681 test_config_global init.defaultBranch nmb &&
682 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=env git init main-branch-env &&
683 git -C main-branch-env symbolic-ref HEAD >actual &&
684 grep env actual
685'
686
Don Goodman-Wilson8747ebb2020-06-24 14:46:33 +0000687test_expect_success 'invalid default branch name' '
Johannes Schindelin704fed92020-10-23 14:00:00 +0000688 test_must_fail env GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME="with space" \
689 git init initial-branch-invalid 2>err &&
Junio C Hamano67892752023-10-31 14:23:30 +0900690 test_grep "invalid branch name" err
Don Goodman-Wilson8747ebb2020-06-24 14:46:33 +0000691'
692
Johannes Schindelincfaff3a2020-12-11 11:36:55 +0000693test_expect_success 'branch -m with the initial branch' '
694 git init rename-initial &&
695 git -C rename-initial branch -m renamed &&
Ævar Arnfjörð Bjarmason4bd07852023-02-06 23:44:31 +0100696 echo renamed >expect &&
697 git -C rename-initial symbolic-ref --short HEAD >actual &&
698 test_cmp expect actual &&
699
Johannes Schindelincfaff3a2020-12-11 11:36:55 +0000700 git -C rename-initial branch -m renamed again &&
Ævar Arnfjörð Bjarmason4bd07852023-02-06 23:44:31 +0100701 echo again >expect &&
702 git -C rename-initial symbolic-ref --short HEAD >actual &&
703 test_cmp expect actual
Johannes Schindelincfaff3a2020-12-11 11:36:55 +0000704'
705
Patrick Steinhardt407997c2024-05-22 12:38:46 +0200706test_expect_success 'init with includeIf.onbranch condition' '
707 test_when_finished "rm -rf repo" &&
708 git -c includeIf.onbranch:main.path=nonexistent init repo &&
709 echo $GIT_DEFAULT_REF_FORMAT >expect &&
710 git -C repo rev-parse --show-ref-format >actual &&
711 test_cmp expect actual
712'
713
714test_expect_success 'init with includeIf.onbranch condition with existing directory' '
715 test_when_finished "rm -rf repo" &&
716 mkdir repo &&
717 git -c includeIf.onbranch:nonexistent.path=/does/not/exist init repo &&
718 echo $GIT_DEFAULT_REF_FORMAT >expect &&
719 git -C repo rev-parse --show-ref-format >actual &&
720 test_cmp expect actual
721'
722
723test_expect_success 're-init with includeIf.onbranch condition' '
724 test_when_finished "rm -rf repo" &&
725 git init repo &&
726 git -c includeIf.onbranch:nonexistent.path=/does/not/exist init repo &&
727 echo $GIT_DEFAULT_REF_FORMAT >expect &&
728 git -C repo rev-parse --show-ref-format >actual &&
729 test_cmp expect actual
730'
731
732test_expect_success 're-init with includeIf.onbranch condition' '
733 test_when_finished "rm -rf repo" &&
734 git init repo &&
735 git -c includeIf.onbranch:nonexistent.path=/does/not/exist init repo &&
736 echo $GIT_DEFAULT_REF_FORMAT >expect &&
737 git -C repo rev-parse --show-ref-format >actual &&
738 test_cmp expect actual
739'
740
741test_expect_success 're-init skips non-matching includeIf.onbranch' '
742 test_when_finished "rm -rf repo config" &&
743 cat >config <<-EOF &&
744 [
745 garbage
746 EOF
747 git init repo &&
748 git -c includeIf.onbranch:nonexistent.path="$(test-tool path-utils absolute_path config)" init repo
749'
750
751test_expect_success 're-init reads matching includeIf.onbranch' '
752 test_when_finished "rm -rf repo config" &&
753 cat >config <<-EOF &&
754 [
755 garbage
756 EOF
757 path="$(test-tool path-utils absolute_path config)" &&
758 git init --initial-branch=branch repo &&
759 cat >expect <<-EOF &&
760 fatal: bad config line 1 in file $path
761 EOF
762 test_must_fail git -c includeIf.onbranch:branch.path="$path" init repo 2>err &&
763 test_cmp expect err
764'
765
Junio C Hamano6adcca32007-08-27 00:58:06 -0700766test_done