2
0

test_stash.py 7.7 KB

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