test_rebase.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # test_rebase.py -- tests for porcelain rebase
  2. # Copyright (C) 2025 Dulwich contributors
  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 rebase functions."""
  22. import os
  23. import tempfile
  24. from dulwich import porcelain
  25. from dulwich.repo import Repo
  26. from .. import TestCase
  27. class RebasePorcelainTestCase(TestCase):
  28. """Tests for the porcelain rebase function."""
  29. def setUp(self):
  30. """Set up test repository."""
  31. super().setUp()
  32. self.test_dir = tempfile.mkdtemp()
  33. self.repo = Repo.init(self.test_dir)
  34. # Create initial commit
  35. with open(os.path.join(self.test_dir, "README.md"), "wb") as f:
  36. f.write(b"# Test Repository\n")
  37. self.repo.get_worktree().stage(["README.md"])
  38. self.initial_commit = self.repo.get_worktree().commit(
  39. message=b"Initial commit",
  40. committer=b"Test User <test@example.com>",
  41. author=b"Test User <test@example.com>",
  42. )
  43. def tearDown(self):
  44. """Clean up test directory."""
  45. import shutil
  46. shutil.rmtree(self.test_dir)
  47. def test_porcelain_rebase(self):
  48. """Test rebase through porcelain interface."""
  49. # Create and checkout feature branch
  50. self.repo.refs[b"refs/heads/feature"] = self.initial_commit
  51. porcelain.checkout(self.repo, "feature")
  52. # Add commit to feature branch
  53. with open(os.path.join(self.test_dir, "feature.txt"), "wb") as f:
  54. f.write(b"Feature file\n")
  55. porcelain.add(self.repo, ["feature.txt"])
  56. porcelain.commit(
  57. self.repo,
  58. message="Add feature",
  59. author="Test User <test@example.com>",
  60. committer="Test User <test@example.com>",
  61. )
  62. # Switch to main and add different commit
  63. porcelain.checkout(self.repo, "master")
  64. with open(os.path.join(self.test_dir, "main.txt"), "wb") as f:
  65. f.write(b"Main file\n")
  66. porcelain.add(self.repo, ["main.txt"])
  67. porcelain.commit(
  68. self.repo,
  69. message="Main update",
  70. author="Test User <test@example.com>",
  71. committer="Test User <test@example.com>",
  72. )
  73. # Switch back to feature and rebase
  74. porcelain.checkout(self.repo, "feature")
  75. # Perform rebase
  76. new_shas = porcelain.rebase(self.repo, "master")
  77. # Should have rebased one commit
  78. self.assertEqual(len(new_shas), 1)
  79. # Check that the rebased commit has the correct parent and tree
  80. feature_head = self.repo.refs[b"refs/heads/feature"]
  81. feature_commit_obj = self.repo[feature_head]
  82. # Should have master as parent
  83. master_head = self.repo.refs[b"refs/heads/master"]
  84. self.assertEqual(feature_commit_obj.parents, [master_head])
  85. # Tree should have both files
  86. tree = self.repo[feature_commit_obj.tree]
  87. self.assertIn(b"feature.txt", tree)
  88. self.assertIn(b"main.txt", tree)
  89. self.assertIn(b"README.md", tree)