summaryrefslogtreecommitdiff
path: root/git/objects/base.py
diff options
context:
space:
mode:
authorSebastian Thiel <byronimo@gmail.com>2014-07-25 11:26:09 +0200
committerSebastian Thiel <byronimo@gmail.com>2014-07-25 11:26:09 +0200
commita66cfe99c1af3d745e929da6a61e1257e3a376b1 (patch)
treed8bc5213dd7e7c7f0befdf65afecb13d5435f873 /git/objects/base.py
parent75194159abce545bfa38c3172efb42da9b0017dc (diff)
parenta23d0d8617ba3119069e610fc7b0850a17322726 (diff)
downloadgitpython-a66cfe99c1af3d745e929da6a61e1257e3a376b1.tar.gz
Merge pull request #173 from craigez/feature/pep8
Autopep8 style whitespace cleanups & pre-commit hook
Diffstat (limited to 'git/objects/base.py')
-rw-r--r--git/objects/base.py87
1 files changed, 45 insertions, 42 deletions
diff --git a/git/objects/base.py b/git/objects/base.py
index cb6f9761..584845f8 100644
--- a/git/objects/base.py
+++ b/git/objects/base.py
@@ -6,41 +6,43 @@
from util import get_object_type_by_name
from git.util import (
- hex_to_bin,
- bin_to_hex,
- dirname,
- basename,
- LazyMixin,
- join_path_native,
- stream_copy
- )
+ hex_to_bin,
+ bin_to_hex,
+ dirname,
+ basename,
+ LazyMixin,
+ join_path_native,
+ stream_copy
+)
from git.db.interface import RepositoryPathsMixin
from git.exc import UnsupportedOperation
from git.typ import ObjectType
-
+
_assertion_msg_format = "Created object %r whose python type %r disagrees with the acutal git object type %r"
__all__ = ("Object", "IndexObject")
+
class Object(LazyMixin):
+
"""Implements an Object which may be Blobs, Trees, Commits and Tags"""
- NULL_HEX_SHA = '0'*40
- NULL_BIN_SHA = '\0'*20
-
+ NULL_HEX_SHA = '0' * 40
+ NULL_BIN_SHA = '\0' * 20
+
TYPES = (ObjectType.blob, ObjectType.tree, ObjectType.commit, ObjectType.tag)
- __slots__ = ("odb", "binsha", "size" )
-
+ __slots__ = ("odb", "binsha", "size")
+
type = None # to be set by subclass
type_id = None # to be set by subclass
-
+
def __init__(self, odb, binsha):
"""Initialize an object by identifying it by its binary sha.
All keyword arguments will be set on demand if None.
-
+
:param odb: repository this object is located in
-
+
:param binsha: 20 byte SHA1"""
- super(Object,self).__init__()
+ super(Object, self).__init__()
self.odb = odb
self.binsha = binsha
assert len(binsha) == 20, "Require 20 byte binary sha, got %r, len = %i" % (binsha, len(binsha))
@@ -51,13 +53,13 @@ class Object(LazyMixin):
:return: New Object instance of a type appropriate to the object type behind
id. The id of the newly created object will be a binsha even though
the input id may have been a Reference or Rev-Spec
-
+
:param id: reference, rev-spec, or hexsha
-
+
:note: This cannot be a __new__ method as it would always call __init__
with the input id which is not necessarily a binsha."""
return odb.rev_parse(str(id))
-
+
@classmethod
def new_from_sha(cls, odb, sha1):
"""
@@ -67,41 +69,41 @@ class Object(LazyMixin):
if sha1 == cls.NULL_BIN_SHA:
# the NULL binsha is always the root commit
return get_object_type_by_name('commit')(odb, sha1)
- #END handle special case
+ # END handle special case
oinfo = odb.info(sha1)
inst = get_object_type_by_name(oinfo.type)(odb, oinfo.binsha)
inst.size = oinfo.size
- return inst
-
+ return inst
+
def _set_cache_(self, attr):
"""Retrieve object information"""
- if attr == "size":
+ if attr == "size":
oinfo = self.odb.info(self.binsha)
self.size = oinfo.size
# assert oinfo.type == self.type, _assertion_msg_format % (self.binsha, oinfo.type, self.type)
else:
- super(Object,self)._set_cache_(attr)
-
+ super(Object, self)._set_cache_(attr)
+
def __eq__(self, other):
""":return: True if the objects have the same SHA1"""
if not hasattr(other, 'binsha'):
return False
return self.binsha == other.binsha
-
+
def __ne__(self, other):
""":return: True if the objects do not have the same SHA1 """
if not hasattr(other, 'binsha'):
return True
return self.binsha != other.binsha
-
+
def __hash__(self):
""":return: Hash of our id allowing objects to be used in dicts and sets"""
return hash(self.binsha)
-
+
def __str__(self):
""":return: string of our SHA1 as understood by all git commands"""
return bin_to_hex(self.binsha)
-
+
def __repr__(self):
""":return: string with pythonic representation of our object"""
return '<git.%s "%s">' % (self.__class__.__name__, self.hexsha)
@@ -124,16 +126,17 @@ class Object(LazyMixin):
istream = self.odb.stream(self.binsha)
stream_copy(istream, ostream)
return self
-
+
class IndexObject(Object):
+
"""Base for all objects that can be part of the index file , namely Tree, Blob and
SubModule objects"""
__slots__ = ("path", "mode")
-
+
# for compatability with iterable lists
_id_attribute_ = 'path'
-
+
def __init__(self, odb, binsha, mode=None, path=None):
"""Initialize a newly instanced IndexObject
:param odb: is the object database we are located in
@@ -151,33 +154,34 @@ class IndexObject(Object):
self.mode = mode
if path is not None:
self.path = path
-
+
def __hash__(self):
""":return:
Hash of our path as index items are uniquely identifyable by path, not
by their data !"""
return hash(self.path)
-
+
def _set_cache_(self, attr):
if attr in IndexObject.__slots__:
# they cannot be retrieved lateron ( not without searching for them )
- raise AttributeError( "path and mode attributes must have been set during %s object creation" % type(self).__name__ )
+ raise AttributeError(
+ "path and mode attributes must have been set during %s object creation" % type(self).__name__)
else:
super(IndexObject, self)._set_cache_(attr)
# END hanlde slot attribute
-
+
@property
def name(self):
""":return: Name portion of the path, effectively being the basename"""
return basename(self.path)
-
+
@property
def abspath(self):
"""
:return:
Absolute path to this index object in the file system ( as opposed to the
.path field which is a path relative to the git repository ).
-
+
The returned path will be native to the system and contains '\' on windows.
:raise UnsupportedOperation: if underlying odb does not support the required method to obtain a working dir"""
# TODO: Here we suddenly need something better than a plain object database
@@ -187,6 +191,5 @@ class IndexObject(Object):
root = self.odb.working_tree_dir
else:
raise UnsupportedOperation("Cannot provide absolute path from a database without Repository path support")
- #END handle odb type
+ # END handle odb type
return join_path_native(root, self.path)
-