|
| 1 | + |
| 2 | +__all__ = ['Pygit2GitODB', 'Pygit2GitDB', 'Pygit2CompatibilityGitDB'] |
| 3 | + |
| 4 | +from git.db.py.complex import PureGitODB |
| 5 | +from git.db.py.base import ( |
| 6 | + PureRepositoryPathsMixin, |
| 7 | + PureConfigurationMixin, |
| 8 | + PureIndexDB, |
| 9 | + ) |
| 10 | +from git.db.py.resolve import PureReferencesMixin |
| 11 | +from git.db.py.transport import PureTransportDB |
| 12 | +from git.db.py.submodule import PureSubmoduleDB |
| 13 | + |
| 14 | +from git.db.cmd.complex import CmdHighLevelRepository, GitCommandMixin |
| 15 | +from git.db.compat import RepoCompatibilityInterface |
| 16 | + |
| 17 | +from pygit2 import Repository as Pygit2Repo |
| 18 | + |
| 19 | +from git.base import OInfo, OStream |
| 20 | +from git.fun import type_id_to_type_map, type_to_type_id_map |
| 21 | +from git.util import hex_to_bin |
| 22 | + |
| 23 | +from cStringIO import StringIO |
| 24 | +import os |
| 25 | + |
| 26 | + |
| 27 | +class Pygit2GitODB(PureGitODB): |
| 28 | + """A full fledged database to read and write object files from all kinds of sources.""" |
| 29 | + |
| 30 | + def __init__(self, objects_root): |
| 31 | + """Initalize this instance""" |
| 32 | + PureGitODB.__init__(self, objects_root) |
| 33 | + if hasattr(self, 'git_dir'): |
| 34 | + wd = self.git_dir |
| 35 | + else: |
| 36 | + wd = os.path.dirname(objects_root) |
| 37 | + #END try to figure out good entry for pygit2 - it needs the .gitdir |
| 38 | + print objects_root |
| 39 | + print wd |
| 40 | + self._py2_repo = Pygit2Repo(wd) |
| 41 | + |
| 42 | + def __getattr__(self, attr): |
| 43 | + try: |
| 44 | + # supply LazyMixin with this call first |
| 45 | + return super(Pygit2GitODB, self).__getattr__(attr) |
| 46 | + except AttributeError: |
| 47 | + # now assume its on the pygit2 repository ... for now |
| 48 | + return getattr(self._py2_repo, attr) |
| 49 | + #END handle attr |
| 50 | + |
| 51 | + #{ Object DBR |
| 52 | + |
| 53 | + def info(self, binsha): |
| 54 | + type_id, uncomp_data = self._py2_repo.read(binsha) |
| 55 | + return OInfo(binsha, type_id_to_type_map[type_id], len(uncomp_data)) |
| 56 | + |
| 57 | + def stream(self, binsha): |
| 58 | + type_id, uncomp_data = self._py2_repo.read(binsha) |
| 59 | + return OStream(binsha, type_id_to_type_map[type_id], len(uncomp_data), StringIO(uncomp_data)) |
| 60 | + |
| 61 | + # #}END object dbr |
| 62 | + # |
| 63 | + # #{ Object DBW |
| 64 | + def store(self, istream): |
| 65 | + # TODO: remove this check once the required functionality was merged in pygit2 |
| 66 | + if hasattr(self._py2_repo, 'write'): |
| 67 | + istream.binsha = hex_to_bin(self._py2_repo.write(type_to_type_id_map[istream.type], istream.read())) |
| 68 | + return istream |
| 69 | + else: |
| 70 | + return super(Pygit2GitODB, self).store(istream) |
| 71 | + #END handle write support |
| 72 | + |
| 73 | + #}END object dbw |
| 74 | + |
| 75 | +class Pygit2GitDB( PureRepositoryPathsMixin, PureConfigurationMixin, |
| 76 | + PureReferencesMixin, PureSubmoduleDB, |
| 77 | + PureIndexDB, |
| 78 | + PureTransportDB, # not fully implemented |
| 79 | + GitCommandMixin, |
| 80 | + CmdHighLevelRepository, |
| 81 | + Pygit2GitODB): # must come last, as it doesn't pass on __init__ with super |
| 82 | + |
| 83 | + |
| 84 | + def __init__(self, root_path): |
| 85 | + """Initialize ourselves on the .git directory, or the .git/objects directory.""" |
| 86 | + PureRepositoryPathsMixin._initialize(self, root_path) |
| 87 | + super(Pygit2GitDB, self).__init__(self.objects_dir) |
| 88 | + |
| 89 | + |
| 90 | +class Pygit2CompatibilityGitDB(RepoCompatibilityInterface, Pygit2GitDB): |
| 91 | + """Basic pygit2 compatibility database""" |
| 92 | + pass |
| 93 | + |
0 commit comments