2
0

test_porcelain_cherry_pick.py 8.4 KB

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