You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
4.9KB

  1. """Module with additional types used by the index"""
  2. from binascii import b2a_hex
  3. from .util import (
  4. pack,
  5. unpack
  6. )
  7. from git.objects import Blob
  8. __all__ = ('BlobFilter', 'BaseIndexEntry', 'IndexEntry')
  9. #{ Invariants
  10. CE_NAMEMASK = 0x0fff
  11. CE_STAGEMASK = 0x3000
  12. CE_EXTENDED = 0x4000
  13. CE_VALID = 0x8000
  14. CE_STAGESHIFT = 12
  15. #} END invariants
  16. class BlobFilter(object):
  17. """
  18. Predicate to be used by iter_blobs allowing to filter only return blobs which
  19. match the given list of directories or files.
  20. The given paths are given relative to the repository.
  21. """
  22. __slots__ = 'paths'
  23. def __init__(self, paths):
  24. """
  25. :param paths:
  26. tuple or list of paths which are either pointing to directories or
  27. to files relative to the current repository
  28. """
  29. self.paths = paths
  30. def __call__(self, stage_blob):
  31. path = stage_blob[1].path
  32. for p in self.paths:
  33. if path.startswith(p):
  34. return True
  35. # END for each path in filter paths
  36. return False
  37. class BaseIndexEntry(tuple):
  38. """Small Brother of an index entry which can be created to describe changes
  39. done to the index in which case plenty of additional information is not required.
  40. As the first 4 data members match exactly to the IndexEntry type, methods
  41. expecting a BaseIndexEntry can also handle full IndexEntries even if they
  42. use numeric indices for performance reasons. """
  43. def __str__(self):
  44. return "%o %s %i\t%s" % (self.mode, self.hexsha, self.stage, self.path)
  45. def __repr__(self):
  46. return "(%o, %s, %i, %s)" % (self.mode, self.hexsha, self.stage, self.path)
  47. @property
  48. def mode(self):
  49. """ File Mode, compatible to stat module constants """
  50. return self[0]
  51. @property
  52. def binsha(self):
  53. """binary sha of the blob """
  54. return self[1]
  55. @property
  56. def hexsha(self):
  57. """hex version of our sha"""
  58. return b2a_hex(self[1]).decode('ascii')
  59. @property
  60. def stage(self):
  61. """Stage of the entry, either:
  62. * 0 = default stage
  63. * 1 = stage before a merge or common ancestor entry in case of a 3 way merge
  64. * 2 = stage of entries from the 'left' side of the merge
  65. * 3 = stage of entries from the right side of the merge
  66. :note: For more information, see http://www.kernel.org/pub/software/scm/git/docs/git-read-tree.html
  67. """
  68. return (self[2] & CE_STAGEMASK) >> CE_STAGESHIFT
  69. @property
  70. def path(self):
  71. """:return: our path relative to the repository working tree root"""
  72. return self[3]
  73. @property
  74. def flags(self):
  75. """:return: flags stored with this entry"""
  76. return self[2]
  77. @classmethod
  78. def from_blob(cls, blob, stage=0):
  79. """:return: Fully equipped BaseIndexEntry at the given stage"""
  80. return cls((blob.mode, blob.binsha, stage << CE_STAGESHIFT, blob.path))
  81. def to_blob(self, repo):
  82. """:return: Blob using the information of this index entry"""
  83. return Blob(repo, self.binsha, self.mode, self.path)
  84. class IndexEntry(BaseIndexEntry):
  85. """Allows convenient access to IndexEntry data without completely unpacking it.
  86. Attributes usully accessed often are cached in the tuple whereas others are
  87. unpacked on demand.
  88. See the properties for a mapping between names and tuple indices. """
  89. @property
  90. def ctime(self):
  91. """
  92. :return:
  93. Tuple(int_time_seconds_since_epoch, int_nano_seconds) of the
  94. file's creation time"""
  95. return unpack(">LL", self[4])
  96. @property
  97. def mtime(self):
  98. """See ctime property, but returns modification time """
  99. return unpack(">LL", self[5])
  100. @property
  101. def dev(self):
  102. """ Device ID """
  103. return self[6]
  104. @property
  105. def inode(self):
  106. """ Inode ID """
  107. return self[7]
  108. @property
  109. def uid(self):
  110. """ User ID """
  111. return self[8]
  112. @property
  113. def gid(self):
  114. """ Group ID """
  115. return self[9]
  116. @property
  117. def size(self):
  118. """:return: Uncompressed size of the blob """
  119. return self[10]
  120. @classmethod
  121. def from_base(cls, base):
  122. """
  123. :return:
  124. Minimal entry as created from the given BaseIndexEntry instance.
  125. Missing values will be set to null-like values
  126. :param base: Instance of type BaseIndexEntry"""
  127. time = pack(">LL", 0, 0)
  128. return IndexEntry((base.mode, base.binsha, base.flags, base.path, time, time, 0, 0, 0, 0, 0))
  129. @classmethod
  130. def from_blob(cls, blob, stage=0):
  131. """:return: Minimal entry resembling the given blob object"""
  132. time = pack(">LL", 0, 0)
  133. return IndexEntry((blob.mode, blob.binsha, stage << CE_STAGESHIFT, blob.path,
  134. time, time, 0, 0, 0, 0, blob.size))