2
0

test_stash.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. # test_stash.py -- tests for stash
  2. # Copyright (C) 2018 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 stashes."""
  22. import os
  23. import shutil
  24. import tempfile
  25. from dulwich.objects import Blob, Tree
  26. from dulwich.repo import Repo
  27. from dulwich.stash import DEFAULT_STASH_REF, Stash
  28. from dulwich.worktree import add_worktree
  29. from . import TestCase
  30. class StashTests(TestCase):
  31. """Tests for stash."""
  32. def setUp(self):
  33. self.test_dir = tempfile.mkdtemp()
  34. self.repo_dir = os.path.join(self.test_dir, "repo")
  35. os.makedirs(self.repo_dir)
  36. self.repo = Repo.init(self.repo_dir)
  37. # Create initial commit so we can have a HEAD
  38. blob = Blob()
  39. blob.data = b"initial data"
  40. self.repo.object_store.add_object(blob)
  41. tree = Tree()
  42. tree.add(b"initial.txt", 0o100644, blob.id)
  43. tree_id = self.repo.object_store.add_object(tree)
  44. self.commit_id = self.repo.get_worktree().commit(
  45. message=b"Initial commit",
  46. tree=tree_id,
  47. )
  48. def tearDown(self):
  49. shutil.rmtree(self.test_dir)
  50. def test_obtain(self) -> None:
  51. stash = Stash.from_repo(self.repo)
  52. self.assertIsInstance(stash, Stash)
  53. def test_empty_stash(self) -> None:
  54. stash = Stash.from_repo(self.repo)
  55. # Make sure logs directory exists for reflog
  56. os.makedirs(os.path.join(self.repo.commondir(), "logs"), exist_ok=True)
  57. self.assertEqual(0, len(stash))
  58. self.assertEqual([], list(stash.stashes()))
  59. def test_push_stash(self) -> None:
  60. stash = Stash.from_repo(self.repo)
  61. # Make sure logs directory exists for reflog
  62. os.makedirs(os.path.join(self.repo.commondir(), "logs"), exist_ok=True)
  63. # Create a file and add it to the index
  64. file_path = os.path.join(self.repo_dir, "testfile.txt")
  65. with open(file_path, "wb") as f:
  66. f.write(b"test data")
  67. self.repo.get_worktree().stage(["testfile.txt"])
  68. # Push to stash
  69. commit_id = stash.push(message=b"Test stash message")
  70. self.assertIsNotNone(commit_id)
  71. # Verify stash was created
  72. self.assertEqual(1, len(stash))
  73. # Verify stash entry
  74. entry = stash[0]
  75. self.assertEqual(commit_id, entry.new_sha)
  76. self.assertTrue(b"Test stash message" in entry.message)
  77. def test_drop_stash(self) -> None:
  78. stash = Stash.from_repo(self.repo)
  79. # Make sure logs directory exists for reflog
  80. logs_dir = os.path.join(self.repo.commondir(), "logs")
  81. os.makedirs(logs_dir, exist_ok=True)
  82. # Create a couple of files and stash them
  83. file1_path = os.path.join(self.repo_dir, "testfile1.txt")
  84. with open(file1_path, "wb") as f:
  85. f.write(b"test data 1")
  86. self.repo.get_worktree().stage(["testfile1.txt"])
  87. commit_id1 = stash.push(message=b"Test stash 1")
  88. file2_path = os.path.join(self.repo_dir, "testfile2.txt")
  89. with open(file2_path, "wb") as f:
  90. f.write(b"test data 2")
  91. self.repo.get_worktree().stage(["testfile2.txt"])
  92. stash.push(message=b"Test stash 2")
  93. self.assertEqual(2, len(stash))
  94. # Drop the newest stash
  95. stash.drop(0)
  96. self.assertEqual(1, len(stash))
  97. self.assertEqual(commit_id1, stash[0].new_sha)
  98. # Drop the remaining stash
  99. stash.drop(0)
  100. self.assertEqual(0, len(stash))
  101. self.assertNotIn(DEFAULT_STASH_REF, self.repo.refs)
  102. def test_custom_ref(self) -> None:
  103. custom_ref = b"refs/custom_stash"
  104. stash = Stash(self.repo, ref=custom_ref)
  105. self.assertEqual(custom_ref, stash._ref)
  106. def test_pop_stash(self) -> None:
  107. stash = Stash.from_repo(self.repo)
  108. # Make sure logs directory exists for reflog
  109. os.makedirs(os.path.join(self.repo.commondir(), "logs"), exist_ok=True)
  110. # Create a file and add it to the index
  111. file_path = os.path.join(self.repo_dir, "testfile.txt")
  112. with open(file_path, "wb") as f:
  113. f.write(b"test data")
  114. self.repo.get_worktree().stage(["testfile.txt"])
  115. # Push to stash
  116. stash.push(message=b"Test stash message")
  117. self.assertEqual(1, len(stash))
  118. # After stash push, the file should be removed from working tree
  119. # (matching git's behavior)
  120. self.assertFalse(os.path.exists(file_path))
  121. # Pop the stash
  122. stash.pop(0)
  123. # Verify file is restored
  124. self.assertTrue(os.path.exists(file_path))
  125. with open(file_path, "rb") as f:
  126. self.assertEqual(b"test data", f.read())
  127. # Verify stash is empty
  128. self.assertEqual(0, len(stash))
  129. # Verify the file is in the index
  130. index = self.repo.open_index()
  131. self.assertIn(b"testfile.txt", index)
  132. def test_pop_stash_with_index_changes(self) -> None:
  133. stash = Stash.from_repo(self.repo)
  134. # Make sure logs directory exists for reflog
  135. os.makedirs(os.path.join(self.repo.commondir(), "logs"), exist_ok=True)
  136. # First commit a file so we have tracked files
  137. tracked_path = os.path.join(self.repo_dir, "tracked.txt")
  138. with open(tracked_path, "wb") as f:
  139. f.write(b"original content")
  140. self.repo.get_worktree().stage(["tracked.txt"])
  141. self.repo.get_worktree().commit(
  142. message=b"Add tracked file",
  143. )
  144. # Modify the tracked file and stage it
  145. with open(tracked_path, "wb") as f:
  146. f.write(b"staged changes")
  147. self.repo.get_worktree().stage(["tracked.txt"])
  148. # Modify it again but don't stage
  149. with open(tracked_path, "wb") as f:
  150. f.write(b"working tree changes")
  151. # Create a new file and stage it
  152. new_file_path = os.path.join(self.repo_dir, "new.txt")
  153. with open(new_file_path, "wb") as f:
  154. f.write(b"new file content")
  155. self.repo.get_worktree().stage(["new.txt"])
  156. # Push to stash
  157. stash.push(message=b"Test stash with index")
  158. self.assertEqual(1, len(stash))
  159. # After stash push, new file should be removed and tracked file reset
  160. self.assertFalse(os.path.exists(new_file_path))
  161. with open(tracked_path, "rb") as f:
  162. self.assertEqual(b"original content", f.read())
  163. # Pop the stash
  164. stash.pop(0)
  165. # Verify tracked file has working tree changes
  166. self.assertTrue(os.path.exists(tracked_path))
  167. with open(tracked_path, "rb") as f:
  168. self.assertEqual(b"working tree changes", f.read())
  169. # Verify new file is restored
  170. self.assertTrue(os.path.exists(new_file_path))
  171. with open(new_file_path, "rb") as f:
  172. self.assertEqual(b"new file content", f.read())
  173. # Verify index has the staged changes
  174. index = self.repo.open_index()
  175. self.assertIn(b"new.txt", index)
  176. class StashInWorktreeTest(StashTests):
  177. """Tests for stash in a worktree."""
  178. def setUp(self) -> None:
  179. super().setUp()
  180. self.repo_dir = os.path.join(self.test_dir, "wt")
  181. self.repo = add_worktree(self.repo, self.repo_dir, "worktree")