test_stash.py 7.4 KB

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