Example #1
0
    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"""
        return RefLog.entry_at(RefLog.path(self), index)
Example #2
0
    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"""
        return RefLog.entry_at(RefLog.path(self), index)
Example #3
0
    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))
Example #4
0
    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))
Example #5
0
    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
            will be used
        :return: added RefLogEntry instance"""
        return RefLog.append_entry(self.repo.config_reader(), RefLog.path(self), oldbinsha,
                                   (newbinsha is None and self.commit.binsha) or newbinsha,
                                   message)
Example #6
0
	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
			will be used
		:return: added RefLogEntry instance"""
		return RefLog.append_entry(self.repo.config_reader(), RefLog.path(self), oldbinsha, 
									(newbinsha is None and self.commit.binsha) or newbinsha, 
									message) 
Example #7
0
    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.
            Alternatively the symbolic reference to be deleted"""
        full_ref_path = cls.to_full_path(path)
        abs_path = join(repo.git_dir, full_ref_path)
        if exists(abs_path):
            os.remove(abs_path)
        else:
            # check packed refs
            pack_file_path = cls._get_packed_refs_path(repo)
            try:
                reader = open(pack_file_path, 'rb')
            except (OSError, IOError):
                pass  # it didnt exist at all
            else:
                new_lines = list()
                made_change = False
                dropped_last_line = False
                for line in reader:
                    # keep line if it is a comment or if the ref to delete is not
                    # in the line
                    # If we deleted the last line and this one is a tag-reference object,
                    # we drop it as well
                    if ( line.startswith('#') or full_ref_path not in line ) and \
                            (not dropped_last_line or dropped_last_line and not line.startswith(
                                    '^') ):
                        new_lines.append(line)
                        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
                    # open the file in text mode and change LF to CRLF !
                    open(pack_file_path, 'wb').writelines(new_lines)
                # 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)
Example #8
0
    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.
			Alternatively the symbolic reference to be deleted"""
        full_ref_path = cls.to_full_path(path)
        abs_path = join(repo.git_dir, full_ref_path)
        if exists(abs_path):
            os.remove(abs_path)
        else:
            # check packed refs
            pack_file_path = cls._get_packed_refs_path(repo)
            try:
                reader = open(pack_file_path, 'rb')
            except (OSError, IOError):
                pass  # it didnt exist at all
            else:
                new_lines = list()
                made_change = False
                dropped_last_line = False
                for line in reader:
                    # keep line if it is a comment or if the ref to delete is not
                    # in the line
                    # If we deleted the last line and this one is a tag-reference object,
                    # we drop it as well
                    if ( line.startswith('#') or full_ref_path not in line ) and \
                     ( not dropped_last_line or dropped_last_line and not line.startswith('^') ):
                        new_lines.append(line)
                        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
                    # open the file in text mode and change LF to CRLF !
                    open(pack_file_path, 'wb').writelines(new_lines)
                # 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)
Example #9
0
    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
            will be used
        :return: added RefLogEntry instance"""
        # NOTE: we use the committer of the currently active commit - this should be
        # correct to allow overriding the committer on a per-commit level.
        # See https://github.com/gitpython-developers/GitPython/pull/146
        try:
            committer_or_reader = self.commit.committer
        except ValueError:
            committer_or_reader = self.repo.config_reader()
        # end handle newly cloned repositories
        return RefLog.append_entry(committer_or_reader, RefLog.path(self),
                                   oldbinsha,
                                   (newbinsha is None and self.commit.binsha)
                                   or newbinsha, message)