object_store.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # object_store.py -- Object store for git objects
  2. # Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # or (at your option) a later version of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. from objects import (
  19. ShaFile,
  20. hex_to_sha,
  21. )
  22. import os, tempfile
  23. from pack import (
  24. iter_sha1,
  25. load_packs,
  26. write_pack_index_v2,
  27. PackData,
  28. )
  29. import tempfile
  30. import urllib2
  31. PACKDIR = 'pack'
  32. class ObjectStore(object):
  33. def __init__(self, path):
  34. self.path = path
  35. self._packs = None
  36. def pack_dir(self):
  37. return os.path.join(self.path, PACKDIR)
  38. def __contains__(self, sha):
  39. # TODO: This can be more efficient
  40. try:
  41. self[sha]
  42. return True
  43. except KeyError:
  44. return False
  45. @property
  46. def packs(self):
  47. """List with pack objects."""
  48. if self._packs is None:
  49. self._packs = list(load_packs(self.pack_dir()))
  50. return self._packs
  51. def _get_shafile(self, sha):
  52. dir = sha[:2]
  53. file = sha[2:]
  54. # Check from object dir
  55. path = os.path.join(self.path, dir, file)
  56. if os.path.exists(path):
  57. return ShaFile.from_file(path)
  58. return None
  59. def get_raw(self, sha):
  60. """Obtain the raw text for an object.
  61. :param sha: Sha for the object.
  62. :return: tuple with object type and object contents.
  63. """
  64. for pack in self.packs:
  65. if sha in pack:
  66. return pack.get_raw(sha, self.get_raw)
  67. # FIXME: Are pack deltas ever against on-disk shafiles ?
  68. ret = self._get_shafile(sha)
  69. if ret is not None:
  70. return ret.as_raw_string()
  71. raise KeyError(sha)
  72. def __getitem__(self, sha):
  73. assert len(sha) == 40, "Incorrect length sha: %s" % str(sha)
  74. ret = self._get_shafile(sha)
  75. if ret is not None:
  76. return ret
  77. # Check from packs
  78. type, uncomp = self.get_raw(sha)
  79. return ShaFile.from_raw_string(type, uncomp)
  80. def move_in_thin_pack(self, path):
  81. """Move a specific file containing a pack into the pack directory.
  82. :note: The file should be on the same file system as the
  83. packs directory.
  84. :param path: Path to the pack file.
  85. """
  86. p = PackData(path)
  87. temppath = os.path.join(self.pack_dir(), sha_to_hex(urllib2.randombytes(20))+".temppack")
  88. write_pack(temppath, p.iterobjects(self.get_raw), len(p))
  89. pack_sha = PackIndex(temppath+".idx").objects_sha1()
  90. os.rename(temppath+".pack",
  91. os.path.join(self.pack_dir(), "pack-%s.pack" % pack_sha))
  92. os.rename(temppath+".idx",
  93. os.path.join(self.pack_dir(), "pack-%s.idx" % pack_sha))
  94. def move_in_pack(self, path):
  95. """Move a specific file containing a pack into the pack directory.
  96. :note: The file should be on the same file system as the
  97. packs directory.
  98. :param path: Path to the pack file.
  99. """
  100. p = PackData(path)
  101. entries = p.sorted_entries()
  102. basename = os.path.join(self.pack_dir(),
  103. "pack-%s" % iter_sha1(entry[0] for entry in entries))
  104. write_pack_index_v2(basename+".idx", entries, p.calculate_checksum())
  105. os.rename(path, basename + ".pack")
  106. def add_thin_pack(self):
  107. """Add a new thin pack to this object store.
  108. Thin packs are packs that contain deltas with parents that exist
  109. in a different pack.
  110. """
  111. fd, path = tempfile.mkstemp(dir=self.pack_dir(), suffix=".pack")
  112. f = os.fdopen(fd, 'w')
  113. def commit():
  114. if os.path.getsize(path) > 0:
  115. self.move_in_thin_pack(path)
  116. return f, commit
  117. def add_pack(self):
  118. """Add a new pack to this object store.
  119. :return: Fileobject to write to and a commit function to
  120. call when the pack is finished.
  121. """
  122. fd, path = tempfile.mkstemp(dir=self.pack_dir(), suffix=".pack")
  123. f = os.fdopen(fd, 'w')
  124. def commit():
  125. if os.path.getsize(path) > 0:
  126. self.move_in_pack(path)
  127. return f, commit
  128. def add_objects(self, objects):
  129. if len(objects) == 0:
  130. return
  131. f, commit = self.add_pack()
  132. write_pack_data(f, objects, len(objects))
  133. commit()