stash.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. from __future__ import absolute_import
  22. import os
  23. from dulwich.file import GitFile
  24. from dulwich.index import (
  25. commit_tree,
  26. iter_fresh_objects,
  27. )
  28. from dulwich.reflog import drop_reflog_entry, read_reflog
  29. DEFAULT_STASH_REF = b"refs/stash"
  30. class Stash(object):
  31. """A Git stash.
  32. Note that this doesn't currently update the working tree.
  33. """
  34. def __init__(self, repo, ref=DEFAULT_STASH_REF):
  35. self._ref = ref
  36. self._repo = repo
  37. @property
  38. def _reflog_path(self):
  39. return os.path.join(
  40. self._repo.commondir(), "logs", os.fsdecode(self._ref)
  41. )
  42. def stashes(self):
  43. try:
  44. with GitFile(self._reflog_path, "rb") as f:
  45. return reversed(list(read_reflog(f)))
  46. except FileNotFoundError:
  47. return []
  48. @classmethod
  49. def from_repo(cls, repo):
  50. """Create a new stash from a Repo object."""
  51. return cls(repo)
  52. def drop(self, index):
  53. """Drop entry with specified index."""
  54. with open(self._reflog_path, "rb+") as f:
  55. drop_reflog_entry(f, index, rewrite=True)
  56. if len(self) == 0:
  57. os.remove(self._reflog_path)
  58. del self._repo.refs[self._ref]
  59. return
  60. if index == 0:
  61. self._repo.refs[self._ref] = self[0].new_sha
  62. def pop(self, index):
  63. raise NotImplementedError(self.pop)
  64. def push(self, committer=None, author=None, message=None):
  65. """Create a new stash.
  66. Args:
  67. committer: Optional committer name to use
  68. author: Optional author name to use
  69. message: Optional commit message
  70. """
  71. # First, create the index commit.
  72. commit_kwargs = {}
  73. if committer is not None:
  74. commit_kwargs["committer"] = committer
  75. if author is not None:
  76. commit_kwargs["author"] = author
  77. index = self._repo.open_index()
  78. index_tree_id = index.commit(self._repo.object_store)
  79. index_commit_id = self._repo.do_commit(
  80. ref=None,
  81. tree=index_tree_id,
  82. message=b"Index stash",
  83. merge_heads=[self._repo.head()],
  84. no_verify=True,
  85. **commit_kwargs
  86. )
  87. # Then, the working tree one.
  88. stash_tree_id = commit_tree(
  89. self._repo.object_store,
  90. iter_fresh_objects(
  91. index,
  92. os.fsencode(self._repo.path),
  93. object_store=self._repo.object_store,
  94. ),
  95. )
  96. if message is None:
  97. message = b"A stash on " + self._repo.head()
  98. # TODO(jelmer): Just pass parents into do_commit()?
  99. self._repo.refs[self._ref] = self._repo.head()
  100. cid = self._repo.do_commit(
  101. ref=self._ref,
  102. tree=stash_tree_id,
  103. message=message,
  104. merge_heads=[index_commit_id],
  105. no_verify=True,
  106. **commit_kwargs
  107. )
  108. return cid
  109. def __getitem__(self, index):
  110. return list(self.stashes())[index]
  111. def __len__(self):
  112. return len(list(self.stashes()))