object_store.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. )
  21. import os, tempfile
  22. from pack import (
  23. iter_sha1,
  24. load_packs,
  25. write_pack_index_v2,
  26. PackData,
  27. )
  28. import tempfile
  29. PACKDIR = 'pack'
  30. class ObjectStore(object):
  31. def __init__(self, path):
  32. self.path = path
  33. self._packs = None
  34. def pack_dir(self):
  35. return os.path.join(self.path, PACKDIR)
  36. def __contains__(self, sha):
  37. # TODO: This can be more efficient
  38. try:
  39. self[sha]
  40. return True
  41. except KeyError:
  42. return False
  43. @property
  44. def packs(self):
  45. """List with pack objects."""
  46. if self._packs is None:
  47. self._packs = list(load_packs(self.pack_dir()))
  48. return self._packs
  49. def _get_shafile(self, sha):
  50. dir = sha[:2]
  51. file = sha[2:]
  52. # Check from object dir
  53. path = os.path.join(self.path, dir, file)
  54. if os.path.exists(path):
  55. return ShaFile.from_file(path)
  56. return None
  57. def get_raw(self, sha):
  58. """Obtain the raw text for an object.
  59. :param sha: Sha for the object.
  60. :return: tuple with object type and object contents.
  61. """
  62. for pack in self.packs:
  63. if sha in pack:
  64. return pack.get_raw(sha, self.get_raw)
  65. # FIXME: Are pack deltas ever against on-disk shafiles ?
  66. ret = self._get_shafile(sha)
  67. if ret is not None:
  68. return ret.as_raw_string()
  69. raise KeyError(sha)
  70. def __getitem__(self, sha):
  71. assert len(sha) == 40, "Incorrect length sha: %s" % str(sha)
  72. ret = self._get_shafile(sha)
  73. if ret is not None:
  74. return ret
  75. # Check from packs
  76. type, uncomp = self.get_raw(sha)
  77. return ShaFile.from_raw_string(type, uncomp)
  78. def move_in_pack(self, path):
  79. """Move a specific file containing a pack into the pack directory.
  80. :note: The file should be on the same file system as the
  81. packs directory.
  82. :param path: Path to the pack file.
  83. """
  84. p = PackData(path)
  85. entries = p.sorted_entries(self.get_raw)
  86. basename = os.path.join(self.pack_dir(),
  87. "pack-%s" % iter_sha1(entry[0] for entry in entries))
  88. write_pack_index_v2(basename+".idx", entries, p.calculate_checksum())
  89. os.rename(path, basename + ".pack")
  90. def add_pack(self):
  91. """Add a new pack to this object store.
  92. :return: Fileobject to write to and a commit function to
  93. call when the pack is finished.
  94. """
  95. fd, path = tempfile.mkstemp(dir=self.pack_dir(), suffix=".pack")
  96. f = os.fdopen(fd, 'w')
  97. def commit():
  98. if os.path.getsize(path) > 0:
  99. self.move_in_pack(path)
  100. return f, commit
  101. def add_objects(self, objects):
  102. if len(objects) == 0:
  103. return
  104. f, commit = self.add_pack()
  105. write_pack_data(f, objects, len(objects))
  106. commit()