2
0

test_repository.py 9.0 KB

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