summaryrefslogtreecommitdiff
path: root/git/refs/symbolic.py
diff options
context:
space:
mode:
authorAntoine Musso <hashar@free.fr>2014-11-16 20:15:50 +0100
committerAntoine Musso <hashar@free.fr>2014-11-16 20:46:41 +0100
commitf5d11b750ecc982541d1f936488248f0b42d75d3 (patch)
tree8be522510315f5adc32c0c55acd45dc1074294da /git/refs/symbolic.py
parent7aba59a2609ec768d5d495dafd23a4bce8179741 (diff)
downloadgitpython-f5d11b750ecc982541d1f936488248f0b42d75d3.tar.gz
pep8 linting (whitespaces)
W191 indentation contains tabs E221 multiple spaces before operator E222 multiple spaces after operator E225 missing whitespace around operator E271 multiple spaces after keyword W292 no newline at end of file W293 blank line contains whitespace W391 blank line at end of file
Diffstat (limited to 'git/refs/symbolic.py')
-rw-r--r--git/refs/symbolic.py178
1 files changed, 89 insertions, 89 deletions
diff --git a/git/refs/symbolic.py b/git/refs/symbolic.py
index ef21950f..7a4bf59b 100644
--- a/git/refs/symbolic.py
+++ b/git/refs/symbolic.py
@@ -27,7 +27,7 @@ class SymbolicReference(object):
"""Represents a special case of a reference such that this reference is symbolic.
It does not point to a specific commit, but to another Head, which itself
specifies a commit.
-
+
A typical example for a symbolic reference is HEAD."""
__slots__ = ("repo", "path")
_resolve_ref_on_create = False
@@ -35,28 +35,28 @@ class SymbolicReference(object):
_common_path_default = ""
_remote_common_path_default = "refs/remotes"
_id_attribute_ = "name"
-
+
def __init__(self, repo, path):
self.repo = repo
self.path = path
-
+
def __str__(self):
return self.path
-
+
def __repr__(self):
return '<git.%s "%s">' % (self.__class__.__name__, self.path)
-
+
def __eq__(self, other):
if hasattr(other, 'path'):
return self.path == other.path
return False
-
+
def __ne__(self, other):
return not ( self == other )
-
+
def __hash__(self):
return hash(self.path)
-
+
@property
def name(self):
"""
@@ -64,15 +64,15 @@ class SymbolicReference(object):
In case of symbolic references, the shortest assumable name
is the path itself."""
return self.path
-
+
@property
def abspath(self):
return join_path_native(self.repo.git_dir, self.path)
-
+
@classmethod
def _get_packed_refs_path(cls, repo):
return join(repo.git_dir, 'packed-refs')
-
+
@classmethod
def _iter_packed_refs(cls, repo):
"""Returns an iterator yielding pairs of sha1/path pairs for the corresponding refs.
@@ -89,12 +89,12 @@ class SymbolicReference(object):
# END abort if we do not understand the packing scheme
continue
# END parse comment
-
+
# skip dereferenced tag object entries - previous line was actual
# tag reference for it
if line[0] == '^':
continue
-
+
yield tuple(line.split(' ', 1))
# END for each line
except (OSError,IOError):
@@ -104,7 +104,7 @@ class SymbolicReference(object):
# but some python version woudn't allow yields within that.
# I believe files are closing themselves on destruction, so it is
# alright.
-
+
@classmethod
def dereference_recursive(cls, repo, ref_path):
"""
@@ -116,7 +116,7 @@ class SymbolicReference(object):
if hexsha is not None:
return hexsha
# END recursive dereferencing
-
+
@classmethod
def _get_ref_info(cls, repo, ref_path):
"""Return: (sha, target_ref_path) if available, the sha the file at
@@ -140,17 +140,17 @@ class SymbolicReference(object):
# END handle packed refs
if tokens is None:
raise ValueError("Reference at %r does not exist" % ref_path)
-
+
# is it a reference ?
if tokens[0] == 'ref:':
return (None, tokens[1])
-
+
# its a commit
if repo.re_hexsha_only.match(tokens[0]):
return (tokens[0], None)
-
+
raise ValueError("Failed to parse reference information from %r" % ref_path)
-
+
def _get_object(self):
"""
:return:
@@ -159,7 +159,7 @@ class SymbolicReference(object):
# have to be dynamic here as we may be a tag which can point to anything
# Our path will be resolved to the hexsha which will be used accordingly
return Object.new_from_sha(self.repo, hex_to_bin(self.dereference_recursive(self.repo, self.path)))
-
+
def _get_commit(self):
"""
:return:
@@ -169,15 +169,15 @@ class SymbolicReference(object):
if obj.type == 'tag':
obj = obj.object
#END dereference tag
-
+
if obj.type != Commit.type:
raise TypeError("Symbolic Reference pointed to object %r, commit was required" % obj)
#END handle type
return obj
-
+
def set_commit(self, commit, logmsg = None):
"""As set_object, but restricts the type of object to be a Commit
-
+
:raise ValueError: If commit is not a Commit object or doesn't point to
a commit
:return: self"""
@@ -194,21 +194,21 @@ class SymbolicReference(object):
raise ValueError("Invalid object: %s" % commit)
#END handle exception
# END verify type
-
+
if invalid_type:
raise ValueError("Need commit, got %r" % commit)
#END handle raise
-
+
# we leave strings to the rev-parse method below
self.set_object(commit, logmsg)
-
+
return self
-
-
+
+
def set_object(self, object, logmsg = None):
"""Set the object we point to, possibly dereference our symbolic reference first.
If the reference does not exist, it will be created
-
+
:param object: a refspec, a SymbolicReference or an Object instance. SymbolicReferences
will be dereferenced beforehand to obtain the object they point to
:param logmsg: If not None, the message will be used in the reflog entry to be
@@ -218,23 +218,23 @@ class SymbolicReference(object):
if isinstance(object, SymbolicReference):
object = object.object
#END resolve references
-
+
is_detached = True
try:
is_detached = self.is_detached
except ValueError:
pass
# END handle non-existing ones
-
+
if is_detached:
return self.set_reference(object, logmsg)
-
+
# set the commit on our reference
return self._get_reference().set_object(object, logmsg)
-
+
commit = property(_get_commit, set_commit, doc="Query or set commits directly")
object = property(_get_object, set_object, doc="Return the object our ref currently refers to")
-
+
def _get_reference(self):
""":return: Reference Object we point to
:raise TypeError: If this symbolic reference is detached, hence it doesn't point
@@ -243,22 +243,22 @@ class SymbolicReference(object):
if target_ref_path is None:
raise TypeError("%s is a detached symbolic reference as it points to %r" % (self, sha))
return self.from_path(self.repo, target_ref_path)
-
+
def set_reference(self, ref, logmsg = None):
"""Set ourselves to the given ref. It will stay a symbol if the ref is a Reference.
Otherwise an Object, given as Object instance or refspec, is assumed and if valid,
will be set which effectively detaches the refererence if it was a purely
symbolic one.
-
+
:param ref: SymbolicReference instance, Object instance or refspec string
Only if the ref is a SymbolicRef instance, we will point to it. Everthiny
else is dereferenced to obtain the actual object.
:param logmsg: If set to a string, the message will be used in the reflog.
Otherwise, a reflog entry is not written for the changed reference.
The previous commit of the entry will be the commit we point to now.
-
+
See also: log_append()
-
+
:return: self
:note: This symbolic reference will not be dereferenced. For that, see
``set_object(...)``"""
@@ -279,12 +279,12 @@ class SymbolicReference(object):
else:
raise ValueError("Unrecognized Value: %r" % ref)
# END try commit attribute
-
+
# typecheck
if obj is not None and self._points_to_commits_only and obj.type != Commit.type:
raise TypeError("Require commit, got %r" % obj)
#END verify type
-
+
oldbinsha = None
if logmsg is not None:
try:
@@ -293,27 +293,27 @@ class SymbolicReference(object):
oldbinsha = Commit.NULL_BIN_SHA
#END handle non-existing
#END retrieve old hexsha
-
+
fpath = self.abspath
assure_directory_exists(fpath, is_file=True)
-
+
lfd = LockedFD(fpath)
fd = lfd.open(write=True, stream=True)
fd.write(write_value)
lfd.commit()
-
+
# Adjust the reflog
if logmsg is not None:
self.log_append(oldbinsha, logmsg)
#END handle reflog
-
+
return self
-
+
# aliased reference
reference = property(_get_reference, set_reference, doc="Returns the Reference we point to")
ref = reference
-
+
def is_valid(self):
"""
:return:
@@ -325,7 +325,7 @@ class SymbolicReference(object):
return False
else:
return True
-
+
@property
def is_detached(self):
"""
@@ -337,19 +337,19 @@ class SymbolicReference(object):
return False
except TypeError:
return True
-
+
def log(self):
"""
:return: RefLog for this reference. Its last entry reflects the latest change
applied to this reference
-
+
.. note:: As the log is parsed every time, its recommended to cache it for use
instead of calling this method repeatedly. It should be considered read-only."""
return RefLog.from_file(RefLog.path(self))
-
+
def log_append(self, oldbinsha, message, newbinsha=None):
"""Append a logentry to the logfile of this ref
-
+
:param oldbinsha: binary sha this ref used to point to
:param message: A message describing the change
:param newbinsha: The sha the ref points to now. If None, our current commit sha
@@ -362,7 +362,7 @@ class SymbolicReference(object):
def log_entry(self, index):
""":return: RefLogEntry at the given index
:param index: python list compatible positive or negative index
-
+
.. note:: This method must read part of the reflog during execution, hence
it should be used sparringly, or only if you need just one index.
In that case, it will be faster than the ``log()`` method"""
@@ -381,14 +381,14 @@ class SymbolicReference(object):
if not path.startswith(cls._common_path_default+"/"):
full_ref_path = '%s/%s' % (cls._common_path_default, path)
return full_ref_path
-
+
@classmethod
def delete(cls, repo, path):
"""Delete the reference at the given path
-
+
:param repo:
Repository to delete the reference from
-
+
:param path:
Short or full path pointing to the reference, i.e. refs/myreference
or just "myreference", hence 'refs/' is implied.
@@ -419,13 +419,13 @@ class SymbolicReference(object):
dropped_last_line = False
continue
# END skip comments and lines without our path
-
+
# drop this line
made_change = True
dropped_last_line = True
# END for each line in packed refs
reader.close()
-
+
# write the new lines
if made_change:
# write-binary is required, otherwise windows will
@@ -434,14 +434,14 @@ class SymbolicReference(object):
# END write out file
# END open exception handling
# END handle deletion
-
+
# delete the reflog
reflog_path = RefLog.path(cls(repo, full_ref_path))
if os.path.isfile(reflog_path):
os.remove(reflog_path)
#END remove reflog
-
-
+
+
@classmethod
def _create(cls, repo, path, resolve, reference, force, logmsg=None):
"""internal method used to create a new symbolic reference.
@@ -451,12 +451,12 @@ class SymbolicReference(object):
instead"""
full_ref_path = cls.to_full_path(path)
abs_ref_path = join(repo.git_dir, full_ref_path)
-
+
# figure out target data
target = reference
if resolve:
target = repo.rev_parse(str(reference))
-
+
if not force and isfile(abs_ref_path):
target_data = str(target)
if isinstance(target, SymbolicReference):
@@ -467,61 +467,61 @@ class SymbolicReference(object):
if existing_data != target_data:
raise OSError("Reference at %r does already exist, pointing to %r, requested was %r" % (full_ref_path, existing_data, target_data))
# END no force handling
-
+
ref = cls(repo, full_ref_path)
ref.set_reference(target, logmsg)
return ref
-
+
@classmethod
def create(cls, repo, path, reference='HEAD', force=False, logmsg=None):
"""Create a new symbolic reference, hence a reference pointing to another reference.
-
+
:param repo:
Repository to create the reference in
-
+
:param path:
full path at which the new symbolic reference is supposed to be
created at, i.e. "NEW_HEAD" or "symrefs/my_new_symref"
-
+
:param reference:
The reference to which the new symbolic reference should point to.
If it is a commit'ish, the symbolic ref will be detached.
-
+
:param force:
if True, force creation even if a symbolic reference with that name already exists.
Raise OSError otherwise
-
+
:param logmsg:
If not None, the message to append to the reflog. Otherwise no reflog
entry is written.
-
+
:return: Newly created symbolic Reference
-
+
:raise OSError:
If a (Symbolic)Reference with the same name but different contents
already exists.
-
+
:note: This does not alter the current HEAD, index or Working Tree"""
return cls._create(repo, path, cls._resolve_ref_on_create, reference, force, logmsg)
-
+
def rename(self, new_path, force=False):
"""Rename self to a new path
-
+
:param new_path:
Either a simple name or a full path, i.e. new_name or features/new_name.
The prefix refs/ is implied for references and will be set as needed.
In case this is a symbolic ref, there is no implied prefix
-
+
:param force:
If True, the rename will succeed even if a head with the target name
already exists. It will be overwritten in that case
-
+
:return: self
:raise OSError: In case a file at path but a different contents already exists """
new_path = self.to_full_path(new_path)
if self.path == new_path:
return self
-
+
new_abs_path = join(self.repo.git_dir, new_path)
cur_abs_path = join(self.repo.git_dir, self.path)
if isfile(new_abs_path):
@@ -534,23 +534,23 @@ class SymbolicReference(object):
# END not force handling
os.remove(new_abs_path)
# END handle existing target file
-
+
dname = dirname(new_abs_path)
if not isdir(dname):
os.makedirs(dname)
# END create directory
-
+
rename(cur_abs_path, new_abs_path)
self.path = new_path
-
+
return self
-
+
@classmethod
def _iter_items(cls, repo, common_path = None):
if common_path is None:
common_path = cls._common_path_default
rela_paths = set()
-
+
# walk loose refs
# Currently we do not follow links
for root, dirs, files in os.walk(join_path_native(repo.git_dir, common_path)):
@@ -559,20 +559,20 @@ class SymbolicReference(object):
if refs_id:
dirs[0:] = ['refs']
# END prune non-refs folders
-
+
for f in files:
abs_path = to_native_path_linux(join_path(root, f))
rela_paths.add(abs_path.replace(to_native_path_linux(repo.git_dir) + '/', ""))
# END for each file in root directory
# END for each directory to walk
-
+
# read packed refs
for sha, rela_path in cls._iter_packed_refs(repo):
if rela_path.startswith(common_path):
rela_paths.add(rela_path)
# END relative path matches common path
# END packed refs reading
-
+
# return paths in sorted order
for path in sorted(rela_paths):
try:
@@ -580,7 +580,7 @@ class SymbolicReference(object):
except ValueError:
continue
# END for each sorted relative refpath
-
+
@classmethod
def iter_items(cls, repo, common_path = None):
"""Find all refs in the repository
@@ -596,11 +596,11 @@ class SymbolicReference(object):
:return:
git.SymbolicReference[], each of them is guaranteed to be a symbolic
ref which is not detached and pointing to a valid ref
-
+
List is lexigraphically sorted
The returned objects represent actual subclasses, such as Head or TagReference"""
return ( r for r in cls._iter_items(repo, common_path) if r.__class__ == SymbolicReference or not r.is_detached )
-
+
@classmethod
def from_path(cls, repo, path):
"""
@@ -611,7 +611,7 @@ class SymbolicReference(object):
depending on the given path"""
if not path:
raise ValueError("Cannot create Reference from %r" % path)
-
+
for ref_type in (HEAD, Head, RemoteReference, TagReference, Reference, SymbolicReference):
try:
instance = ref_type(repo, path)