Skip to content

Commit 00c5301

Browse files
committed
Fixed ref test up to the point where there is an issue resolving objects in the database for some reason.
1 parent a6778e0 commit 00c5301

File tree

6 files changed

+51
-44
lines changed

6 files changed

+51
-44
lines changed

gitdb/object/base.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from gitdb.util import (
88
hex_to_bin,
99
bin_to_hex,
10+
dirname,
1011
basename,
1112
LazyMixin,
1213
join_path_native,
@@ -65,15 +66,15 @@ def new_from_sha(cls, odb, sha1):
6566
# the NULL binsha is always the root commit
6667
return get_object_type_by_name('commit')(odb, sha1)
6768
#END handle special case
68-
oinfo = odb.odb.info(sha1)
69+
oinfo = odb.info(sha1)
6970
inst = get_object_type_by_name(oinfo.type)(odb, oinfo.binsha)
7071
inst.size = oinfo.size
7172
return inst
7273

7374
def _set_cache_(self, attr):
7475
"""Retrieve object information"""
7576
if attr == "size":
76-
oinfo = self.odb.odb.info(self.binsha)
77+
oinfo = self.odb.info(self.binsha)
7778
self.size = oinfo.size
7879
# assert oinfo.type == self.type, _assertion_msg_format % (self.binsha, oinfo.type, self.type)
7980
else:
@@ -108,13 +109,13 @@ def hexsha(self):
108109
def data_stream(self):
109110
""" :return: File Object compatible stream to the uncompressed raw data of the object
110111
:note: returned streams must be read in order"""
111-
return self.odb.odb.stream(self.binsha)
112+
return self.odb.stream(self.binsha)
112113

113114
def stream_data(self, ostream):
114115
"""Writes our data directly to the given output stream
115116
:param ostream: File object compatible stream object.
116117
:return: self"""
117-
istream = self.odb.odb.stream(self.binsha)
118+
istream = self.odb.stream(self.binsha)
118119
stream_copy(istream, ostream)
119120
return self
120121

@@ -129,7 +130,7 @@ class IndexObject(Object):
129130

130131
def __init__(self, odb, binsha, mode=None, path=None):
131132
"""Initialize a newly instanced IndexObject
132-
:param odb: is the Repo we are located in
133+
:param odb: is the object database we are located in
133134
:param binsha: 20 byte sha1
134135
:param mode: is the stat compatible file mode as int, use the stat module
135136
to evaluate the infomration
@@ -172,5 +173,6 @@ def abspath(self):
172173
.path field which is a path relative to the git repository ).
173174
174175
The returned path will be native to the system and contains '\' on windows. """
175-
return join_path_native(self.odb.working_tree_dir, self.path)
176+
assert False, "Only works if repository is not bare - provide this check in an interface"
177+
return join_path_native(dirname(self.odb.root_path()), self.path)
176178

gitdb/ref/head.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ class HEAD(SymbolicReference):
1414
_ORIG_HEAD_NAME = 'ORIG_HEAD'
1515
__slots__ = tuple()
1616

17-
def __init__(self, repo, path=_HEAD_NAME):
17+
def __init__(self, odb, path=_HEAD_NAME):
1818
if path != self._HEAD_NAME:
1919
raise ValueError("HEAD instance must point to %r, got %r" % (self._HEAD_NAME, path))
20-
super(HEAD, self).__init__(repo, path)
20+
super(HEAD, self).__init__(odb, path)
2121

2222
def orig_head(self):
2323
"""
2424
:return: SymbolicReference pointing at the ORIG_HEAD, which is maintained
2525
to contain the previous value of HEAD"""
26-
return SymbolicReference(self.repo, self._ORIG_HEAD_NAME)
26+
return SymbolicReference(self.odb, self._ORIG_HEAD_NAME)
2727

2828

2929
class Head(Reference):
@@ -69,9 +69,9 @@ def tracking_branch(self):
6969
not a tracking branch"""
7070
reader = self.config_reader()
7171
if reader.has_option(self.k_config_remote) and reader.has_option(self.k_config_remote_ref):
72-
ref = Head(self.repo, Head.to_full_path(reader.get_value(self.k_config_remote_ref)))
72+
ref = Head(self.odb, Head.to_full_path(reader.get_value(self.k_config_remote_ref)))
7373
remote_refpath = self.RemoteReferenceCls.to_full_path(join_path(reader.get_value(self.k_config_remote), ref.name))
74-
return self.RemoteReferenceCls(self.repo, remote_refpath)
74+
return self.RemoteReferenceCls(self.odb, remote_refpath)
7575
# END handle have tracking branch
7676

7777
# we are not a tracking branch
@@ -82,9 +82,9 @@ def tracking_branch(self):
8282

8383
def _config_parser(self, read_only):
8484
if read_only:
85-
parser = self.repo.config_reader()
85+
parser = self.odb.config_reader()
8686
else:
87-
parser = self.repo.config_writer()
87+
parser = self.odb.config_writer()
8888
# END handle parser instance
8989

9090
return SectionConstraint(parser, 'branch "%s"' % self.name)

gitdb/ref/log.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def path(cls, ref):
151151
instance would be found. The path is not guaranteed to point to a valid
152152
file though.
153153
:param ref: SymbolicReference instance"""
154-
return join(ref.repo.git_dir, "logs", to_native_path(ref.path))
154+
return join(ref.odb.root_path(), "logs", to_native_path(ref.path))
155155

156156
@classmethod
157157
def iter_entries(cls, stream):

gitdb/ref/symbolic.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ def name(self):
8080

8181
@property
8282
def abspath(self):
83-
return join_path_native(self.odb.git_dir, self.path)
83+
return join_path_native(self.odb.root_path(), self.path)
8484

8585
@classmethod
8686
def _get_packed_refs_path(cls, odb):
87-
return join(odb.git_dir, 'packed-refs')
87+
return join(odb.root_path(), 'packed-refs')
8888

8989
@classmethod
9090
def _iter_packed_refs(cls, odb):
@@ -137,7 +137,7 @@ def _get_ref_info(cls, odb, ref_path):
137137
point to, or None"""
138138
tokens = None
139139
try:
140-
fp = open(join(odb.git_dir, ref_path), 'r')
140+
fp = open(join(odb.root_path(), ref_path), 'r')
141141
value = fp.read().rstrip()
142142
fp.close()
143143
tokens = value.split(" ")
@@ -159,7 +159,7 @@ def _get_ref_info(cls, odb, ref_path):
159159
return (None, tokens[1])
160160

161161
# its a commit
162-
if re_hexsha_only.match(tokens[0]):
162+
if cls.re_hexsha_only.match(tokens[0]):
163163
return (tokens[0], None)
164164

165165
raise ValueError("Failed to parse reference information from %r" % ref_path)
@@ -177,7 +177,7 @@ def _get_object(self):
177177
The object our ref currently refers to."""
178178
# have to be dynamic here as we may be a tag which can point to anything
179179
# Our path will be resolved to the hexsha which will be used accordingly
180-
return self.ObjectCls.new_from_sha(self._get_object_sha())
180+
return self.ObjectCls.new_from_sha(self.odb, self._get_object_sha())
181181

182182
def set_object(self, object_id, logmsg = None):
183183
"""Set the object we point to, possibly dereference our symbolic reference first.
@@ -341,7 +341,7 @@ def is_valid(self):
341341
a valid object or reference."""
342342
try:
343343
self.object
344-
except (OSError, ValueError):
344+
except (OSError, ValueError, BadObject):
345345
return False
346346
else:
347347
return True
@@ -414,7 +414,7 @@ def delete(cls, odb, path):
414414
or just "myreference", hence 'refs/' is implied.
415415
Alternatively the symbolic reference to be deleted"""
416416
full_ref_path = cls.to_full_path(path)
417-
abs_path = join(odb.git_dir, full_ref_path)
417+
abs_path = join(odb.root_path(), full_ref_path)
418418
if exists(abs_path):
419419
os.remove(abs_path)
420420
else:
@@ -467,7 +467,7 @@ def _create(cls, odb, path, resolve, reference, force, logmsg=None):
467467
corresponding object and a detached symbolic reference will be created
468468
instead"""
469469
full_ref_path = cls.to_full_path(path)
470-
abs_ref_path = join(odb.git_dir, full_ref_path)
470+
abs_ref_path = join(odb.root_path(), full_ref_path)
471471

472472
# figure out target data
473473
target = reference
@@ -539,8 +539,8 @@ def rename(self, new_path, force=False):
539539
if self.path == new_path:
540540
return self
541541

542-
new_abs_path = join(self.odb.git_dir, new_path)
543-
cur_abs_path = join(self.odb.git_dir, self.path)
542+
new_abs_path = join(self.odb.root_path(), new_path)
543+
cur_abs_path = join(self.odb.root_path(), self.path)
544544
if isfile(new_abs_path):
545545
if not force:
546546
# if they point to the same file, its not an error
@@ -570,7 +570,7 @@ def _iter_items(cls, odb, common_path = None):
570570

571571
# walk loose refs
572572
# Currently we do not follow links
573-
for root, dirs, files in os.walk(join_path_native(odb.git_dir, common_path)):
573+
for root, dirs, files in os.walk(join_path_native(odb.root_path(), common_path)):
574574
if 'refs/' not in root: # skip non-refs subfolders
575575
refs_id = [ i for i,d in enumerate(dirs) if d == 'refs' ]
576576
if refs_id:
@@ -579,7 +579,7 @@ def _iter_items(cls, odb, common_path = None):
579579

580580
for f in files:
581581
abs_path = to_native_path_linux(join_path(root, f))
582-
rela_paths.add(abs_path.replace(to_native_path_linux(odb.git_dir) + '/', ""))
582+
rela_paths.add(abs_path.replace(to_native_path_linux(odb.root_path()) + '/', ""))
583583
# END for each file in root directory
584584
# END for each directory to walk
585585

@@ -611,8 +611,8 @@ def iter_items(cls, odb, common_path = None):
611611
refs suitable for the actual class are returned.
612612
613613
:return:
614-
git.SymbolicReference[], each of them is guaranteed to be a symbolic
615-
ref which is not detached.
614+
git.SymbolicReference[], each of them is guaranteed to be a *only* a symbolic
615+
ref, or a derived class which is not detached
616616
617617
List is lexigraphically sorted
618618
The returned objects represent actual subclasses, such as Head or TagReference"""

gitdb/test/lib.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@ def with_rw_repo(func):
6666
that should exist
6767
Wrapped function obtains a git repository """
6868
def wrapper(self, path):
69-
src_dir = os.path.dirname(os.path.dirname(__file__))
69+
src_dir = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
7070
assert(os.path.isdir(path))
71-
os.rmdir(path) # created by wrapper
71+
os.rmdir(path) # created by wrapper, but must not exist for copy operation
7272
shutil.copytree(src_dir, path)
73-
return func(self, GitDB(os.path.join(path, '.git')))
73+
target_gitdir = os.path.join(path, '.git')
74+
assert os.path.isdir(target_gitdir)
75+
return func(self, GitDB(target_gitdir))
7476
#END wrapper
7577
wrapper.__name__ = func.__name__
7678
return with_rw_directory(wrapper)

gitdb/test/test_refs.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from itertools import chain
1515
import os
16+
from nose import SkipTest
1617

1718
class TestRefs(TestBase):
1819

@@ -28,7 +29,7 @@ def test_from_path(self):
2829

2930
def test_tag_base(self):
3031
tag_object_refs = list()
31-
for tag in self.rorepo.tags:
32+
for tag in TagReference.list_items(self.rorepo):
3233
assert "refs/tags" in tag.path
3334
assert tag.name
3435
assert isinstance( tag.commit, Commit )
@@ -55,9 +56,9 @@ def test_tags(self):
5556
# tag refs can point to tag objects or to commits
5657
s = set()
5758
ref_count = 0
58-
for ref in chain(self.rorepo.tags, self.rorepo.heads):
59+
for ref in chain(TagReference.list_items(self.rorepo), Head.list_items(self.rorepo)):
5960
ref_count += 1
60-
assert isinstance(ref, ref.Reference)
61+
assert isinstance(ref, Reference)
6162
assert str(ref) == ref.name
6263
assert repr(ref)
6364
assert ref == ref
@@ -69,7 +70,7 @@ def test_tags(self):
6970

7071
@with_rw_repo
7172
def test_heads(self, rw_repo):
72-
for head in rw_repo.heads:
73+
for head in Head.iter_items(rw_repo):
7374
assert head.name
7475
assert head.path
7576
assert "refs/heads" in head.path
@@ -97,7 +98,7 @@ def test_heads(self, rw_repo):
9798
# END for each head
9899

99100
# verify REFLOG gets altered
100-
head = rw_repo.head
101+
head = HEAD(rw_repo)
101102
cur_head = head.ref
102103
cur_commit = cur_head.commit
103104
pcommit = cur_head.commit.parents[0].parents[0]
@@ -140,22 +141,24 @@ def test_heads(self, rw_repo):
140141

141142
def test_refs(self):
142143
types_found = set()
143-
for ref in self.rorepo.refs:
144+
for ref in Reference.list_items(self.rorepo):
144145
types_found.add(type(ref))
145146
assert len(types_found) >= 3
146147

147148
def test_is_valid(self):
148149
assert Reference(self.rorepo, 'refs/doesnt/exist').is_valid() == False
149-
assert self.rorepo.head.is_valid()
150-
assert self.rorepo.head.reference.is_valid()
150+
assert HEAD(self.rorepo).is_valid()
151+
assert HEAD(self.rorepo).reference.is_valid()
151152
assert SymbolicReference(self.rorepo, 'hellothere').is_valid() == False
152153

153154
def test_orig_head(self):
154-
assert type(self.rorepo.head.orig_head()) == SymbolicReference
155+
assert type(HEAD(self.rorepo).orig_head()) == SymbolicReference
155156

156157
#@with_rw_repo('0.1.6')
157158
# todo reenable
158-
def _disabled_test_head_reset(self, rw_repo):
159+
#def test_head_reset(self, rw_repo):
160+
def test_head_reset(self):
161+
raise SkipTest()
159162
cur_head = rw_repo.head
160163
old_head_commit = cur_head.commit
161164
new_head_commit = cur_head.ref.commit.parents[0]
@@ -416,7 +419,7 @@ def _disabled_test_head_reset(self, rw_repo):
416419
symbol_ref_path = "refs/symbol_ref"
417420
symref = SymbolicReference(rw_repo, symbol_ref_path)
418421
assert symref.path == symbol_ref_path
419-
symbol_ref_abspath = os.path.join(rw_repo.git_dir, symref.path)
422+
symbol_ref_abspath = os.path.join(rw_repo.root_path(), symref.path)
420423

421424
# set it
422425
symref.reference = new_head
@@ -473,7 +476,7 @@ def _disabled_test_head_reset(self, rw_repo):
473476
rw_repo.head.reference = Head.create(rw_repo, "master")
474477

475478
# At least the head should still exist
476-
assert os.path.isfile(os.path.join(rw_repo.git_dir, 'HEAD'))
479+
assert os.path.isfile(os.path.join(rw_repo.root_path(), 'HEAD'))
477480
refs = list(SymbolicReference.iter_items(rw_repo))
478481
assert len(refs) == 1
479482

@@ -519,5 +522,5 @@ def test_dereference_recursive(self):
519522
assert SymbolicReference.dereference_recursive(self.rorepo, 'HEAD')
520523

521524
def test_reflog(self):
522-
assert isinstance(self.rorepo.heads.master.log(), RefLog)
525+
assert isinstance(Head.list_items(self.rorepo).master.log(), RefLog)
523526

0 commit comments

Comments
 (0)