test_cherry_pick.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. # test_porcelain_cherry_pick.py -- Tests for porcelain cherry-pick functionality
  2. # Copyright (C) 2024 Jelmer Vernooij <jelmer@jelmer.uk>
  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 published 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. """Tests for porcelain cherry-pick functionality."""
  22. import importlib.util
  23. import os
  24. import tempfile
  25. from dulwich import porcelain
  26. from .. import DependencyMissing, TestCase
  27. class PorcelainCherryPickTests(TestCase):
  28. """Tests for the porcelain cherry-pick functionality."""
  29. def test_cherry_pick_simple(self):
  30. """Test simple cherry-pick."""
  31. with tempfile.TemporaryDirectory() as tmpdir:
  32. # Initialize repo
  33. porcelain.init(tmpdir)
  34. # Create initial commit
  35. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  36. f.write("Initial content\n")
  37. porcelain.add(tmpdir, paths=["file1.txt"])
  38. porcelain.commit(tmpdir, message=b"Initial commit")
  39. # Create a branch and switch to it
  40. porcelain.branch_create(tmpdir, "feature")
  41. porcelain.checkout(tmpdir, "feature")
  42. # Add a file on feature branch
  43. with open(os.path.join(tmpdir, "file2.txt"), "w") as f:
  44. f.write("Feature content\n")
  45. porcelain.add(tmpdir, paths=["file2.txt"])
  46. feature_commit = porcelain.commit(tmpdir, message=b"Add feature file")
  47. # Go back to master
  48. porcelain.checkout(tmpdir, "master")
  49. # Cherry-pick the feature commit
  50. new_commit = porcelain.cherry_pick(tmpdir, feature_commit)
  51. self.assertIsNotNone(new_commit)
  52. self.assertTrue(os.path.exists(os.path.join(tmpdir, "file2.txt")))
  53. # Check the content
  54. with open(os.path.join(tmpdir, "file2.txt")) as f:
  55. self.assertEqual(f.read(), "Feature content\n")
  56. def test_cherry_pick_no_commit(self):
  57. """Test cherry-pick with --no-commit."""
  58. with tempfile.TemporaryDirectory() as tmpdir:
  59. # Initialize repo
  60. porcelain.init(tmpdir)
  61. # Create initial commit
  62. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  63. f.write("Initial content\n")
  64. porcelain.add(tmpdir, paths=["file1.txt"])
  65. porcelain.commit(tmpdir, message=b"Initial commit")
  66. # Create a branch and switch to it
  67. porcelain.branch_create(tmpdir, "feature")
  68. porcelain.checkout(tmpdir, "feature")
  69. # Add a file on feature branch
  70. with open(os.path.join(tmpdir, "file2.txt"), "w") as f:
  71. f.write("Feature content\n")
  72. porcelain.add(tmpdir, paths=["file2.txt"])
  73. feature_commit = porcelain.commit(tmpdir, message=b"Add feature file")
  74. # Go back to master
  75. porcelain.checkout(tmpdir, "master")
  76. # Cherry-pick with no-commit
  77. result = porcelain.cherry_pick(tmpdir, feature_commit, no_commit=True)
  78. self.assertIsNone(result)
  79. self.assertTrue(os.path.exists(os.path.join(tmpdir, "file2.txt")))
  80. # Check that the change is staged but not committed
  81. status = porcelain.status(tmpdir)
  82. # The file should be in staged changes
  83. self.assertTrue(
  84. any(b"file2.txt" in changes for changes in status.staged.values())
  85. )
  86. def test_cherry_pick_conflict(self):
  87. """Test cherry-pick with conflicts."""
  88. # Check if merge3 module is available
  89. if importlib.util.find_spec("merge3") is None:
  90. raise DependencyMissing("merge3")
  91. with tempfile.TemporaryDirectory() as tmpdir:
  92. # Initialize repo
  93. porcelain.init(tmpdir)
  94. # Create initial commit with a file
  95. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  96. f.write("Initial content\n")
  97. porcelain.add(tmpdir, paths=["file1.txt"])
  98. porcelain.commit(tmpdir, message=b"Initial commit")
  99. # Create a branch and modify the file
  100. porcelain.branch_create(tmpdir, "feature")
  101. porcelain.checkout(tmpdir, "feature")
  102. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  103. f.write("Feature modification\n")
  104. porcelain.add(tmpdir, paths=["file1.txt"])
  105. feature_commit = porcelain.commit(tmpdir, message=b"Modify file on feature")
  106. # Go back to master and make a different modification
  107. porcelain.checkout(tmpdir, "master")
  108. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  109. f.write("Master modification\n")
  110. porcelain.add(tmpdir, paths=["file1.txt"])
  111. porcelain.commit(tmpdir, message=b"Modify file on master")
  112. # Try to cherry-pick - should raise error due to conflicts
  113. with self.assertRaises(porcelain.Error) as cm:
  114. porcelain.cherry_pick(tmpdir, feature_commit)
  115. self.assertIn("Conflicts in:", str(cm.exception))
  116. self.assertIn("file1.txt", str(cm.exception))
  117. def test_cherry_pick_root_commit(self):
  118. """Test cherry-pick of root commit (should fail)."""
  119. with tempfile.TemporaryDirectory() as tmpdir:
  120. # Initialize repo
  121. porcelain.init(tmpdir)
  122. # Create initial commit
  123. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  124. f.write("Initial content\n")
  125. porcelain.add(tmpdir, paths=["file1.txt"])
  126. root_commit = porcelain.commit(tmpdir, message=b"Root commit")
  127. # Create another branch with a different initial commit
  128. porcelain.branch_create(tmpdir, "other")
  129. porcelain.checkout(tmpdir, "other")
  130. # Try to cherry-pick root commit - should fail
  131. with self.assertRaises(porcelain.Error) as cm:
  132. porcelain.cherry_pick(tmpdir, root_commit)
  133. self.assertIn("Cannot cherry-pick root commit", str(cm.exception))
  134. def test_cherry_pick_abort(self):
  135. """Test aborting a cherry-pick."""
  136. # Check if merge3 module is available
  137. if importlib.util.find_spec("merge3") is None:
  138. raise DependencyMissing("merge3")
  139. with tempfile.TemporaryDirectory() as tmpdir:
  140. # Initialize repo
  141. porcelain.init(tmpdir)
  142. # Create initial commit
  143. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  144. f.write("Initial content\n")
  145. porcelain.add(tmpdir, paths=["file1.txt"])
  146. porcelain.commit(tmpdir, message=b"Initial commit")
  147. # Create branch and make conflicting changes
  148. porcelain.branch_create(tmpdir, "feature")
  149. porcelain.checkout(tmpdir, "feature")
  150. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  151. f.write("Feature content\n")
  152. porcelain.add(tmpdir, paths=["file1.txt"])
  153. feature_commit = porcelain.commit(tmpdir, message=b"Feature change")
  154. # Go back to master and make different change
  155. porcelain.checkout(tmpdir, "master")
  156. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  157. f.write("Master content\n")
  158. porcelain.add(tmpdir, paths=["file1.txt"])
  159. porcelain.commit(tmpdir, message=b"Master change")
  160. # Try to cherry-pick - will conflict
  161. try:
  162. porcelain.cherry_pick(tmpdir, feature_commit)
  163. except porcelain.Error:
  164. pass # Expected
  165. # Abort the cherry-pick
  166. result = porcelain.cherry_pick(tmpdir, None, abort=True)
  167. self.assertIsNone(result)
  168. # Check that we're back to the master state
  169. with open(os.path.join(tmpdir, "file1.txt")) as f:
  170. self.assertEqual(f.read(), "Master content\n")