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.

86 lines
2.6KB

  1. # Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors
  2. #
  3. # This module is part of GitDB and is released under
  4. # the New BSD License: http://www.opensource.org/licenses/bsd-license.php
  5. from gitdb.db.base import (
  6. CompoundDB,
  7. ObjectDBW,
  8. FileDBBase
  9. )
  10. from gitdb.db.loose import LooseObjectDB
  11. from gitdb.db.pack import PackedDB
  12. from gitdb.db.ref import ReferenceDB
  13. from gitdb.exc import InvalidDBRoot
  14. import os
  15. __all__ = ('GitDB', )
  16. class GitDB(FileDBBase, ObjectDBW, CompoundDB):
  17. """A git-style object database, which contains all objects in the 'objects'
  18. subdirectory
  19. ``IMPORTANT``: The usage of this implementation is highly discouraged as it fails to release file-handles.
  20. This can be a problem with long-running processes and/or big repositories.
  21. """
  22. # Configuration
  23. PackDBCls = PackedDB
  24. LooseDBCls = LooseObjectDB
  25. ReferenceDBCls = ReferenceDB
  26. # Directories
  27. packs_dir = 'pack'
  28. loose_dir = ''
  29. alternates_dir = os.path.join('info', 'alternates')
  30. def __init__(self, root_path):
  31. """Initialize ourselves on a git objects directory"""
  32. super(GitDB, self).__init__(root_path)
  33. def _set_cache_(self, attr):
  34. if attr == '_dbs' or attr == '_loose_db':
  35. self._dbs = list()
  36. loose_db = None
  37. for subpath, dbcls in ((self.packs_dir, self.PackDBCls),
  38. (self.loose_dir, self.LooseDBCls),
  39. (self.alternates_dir, self.ReferenceDBCls)):
  40. path = self.db_path(subpath)
  41. if os.path.exists(path):
  42. self._dbs.append(dbcls(path))
  43. if dbcls is self.LooseDBCls:
  44. loose_db = self._dbs[-1]
  45. # END remember loose db
  46. # END check path exists
  47. # END for each db type
  48. # should have at least one subdb
  49. if not self._dbs:
  50. raise InvalidDBRoot(self.root_path())
  51. # END handle error
  52. # we the first one should have the store method
  53. assert loose_db is not None and hasattr(loose_db, 'store'), "First database needs store functionality"
  54. # finally set the value
  55. self._loose_db = loose_db
  56. else:
  57. super(GitDB, self)._set_cache_(attr)
  58. # END handle attrs
  59. #{ ObjectDBW interface
  60. def store(self, istream):
  61. return self._loose_db.store(istream)
  62. def ostream(self):
  63. return self._loose_db.ostream()
  64. def set_ostream(self, ostream):
  65. return self._loose_db.set_ostream(ostream)
  66. #} END objectdbw interface