test_repository.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. # test_repo.py -- Git repo compatibility tests
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as public by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Compatibility tests for dulwich repositories."""
  22. import os
  23. import tempfile
  24. from io import BytesIO
  25. from itertools import chain
  26. from dulwich.objects import hex_to_sha
  27. from dulwich.repo import Repo, check_ref_format
  28. from .utils import CompatTestCase, require_git_version, rmtree_ro, run_git_or_fail
  29. class ObjectStoreTestCase(CompatTestCase):
  30. """Tests for git repository compatibility."""
  31. def setUp(self) -> None:
  32. super().setUp()
  33. self._repo = self.import_repo("server_new.export")
  34. def _run_git(self, args):
  35. return run_git_or_fail(args, cwd=self._repo.path)
  36. def _parse_refs(self, output):
  37. refs = {}
  38. for line in BytesIO(output):
  39. fields = line.rstrip(b"\n").split(b" ")
  40. self.assertEqual(3, len(fields))
  41. refname, type_name, sha = fields
  42. check_ref_format(refname[5:])
  43. hex_to_sha(sha)
  44. refs[refname] = (type_name, sha)
  45. return refs
  46. def _parse_objects(self, output):
  47. return {s.rstrip(b"\n").split(b" ")[0] for s in BytesIO(output)}
  48. def test_bare(self) -> None:
  49. self.assertTrue(self._repo.bare)
  50. self.assertFalse(os.path.exists(os.path.join(self._repo.path, ".git")))
  51. def test_head(self) -> None:
  52. output = self._run_git(["rev-parse", "HEAD"])
  53. head_sha = output.rstrip(b"\n")
  54. hex_to_sha(head_sha)
  55. self.assertEqual(head_sha, self._repo.refs[b"HEAD"])
  56. def test_refs(self) -> None:
  57. output = self._run_git(
  58. ["for-each-ref", "--format=%(refname) %(objecttype) %(objectname)"]
  59. )
  60. expected_refs = self._parse_refs(output)
  61. actual_refs = {}
  62. for refname, sha in self._repo.refs.as_dict().items():
  63. if refname == b"HEAD":
  64. continue # handled in test_head
  65. obj = self._repo[sha]
  66. self.assertEqual(sha, obj.id)
  67. actual_refs[refname] = (obj.type_name, obj.id)
  68. self.assertEqual(expected_refs, actual_refs)
  69. # TODO(dborowitz): peeled ref tests
  70. def _get_loose_shas(self):
  71. output = self._run_git(["rev-list", "--all", "--objects", "--unpacked"])
  72. return self._parse_objects(output)
  73. def _get_all_shas(self):
  74. output = self._run_git(["rev-list", "--all", "--objects"])
  75. return self._parse_objects(output)
  76. def assertShasMatch(self, expected_shas, actual_shas_iter) -> None:
  77. actual_shas = set()
  78. for sha in actual_shas_iter:
  79. obj = self._repo[sha]
  80. self.assertEqual(sha, obj.id)
  81. actual_shas.add(sha)
  82. self.assertEqual(expected_shas, actual_shas)
  83. def test_loose_objects(self) -> None:
  84. # TODO(dborowitz): This is currently not very useful since
  85. # fast-imported repos only contained packed objects.
  86. expected_shas = self._get_loose_shas()
  87. self.assertShasMatch(
  88. expected_shas, self._repo.object_store._iter_loose_objects()
  89. )
  90. def test_packed_objects(self) -> None:
  91. expected_shas = self._get_all_shas() - self._get_loose_shas()
  92. self.assertShasMatch(
  93. expected_shas, chain.from_iterable(self._repo.object_store.packs)
  94. )
  95. def test_all_objects(self) -> None:
  96. expected_shas = self._get_all_shas()
  97. self.assertShasMatch(expected_shas, iter(self._repo.object_store))
  98. class WorkingTreeTestCase(ObjectStoreTestCase):
  99. """Test for compatibility with git-worktree."""
  100. min_git_version = (2, 5, 0)
  101. def create_new_worktree(self, repo_dir, branch):
  102. """Create a new worktree using git-worktree.
  103. Args:
  104. repo_dir: The directory of the main working tree.
  105. branch: The branch or commit to checkout in the new worktree.
  106. Returns: The path to the new working tree.
  107. """
  108. temp_dir = tempfile.mkdtemp()
  109. run_git_or_fail(["worktree", "add", temp_dir, branch], cwd=repo_dir)
  110. self.addCleanup(rmtree_ro, temp_dir)
  111. return temp_dir
  112. def setUp(self) -> None:
  113. super().setUp()
  114. self._worktree_path = self.create_new_worktree(self._repo.path, "branch")
  115. self._worktree_repo = Repo(self._worktree_path)
  116. self.addCleanup(self._worktree_repo.close)
  117. self._mainworktree_repo = self._repo
  118. self._number_of_working_tree = 2
  119. self._repo = self._worktree_repo
  120. def test_refs(self) -> None:
  121. super().test_refs()
  122. self.assertEqual(
  123. self._mainworktree_repo.refs.allkeys(), self._repo.refs.allkeys()
  124. )
  125. def test_head_equality(self) -> None:
  126. self.assertNotEqual(
  127. self._repo.refs[b"HEAD"], self._mainworktree_repo.refs[b"HEAD"]
  128. )
  129. def test_bare(self) -> None:
  130. self.assertFalse(self._repo.bare)
  131. self.assertTrue(os.path.isfile(os.path.join(self._repo.path, ".git")))
  132. def _parse_worktree_list(self, output):
  133. worktrees = []
  134. for line in BytesIO(output):
  135. fields = line.rstrip(b"\n").split()
  136. worktrees.append(tuple(f.decode() for f in fields))
  137. return worktrees
  138. def test_git_worktree_list(self) -> None:
  139. # 'git worktree list' was introduced in 2.7.0
  140. require_git_version((2, 7, 0))
  141. output = run_git_or_fail(["worktree", "list"], cwd=self._repo.path)
  142. worktrees = self._parse_worktree_list(output)
  143. self.assertEqual(len(worktrees), self._number_of_working_tree)
  144. self.assertEqual(worktrees[0][1], "(bare)")
  145. self.assertTrue(os.path.samefile(worktrees[0][0], self._mainworktree_repo.path))
  146. output = run_git_or_fail(["worktree", "list"], cwd=self._mainworktree_repo.path)
  147. worktrees = self._parse_worktree_list(output)
  148. self.assertEqual(len(worktrees), self._number_of_working_tree)
  149. self.assertEqual(worktrees[0][1], "(bare)")
  150. self.assertTrue(os.path.samefile(worktrees[0][0], self._mainworktree_repo.path))
  151. def test_git_worktree_config(self) -> None:
  152. """Test that git worktree config parsing matches the git CLI's behavior."""
  153. # Set some config value in the main repo using the git CLI
  154. require_git_version((2, 7, 0))
  155. test_name = "Jelmer"
  156. test_email = "jelmer@apache.org"
  157. run_git_or_fail(["config", "user.name", test_name], cwd=self._repo.path)
  158. run_git_or_fail(["config", "user.email", test_email], cwd=self._repo.path)
  159. worktree_cfg = self._worktree_repo.get_config()
  160. main_cfg = self._repo.get_config()
  161. # Assert that both the worktree repo and main repo have the same view of the config,
  162. # and that the config matches what we set with the git cli
  163. self.assertEqual(worktree_cfg, main_cfg)
  164. for c in [worktree_cfg, main_cfg]:
  165. self.assertEqual(test_name.encode(), c.get((b"user",), b"name"))
  166. self.assertEqual(test_email.encode(), c.get((b"user",), b"email"))
  167. # Read the config values in the worktree with the git cli and assert they match
  168. # the dulwich-parsed configs
  169. output_name = (
  170. run_git_or_fail(["config", "user.name"], cwd=self._mainworktree_repo.path)
  171. .decode()
  172. .rstrip("\n")
  173. )
  174. output_email = (
  175. run_git_or_fail(["config", "user.email"], cwd=self._mainworktree_repo.path)
  176. .decode()
  177. .rstrip("\n")
  178. )
  179. self.assertEqual(test_name, output_name)
  180. self.assertEqual(test_email, output_email)
  181. class InitNewWorkingDirectoryTestCase(WorkingTreeTestCase):
  182. """Test compatibility of Repo.init_new_working_directory."""
  183. min_git_version = (2, 5, 0)
  184. def setUp(self) -> None:
  185. super().setUp()
  186. self._other_worktree = self._repo
  187. worktree_repo_path = tempfile.mkdtemp()
  188. self.addCleanup(rmtree_ro, worktree_repo_path)
  189. self._repo = Repo._init_new_working_directory(
  190. worktree_repo_path, self._mainworktree_repo
  191. )
  192. self.addCleanup(self._repo.close)
  193. self._number_of_working_tree = 3
  194. def test_head_equality(self) -> None:
  195. self.assertEqual(
  196. self._repo.refs[b"HEAD"], self._mainworktree_repo.refs[b"HEAD"]
  197. )
  198. def test_bare(self) -> None:
  199. self.assertFalse(self._repo.bare)
  200. self.assertTrue(os.path.isfile(os.path.join(self._repo.path, ".git")))