コード例 #1
0
ファイル: nodes.py プロジェクト: elfixit/rhodecode
 def _set_kind(self, kind):
     if hasattr(self, '_kind'):
         raise NodeError("Cannot change node's kind")
     else:
         self._kind = kind
         # Post setter check (path's trailing slash)
         if self.path.endswith('/'):
             raise NodeError("Node's path cannot end with slash")
コード例 #2
0
    def __init__(self, path, kind):
        self._validate_path(path)  # can throw exception if path is invalid
        self.path = safe_str(path.rstrip('/'))  # we store paths as str
        if path == '' and kind != NodeKind.DIR:
            raise NodeError("Only DirNode and its subclasses may be "
                            "initialized with empty path")
        self.kind = kind

        if self.is_root() and not self.is_dir():
            raise NodeError("Root node cannot be FILE kind")
コード例 #3
0
ファイル: nodes.py プロジェクト: elfixit/rhodecode
 def __init__(self, path, kind):
     if path.startswith('/'):
         raise NodeError("Cannot initialize Node objects with slash at "
             "the beginning as only relative paths are supported")
     self.path = path.rstrip('/')
     if path == '' and kind != NodeKind.DIR:
         raise NodeError("Only DirNode and its subclasses may be "
                         "initialized with empty path")
     self.kind = kind
     #self.dirs, self.files = [], []
     if self.is_root() and not self.is_dir():
         raise NodeError("Root node cannot be FILE kind")
コード例 #4
0
ファイル: nodes.py プロジェクト: elfixit/rhodecode
    def get_mimetype(self):
        """
        Mimetype is calculated based on the file's content. If ``_mimetype``
        attribute is available, it will be returned (backends which store
        mimetypes or can easily recognize them, should set this private
        attribute to indicate that type should *NOT* be calculated).
        """
        if hasattr(self, '_mimetype'):
            if (isinstance(self._mimetype, (tuple, list,)) and
                len(self._mimetype) == 2):
                return self._mimetype
            else:
                raise NodeError('given _mimetype attribute must be an 2 '
                               'element list or tuple')

        mtype, encoding = mimetypes.guess_type(self.name)

        if mtype is None:
            if self.is_binary:
                mtype = 'application/octet-stream'
                encoding = None
            else:
                mtype = 'text/plain'
                encoding = None
        return mtype, encoding
コード例 #5
0
ファイル: nodes.py プロジェクト: elfixit/rhodecode
 def history(self):
     """
     Returns a list of changeset for this file in which the file was changed
     """
     if self.changeset is None:
         raise NodeError('Unable to get changeset for this FileNode')
     return self.changeset.get_file_history(self.path)
コード例 #6
0
ファイル: nodes.py プロジェクト: elfixit/rhodecode
 def annotate(self):
     """
     Returns a list of three element tuples with lineno,changeset and line
     """
     if self.changeset is None:
         raise NodeError('Unable to get changeset for this FileNode')
     return self.changeset.get_file_annotate(self.path)
コード例 #7
0
 def annotate(self):
     """
     Returns a list of three element tuples with lineno, commit and line
     """
     if self.commit is None:
         raise NodeError('Unable to get commit for this FileNode')
     pre_load = ["author", "date", "message"]
     return self.commit.get_file_annotate(self.path, pre_load=pre_load)
コード例 #8
0
ファイル: nodes.py プロジェクト: elfixit/rhodecode
 def state(self):
     if not self.changeset:
         raise NodeError("Cannot check state of the node if it's not "
             "linked with changeset")
     elif self.path in (node.path for node in self.changeset.added):
         return NodeState.ADDED
     elif self.path in (node.path for node in self.changeset.changed):
         return NodeState.CHANGED
     else:
         return NodeState.NOT_CHANGED
コード例 #9
0
ファイル: nodes.py プロジェクト: elfixit/rhodecode
    def get_node(self, path):
        """
        Returns node from within this particular ``DirNode``, so it is now
        allowed to fetch, i.e. node located at 'docs/api/index.rst' from node
        'docs'. In order to access deeper nodes one must fetch nodes between
        them first - this would work::

           docs = root.get_node('docs')
           docs.get_node('api').get_node('index.rst')

        :param: path - relative to the current node

        .. note::
           To access lazily (as in example above) node have to be initialized
           with related changeset object - without it node is out of
           context and may know nothing about anything else than nearest
           (located at same level) nodes.
        """
        try:
            path = path.rstrip('/')
            if path == '':
                raise NodeError("Cannot retrieve node without path")
            self.nodes  # access nodes first in order to set _nodes_dict
            paths = path.split('/')
            if len(paths) == 1:
                if not self.is_root():
                    path = '/'.join((self.path, paths[0]))
                else:
                    path = paths[0]
                return self._nodes_dict[path]
            elif len(paths) > 1:
                if self.changeset is None:
                    raise NodeError("Cannot access deeper "
                                    "nodes without changeset")
                else:
                    path1, path2 = paths[0], '/'.join(paths[1:])
                    return self.get_node(path1).get_node(path2)
            else:
                raise KeyError
        except KeyError:
            raise NodeError("Node does not exist at %s" % path)
コード例 #10
0
    def __init__(self, path, nodes=(), commit=None):
        """
        Only one of ``nodes`` and ``commit`` may be given. Passing both
        would raise ``NodeError`` exception.

        :param path: relative path to the node
        :param nodes: content may be passed to constructor
        :param commit: if given, will use it to lazily fetch content
        """
        if nodes and commit:
            raise NodeError("Cannot use both nodes and commit")
        super(DirNode, self).__init__(path, NodeKind.DIR)
        self.commit = commit
        self._nodes = nodes
コード例 #11
0
    def __init__(self, path, content=None, commit=None, mode=None):
        """
        Only one of ``content`` and ``commit`` may be given. Passing both
        would raise ``NodeError`` exception.

        :param path: relative path to the node
        :param content: content may be passed to constructor
        :param commit: if given, will use it to lazily fetch content
        :param mode: ST_MODE (i.e. 0100644)
        """
        if content and commit:
            raise NodeError("Cannot use both content and commit")
        super(FileNode, self).__init__(path, kind=NodeKind.FILE)
        self.commit = commit
        self._content = content
        self._mode = mode or FILEMODE_DEFAULT
コード例 #12
0
 def _get_file_node(self, commit_id, f_path):
     if commit_id not in ['', None, 'None', '0' * 12, '0' * 40]:
         commit = c.rhodecode_repo.get_commit(commit_id=commit_id)
         try:
             node = commit.get_node(f_path)
             if node.is_dir():
                 raise NodeError('%s path is a %s not a file' %
                                 (node, type(node)))
         except NodeDoesNotExistError:
             commit = EmptyCommit(commit_id=commit_id,
                                  idx=commit.idx,
                                  repo=commit.repository,
                                  alias=commit.repository.alias,
                                  message=commit.message,
                                  author=commit.author,
                                  date=commit.date)
             node = FileNode(f_path, '', commit=commit)
     else:
         commit = EmptyCommit(repo=c.rhodecode_repo,
                              alias=c.rhodecode_repo.alias)
         node = FileNode(f_path, '', commit=commit)
     return node
コード例 #13
0
ファイル: nodes.py プロジェクト: elfixit/rhodecode
 def content(self):
     raise NodeError("%s represents a dir and has no ``content`` attribute"
         % self)
コード例 #14
0
 def _validate_path(self, path):
     if path.startswith('/'):
         raise NodeError(
             "Cannot initialize Node objects with slash at "
             "the beginning as only relative paths are supported. "
             "Got %s" % (path, ))
コード例 #15
0
 def last_commit(self):
     if self.commit:
         pre_load = ["author", "date", "message"]
         return self.commit.get_file_commit(self.path, pre_load=pre_load)
     raise NodeError("Cannot retrieve last commit of the file without "
                     "related commit attribute")
コード例 #16
0
ファイル: nodes.py プロジェクト: elfixit/rhodecode
 def last_changeset(self):
     if self.changeset:
         return self.changeset.get_file_changeset(self.path)
     raise NodeError("Cannot retrieve last changeset of the file without "
         "related changeset attribute")
コード例 #17
0
ファイル: nodes.py プロジェクト: elfixit/rhodecode
 def message(self):
     if self.changeset:
         return self.last_changeset.message
     raise NodeError("Cannot retrieve message of the file without related "
         "changeset attribute")
コード例 #18
0
    def diff_2way(self, repo_name, f_path):
        diff1 = request.GET.get('diff1', '')
        diff2 = request.GET.get('diff2', '')
        try:
            if diff1 not in ['', None, 'None', '0' * 12, '0' * 40]:
                c.changeset_1 = c.rhodecode_repo.get_changeset(diff1)
                try:
                    node1 = c.changeset_1.get_node(f_path)
                    if node1.is_dir():
                        raise NodeError('%s path is a %s not a file' %
                                        (node1, type(node1)))
                except NodeDoesNotExistError:
                    c.changeset_1 = EmptyChangeset(
                        cs=diff1,
                        revision=c.changeset_1.revision,
                        repo=c.rhodecode_repo)
                    node1 = FileNode(f_path, '', changeset=c.changeset_1)
            else:
                c.changeset_1 = EmptyChangeset(repo=c.rhodecode_repo)
                node1 = FileNode(f_path, '', changeset=c.changeset_1)

            if diff2 not in ['', None, 'None', '0' * 12, '0' * 40]:
                c.changeset_2 = c.rhodecode_repo.get_changeset(diff2)
                try:
                    node2 = c.changeset_2.get_node(f_path)
                    if node2.is_dir():
                        raise NodeError('%s path is a %s not a file' %
                                        (node2, type(node2)))
                except NodeDoesNotExistError:
                    c.changeset_2 = EmptyChangeset(
                        cs=diff2,
                        revision=c.changeset_2.revision,
                        repo=c.rhodecode_repo)
                    node2 = FileNode(f_path, '', changeset=c.changeset_2)
            else:
                c.changeset_2 = EmptyChangeset(repo=c.rhodecode_repo)
                node2 = FileNode(f_path, '', changeset=c.changeset_2)
        except (RepositoryError, NodeError):
            log.error(traceback.format_exc())
            return redirect(
                url('files_home', repo_name=c.repo_name, f_path=f_path))
        if node2.is_binary:
            node2_content = 'binary file'
        else:
            node2_content = node2.content

        if node1.is_binary:
            node1_content = 'binary file'
        else:
            node1_content = node1.content

        html_escape_table = {
            "&": "\u0026",
            '"': "\u0022",
            "'": "\u0027",
            ">": "\u003e",
            "<": "\u003c",
            '\\': "\u005c",
            '\n': '\\n'
        }

        c.orig1 = h.html_escape((node1_content), html_escape_table)
        c.orig2 = h.html_escape((node2_content), html_escape_table)
        c.node1 = node1
        c.node2 = node2
        c.cs1 = c.changeset_1
        c.cs2 = c.changeset_2

        return render('files/diff_2way.html')
コード例 #19
0
ファイル: nodes.py プロジェクト: elfixit/rhodecode
 def state(self):
     raise NodeError("Cannot access state of DirNode")
コード例 #20
0
    def diff(self, repo_name, f_path):
        ignore_whitespace = request.GET.get('ignorews') == '1'
        line_context = request.GET.get('context', 3)
        diff1 = request.GET.get('diff1', '')
        diff2 = request.GET.get('diff2', '')
        c.action = request.GET.get('diff')
        c.no_changes = diff1 == diff2
        c.f_path = f_path
        c.big_diff = False
        c.anchor_url = anchor_url
        c.ignorews_url = _ignorews_url
        c.context_url = _context_url
        c.changes = OrderedDict()
        c.changes[diff2] = []

        #special case if we want a show rev only, it's impl here
        #to reduce JS and callbacks

        if request.GET.get('show_rev'):
            if str2bool(request.GET.get('annotate', 'False')):
                _url = url('files_annotate_home', repo_name=c.repo_name,
                           revision=diff1, f_path=c.f_path)
            else:
                _url = url('files_home', repo_name=c.repo_name,
                           revision=diff1, f_path=c.f_path)

            return redirect(_url)
        try:
            if diff1 not in ['', None, 'None', '0' * 12, '0' * 40]:
                c.changeset_1 = c.rhodecode_repo.get_changeset(diff1)
                try:
                    node1 = c.changeset_1.get_node(f_path)
                    if node1.is_dir():
                        raise NodeError('%s path is a %s not a file'
                                        % (node1, type(node1)))
                except NodeDoesNotExistError:
                    c.changeset_1 = EmptyChangeset(cs=diff1,
                                                   revision=c.changeset_1.revision,
                                                   repo=c.rhodecode_repo)
                    node1 = FileNode(f_path, '', changeset=c.changeset_1)
            else:
                c.changeset_1 = EmptyChangeset(repo=c.rhodecode_repo)
                node1 = FileNode(f_path, '', changeset=c.changeset_1)

            if diff2 not in ['', None, 'None', '0' * 12, '0' * 40]:
                c.changeset_2 = c.rhodecode_repo.get_changeset(diff2)
                try:
                    node2 = c.changeset_2.get_node(f_path)
                    if node2.is_dir():
                        raise NodeError('%s path is a %s not a file'
                                        % (node2, type(node2)))
                except NodeDoesNotExistError:
                    c.changeset_2 = EmptyChangeset(cs=diff2,
                                                   revision=c.changeset_2.revision,
                                                   repo=c.rhodecode_repo)
                    node2 = FileNode(f_path, '', changeset=c.changeset_2)
            else:
                c.changeset_2 = EmptyChangeset(repo=c.rhodecode_repo)
                node2 = FileNode(f_path, '', changeset=c.changeset_2)
        except (RepositoryError, NodeError):
            log.error(traceback.format_exc())
            return redirect(url('files_home', repo_name=c.repo_name,
                                f_path=f_path))

        if c.action == 'download':
            _diff = diffs.get_gitdiff(node1, node2,
                                      ignore_whitespace=ignore_whitespace,
                                      context=line_context)
            diff = diffs.DiffProcessor(_diff, format='gitdiff')

            diff_name = '%s_vs_%s.diff' % (diff1, diff2)
            response.content_type = 'text/plain'
            response.content_disposition = (
                'attachment; filename=%s' % diff_name
            )
            return diff.as_raw()

        elif c.action == 'raw':
            _diff = diffs.get_gitdiff(node1, node2,
                                      ignore_whitespace=ignore_whitespace,
                                      context=line_context)
            diff = diffs.DiffProcessor(_diff, format='gitdiff')
            response.content_type = 'text/plain'
            return diff.as_raw()

        else:
            fid = h.FID(diff2, node2.path)
            line_context_lcl = get_line_ctx(fid, request.GET)
            ign_whitespace_lcl = get_ignore_ws(fid, request.GET)

            lim = request.GET.get('fulldiff') or self.cut_off_limit
            _, cs1, cs2, diff, st = diffs.wrapped_diff(filenode_old=node1,
                                         filenode_new=node2,
                                         cut_off_limit=lim,
                                         ignore_whitespace=ign_whitespace_lcl,
                                         line_context=line_context_lcl,
                                         enable_comments=False)
            op = ''
            filename = node1.path
            cs_changes = {
                'fid': [cs1, cs2, op, filename, diff, st]
            }
            c.changes = cs_changes

        return render('files/file_diff.html')
コード例 #21
0
ファイル: nodes.py プロジェクト: elfixit/rhodecode
 def size(self):
     if self.changeset:
         return self.changeset.get_file_size(self.path)
     raise NodeError("Cannot retrieve size of the file without related "
         "changeset attribute")