object_store.py 3.5 KB

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