diff --git a/rhodecode/apps/repository/views/repo_commits.py b/rhodecode/apps/repository/views/repo_commits.py --- a/rhodecode/apps/repository/views/repo_commits.py +++ b/rhodecode/apps/repository/views/repo_commits.py @@ -34,7 +34,9 @@ from rhodecode.lib.auth import ( LoginRequired, HasRepoPermissionAnyDecorator, NotAnonymous, CSRFRequired) from rhodecode.lib.compat import OrderedDict -from rhodecode.lib.diffs import cache_diff, load_cached_diff, diff_cache_exist +from rhodecode.lib.diffs import ( + cache_diff, load_cached_diff, diff_cache_exist, get_diff_context, + get_diff_whitespace_flag) from rhodecode.lib.exceptions import StatusChangeOnClosedPullRequestError import rhodecode.lib.helpers as h from rhodecode.lib.utils2 import safe_unicode, str2bool @@ -55,96 +57,8 @@ def _update_with_GET(params, request): params[k] += request.GET.getall(k) -def get_ignore_ws(fid, request): - ig_ws_global = request.GET.get('ignorews') - ig_ws = filter(lambda k: k.startswith('WS'), request.GET.getall(fid)) - if ig_ws: - try: - return int(ig_ws[0].split(':')[-1]) - except Exception: - pass - return ig_ws_global -def _ignorews_url(request, fileid=None): - _ = request.translate - fileid = str(fileid) if fileid else None - params = collections.defaultdict(list) - _update_with_GET(params, request) - label = _('Show whitespace') - tooltiplbl = _('Show whitespace for all diffs') - ig_ws = get_ignore_ws(fileid, request) - ln_ctx = get_line_ctx(fileid, request) - - if ig_ws is None: - params['ignorews'] += [1] - label = _('Ignore whitespace') - tooltiplbl = _('Ignore whitespace for all diffs') - ctx_key = 'context' - ctx_val = ln_ctx - - # if we have passed in ln_ctx pass it along to our params - if ln_ctx: - params[ctx_key] += [ctx_val] - - if fileid: - params['anchor'] = 'a_' + fileid - return h.link_to(label, request.current_route_path(_query=params), - title=tooltiplbl, class_='tooltip') - - -def get_line_ctx(fid, request): - ln_ctx_global = request.GET.get('context') - if fid: - ln_ctx = filter(lambda k: k.startswith('C'), request.GET.getall(fid)) - else: - _ln_ctx = filter(lambda k: k.startswith('C'), request.GET) - ln_ctx = request.GET.get(_ln_ctx[0]) if _ln_ctx else ln_ctx_global - if ln_ctx: - ln_ctx = [ln_ctx] - - if ln_ctx: - retval = ln_ctx[0].split(':')[-1] - else: - retval = ln_ctx_global - - try: - return min(diffs.MAX_CONTEXT, int(retval)) - except Exception: - return 3 - - -def _context_url(request, fileid=None): - """ - Generates a url for context lines. - - :param fileid: - """ - - _ = request.translate - fileid = str(fileid) if fileid else None - ig_ws = get_ignore_ws(fileid, request) - ln_ctx = (get_line_ctx(fileid, request) or 3) * 2 - - params = collections.defaultdict(list) - _update_with_GET(params, request) - - if ln_ctx > 0: - params['context'] += [ln_ctx] - - if ig_ws: - ig_ws_key = 'ignorews' - ig_ws_val = 1 - params[ig_ws_key] += [ig_ws_val] - - lbl = _('Increase context') - tooltiplbl = _('Increase context for all diffs') - - if fileid: - params['anchor'] = 'a_' + fileid - return h.link_to(lbl, request.current_route_path(_query=params), - title=tooltiplbl, class_='tooltip') - class RepoCommitsView(RepoAppView): def load_default_context(self): @@ -162,13 +76,11 @@ class RepoCommitsView(RepoAppView): def _commit(self, commit_id_range, method): _ = self.request.translate c = self.load_default_context() - c.ignorews_url = _ignorews_url - c.context_url = _context_url c.fulldiff = self.request.GET.get('fulldiff') # fetch global flags of ignore ws or context lines - context_lcl = get_line_ctx('', self.request) - ign_whitespace_lcl = get_ignore_ws('', self.request) + diff_context = get_diff_context(self.request) + hide_whitespace_changes = get_diff_whitespace_flag(self.request) # diff_limit will cut off the whole diff if the limit is applied # otherwise it will just hide the big files from the front-end @@ -258,7 +170,7 @@ class RepoCommitsView(RepoAppView): self.db_repo) cache_file_path = diff_cache_exist( cache_path, 'diff', commit.raw_id, - ign_whitespace_lcl, context_lcl, c.fulldiff) + hide_whitespace_changes, diff_context, c.fulldiff) caching_enabled = self._is_diff_cache_enabled(self.db_repo) force_recache = str2bool(self.request.GET.get('force_recache')) @@ -273,8 +185,8 @@ class RepoCommitsView(RepoAppView): else: vcs_diff = self.rhodecode_vcs_repo.get_diff( commit1, commit2, - ignore_whitespace=ign_whitespace_lcl, - context=context_lcl) + ignore_whitespace=hide_whitespace_changes, + context=diff_context) diff_processor = diffs.DiffProcessor( vcs_diff, format='newdiff', diff_limit=diff_limit, @@ -300,7 +212,7 @@ class RepoCommitsView(RepoAppView): # TODO(marcink): no cache usage here... _diff = self.rhodecode_vcs_repo.get_diff( commit1, commit2, - ignore_whitespace=ign_whitespace_lcl, context=context_lcl) + ignore_whitespace=hide_whitespace_changes, context=diff_context) diff_processor = diffs.DiffProcessor( _diff, format='newdiff', diff_limit=diff_limit, file_limit=file_limit, show_full_diff=c.fulldiff) diff --git a/rhodecode/apps/repository/views/repo_compare.py b/rhodecode/apps/repository/views/repo_compare.py --- a/rhodecode/apps/repository/views/repo_compare.py +++ b/rhodecode/apps/repository/views/repo_compare.py @@ -44,10 +44,7 @@ log = logging.getLogger(__name__) class RepoCompareView(RepoAppView): def load_default_context(self): c = self._get_local_tmpl_context(include_app_defaults=True) - c.rhodecode_repo = self.rhodecode_vcs_repo - - return c def _get_commit_or_redirect( @@ -145,6 +142,10 @@ class RepoCompareView(RepoAppView): # c.fulldiff disables cut_off_limit c.fulldiff = str2bool(self.request.GET.get('fulldiff')) + # fetch global flags of ignore ws or context lines + diff_context = diffs.get_diff_context(self.request) + hide_whitespace_changes = diffs.get_diff_whitespace_flag(self.request) + c.file_path = target_path c.commit_statuses = ChangesetStatus.STATUSES @@ -288,7 +289,8 @@ class RepoCompareView(RepoAppView): txt_diff = source_repo.scm_instance().get_diff( commit1=source_commit, commit2=target_commit, - path=target_path, path1=source_path) + path=target_path, path1=source_path, + ignore_whitespace=hide_whitespace_changes, context=diff_context) diff_processor = diffs.DiffProcessor( txt_diff, format='newdiff', diff_limit=diff_limit, diff --git a/rhodecode/apps/repository/views/repo_pull_requests.py b/rhodecode/apps/repository/views/repo_pull_requests.py --- a/rhodecode/apps/repository/views/repo_pull_requests.py +++ b/rhodecode/apps/repository/views/repo_pull_requests.py @@ -219,10 +219,11 @@ class RepoPullRequestsView(RepoAppView, def _get_diffset(self, source_repo_name, source_repo, source_ref_id, target_ref_id, target_commit, source_commit, diff_limit, file_limit, - fulldiff): + fulldiff, hide_whitespace_changes, diff_context): vcs_diff = PullRequestModel().get_diff( - source_repo, source_ref_id, target_ref_id) + source_repo, source_ref_id, target_ref_id, + hide_whitespace_changes, diff_context) diff_processor = diffs.DiffProcessor( vcs_diff, format='newdiff', diff_limit=diff_limit, @@ -243,11 +244,11 @@ class RepoPullRequestsView(RepoAppView, def _get_range_diffset(self, source_scm, source_repo, commit1, commit2, diff_limit, file_limit, - fulldiff, ign_whitespace_lcl, context_lcl): + fulldiff, hide_whitespace_changes, diff_context): vcs_diff = source_scm.get_diff( commit1, commit2, - ignore_whitespace=ign_whitespace_lcl, - context=context_lcl) + ignore_whitespace=hide_whitespace_changes, + context=diff_context) diff_processor = diffs.DiffProcessor( vcs_diff, format='newdiff', diff_limit=diff_limit, @@ -280,6 +281,11 @@ class RepoPullRequestsView(RepoAppView, from_version = self.request.GET.get('from_version') or version merge_checks = self.request.GET.get('merge_checks') c.fulldiff = str2bool(self.request.GET.get('fulldiff')) + + # fetch global flags of ignore ws or context lines + diff_context = diffs.get_diff_context(self.request) + hide_whitespace_changes = diffs.get_diff_whitespace_flag(self.request) + force_refresh = str2bool(self.request.GET.get('force_refresh')) (pull_request_latest, @@ -490,7 +496,8 @@ class RepoPullRequestsView(RepoAppView, cache_path = self.rhodecode_vcs_repo.get_create_shadow_cache_pr_path(target_repo) cache_file_path = diff_cache_exist( cache_path, 'pull_request', pull_request_id, version_normalized, - from_version_normalized, source_ref_id, target_ref_id, c.fulldiff) + from_version_normalized, source_ref_id, target_ref_id, + hide_whitespace_changes, diff_context, c.fulldiff) caching_enabled = self._is_diff_cache_enabled(c.target_repo) force_recache = self.get_recache_flag() @@ -561,7 +568,8 @@ class RepoPullRequestsView(RepoAppView, c.source_repo.repo_name, commits_source_repo, source_ref_id, target_ref_id, target_commit, source_commit, - diff_limit, file_limit, c.fulldiff) + diff_limit, file_limit, c.fulldiff, + hide_whitespace_changes, diff_context) # save cached diff if caching_enabled: diff --git a/rhodecode/lib/diffs.py b/rhodecode/lib/diffs.py --- a/rhodecode/lib/diffs.py +++ b/rhodecode/lib/diffs.py @@ -41,7 +41,16 @@ log = logging.getLogger(__name__) # define max context, a file with more than this numbers of lines is unusable # in browser anyway -MAX_CONTEXT = 1024 * 1014 +MAX_CONTEXT = 20 * 1024 +DEFAULT_CONTEXT = 3 + + +def get_diff_context(request): + return MAX_CONTEXT if request.GET.get('fullcontext', '') == '1' else DEFAULT_CONTEXT + + +def get_diff_whitespace_flag(request): + return request.GET.get('ignorews', '') == '1' class OPS(object): diff --git a/rhodecode/model/pull_request.py b/rhodecode/model/pull_request.py --- a/rhodecode/model/pull_request.py +++ b/rhodecode/model/pull_request.py @@ -73,7 +73,7 @@ class PullRequestModel(BaseModel): cls = PullRequest - DIFF_CONTEXT = 3 + DIFF_CONTEXT = diffs.DEFAULT_CONTEXT MERGE_STATUS_MESSAGES = { MergeFailureReason.NONE: lazy_ugettext( @@ -877,19 +877,21 @@ class PullRequestModel(BaseModel): diff_context = ( self.DIFF_CONTEXT + CommentsModel.needed_extra_diff_context()) - + hide_whitespace_changes = False source_repo = pull_request_version.source_repo source_ref_id = pull_request_version.source_ref_parts.commit_id target_ref_id = pull_request_version.target_ref_parts.commit_id old_diff = self._get_diff_from_pr_or_version( - source_repo, source_ref_id, target_ref_id, context=diff_context) + source_repo, source_ref_id, target_ref_id, + hide_whitespace_changes=hide_whitespace_changes, diff_context=diff_context) source_repo = pull_request.source_repo source_ref_id = pull_request.source_ref_parts.commit_id target_ref_id = pull_request.target_ref_parts.commit_id new_diff = self._get_diff_from_pr_or_version( - source_repo, source_ref_id, target_ref_id, context=diff_context) + source_repo, source_ref_id, target_ref_id, + hide_whitespace_changes=hide_whitespace_changes, diff_context=diff_context) old_diff_data = diffs.DiffProcessor(old_diff) old_diff_data.prepare() @@ -1488,12 +1490,17 @@ class PullRequestModel(BaseModel): raise EmptyRepositoryError() return groups, selected - def get_diff(self, source_repo, source_ref_id, target_ref_id, context=DIFF_CONTEXT): + def get_diff(self, source_repo, source_ref_id, target_ref_id, + hide_whitespace_changes, diff_context): + return self._get_diff_from_pr_or_version( - source_repo, source_ref_id, target_ref_id, context=context) + source_repo, source_ref_id, target_ref_id, + hide_whitespace_changes=hide_whitespace_changes, diff_context=diff_context) def _get_diff_from_pr_or_version( - self, source_repo, source_ref_id, target_ref_id, context): + self, source_repo, source_ref_id, target_ref_id, + hide_whitespace_changes, diff_context): + target_commit = source_repo.get_commit( commit_id=safe_str(target_ref_id)) source_commit = source_repo.get_commit( @@ -1517,7 +1524,8 @@ class PullRequestModel(BaseModel): safe_unicode(vcs_repo.path)) vcs_diff = vcs_repo.get_diff( - commit1=target_commit, commit2=source_commit, context=context) + commit1=target_commit, commit2=source_commit, + ignore_whitespace=hide_whitespace_changes, context=diff_context) return vcs_diff def _is_merge_enabled(self, pull_request): diff --git a/rhodecode/templates/changeset/changeset.mako b/rhodecode/templates/changeset/changeset.mako --- a/rhodecode/templates/changeset/changeset.mako +++ b/rhodecode/templates/changeset/changeset.mako @@ -150,10 +150,6 @@ ${_('Download Diff')} - | - ${c.ignorews_url(request)} - | - ${c.context_url(request)} diff --git a/rhodecode/templates/changeset/changeset_range.mako b/rhodecode/templates/changeset/changeset_range.mako --- a/rhodecode/templates/changeset/changeset_range.mako +++ b/rhodecode/templates/changeset/changeset_range.mako @@ -68,29 +68,6 @@ - <%doc> - ##TODO(marcink): implement this and diff menus -
-
- ${_('Diff options')}: -
-
- -
-
- diff --git a/rhodecode/templates/codeblocks/diffs.mako b/rhodecode/templates/codeblocks/diffs.mako --- a/rhodecode/templates/codeblocks/diffs.mako +++ b/rhodecode/templates/codeblocks/diffs.mako @@ -467,46 +467,23 @@ from rhodecode.lib.diffs import NEW_FILE title="${h.tooltip(_('Show file at commit: %(commit_id)s') % {'commit_id': filediff.diffset.target_ref[:12]})}" > ${_('Show file after')} - | + %else: - ${_('Show file after')} - | + ${_('Show file after')} + %endif - - ${_('Raw diff')} - | - - ${_('Download diff')} - + % if use_comments: - | - % endif - - ## TODO: dan: refactor ignorews_url and context_url into the diff renderer same as diffmode=unified/sideside. Also use ajax to load more context (by clicking hunks) - %if hasattr(c, 'ignorews_url'): - ${c.ignorews_url(request, h.FID(filediff.raw_id, filediff.patch['filename']))} - %endif - %if hasattr(c, 'context_url'): - ${c.context_url(request, h.FID(filediff.raw_id, filediff.patch['filename']))} - %endif - - %if use_comments: + | ${_('Show comments')}${_('Hide comments')} - %endif + % endif + %endif @@ -743,17 +720,24 @@ def get_comments_for(diff_type, comments
+ ## DIFF OPTIONS via Select2 +
+ ${h.hidden('diff_menu')} +
+ ${_('Side by Side')} + ${_('Unified')} + % if range_diff_on is True: ${_('Collapse All Files')} - ${_('Wide Mode Diff')} - -
+
@@ -963,6 +942,72 @@ def get_comments_for(diff_type, comments } ); + var preloadData = { + results: [ + ## Wide diff mode + { + id: 1, + text: _gettext('Toggle Wide Mode Diff'), + action: function () { + updateSticky(); + Rhodecode.comments.toggleWideMode(this); + return null; + }, + url: null, + }, + + ## Whitespace change + % if request.GET.get('ignorews', '') == '1': + { + id: 2, + text: _gettext('Show whitespace changes'), + action: function () {}, + url: "${h.current_route_path(request, ignorews=0)|n}" + }, + % else: + { + id: 2, + text: _gettext('Hide whitespace changes'), + action: function () {}, + url: "${h.current_route_path(request, ignorews=1)|n}" + }, + % endif + + ## FULL CONTEXT + % if request.GET.get('fullcontext', '') == '1': + { + id: 3, + text: _gettext('Hide full context diff'), + action: function () {}, + url: "${h.current_route_path(request, fullcontext=0)|n}" + }, + % else: + { + id: 3, + text: _gettext('Show full context diff'), + action: function () {}, + url: "${h.current_route_path(request, fullcontext=1)|n}" + }, + % endif + + ] + }; + + $("#diff_menu").select2({ + minimumResultsForSearch: -1, + containerCssClass: "drop-menu", + dropdownCssClass: "drop-menu-dropdown", + dropdownAutoWidth: true, + data: preloadData, + placeholder: "${_('Diff Options')}", + }); + $("#diff_menu").on('select2-selecting', function (e) { + e.choice.action(); + if (e.choice.url !== null) { + window.location = e.choice.url + } + }); + }); diff --git a/rhodecode/templates/compare/compare_diff.mako b/rhodecode/templates/compare/compare_diff.mako --- a/rhodecode/templates/compare/compare_diff.mako +++ b/rhodecode/templates/compare/compare_diff.mako @@ -129,30 +129,6 @@
- <%doc> - ##TODO(marcink): implement this and diff menus -
-
- ${_('Diff options')}: -
-
- -
-
- - ## commit status form