stash.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # stash.py
  2. # Copyright (C) 2018 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Stash handling."""
  21. import os
  22. from .file import GitFile
  23. from .index import commit_tree, iter_fresh_objects
  24. from .reflog import drop_reflog_entry, read_reflog
  25. DEFAULT_STASH_REF = b"refs/stash"
  26. class Stash:
  27. """A Git stash.
  28. Note that this doesn't currently update the working tree.
  29. """
  30. def __init__(self, repo, ref=DEFAULT_STASH_REF) -> None:
  31. self._ref = ref
  32. self._repo = repo
  33. @property
  34. def _reflog_path(self):
  35. return os.path.join(self._repo.commondir(), "logs", os.fsdecode(self._ref))
  36. def stashes(self):
  37. try:
  38. with GitFile(self._reflog_path, "rb") as f:
  39. return reversed(list(read_reflog(f)))
  40. except FileNotFoundError:
  41. return []
  42. @classmethod
  43. def from_repo(cls, repo):
  44. """Create a new stash from a Repo object."""
  45. return cls(repo)
  46. def drop(self, index):
  47. """Drop entry with specified index."""
  48. with open(self._reflog_path, "rb+") as f:
  49. drop_reflog_entry(f, index, rewrite=True)
  50. if len(self) == 0:
  51. os.remove(self._reflog_path)
  52. del self._repo.refs[self._ref]
  53. return
  54. if index == 0:
  55. self._repo.refs[self._ref] = self[0].new_sha
  56. def pop(self, index):
  57. raise NotImplementedError(self.pop)
  58. def push(self, committer=None, author=None, message=None):
  59. """Create a new stash.
  60. Args:
  61. committer: Optional committer name to use
  62. author: Optional author name to use
  63. message: Optional commit message
  64. """
  65. # First, create the index commit.
  66. commit_kwargs = {}
  67. if committer is not None:
  68. commit_kwargs["committer"] = committer
  69. if author is not None:
  70. commit_kwargs["author"] = author
  71. index = self._repo.open_index()
  72. index_tree_id = index.commit(self._repo.object_store)
  73. index_commit_id = self._repo.do_commit(
  74. ref=None,
  75. tree=index_tree_id,
  76. message=b"Index stash",
  77. merge_heads=[self._repo.head()],
  78. no_verify=True,
  79. **commit_kwargs,
  80. )
  81. # Then, the working tree one.
  82. stash_tree_id = commit_tree(
  83. self._repo.object_store,
  84. iter_fresh_objects(
  85. index,
  86. os.fsencode(self._repo.path),
  87. object_store=self._repo.object_store,
  88. ),
  89. )
  90. if message is None:
  91. message = b"A stash on " + self._repo.head()
  92. # TODO(jelmer): Just pass parents into do_commit()?
  93. self._repo.refs[self._ref] = self._repo.head()
  94. cid = self._repo.do_commit(
  95. ref=self._ref,
  96. tree=stash_tree_id,
  97. message=message,
  98. merge_heads=[index_commit_id],
  99. no_verify=True,
  100. **commit_kwargs,
  101. )
  102. return cid
  103. def __getitem__(self, index):
  104. return list(self.stashes())[index]
  105. def __len__(self) -> int:
  106. return len(list(self.stashes()))