Skip to content

Commit a848569

Browse files
committed
Merge branch 'pygit2'
2 parents 023dc12 + a5a0fa2 commit a848569

12 files changed

+213
-7
lines changed

git/db/interface.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ def root_path(self):
164164
def db_path(self, rela_path):
165165
"""
166166
:return: the given relative path relative to our database root, allowing
167-
to pontentially access datafiles"""
167+
to pontentially access datafiles
168+
:param rela_path: if not None or '', the relative path will be appended
169+
to the database root path. Otherwise you will obtain the database root path itself"""
168170
raise NotImplementedError()
169171
#} END interface
170172

git/db/py/base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ def __init__(self, root_path):
108108
def root_path(self):
109109
return self._root_path
110110

111-
def db_path(self, rela_path):
111+
def db_path(self, rela_path=None):
112+
if not rela_path:
113+
return self._root_path
112114
return join(self._root_path, rela_path)
113115
#} END interface
114116

git/db/pygit2/__init__.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""Pygit2 module initialization"""
2+
3+
def init_pygit2():
4+
""":raise ImportError: if pygit2 is not present"""
5+
try:
6+
import pygit2
7+
except ImportError:
8+
raise ImportError("Could not find 'pygit2' in the PYTHONPATH - pygit2 functionality is not available")
9+
#END handle pygit2 import
10+
11+
init_pygit2()

git/db/pygit2/complex.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+

git/test/db/dulwich/test_base.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020

2121
#END handle imports
2222

23-
class TestPyDBBase(RepoBase):
23+
class TestDulwichDBBase(RepoBase):
2424
__metaclass__ = DulwichRequiredMetaMixin
2525
RepoCls = DulwichDB
2626

2727
@needs_dulwich_or_skip
2828
@with_rw_repo('HEAD', bare=False)
2929
def test_basics(self, rw_repo):
3030
db = DulwichDB(rw_repo.working_tree_dir)
31-
print db.git_dir
3231

3332

git/test/db/pygit2/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (C) 2010, 2011 Sebastian Thiel ([email protected]) 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

git/test/db/pygit2/lib.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""pygit2 specific utilities, as well as all the default ones"""
2+
3+
from git.test.lib import (
4+
InheritedTestMethodsOverrideWrapperMetaClsAutoMixin,
5+
needs_module_or_skip
6+
)
7+
8+
__all__ = ['needs_pygit2_or_skip', 'Pygit2RequiredMetaMixin']
9+
10+
#{ Decoorators
11+
12+
def needs_pygit2_or_skip(func):
13+
"""Skip this test if we have no pygit2 - print warning"""
14+
return needs_module_or_skip('pygit2')(func)
15+
16+
#}END decorators
17+
18+
#{ MetaClasses
19+
20+
class Pygit2RequiredMetaMixin(InheritedTestMethodsOverrideWrapperMetaClsAutoMixin):
21+
decorator = [needs_pygit2_or_skip]
22+
23+
#} END metaclasses

git/test/db/pygit2/test_base.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright (C) 2010, 2011 Sebastian Thiel ([email protected]) 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 lib import *
6+
from git.test.lib import TestBase, with_rw_repo
7+
from git.test.db.base import RepoBase
8+
9+
10+
11+
try:
12+
import pygit2
13+
except ImportError:
14+
# om this case, all other pygit2 tests will be skipped
15+
# Need to properly initialize the class though, otherwise it would fail
16+
from git.db.complex import PureCompatibilityGitDB as Pygit2DB
17+
else:
18+
# now we know pygit2 is available, to do futher imports
19+
from git.db.pygit2.complex import Pygit2CompatibilityGitDB as Pygit2DB
20+
21+
#END handle imports
22+
23+
class TestPyGit2DBBase(RepoBase):
24+
__metaclass__ = Pygit2RequiredMetaMixin
25+
RepoCls = Pygit2DB
26+
27+
@needs_pygit2_or_skip
28+
@with_rw_repo('HEAD', bare=False)
29+
def test_basics(self, rw_repo):
30+
db = Pygit2DB(rw_repo.working_tree_dir)
31+
32+
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
from git.db.dulwich.complex import DulwichGitODB
1+
try:
2+
from git.db.dulwich.complex import DulwichGitODB
3+
except ImportError:
4+
from git.db.py.complex import PureGitODB as DulwichGitODB
5+
#END handle import
6+
7+
from git.test.db.dulwich.lib import DulwichRequiredMetaMixin
28
from looseodb_impl import TestLooseDBWPerformanceBase
39

410
class TestPureLooseDB(TestLooseDBWPerformanceBase):
11+
__metaclass__ = DulwichRequiredMetaMixin
512
LooseODBCls = DulwichGitODB
613

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
try:
2+
from git.db.pygit2.complex import Pygit2GitODB
3+
except ImportError:
4+
from git.db.py.complex import PureGitODB as Pygit2GitODB
5+
#END handle import
6+
7+
from git.test.db.pygit2.lib import Pygit2RequiredMetaMixin
8+
from looseodb_impl import TestLooseDBWPerformanceBase
9+
10+
class TestPureLooseDB(TestLooseDBWPerformanceBase):
11+
__metaclass__ = Pygit2RequiredMetaMixin
12+
LooseODBCls = Pygit2GitODB
13+
+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
from git.db.dulwich.complex import DulwichCompatibilityGitDB
1+
try:
2+
from git.db.dulwich.complex import DulwichCompatibilityGitDB
3+
except ImportError:
4+
from git.db.complex import PureCompatibilityGitDB as DulwichCompatibilityGitDB
5+
#END handle dulwich compatibility
6+
7+
from git.test.db.dulwich.lib import DulwichRequiredMetaMixin
28
from odb_impl import TestObjDBPerformanceBase
39

4-
class TestPureDB(TestObjDBPerformanceBase):
10+
class TestDulwichDB(TestObjDBPerformanceBase):
11+
__metaclass__ = DulwichRequiredMetaMixin
512
RepoCls = DulwichCompatibilityGitDB
613

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
try:
2+
from git.db.pygit2.complex import Pygit2CompatibilityGitDB
3+
except ImportError:
4+
from git.db.complex import PureCompatibilityGitDB as Pygit2CompatibilityGitDB
5+
#END handle pygit2 compatibility
6+
7+
from git.test.db.pygit2.lib import Pygit2RequiredMetaMixin
8+
from odb_impl import TestObjDBPerformanceBase
9+
10+
class TestPygit2DB(TestObjDBPerformanceBase):
11+
__metaclass__ = Pygit2RequiredMetaMixin
12+
RepoCls = Pygit2CompatibilityGitDB
13+

0 commit comments

Comments
 (0)