stash.py 3.5 KB

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