3 test_description
='test fetching over git protocol'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9 .
"$TEST_DIRECTORY"/lib-git-daemon.sh
12 check_verbose_connect
() {
13 test_i18ngrep
-F "Looking up 127.0.0.1 ..." stderr
&&
14 test_i18ngrep
-F "Connecting to 127.0.0.1 (port " stderr
&&
15 test_i18ngrep
-F "done." stderr
18 test_expect_success
'setup repository' '
19 git config push.default matching &&
25 test_expect_success
'create git-accessible bare repository' '
26 mkdir "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git" &&
27 (cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git" &&
29 : >git-daemon-export-ok
31 git remote add public "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git" &&
32 git push public main:main
35 test_expect_success
'clone git repository' '
36 git clone -v "$GIT_DAEMON_URL/repo.git" clone 2>stderr &&
37 check_verbose_connect &&
38 test_cmp file clone/file
41 test_expect_success
'fetch changes via git protocol' '
42 echo content >>file &&
43 git commit -a -m two &&
45 (cd clone && git pull -v) 2>stderr &&
46 check_verbose_connect &&
47 test_cmp file clone/file
50 test_expect_success
'no-op fetch -v stderr is as expected' '
51 (cd clone && git fetch -v) 2>stderr &&
55 test_expect_success
'no-op fetch without "-v" is quiet' '
56 (cd clone && git fetch 2>../stderr) &&
57 test_must_be_empty stderr
60 test_expect_success
'remote detects correct HEAD' '
61 git push public main:other &&
63 git remote set-head -d origin &&
64 git remote set-head -a origin &&
65 git symbolic-ref refs/remotes/origin/HEAD > output &&
66 echo refs/remotes/origin/main > expect &&
67 test_cmp expect output
71 test_expect_success
'prepare pack objects' '
72 cp -R "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo.git "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git &&
73 (cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git &&
74 git --bare repack -a -d
78 test_expect_success
'fetch notices corrupt pack' '
79 cp -R "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
80 (cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
81 p=$(ls objects/pack/pack-*.pack) &&
83 printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
85 mkdir repo_bad1.git &&
88 test_must_fail git --bare fetch "$GIT_DAEMON_URL/repo_bad1.git" &&
89 test 0 = $(ls objects/pack/pack-*.pack | wc -l)
93 test_expect_success
'fetch notices corrupt idx' '
94 cp -R "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
95 (cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
96 rm -f objects/pack/multi-pack-index &&
97 p=$(ls objects/pack/pack-*.idx) &&
99 printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
101 mkdir repo_bad2.git &&
104 test_must_fail git --bare fetch "$GIT_DAEMON_URL/repo_bad2.git" &&
105 test 0 = $(ls objects/pack | wc -l)
109 test_expect_success
'client refuses to ask for repo with newline' '
110 test_must_fail git clone "$GIT_DAEMON_URL/repo$LF.git" dst 2>stderr &&
111 test_i18ngrep newline.is.forbidden stderr
122 chmod -x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git"
138 shift || error
"invalid number of arguments"
140 if test -x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo"
142 if test -n "$do_export"
144 : >"$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo/git-daemon-export-ok"
146 rm -f "$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo/git-daemon-export-ok"
150 test_must_fail git
"$cmd" "$GIT_DAEMON_URL/$repo" "$@" 2>output
&&
151 test_i18ngrep
"fatal: remote error: $msg: /$repo" output
&&
153 chmod +x
"$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git"
157 msg
="access denied or repository not exported"
158 test_expect_success
'clone non-existent' "test_remote_error '$msg' clone nowhere.git"
159 test_expect_success
'push disabled' "test_remote_error '$msg' push repo.git main"
160 test_expect_success
'read access denied' "test_remote_error -x '$msg' fetch repo.git"
161 test_expect_success
'not exported' "test_remote_error -n '$msg' fetch repo.git"
164 start_git_daemon
--informative-errors
166 test_expect_success
'clone non-existent' "test_remote_error 'no such repository' clone nowhere.git"
167 test_expect_success
'push disabled' "test_remote_error 'service not enabled' push repo.git main"
168 test_expect_success
'read access denied' "test_remote_error -x 'no such repository' fetch repo.git"
169 test_expect_success
'not exported' "test_remote_error -n 'repository not exported' fetch repo.git"
172 start_git_daemon
--interpolated-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH/%H%D"
174 test_expect_success
'access repo via interpolated hostname' '
175 repo="$GIT_DAEMON_DOCUMENT_ROOT_PATH/localhost/interp.git" &&
176 git init --bare "$repo" &&
177 git push "$repo" HEAD &&
178 >"$repo"/git-daemon-export-ok &&
179 GIT_OVERRIDE_VIRTUAL_HOST=localhost \
180 git ls-remote "$GIT_DAEMON_URL/interp.git" &&
181 GIT_OVERRIDE_VIRTUAL_HOST=LOCALHOST \
182 git ls-remote "$GIT_DAEMON_URL/interp.git"
185 test_expect_success
'hostname cannot break out of directory' '
186 repo="$GIT_DAEMON_DOCUMENT_ROOT_PATH/../escape.git" &&
187 git init --bare "$repo" &&
188 git push "$repo" HEAD &&
189 >"$repo"/git-daemon-export-ok &&
191 env GIT_OVERRIDE_VIRTUAL_HOST=.. \
192 git ls-remote "$GIT_DAEMON_URL/escape.git"
195 test_expect_success FAKENC
'hostname interpolation works after LF-stripping' '
197 printf "git-upload-pack /interp.git\n\0host=localhost" | packetize_raw
200 fake_nc "$GIT_DAEMON_HOST_PORT" <input >output &&
201 depacketize <output >output.raw &&
203 # just pick out the value of main, which avoids any protocol
205 perl -lne "print \$1 if m{^(\\S+) refs/heads/main}" <output.raw >actual &&
206 git -C "$repo" rev-parse main >expect &&
207 test_cmp expect actual