1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
# server_utils.py -- Git server compatibility utilities
# Copyright (C) 2010 Google, Inc.
#
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
# General Public License as published by the Free Software Foundation; version 2.0
# or (at your option) any later version. You can redistribute it and/or
# modify it under the terms of either of these two licenses.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# You should have received a copy of the licenses; if not, see
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
# License, Version 2.0.
#
"""Utilities for testing git server compatibility."""
import errno
import os
import shutil
import socket
import tempfile
from dulwich.objects import hex_to_sha
from dulwich.protocol import CAPABILITY_SIDE_BAND_64K
from dulwich.repo import Repo
from dulwich.server import ReceivePackHandler
from dulwich.tests.utils import tear_down_repo
from .utils import require_git_version, run_git_or_fail
class _StubRepo:
"""A stub repo that just contains a path to tear down."""
def __init__(self, name) -> None:
temp_dir = tempfile.mkdtemp()
self.path = os.path.join(temp_dir, name)
os.mkdir(self.path)
def close(self) -> None:
pass
def _get_shallow(repo):
shallow_file = repo.get_named_file("shallow")
if not shallow_file:
return []
shallows = []
with shallow_file:
for line in shallow_file:
sha = line.strip()
if not sha:
continue
hex_to_sha(sha)
shallows.append(sha)
return shallows
class ServerTests:
"""Base tests for testing servers.
Does not inherit from TestCase so tests are not automatically run.
"""
min_single_branch_version = (
1,
7,
10,
)
def import_repos(self) -> None:
self._old_repo = self.import_repo("server_old.export")
self._new_repo = self.import_repo("server_new.export")
def url(self, port) -> str:
return f"{self.protocol}://localhost:{port}/"
def branch_args(self, branches=None):
if branches is None:
branches = ["master", "branch"]
return [f"{b}:{b}" for b in branches]
def test_push_to_dulwich(self) -> None:
self.import_repos()
self.assertReposNotEqual(self._old_repo, self._new_repo)
port = self._start_server(self._old_repo)
run_git_or_fail(
["push", self.url(port), *self.branch_args()],
cwd=self._new_repo.path,
)
self.assertReposEqual(self._old_repo, self._new_repo)
def test_push_to_dulwich_no_op(self) -> None:
self._old_repo = self.import_repo("server_old.export")
self._new_repo = self.import_repo("server_old.export")
self.assertReposEqual(self._old_repo, self._new_repo)
port = self._start_server(self._old_repo)
run_git_or_fail(
["push", self.url(port), *self.branch_args()],
cwd=self._new_repo.path,
)
self.assertReposEqual(self._old_repo, self._new_repo)
def test_push_to_dulwich_remove_branch(self) -> None:
self._old_repo = self.import_repo("server_old.export")
self._new_repo = self.import_repo("server_old.export")
self.assertReposEqual(self._old_repo, self._new_repo)
port = self._start_server(self._old_repo)
run_git_or_fail(["push", self.url(port), ":master"], cwd=self._new_repo.path)
self.assertEqual(list(self._old_repo.get_refs().keys()), [b"refs/heads/branch"])
def test_fetch_from_dulwich(self) -> None:
self.import_repos()
self.assertReposNotEqual(self._old_repo, self._new_repo)
port = self._start_server(self._new_repo)
run_git_or_fail(
["fetch", self.url(port), *self.branch_args()],
cwd=self._old_repo.path,
)
# flush the pack cache so any new packs are picked up
self._old_repo.object_store._pack_cache_time = 0
self.assertReposEqual(self._old_repo, self._new_repo)
def test_fetch_from_dulwich_no_op(self) -> None:
self._old_repo = self.import_repo("server_old.export")
self._new_repo = self.import_repo("server_old.export")
self.assertReposEqual(self._old_repo, self._new_repo)
port = self._start_server(self._new_repo)
run_git_or_fail(
["fetch", self.url(port), *self.branch_args()],
cwd=self._old_repo.path,
)
# flush the pack cache so any new packs are picked up
self._old_repo.object_store._pack_cache_time = 0
self.assertReposEqual(self._old_repo, self._new_repo)
def test_clone_from_dulwich_empty(self) -> None:
old_repo_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, old_repo_dir)
self._old_repo = Repo.init_bare(old_repo_dir)
port = self._start_server(self._old_repo)
new_repo_base_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, new_repo_base_dir)
new_repo_dir = os.path.join(new_repo_base_dir, "empty_new")
run_git_or_fail(["clone", self.url(port), new_repo_dir], cwd=new_repo_base_dir)
new_repo = Repo(new_repo_dir)
self.addCleanup(new_repo.close)
self.assertReposEqual(self._old_repo, new_repo)
def test_lsremote_from_dulwich(self) -> None:
self._repo = self.import_repo("server_old.export")
port = self._start_server(self._repo)
o = run_git_or_fail(["ls-remote", self.url(port)])
self.assertEqual(len(o.split(b"\n")), 4)
def test_new_shallow_clone_from_dulwich(self) -> None:
require_git_version(self.min_single_branch_version)
self._source_repo = self.import_repo("server_new.export")
self._stub_repo = _StubRepo("shallow")
self.addCleanup(tear_down_repo, self._stub_repo)
port = self._start_server(self._source_repo)
# Fetch at depth 1
run_git_or_fail(
[
"clone",
"--mirror",
"--depth=1",
"--no-single-branch",
self.url(port),
self._stub_repo.path,
]
)
self._stub_repo.close()
clone = self._stub_repo = Repo(self._stub_repo.path)
self.addCleanup(clone.close)
expected_shallow = [
b"35e0b59e187dd72a0af294aedffc213eaa4d03ff",
b"514dc6d3fbfe77361bcaef320c4d21b72bc10be9",
]
self.assertEqual(expected_shallow, _get_shallow(clone))
self.assertReposNotEqual(clone, self._source_repo)
def test_shallow_clone_from_git_is_identical(self) -> None:
require_git_version(self.min_single_branch_version)
self._source_repo = self.import_repo("server_new.export")
self._stub_repo_git = _StubRepo("shallow-git")
self.addCleanup(tear_down_repo, self._stub_repo_git)
self._stub_repo_dw = _StubRepo("shallow-dw")
self.addCleanup(tear_down_repo, self._stub_repo_dw)
# shallow clone using stock git, then using dulwich
run_git_or_fail(
[
"clone",
"--mirror",
"--depth=1",
"--no-single-branch",
"file://" + self._source_repo.path,
self._stub_repo_git.path,
]
)
port = self._start_server(self._source_repo)
run_git_or_fail(
[
"clone",
"--mirror",
"--depth=1",
"--no-single-branch",
self.url(port),
self._stub_repo_dw.path,
]
)
# compare the two clones; they should be equal
repo_git = Repo(self._stub_repo_git.path)
self.addCleanup(repo_git.close)
repo_dw = Repo(self._stub_repo_dw.path)
self.addCleanup(repo_dw.close)
self.assertReposEqual(repo_git, repo_dw)
def test_fetch_same_depth_into_shallow_clone_from_dulwich(self) -> None:
require_git_version(self.min_single_branch_version)
self._source_repo = self.import_repo("server_new.export")
self._stub_repo = _StubRepo("shallow")
self.addCleanup(tear_down_repo, self._stub_repo)
port = self._start_server(self._source_repo)
# Fetch at depth 2
run_git_or_fail(
[
"clone",
"--mirror",
"--depth=2",
"--no-single-branch",
self.url(port),
self._stub_repo.path,
]
)
self._stub_repo.close()
clone = self._stub_repo = Repo(self._stub_repo.path)
self.addCleanup(clone.close)
# Fetching at the same depth is a no-op.
run_git_or_fail(
["fetch", "--depth=2", self.url(port), *self.branch_args()],
cwd=self._stub_repo.path,
)
expected_shallow = [
b"94de09a530df27ac3bb613aaecdd539e0a0655e1",
b"da5cd81e1883c62a25bb37c4d1f8ad965b29bf8d",
]
self.assertEqual(expected_shallow, _get_shallow(clone))
self.assertReposNotEqual(clone, self._source_repo)
def test_fetch_full_depth_into_shallow_clone_from_dulwich(self) -> None:
require_git_version(self.min_single_branch_version)
self._source_repo = self.import_repo("server_new.export")
self._stub_repo = _StubRepo("shallow")
self.addCleanup(tear_down_repo, self._stub_repo)
port = self._start_server(self._source_repo)
# Fetch at depth 2
run_git_or_fail(
[
"clone",
"--mirror",
"--depth=2",
"--no-single-branch",
self.url(port),
self._stub_repo.path,
]
)
self._stub_repo.close()
clone = self._stub_repo = Repo(self._stub_repo.path)
self.addCleanup(clone.close)
# Fetching at the same depth is a no-op.
run_git_or_fail(
["fetch", "--depth=2", self.url(port), *self.branch_args()],
cwd=self._stub_repo.path,
)
# The whole repo only has depth 4, so it should equal server_new.
run_git_or_fail(
["fetch", "--depth=4", self.url(port), *self.branch_args()],
cwd=self._stub_repo.path,
)
self.assertEqual([], _get_shallow(clone))
self.assertReposEqual(clone, self._source_repo)
def test_fetch_from_dulwich_issue_88_standard(self) -> None:
# Basically an integration test to see that the ACK/NAK
# generation works on repos with common head.
self._source_repo = self.import_repo("issue88_expect_ack_nak_server.export")
self._client_repo = self.import_repo("issue88_expect_ack_nak_client.export")
port = self._start_server(self._source_repo)
run_git_or_fail(["fetch", self.url(port), "master"], cwd=self._client_repo.path)
self.assertObjectStoreEqual(
self._source_repo.object_store, self._client_repo.object_store
)
def test_fetch_from_dulwich_issue_88_alternative(self) -> None:
# likewise, but the case where the two repos have no common parent
self._source_repo = self.import_repo("issue88_expect_ack_nak_other.export")
self._client_repo = self.import_repo("issue88_expect_ack_nak_client.export")
port = self._start_server(self._source_repo)
self.assertRaises(
KeyError,
self._client_repo.get_object,
b"02a14da1fc1fc13389bbf32f0af7d8899f2b2323",
)
run_git_or_fail(["fetch", self.url(port), "master"], cwd=self._client_repo.path)
self.assertEqual(
b"commit",
self._client_repo.get_object(
b"02a14da1fc1fc13389bbf32f0af7d8899f2b2323"
).type_name,
)
def test_push_to_dulwich_issue_88_standard(self) -> None:
# Same thing, but we reverse the role of the server/client
# and do a push instead.
self._source_repo = self.import_repo("issue88_expect_ack_nak_client.export")
self._client_repo = self.import_repo("issue88_expect_ack_nak_server.export")
port = self._start_server(self._source_repo)
run_git_or_fail(["push", self.url(port), "master"], cwd=self._client_repo.path)
self.assertReposEqual(self._source_repo, self._client_repo)
# TODO(dborowitz): Come up with a better way of testing various permutations of
# capabilities. The only reason it is the way it is now is that side-band-64k
# was only recently introduced into git-receive-pack.
class NoSideBand64kReceivePackHandler(ReceivePackHandler):
"""ReceivePackHandler that does not support side-band-64k."""
def capabilities(self):
return [c for c in super().capabilities() if c != CAPABILITY_SIDE_BAND_64K]
def ignore_error(error):
"""Check whether this error is safe to ignore."""
(e_type, e_value, _e_tb) = error
return issubclass(e_type, socket.error) and e_value[0] in (
errno.ECONNRESET,
errno.EPIPE,
)
|