Skip to content

Commit b01ca6a

Browse files
committed
db: implemented GitObjectDB using the git command to make sure we can lookup everything. Next is to implement pack-file reading, then alternates which should allow to resolve everything
1 parent 1906ee4 commit b01ca6a

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

lib/git/objects/base.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# This module is part of GitPython and is released under
55
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
66
import os
7-
from git.utils import LazyMixin, join_path_native
7+
from git.utils import LazyMixin, join_path_native, stream_copy
88
import utils
99

1010
_assertion_msg_format = "Created object %r whose python type %r disagrees with the acutal git object type %r"
@@ -76,10 +76,11 @@ def _set_cache_(self, attr):
7676
Retrieve object information
7777
"""
7878
if attr == "size":
79-
hexsha, typename, self.size = self.repo.git.get_object_header(self.sha)
79+
typename, self.size = self.repo.odb.object_info(self.sha)
8080
assert typename == self.type, _assertion_msg_format % (self.sha, typename, self.type)
8181
elif attr == "data":
82-
hexsha, typename, self.size, self.data = self.repo.git.get_object_data(self.sha)
82+
typename, self.size, stream = self.repo.odb.object(self.sha)
83+
self.data = stream.read() # once we have an own odb, we can delay reading
8384
assert typename == self.type, _assertion_msg_format % (self.sha, typename, self.type)
8485
else:
8586
super(Object,self)._set_cache_(attr)
@@ -121,24 +122,17 @@ def __repr__(self):
121122

122123
@property
123124
def data_stream(self):
124-
"""
125-
Returns
126-
File Object compatible stream to the uncompressed raw data of the object
127-
"""
128-
proc = self.repo.git.cat_file(self.type, self.sha, as_process=True)
129-
return utils.ProcessStreamAdapter(proc, "stdout")
125+
""" :return: File Object compatible stream to the uncompressed raw data of the object
126+
:note: returned streams must be read in order"""
127+
type, size, stream = self.repo.odb.object(self.sha)
128+
return stream
130129

131130
def stream_data(self, ostream):
132-
"""
133-
Writes our data directly to the given output stream
134-
135-
``ostream``
136-
File object compatible stream object.
137-
138-
Returns
139-
self
140-
"""
141-
self.repo.git.cat_file(self.type, self.sha, output_stream=ostream)
131+
"""Writes our data directly to the given output stream
132+
:param ostream: File object compatible stream object.
133+
:return: self"""
134+
type, size, istream = self.repo.odb.object(self.sha)
135+
stream_copy(istream, ostream)
142136
return self
143137

144138

lib/git/odb/db.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,27 @@ class ReferenceDB(CompoundDB):
281281
"""A database consisting of database referred to in a file"""
282282

283283

284-
class GitObjectDB(CompoundDB, iObjectDBW):
284+
#class GitObjectDB(CompoundDB, iObjectDBW):
285+
class GitObjectDB(LooseObjectDB):
285286
"""A database representing the default git object store, which includes loose
286287
objects, pack files and an alternates file
287288
288-
It will create objects only in the loose object database."""
289+
It will create objects only in the loose object database.
290+
:note: for now, we use the git command to do all the lookup, just until he
291+
have packs and the other implementations
292+
"""
293+
__slots__ = ('_git', )
294+
def __init__(self, root_path, git):
295+
"""Initialize this instance with the root and a git command"""
296+
super(GitObjectDB, self).__init__(root_path)
297+
self._git = git
298+
299+
def object_info(self, sha):
300+
discard, type, size = self._git.get_object_header(sha)
301+
return type, size
302+
303+
def object(self, sha):
304+
"""For now, all lookup is done by git itself"""
305+
discard, type, size, stream = self._git.stream_object_data(sha)
306+
return type, size, stream
289307

lib/git/repo.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from config import GitConfigParser
1414
from remote import Remote
1515

16-
from odb.db import LooseObjectDB
16+
from odb.db import GitObjectDB
1717

1818
import os
1919
import sys
@@ -68,7 +68,7 @@ class Repo(object):
6868
# represents the configuration level of a configuration file
6969
config_level = ("system", "global", "repository")
7070

71-
def __init__(self, path=None, odbt = LooseObjectDB):
71+
def __init__(self, path=None, odbt = GitObjectDB):
7272
""" Create a new Repo instance
7373
7474
:param path: is the path to either the root git directory or the bare git repo::
@@ -128,7 +128,12 @@ def __init__(self, path=None, odbt = LooseObjectDB):
128128

129129
self.working_dir = self._working_tree_dir or self.git_dir
130130
self.git = Git(self.working_dir)
131-
self.odb = odbt(os.path.join(self.git_dir, 'objects'))
131+
132+
# special handling, in special times
133+
args = [os.path.join(self.git_dir, 'objects')]
134+
if issubclass(odbt, GitObjectDB):
135+
args.append(self.git)
136+
self.odb = odbt(*args)
132137

133138
def __eq__(self, rhs):
134139
if isinstance(rhs, Repo):

0 commit comments

Comments
 (0)