stash.py 3.9 KB

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