stash.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 errno
  23. import os
  24. from dulwich.file import GitFile
  25. from dulwich.index import (
  26. commit_tree,
  27. iter_fresh_objects,
  28. )
  29. from dulwich.reflog import read_reflog
  30. DEFAULT_STASH_REF = b"refs/stash"
  31. class Stash(object):
  32. """A Git stash.
  33. Note that this doesn't currently update the working tree.
  34. """
  35. def __init__(self, repo, ref=DEFAULT_STASH_REF):
  36. self._ref = ref
  37. self._repo = repo
  38. def stashes(self):
  39. reflog_path = os.path.join(
  40. self._repo.commondir(), 'logs', self._ref)
  41. try:
  42. with GitFile(reflog_path, 'rb') as f:
  43. return reversed(list(read_reflog(f)))
  44. except EnvironmentError as e:
  45. if e.errno == errno.ENOENT:
  46. return []
  47. raise
  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. raise NotImplementedError(self.drop)
  55. def pop(self, index):
  56. raise NotImplementedError(self.drop)
  57. def push(self, committer=None, author=None, message=None):
  58. """Create a new stash.
  59. Args:
  60. committer: Optional committer name to use
  61. author: Optional author name to use
  62. message: Optional commit message
  63. """
  64. # First, create the index commit.
  65. commit_kwargs = {}
  66. if committer is not None:
  67. commit_kwargs['committer'] = committer
  68. if author is not None:
  69. commit_kwargs['author'] = author
  70. index = self._repo.open_index()
  71. index_tree_id = index.commit(self._repo.object_store)
  72. index_commit_id = self._repo.do_commit(
  73. ref=None, tree=index_tree_id,
  74. message=b"Index stash",
  75. merge_heads=[self._repo.head()],
  76. **commit_kwargs)
  77. # Then, the working tree one.
  78. stash_tree_id = commit_tree(
  79. self._repo.object_store,
  80. iter_fresh_objects(
  81. index, self._repo.path,
  82. object_store=self._repo.object_store))
  83. if message is None:
  84. message = b"A stash on " + self._repo.head()
  85. # TODO(jelmer): Just pass parents into do_commit()?
  86. self._repo.refs[self._ref] = self._repo.head()
  87. cid = self._repo.do_commit(
  88. ref=self._ref, tree=stash_tree_id,
  89. message=message,
  90. merge_heads=[index_commit_id],
  91. **commit_kwargs)
  92. return cid
  93. def __getitem__(self, index):
  94. return self._stashes()[index]
  95. def __len__(self):
  96. return len(self._stashes())