Files @ 884d2c246570
Branch filter:

Location: kallithea/kallithea/controllers/compare.py - annotation

Søren Løvborg
cleanup: use list comprehensions

It's often the same number of lines, but avoids introducing a needless
"result" variable, and makes the item expression stand out more clearly.

It's also a tiny bit faster, but the readability gains is what matters.
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
5eec79420ce3
d1addaf7a91e
d1addaf7a91e
1948ede028ef
1948ede028ef
d1addaf7a91e
d1addaf7a91e
1948ede028ef
ad38f9f93b3b
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
af3539a458f6
d1addaf7a91e
84eb5b7b1bac
d1addaf7a91e
af3539a458f6
cf7d952c292f
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
a17c8e5f6712
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
7d0727d11104
67d5afe2fa1a
4034992774fa
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
e8565d50d064
e8565d50d064
e8565d50d064
e8565d50d064
e8565d50d064
e8565d50d064
e8565d50d064
e8565d50d064
e8565d50d064
e8565d50d064
e8565d50d064
e8565d50d064
e8565d50d064
e8565d50d064
e8565d50d064
e8565d50d064
e8565d50d064
e8565d50d064
e8565d50d064
e8565d50d064
7ad20e8f16bd
65a964fc9053
d1addaf7a91e
7a5977429125
7a5977429125
7a5977429125
84eb5b7b1bac
d1addaf7a91e
54df936a9bd3
d1addaf7a91e
54df936a9bd3
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
84eb5b7b1bac
f32c68450266
7a5977429125
7a5977429125
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
7a5977429125
7a5977429125
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
84eb5b7b1bac
84eb5b7b1bac
84eb5b7b1bac
84eb5b7b1bac
f32c68450266
84eb5b7b1bac
84eb5b7b1bac
84eb5b7b1bac
d1addaf7a91e
7a5977429125
7a5977429125
7a5977429125
7a5977429125
7a5977429125
7a5977429125
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
4b647864075e
d1addaf7a91e
d1addaf7a91e
4b647864075e
d1addaf7a91e
884d2c246570
884d2c246570
884d2c246570
884d2c246570
884d2c246570
7a5977429125
7a5977429125
84eb5b7b1bac
d1addaf7a91e
d1addaf7a91e
84eb5b7b1bac
078136fd83fb
c987aa2eb2a8
c987aa2eb2a8
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
0e2d450feb03
0e2d450feb03
d1addaf7a91e
7a5977429125
d1addaf7a91e
dc4a768927eb
0e2d450feb03
dc4a768927eb
84eb5b7b1bac
7a5977429125
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
84eb5b7b1bac
d1addaf7a91e
d1addaf7a91e
a17c8e5f6712
d1addaf7a91e
d1addaf7a91e
6cb077e99873
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
a17c8e5f6712
c2e3923eebe4
a6bc489cc536
a6bc489cc536
a6bc489cc536
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
e8565d50d064
c2e3923eebe4
e8565d50d064
c2e3923eebe4
d1addaf7a91e
d1addaf7a91e
60ae17de2a8d
60ae17de2a8d
60ae17de2a8d
60ae17de2a8d
cf7d952c292f
60ae17de2a8d
e8565d50d064
ac752047284f
e8565d50d064
d1addaf7a91e
d1addaf7a91e
d6e96730edfb
d6e96730edfb
6cb077e99873
6cb077e99873
d1addaf7a91e
84eb5b7b1bac
e8565d50d064
e8565d50d064
7879d8d88672
e8565d50d064
e8565d50d064
d1addaf7a91e
67d5afe2fa1a
6cb077e99873
67d5afe2fa1a
d1addaf7a91e
d1addaf7a91e
e8565d50d064
e8565d50d064
e8565d50d064
e8565d50d064
84eb5b7b1bac
84eb5b7b1bac
84eb5b7b1bac
84eb5b7b1bac
84eb5b7b1bac
84eb5b7b1bac
84eb5b7b1bac
84eb5b7b1bac
84eb5b7b1bac
84eb5b7b1bac
84eb5b7b1bac
84eb5b7b1bac
84eb5b7b1bac
84eb5b7b1bac
84eb5b7b1bac
d1addaf7a91e
d1addaf7a91e
c2e3923eebe4
0210d0b769d4
84eb5b7b1bac
d1addaf7a91e
c2e3923eebe4
e14bbd6caa34
3abfe76f1ac7
e14bbd6caa34
3abfe76f1ac7
3abfe76f1ac7
6cb077e99873
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
0210d0b769d4
0210d0b769d4
6cb077e99873
60ae17de2a8d
60ae17de2a8d
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
1cf51cd05e36
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
72acb38da217
72acb38da217
72acb38da217
72acb38da217
4034992774fa
4034992774fa
1cf51cd05e36
d1addaf7a91e
d1addaf7a91e
# -*- coding: utf-8 -*-
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
kallithea.controllers.compare
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

compare controller showing differences between two
repos, branches, bookmarks or tips

This file was forked by the Kallithea project in July 2014.
Original author and date, and relevant copyright and licensing information is below:
:created_on: May 6, 2012
:author: marcink
:copyright: (c) 2013 RhodeCode GmbH, and others.
:license: GPLv3, see LICENSE.md for more details.
"""


import logging
import re

from pylons import request, tmpl_context as c
from pylons.i18n.translation import _
from webob.exc import HTTPFound, HTTPBadRequest, HTTPNotFound

from kallithea.config.routing import url
from kallithea.lib.utils2 import safe_str, safe_int
from kallithea.lib.vcs.utils.hgcompat import unionrepo
from kallithea.lib import helpers as h
from kallithea.lib.base import BaseRepoController, render
from kallithea.lib.auth import LoginRequired, HasRepoPermissionLevelDecorator
from kallithea.lib import diffs
from kallithea.model.db import Repository
from kallithea.lib.diffs import LimitedDiffContainer
from kallithea.controllers.changeset import _ignorews_url, _context_url
from kallithea.lib.graphmod import graph_data
from kallithea.lib.compat import json, OrderedDict

log = logging.getLogger(__name__)


class CompareController(BaseRepoController):

    def __before__(self):
        super(CompareController, self).__before__()

        # The base repository has already been retrieved.
        c.a_repo = c.db_repo

        # Retrieve the "changeset" repository (default: same as base).
        other_repo = request.GET.get('other_repo', None)
        if other_repo is None:
            c.cs_repo = c.a_repo
        else:
            c.cs_repo = Repository.get_by_repo_name(other_repo)
            if c.cs_repo is None:
                msg = _('Could not find other repository %s') % other_repo
                h.flash(msg, category='error')
                raise HTTPFound(location=url('compare_home', repo_name=c.a_repo.repo_name))

        # Verify that it's even possible to compare these two repositories.
        if c.a_repo.scm_instance.alias != c.cs_repo.scm_instance.alias:
            msg = _('Cannot compare repositories of different types')
            h.flash(msg, category='error')
            raise HTTPFound(location=url('compare_home', repo_name=c.a_repo.repo_name))

    @staticmethod
    def _get_changesets(alias, org_repo, org_rev, other_repo, other_rev):
        """
        Returns lists of changesets that can be merged from org_repo@org_rev
        to other_repo@other_rev
        ... and the other way
        ... and the ancestors that would be used for merge

        :param org_repo: repo object, that is most likely the original repo we forked from
        :param org_rev: the revision we want our compare to be made
        :param other_repo: repo object, most likely the fork of org_repo. It has
            all changesets that we need to obtain
        :param other_rev: revision we want out compare to be made on other_repo
        """
        ancestors = None
        if org_rev == other_rev:
            org_changesets = []
            other_changesets = []

        elif alias == 'hg':
            #case two independent repos
            if org_repo != other_repo:
                hgrepo = unionrepo.unionrepository(other_repo.baseui,
                                                   other_repo.path,
                                                   org_repo.path)
                # all ancestors of other_rev will be in other_repo and
                # rev numbers from hgrepo can be used in other_repo - org_rev ancestors cannot

            #no remote compare do it on the same repository
            else:
                hgrepo = other_repo._repo

            ancestors = [hgrepo[ancestor].hex() for ancestor in
                         hgrepo.revs("id(%s) & ::id(%s)", other_rev, org_rev)]
            if ancestors:
                log.debug("shortcut found: %s is already an ancestor of %s", other_rev, org_rev)
            else:
                log.debug("no shortcut found: %s is not an ancestor of %s", other_rev, org_rev)
                ancestors = [hgrepo[ancestor].hex() for ancestor in
                             hgrepo.revs("heads(::id(%s) & ::id(%s))", org_rev, other_rev)] # FIXME: expensive!

            other_revs = hgrepo.revs("ancestors(id(%s)) and not ancestors(id(%s)) and not id(%s)",
                                     other_rev, org_rev, org_rev)
            other_changesets = [other_repo.get_changeset(rev) for rev in other_revs]
            org_revs = hgrepo.revs("ancestors(id(%s)) and not ancestors(id(%s)) and not id(%s)",
                                   org_rev, other_rev, other_rev)
            org_changesets = [org_repo.get_changeset(hgrepo[rev].hex()) for rev in org_revs]

        elif alias == 'git':
            if org_repo != other_repo:
                from dulwich.repo import Repo
                from dulwich.client import SubprocessGitClient

                gitrepo = Repo(org_repo.path)
                SubprocessGitClient(thin_packs=False).fetch(safe_str(other_repo.path), gitrepo)

                gitrepo_remote = Repo(other_repo.path)
                SubprocessGitClient(thin_packs=False).fetch(safe_str(org_repo.path), gitrepo_remote)

                revs = [
                    x.commit.id
                    for x in gitrepo_remote.get_walker(include=[other_rev],
                                                       exclude=[org_rev])
                ]
                other_changesets = [other_repo.get_changeset(rev) for rev in reversed(revs)]
                if other_changesets:
                    ancestors = [other_changesets[0].parents[0].raw_id]
                else:
                    # no changesets from other repo, ancestor is the other_rev
                    ancestors = [other_rev]

                gitrepo.close()
                gitrepo_remote.close()

            else:
                so, se = org_repo.run_git_command(
                    ['log', '--reverse', '--pretty=format:%H',
                     '-s', '%s..%s' % (org_rev, other_rev)]
                )
                other_changesets = [org_repo.get_changeset(cs)
                              for cs in re.findall(r'[0-9a-fA-F]{40}', so)]
                so, se = org_repo.run_git_command(
                    ['merge-base', org_rev, other_rev]
                )
                ancestors = [re.findall(r'[0-9a-fA-F]{40}', so)[0]]
            org_changesets = []

        else:
            raise Exception('Bad alias only git and hg is allowed')

        return other_changesets, org_changesets, ancestors

    @LoginRequired()
    @HasRepoPermissionLevelDecorator('read')
    def index(self, repo_name):
        c.compare_home = True
        c.a_ref_name = c.cs_ref_name = _('Select changeset')
        return render('compare/compare_diff.html')

    @LoginRequired()
    @HasRepoPermissionLevelDecorator('read')
    def compare(self, repo_name, org_ref_type, org_ref_name, other_ref_type, other_ref_name):
        org_ref_name = org_ref_name.strip()
        other_ref_name = other_ref_name.strip()

        # If merge is True:
        #   Show what org would get if merged with other:
        #   List changesets that are ancestors of other but not of org.
        #   New changesets in org is thus ignored.
        #   Diff will be from common ancestor, and merges of org to other will thus be ignored.
        # If merge is False:
        #   Make a raw diff from org to other, no matter if related or not.
        #   Changesets in one and not in the other will be ignored
        merge = bool(request.GET.get('merge'))
        # fulldiff disables cut_off_limit
        c.fulldiff = request.GET.get('fulldiff')
        # partial uses compare_cs.html template directly
        partial = request.environ.get('HTTP_X_PARTIAL_XHR')
        # as_form puts hidden input field with changeset revisions
        c.as_form = partial and request.GET.get('as_form')
        # swap url for compare_diff page - never partial and never as_form
        c.swap_url = h.url('compare_url',
            repo_name=c.cs_repo.repo_name,
            org_ref_type=other_ref_type, org_ref_name=other_ref_name,
            other_repo=c.a_repo.repo_name,
            other_ref_type=org_ref_type, other_ref_name=org_ref_name,
            merge=merge or '')

        # set callbacks for generating markup for icons
        c.ignorews_url = _ignorews_url
        c.context_url = _context_url
        ignore_whitespace = request.GET.get('ignorews') == '1'
        line_context = safe_int(request.GET.get('context'), 3)

        c.a_rev = self._get_ref_rev(c.a_repo, org_ref_type, org_ref_name,
            returnempty=True)
        c.cs_rev = self._get_ref_rev(c.cs_repo, other_ref_type, other_ref_name)

        c.compare_home = False
        c.a_ref_name = org_ref_name
        c.a_ref_type = org_ref_type
        c.cs_ref_name = other_ref_name
        c.cs_ref_type = other_ref_type

        c.cs_ranges, c.cs_ranges_org, c.ancestors = self._get_changesets(
            c.a_repo.scm_instance.alias, c.a_repo.scm_instance, c.a_rev,
            c.cs_repo.scm_instance, c.cs_rev)
        raw_ids = [x.raw_id for x in c.cs_ranges]
        c.cs_comments = c.cs_repo.get_comments(raw_ids)
        c.statuses = c.cs_repo.statuses(raw_ids)

        revs = [ctx.revision for ctx in reversed(c.cs_ranges)]
        c.jsdata = json.dumps(graph_data(c.cs_repo.scm_instance, revs))

        if partial:
            return render('compare/compare_cs.html')

        org_repo = c.a_repo
        other_repo = c.cs_repo

        if merge:
            rev1 = msg = None
            if not c.cs_ranges:
                msg = _('Cannot show empty diff')
            elif not c.ancestors:
                msg = _('No ancestor found for merge diff')
            elif len(c.ancestors) == 1:
                rev1 = c.ancestors[0]
            else:
                msg = _('Multiple merge ancestors found for merge compare')
            if rev1 is None:
                h.flash(msg, category='error')
                log.error(msg)
                raise HTTPNotFound

            # case we want a simple diff without incoming changesets,
            # previewing what will be merged.
            # Make the diff on the other repo (which is known to have other_rev)
            log.debug('Using ancestor %s as rev1 instead of %s',
                      rev1, c.a_rev)
            org_repo = other_repo
        else: # comparing tips, not necessarily linearly related
            if org_repo != other_repo:
                # TODO: we could do this by using hg unionrepo
                log.error('cannot compare across repos %s and %s', org_repo, other_repo)
                h.flash(_('Cannot compare repositories without using common ancestor'), category='error')
                raise HTTPBadRequest
            rev1 = c.a_rev

        diff_limit = self.cut_off_limit if not c.fulldiff else None

        log.debug('running diff between %s and %s in %s',
                  rev1, c.cs_rev, org_repo.scm_instance.path)
        txtdiff = org_repo.scm_instance.get_diff(rev1=rev1, rev2=c.cs_rev,
                                      ignore_whitespace=ignore_whitespace,
                                      context=line_context)

        diff_processor = diffs.DiffProcessor(txtdiff or '', format='gitdiff',
                                             diff_limit=diff_limit)
        _parsed = diff_processor.prepare()

        c.limited_diff = False
        if isinstance(_parsed, LimitedDiffContainer):
            c.limited_diff = True

        c.file_diff_data = []
        c.lines_added = 0
        c.lines_deleted = 0
        for f in _parsed:
            st = f['stats']
            c.lines_added += st['added']
            c.lines_deleted += st['deleted']
            filename = f['filename']
            fid = h.FID('', filename)
            diff = diff_processor.as_html(enable_comments=False,
                                          parsed_lines=[f])
            c.file_diff_data.append((fid, None, f['operation'], f['old_filename'], filename, diff, st))

        return render('compare/compare_diff.html')