blob: ae5cd3f5a0e1b9fc8a109b60260b22b0351665f7 [file] [log] [blame]
Johannes Schindelin83525222007-07-11 15:18:17 +01001#!/bin/sh
2#
3# Copyright (c) 2007 Johannes Schindelin
4#
5
6test_description='Test shared repository initialization'
7
Johannes Schindelin06d53142020-11-18 23:44:21 +00008GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +00009export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10
Jiang Xinbcb71d42022-11-29 21:15:11 +080011TEST_CREATE_REPO_NO_TEMPLATE=1
Ævar Arnfjörð Bjarmasonb2e5d752023-02-07 00:07:36 +010012TEST_PASSES_SANITIZE_LEAK=true
Johannes Schindelin83525222007-07-11 15:18:17 +010013. ./test-lib.sh
14
Matt McCutchen8ed0a742008-10-16 22:32:14 -040015# Remove a default ACL from the test dir if possible.
16setfacl -k . 2>/dev/null
17
Heikki Orsila06cbe852008-04-16 11:34:24 +030018# User must have read permissions to the repo -> failure on --shared=0400
19test_expect_success 'shared = 0400 (faulty permission u-w)' '
Jeff King95508a02015-03-20 06:13:11 -040020 test_when_finished "rm -rf sub" &&
Heikki Orsila06cbe852008-04-16 11:34:24 +030021 mkdir sub && (
Jeff King95508a02015-03-20 06:13:11 -040022 cd sub &&
23 test_must_fail git init --shared=0400
Heikki Orsila06cbe852008-04-16 11:34:24 +030024 )
Heikki Orsila06cbe852008-04-16 11:34:24 +030025'
26
Petr Baudis8c6202d2008-07-12 03:15:03 +020027for u in 002 022
28do
Johannes Sixtee9fb682009-03-13 22:55:27 +010029 test_expect_success POSIXPERM "shared=1 does not clear bits preset by umask $u" '
Jiang Xin5d642292022-11-29 21:15:10 +080030 test_when_finished "rm -rf sub" &&
Petr Baudis8c6202d2008-07-12 03:15:03 +020031 mkdir sub && (
32 cd sub &&
33 umask $u &&
34 git init --shared=1 &&
35 test 1 = "$(git config core.sharedrepository)"
36 ) &&
Jeff King95508a02015-03-20 06:13:11 -040037 actual=$(ls -l sub/.git/HEAD) &&
Petr Baudis8c6202d2008-07-12 03:15:03 +020038 case "$actual" in
39 -rw-rw-r--*)
40 : happy
41 ;;
42 *)
43 echo Oops, .git/HEAD is not 0664 but $actual
44 false
45 ;;
46 esac
47 '
Petr Baudis8c6202d2008-07-12 03:15:03 +020048done
49
Johannes Schindelin83525222007-07-11 15:18:17 +010050test_expect_success 'shared=all' '
Ævar Arnfjörð Bjarmasonce5369e2022-06-03 13:15:07 +020051 git init --template= --shared=all &&
Johannes Schindelin83525222007-07-11 15:18:17 +010052 test 2 = $(git config core.sharedrepository)
53'
54
Johannes Sixtee9fb682009-03-13 22:55:27 +010055test_expect_success POSIXPERM 'update-server-info honors core.sharedRepository' '
Johannes Schindelin83525222007-07-11 15:18:17 +010056 : > a1 &&
57 git add a1 &&
58 test_tick &&
59 git commit -m a1 &&
Ævar Arnfjörð Bjarmasonce5369e2022-06-03 13:15:07 +020060 mkdir .git/info &&
Johannes Schindelin83525222007-07-11 15:18:17 +010061 umask 0277 &&
62 git update-server-info &&
Arjen Laarhoven19b28bf2007-08-17 00:02:17 +020063 actual="$(ls -l .git/info/refs)" &&
64 case "$actual" in
65 -r--r--r--*)
66 : happy
67 ;;
68 *)
69 echo Oops, .git/info/refs is not 0444
70 false
71 ;;
72 esac
Johannes Schindelin83525222007-07-11 15:18:17 +010073'
74
Heikki Orsila06cbe852008-04-16 11:34:24 +030075for u in 0660:rw-rw---- \
76 0640:rw-r----- \
77 0600:rw------- \
78 0666:rw-rw-rw- \
79 0664:rw-rw-r--
80do
81 x=$(expr "$u" : ".*:\([rw-]*\)") &&
82 y=$(echo "$x" | sed -e "s/w/-/g") &&
83 u=$(expr "$u" : "\([0-7]*\)") &&
84 git config core.sharedrepository "$u" &&
85 umask 0277 &&
86
Johannes Sixtee9fb682009-03-13 22:55:27 +010087 test_expect_success POSIXPERM "shared = $u ($y) ro" '
Heikki Orsila06cbe852008-04-16 11:34:24 +030088
89 rm -f .git/info/refs &&
90 git update-server-info &&
Christian Couder73de1c92017-06-25 06:34:28 +020091 actual="$(test_modebits .git/info/refs)" &&
Jeff King8ddfce72023-05-08 15:04:57 -040092 test "x$actual" = "x-$y"
Jeff King95508a02015-03-20 06:13:11 -040093
Heikki Orsila06cbe852008-04-16 11:34:24 +030094 '
95
96 umask 077 &&
Johannes Sixtee9fb682009-03-13 22:55:27 +010097 test_expect_success POSIXPERM "shared = $u ($x) rw" '
Heikki Orsila06cbe852008-04-16 11:34:24 +030098
99 rm -f .git/info/refs &&
100 git update-server-info &&
Christian Couder73de1c92017-06-25 06:34:28 +0200101 actual="$(test_modebits .git/info/refs)" &&
Jeff King8ddfce72023-05-08 15:04:57 -0400102 test "x$actual" = "x-$x"
Heikki Orsila06cbe852008-04-16 11:34:24 +0300103
104 '
105
106done
107
Jeff Kingd91175b2015-01-05 22:50:49 -0500108test_expect_success POSIXPERM 'info/refs respects umask in unshared repo' '
109 rm -f .git/info/refs &&
110 test_unconfig core.sharedrepository &&
111 umask 002 &&
112 git update-server-info &&
113 echo "-rw-rw-r--" >expect &&
Christian Couder73de1c92017-06-25 06:34:28 +0200114 test_modebits .git/info/refs >actual &&
Jeff Kingd91175b2015-01-05 22:50:49 -0500115 test_cmp expect actual
116'
117
Johannes Sixtee9fb682009-03-13 22:55:27 +0100118test_expect_success POSIXPERM 'git reflog expire honors core.sharedRepository' '
Jeff Kingd05c77c2015-01-05 22:49:43 -0500119 umask 077 &&
Pierre Habouzit336d09d2008-06-15 23:37:42 +0200120 git config core.sharedRepository group &&
121 git reflog expire --all &&
Johannes Schindelin06d53142020-11-18 23:44:21 +0000122 actual="$(ls -l .git/logs/refs/heads/main)" &&
Pierre Habouzit336d09d2008-06-15 23:37:42 +0200123 case "$actual" in
124 -rw-rw-*)
125 : happy
126 ;;
127 *)
Han-Wen Nienhuys0218ad52021-05-31 16:56:23 +0000128 echo Ooops, .git/logs/refs/heads/main is not 066x [$actual]
Pierre Habouzit336d09d2008-06-15 23:37:42 +0200129 false
130 ;;
131 esac
132'
133
Junio C Hamano7d5a1802009-04-12 17:55:18 -0700134test_expect_success POSIXPERM 'forced modes' '
Jiang Xin5d642292022-11-29 21:15:10 +0800135 test_when_finished "rm -rf new" &&
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700136 mkdir -p templates/hooks &&
137 echo update-server-info >templates/hooks/post-update &&
138 chmod +x templates/hooks/post-update &&
139 echo : >random-file &&
140 mkdir new &&
141 (
142 cd new &&
143 umask 002 &&
Jiang Xina0883a22022-11-29 21:15:09 +0800144 git init --shared=0660 --template=../templates &&
145 test_path_is_file .git/hooks/post-update &&
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700146 >frotz &&
147 git add frotz &&
148 git commit -a -m initial &&
149 git repack
150 ) &&
Johannes Sixt07868822009-04-12 21:22:02 +0200151 # List repository files meant to be protected; note that
152 # COMMIT_EDITMSG does not matter---0mode is not about a
153 # repository with a work tree.
154 find new/.git -type f -name COMMIT_EDITMSG -prune -o -print |
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700155 xargs ls -ld >actual &&
156
157 # Everything must be unaccessible to others
Johannes Sixt07868822009-04-12 21:22:02 +0200158 test -z "$(sed -e "/^.......---/d" actual)" &&
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700159
Jeff King1b89eaa2009-03-31 16:36:00 -0400160 # All directories must have either 2770 or 770
161 test -z "$(sed -n -e "/^drwxrw[sx]---/d" -e "/^d/p" actual)" &&
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700162
163 # post-update hook must be 0770
164 test -z "$(sed -n -e "/post-update/{
165 /^-rwxrwx---/d
166 p
167 }" actual)" &&
168
Johannes Sixt07868822009-04-12 21:22:02 +0200169 # All files inside objects must be accessible by us
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700170 test -z "$(sed -n -e "/objects\//{
171 /^d/d
Johannes Sixt07868822009-04-12 21:22:02 +0200172 /^-r.-r.----/d
173 p
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700174 }" actual)"
175'
176
Jeff Kingb9605bc2016-09-12 20:24:15 -0700177test_expect_success POSIXPERM 'remote init does not use config from cwd' '
Jiang Xin5d642292022-11-29 21:15:10 +0800178 test_when_finished "rm -rf child.git" &&
Jeff Kingb9605bc2016-09-12 20:24:15 -0700179 git config core.sharedrepository 0666 &&
180 umask 0022 &&
181 git init --bare child.git &&
182 echo "-rw-r--r--" >expect &&
Christian Couder73de1c92017-06-25 06:34:28 +0200183 test_modebits child.git/config >actual &&
Jeff Kingb9605bc2016-09-12 20:24:15 -0700184 test_cmp expect actual
185'
186
Jeff King45439262016-09-12 20:24:23 -0700187test_expect_success POSIXPERM 're-init respects core.sharedrepository (local)' '
188 git config core.sharedrepository 0666 &&
189 umask 0022 &&
190 echo whatever >templates/foo &&
191 git init --template=templates &&
192 echo "-rw-rw-rw-" >expect &&
Christian Couder73de1c92017-06-25 06:34:28 +0200193 test_modebits .git/foo >actual &&
Jeff King45439262016-09-12 20:24:23 -0700194 test_cmp expect actual
195'
196
197test_expect_success POSIXPERM 're-init respects core.sharedrepository (remote)' '
Jiang Xin5d642292022-11-29 21:15:10 +0800198 test_when_finished "rm -rf child.git" &&
Jeff King45439262016-09-12 20:24:23 -0700199 umask 0022 &&
200 git init --bare --shared=0666 child.git &&
201 test_path_is_missing child.git/foo &&
Nguyễn Thái Ngọc Duye1df7fe2019-05-10 17:46:57 +0700202 git init --bare --template=templates child.git &&
Jeff King45439262016-09-12 20:24:23 -0700203 echo "-rw-rw-rw-" >expect &&
Christian Couder73de1c92017-06-25 06:34:28 +0200204 test_modebits child.git/foo >actual &&
Jeff King45439262016-09-12 20:24:23 -0700205 test_cmp expect actual
206'
207
208test_expect_success POSIXPERM 'template can set core.sharedrepository' '
Jiang Xin5d642292022-11-29 21:15:10 +0800209 test_when_finished "rm -rf child.git" &&
Jeff King45439262016-09-12 20:24:23 -0700210 umask 0022 &&
211 git config core.sharedrepository 0666 &&
212 cp .git/config templates/config &&
Nguyễn Thái Ngọc Duye1df7fe2019-05-10 17:46:57 +0700213 git init --bare --template=templates child.git &&
Jeff King45439262016-09-12 20:24:23 -0700214 echo "-rw-rw-rw-" >expect &&
Christian Couder73de1c92017-06-25 06:34:28 +0200215 test_modebits child.git/HEAD >actual &&
Jeff King45439262016-09-12 20:24:23 -0700216 test_cmp expect actual
217'
218
Johannes Schindelin83525222007-07-11 15:18:17 +0100219test_done