2
0

stash.py 3.9 KB

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