Merge branch 'jc/calloc-fix'
[git] / t / t1301-shared-repo.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Johannes Schindelin
4 #
5
6 test_description='Test shared repository initialization'
7
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10
11 . ./test-lib.sh
12
13 # Remove a default ACL from the test dir if possible.
14 setfacl -k . 2>/dev/null
15
16 # User must have read permissions to the repo -> failure on --shared=0400
17 test_expect_success 'shared = 0400 (faulty permission u-w)' '
18         test_when_finished "rm -rf sub" &&
19         mkdir sub && (
20                 cd sub &&
21                 test_must_fail git init --shared=0400
22         )
23 '
24
25 for u in 002 022
26 do
27         test_expect_success POSIXPERM "shared=1 does not clear bits preset by umask $u" '
28                 mkdir sub && (
29                         cd sub &&
30                         umask $u &&
31                         git init --shared=1 &&
32                         test 1 = "$(git config core.sharedrepository)"
33                 ) &&
34                 actual=$(ls -l sub/.git/HEAD) &&
35                 case "$actual" in
36                 -rw-rw-r--*)
37                         : happy
38                         ;;
39                 *)
40                         echo Oops, .git/HEAD is not 0664 but $actual
41                         false
42                         ;;
43                 esac
44         '
45         rm -rf sub
46 done
47
48 test_expect_success 'shared=all' '
49         mkdir sub &&
50         cd sub &&
51         git init --shared=all &&
52         test 2 = $(git config core.sharedrepository)
53 '
54
55 test_expect_success POSIXPERM 'update-server-info honors core.sharedRepository' '
56         : > a1 &&
57         git add a1 &&
58         test_tick &&
59         git commit -m a1 &&
60         umask 0277 &&
61         git update-server-info &&
62         actual="$(ls -l .git/info/refs)" &&
63         case "$actual" in
64         -r--r--r--*)
65                 : happy
66                 ;;
67         *)
68                 echo Oops, .git/info/refs is not 0444
69                 false
70                 ;;
71         esac
72 '
73
74 for u in        0660:rw-rw---- \
75                 0640:rw-r----- \
76                 0600:rw------- \
77                 0666:rw-rw-rw- \
78                 0664:rw-rw-r--
79 do
80         x=$(expr "$u" : ".*:\([rw-]*\)") &&
81         y=$(echo "$x" | sed -e "s/w/-/g") &&
82         u=$(expr "$u" : "\([0-7]*\)") &&
83         git config core.sharedrepository "$u" &&
84         umask 0277 &&
85
86         test_expect_success POSIXPERM "shared = $u ($y) ro" '
87
88                 rm -f .git/info/refs &&
89                 git update-server-info &&
90                 actual="$(test_modebits .git/info/refs)" &&
91                 verbose test "x$actual" = "x-$y"
92
93         '
94
95         umask 077 &&
96         test_expect_success POSIXPERM "shared = $u ($x) rw" '
97
98                 rm -f .git/info/refs &&
99                 git update-server-info &&
100                 actual="$(test_modebits .git/info/refs)" &&
101                 verbose test "x$actual" = "x-$x"
102
103         '
104
105 done
106
107 test_expect_success POSIXPERM 'info/refs respects umask in unshared repo' '
108         rm -f .git/info/refs &&
109         test_unconfig core.sharedrepository &&
110         umask 002 &&
111         git update-server-info &&
112         echo "-rw-rw-r--" >expect &&
113         test_modebits .git/info/refs >actual &&
114         test_cmp expect actual
115 '
116
117 test_expect_success POSIXPERM 'git reflog expire honors core.sharedRepository' '
118         umask 077 &&
119         git config core.sharedRepository group &&
120         git reflog expire --all &&
121         actual="$(ls -l .git/logs/refs/heads/main)" &&
122         case "$actual" in
123         -rw-rw-*)
124                 : happy
125                 ;;
126         *)
127                 echo Ooops, .git/logs/refs/heads/main is not 0662 [$actual]
128                 false
129                 ;;
130         esac
131 '
132
133 test_expect_success POSIXPERM 'forced modes' '
134         mkdir -p templates/hooks &&
135         echo update-server-info >templates/hooks/post-update &&
136         chmod +x templates/hooks/post-update &&
137         echo : >random-file &&
138         mkdir new &&
139         (
140                 cd new &&
141                 umask 002 &&
142                 git init --shared=0660 --template=templates &&
143                 >frotz &&
144                 git add frotz &&
145                 git commit -a -m initial &&
146                 git repack
147         ) &&
148         # List repository files meant to be protected; note that
149         # COMMIT_EDITMSG does not matter---0mode is not about a
150         # repository with a work tree.
151         find new/.git -type f -name COMMIT_EDITMSG -prune -o -print |
152         xargs ls -ld >actual &&
153
154         # Everything must be unaccessible to others
155         test -z "$(sed -e "/^.......---/d" actual)" &&
156
157         # All directories must have either 2770 or 770
158         test -z "$(sed -n -e "/^drwxrw[sx]---/d" -e "/^d/p" actual)" &&
159
160         # post-update hook must be 0770
161         test -z "$(sed -n -e "/post-update/{
162                 /^-rwxrwx---/d
163                 p
164         }" actual)" &&
165
166         # All files inside objects must be accessible by us
167         test -z "$(sed -n -e "/objects\//{
168                 /^d/d
169                 /^-r.-r.----/d
170                 p
171         }" actual)"
172 '
173
174 test_expect_success POSIXPERM 'remote init does not use config from cwd' '
175         git config core.sharedrepository 0666 &&
176         umask 0022 &&
177         git init --bare child.git &&
178         echo "-rw-r--r--" >expect &&
179         test_modebits child.git/config >actual &&
180         test_cmp expect actual
181 '
182
183 test_expect_success POSIXPERM 're-init respects core.sharedrepository (local)' '
184         git config core.sharedrepository 0666 &&
185         umask 0022 &&
186         echo whatever >templates/foo &&
187         git init --template=templates &&
188         echo "-rw-rw-rw-" >expect &&
189         test_modebits .git/foo >actual &&
190         test_cmp expect actual
191 '
192
193 test_expect_success POSIXPERM 're-init respects core.sharedrepository (remote)' '
194         rm -rf child.git &&
195         umask 0022 &&
196         git init --bare --shared=0666 child.git &&
197         test_path_is_missing child.git/foo &&
198         git init --bare --template=templates child.git &&
199         echo "-rw-rw-rw-" >expect &&
200         test_modebits child.git/foo >actual &&
201         test_cmp expect actual
202 '
203
204 test_expect_success POSIXPERM 'template can set core.sharedrepository' '
205         rm -rf child.git &&
206         umask 0022 &&
207         git config core.sharedrepository 0666 &&
208         cp .git/config templates/config &&
209         git init --bare --template=templates child.git &&
210         echo "-rw-rw-rw-" >expect &&
211         test_modebits child.git/HEAD >actual &&
212         test_cmp expect actual
213 '
214
215 test_done