Beispiel #1
0
def html_diff(diff, linenos='inline'):
    """Display diff as HTML"""
    if diff is None:
        return
    difflexer = DiffLexer()
    # Do not give whitespaces the special Whitespace token type as this
    # prevents the html formatter from picking up on trailing whitespaces in
    # the diff.
    difflexer.add_filter(VisibleWhitespaceFilter(wstokentype=False, tabs=True))

    return highlight(
        diff, difflexer,
        HtmlFormatter(linenos=linenos, noclasses=True, style="diffstyle"))
Beispiel #2
0
def html_diff(diff):
    """Display diff as HTML"""
    if diff is None:
        return
    return highlight(diff, DiffLexer(),
                     HtmlFormatter(
                         noclasses=True,
                         style="pastie",
                     ))
Beispiel #3
0
def html_diff(diff):
    """Display diff as HTML"""
    if diff is None:
        return
    difflexer = DiffLexer()
    # Do not give whitespaces the special Whitespace token type as this
    # prevents the html formatter from picking up on trailing whitespaces in
    # the diff.
    difflexer.add_filter(VisibleWhitespaceFilter(wstokentype=False, tabs=True))

    return highlight(
        diff,
        difflexer,
        HtmlFormatter(
            linenos='inline',
            noclasses=True,
            style="diffstyle")
    )
Beispiel #4
0
def view_commit(repo, commitid, username=None):
    """ Render a commit in a repo
    """
    reponame = os.path.join(APP.config['GIT_FOLDER'], repo + '.git')
    if username:
        reponame = os.path.join(APP.config['FORK_FOLDER'], username,
                                repo + '.git')

    if not os.path.exists(reponame):
        flask.abort(404, 'Project not found')

    project = spechub.lib.get_or_create_project(SESSION, repo, username)
    repo_obj = pygit2.Repository(reponame)

    try:
        commit = repo_obj.get(commitid)
    except ValueError:
        flask.abort(404, 'Commit not found')

    if commit.parents:
        diff = commit.tree.diff_to_tree()

        parent = repo_obj.revparse_single('%s^' % commitid)
        diff = repo_obj.diff(parent, commit)
    else:
        # First commit in the repo
        diff = commit.tree.diff_to_tree(swap=True)

    html_diff = highlight(diff.patch, DiffLexer(),
                          HtmlFormatter(
                              noclasses=True,
                              style="tango",
                          ))

    return flask.render_template(
        'commit.html',
        select='logs',
        repo=repo,
        username=username,
        commitid=commitid,
        commit=commit,
        diff=diff,
        html_diff=html_diff,
        forks=spechub.lib.get_forks(SESSION, project),
    )
Beispiel #5
0
    def tokenize_diff(cls, diff):
        """Token generator for a patch string

        :param diff: the patch to format
        :type diff: str
        :rtype: tuple[Token, str]
        """

        heading = diff.splitlines()[0]
        match = Formatter.DIFF_HEADING_RE.match(heading)
        diff = diff[len(heading):]

        if match:
            yield Token.Generic.Heading, 'commit %s' % match.group('commit')
            yield NEW_LINE

        for token in pygments.lex(diff, DiffLexer(encoding='utf-8')):
            yield token
Beispiel #6
0
    def format_testcase_diff(diff):
        """Format a testcase output diff.

        PARAMETERS
            diff: the diff content

        RETURNS
            a list of pygments' Tokens
        """

        def new_line_token():
            """Generate a new line token."""
            return Token.Whitespace, '\n'

        def indent_token():
            """Generate an indentation space token."""
            return Token.Whitespace, ' ' * 4

        tokens = []
        new_line = True

        # Because of logging prefixes, skip the first line to avoid
        # misalignment.
        tokens.append(new_line_token())

        for ttype, value in pygments.lex(diff, DiffLexer()):
            for subval in value.split('\n'):
                if new_line:
                    tokens.append(indent_token())

                new_line = not subval

                if subval:
                    tokens.append((ttype, subval))
                else:
                    tokens.append(new_line_token())

        return tokens
Beispiel #7
0
def request_pull(repo, requestid, username=None):
    """ Request pulling the changes from the fork into the project.
    """

    repo = progit.lib.get_project(SESSION, repo, user=username)

    if not repo:
        flask.abort(404, 'Project not found')

    request = progit.lib.get_pull_request(SESSION,
                                          project_id=repo.id,
                                          requestid=requestid)

    if not request:
        flask.abort(404, 'Pull-request not found')

    repo = request.repo_from

    if repo.is_fork:
        repopath = os.path.join(APP.config['FORK_FOLDER'], repo.path)
    else:
        repopath = os.path.join(APP.config['GIT_FOLDER'], repo.path)
    repo_obj = pygit2.Repository(repopath)

    if repo.parent:
        parentname = os.path.join(APP.config['GIT_FOLDER'], repo.parent.path)
    else:
        parentname = os.path.join(APP.config['GIT_FOLDER'], repo.path)
    orig_repo = pygit2.Repository(parentname)

    diff_commits = []
    diffs = []
    repo_commit = repo_obj[request.stop_id]
    if not repo_obj.is_empty and not orig_repo.is_empty:
        orig_commit = orig_repo[orig_repo.lookup_branch(
            'master').get_object().hex]

        master_commits = [
            commit.oid.hex for commit in orig_repo.walk(
                orig_repo.lookup_branch('master').get_object().hex,
                pygit2.GIT_SORT_TIME)
        ]

        repo_commit = repo_obj[request.start_id]

        for commit in repo_obj.walk(request.stop_id, pygit2.GIT_SORT_TIME):
            if commit.oid.hex in master_commits:
                break
            diff_commits.append(commit)
            diffs.append(
                repo_obj.diff(
                    repo_obj.revparse_single(commit.parents[0].oid.hex),
                    repo_obj.revparse_single(commit.oid.hex)))

    elif orig_repo.is_empty:
        orig_commit = None
        diff = repo_commit.tree.diff_to_tree(swap=True)
    else:
        flask.flash('Fork is empty, there are no commits to request pulling',
                    'error')
        return flask.redirect(
            flask.url_for('view_repo', username=username, repo=repo.name))

    html_diffs = []
    for diff in diffs:
        html_diffs.append(
            highlight(diff.patch, DiffLexer(),
                      HtmlFormatter(
                          noclasses=True,
                          style="tango",
                      )))

    return flask.render_template(
        'pull_request.html',
        select='requests',
        requestid=requestid,
        repo=repo,
        username=username,
        request=request,
        repo_admin=is_repo_admin(request.repo),
        repo_obj=repo_obj,
        orig_repo=orig_repo,
        diff_commits=diff_commits,
        diffs=diffs,
        html_diffs=html_diffs,
    )
Beispiel #8
0
def new_request_pull(repo, username=None, commitid=None):
    """ Request pulling the changes from the fork into the project.
    """
    repo = progit.lib.get_project(SESSION, repo, user=username)

    if not repo:
        flask.abort(404)

    if not is_repo_admin(repo):
        flask.abort(
            403,
            'You are not allowed to create pull-requests for this project')

    if repo.is_fork:
        repopath = os.path.join(APP.config['FORK_FOLDER'], repo.path)
    else:
        repopath = os.path.join(APP.config['GIT_FOLDER'], repo.path)
    repo_obj = pygit2.Repository(repopath)

    if repo.parent:
        parentname = os.path.join(APP.config['GIT_FOLDER'], repo.parent.path)
    else:
        parentname = os.path.join(APP.config['GIT_FOLDER'], repo.path)
    orig_repo = pygit2.Repository(parentname)

    frombranchname = flask.request.args.get('from_branch', 'master')
    frombranch = repo_obj.lookup_branch(frombranchname)
    if not frombranch:
        flask.flash('Branch %s does not exist' % frombranchname, 'error')
        frombranchname = 'master'

    branchname = flask.request.args.get('branch', 'master')
    branch = orig_repo.lookup_branch(branchname)
    if not branch:
        flask.flash('Branch %s does not exist' % branchname, 'error')
        branchname = 'master'

    if commitid is None:
        commitid = repo_obj.head.target
        if branchname:
            branch = repo_obj.lookup_branch(frombranchname)
            commitid = branch.get_object().hex

    diff_commits = []
    diffs = []
    if not repo_obj.is_empty and not orig_repo.is_empty:
        orig_commit = orig_repo[orig_repo.lookup_branch(
            branchname).get_object().hex]

        master_commits = [
            commit.oid.hex for commit in orig_repo.walk(
                orig_repo.lookup_branch(branchname).get_object().hex,
                pygit2.GIT_SORT_TIME)
        ]

        repo_commit = repo_obj[commitid]

        for commit in repo_obj.walk(repo_commit.oid.hex, pygit2.GIT_SORT_TIME):
            if commit.oid.hex in master_commits:
                break
            diff_commits.append(commit)
            diffs.append(
                repo_obj.diff(
                    repo_obj.revparse_single(commit.parents[0].oid.hex),
                    repo_obj.revparse_single(commit.oid.hex)))

    elif orig_repo.is_empty:
        orig_commit = None
        repo_commit = repo_obj[repo_obj.head.target]
        diff = repo_commit.tree.diff_to_tree(swap=True)
    else:
        flask.flash('Fork is empty, there are no commits to request pulling',
                    'error')
        return flask.redirect(
            flask.url_for('view_repo', username=username, repo=repo.name))

    html_diffs = []
    for diff in diffs:
        html_diffs.append(
            highlight(diff.patch, DiffLexer(),
                      HtmlFormatter(
                          noclasses=True,
                          style="tango",
                      )))

    form = progit.forms.RequestPullForm()
    if form.validate_on_submit():
        try:
            if orig_commit:
                orig_commit = orig_commit.oid.hex

            parent = repo
            if repo.parent:
                parent = repo.parent

            message = progit.lib.new_pull_request(
                SESSION,
                repo=parent,
                repo_from=repo,
                branch=branchname,
                title=form.title.data,
                start_id=orig_commit,
                stop_id=repo_commit.oid.hex,
                user=flask.g.fas_user.username,
            )
            SESSION.commit()
            flask.flash(message)

            if not parent.is_fork:
                url = flask.url_for('request_pulls',
                                    username=None,
                                    repo=parent.name)
            else:
                url = flask.url_for('request_pulls',
                                    username=parent.user,
                                    repo=parent.name)

            return flask.redirect(url)
        except progit.exceptions.ProgitException, err:
            flask.flash(str(err), 'error')
        except SQLAlchemyError, err:  # pragma: no cover
            SESSION.rollback()
            flask.flash(str(err), 'error')