Commit f727d518 authored by Matthias Käppler's avatar Matthias Käppler Committed by Bob Van Landuyt

Improve diff_files endpoint performance

parent e70d6aa7
...@@ -33,7 +33,7 @@ if (filesContainer.length) { ...@@ -33,7 +33,7 @@ if (filesContainer.length) {
axios axios
.get(batchPath) .get(batchPath)
.then(({ data }) => { .then(({ data }) => {
filesContainer.html($(data.html)); filesContainer.html($(data));
syntaxHighlight(filesContainer); syntaxHighlight(filesContainer);
handleLocationHash(); handleLocationHash();
new Diff(); new Diff();
......
...@@ -49,7 +49,7 @@ class Projects::CommitController < Projects::ApplicationController ...@@ -49,7 +49,7 @@ class Projects::CommitController < Projects::ApplicationController
end end
def diff_files def diff_files
render json: { html: view_to_html_string('projects/commit/diff_files', diffs: @diffs, environment: @environment) } render template: 'projects/commit/diff_files', layout: false, locals: { diffs: @diffs, environment: @environment }
end end
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
......
---
title: Improve diff_files endpoint performance
merge_request: 59489
author:
type: performance
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
module Gitlab module Gitlab
module Diff module Diff
class Highlight class Highlight
PREFIX_REGEXP = /\A(.)/.freeze
attr_reader :diff_file, :diff_lines, :repository, :project attr_reader :diff_file, :diff_lines, :repository, :project
delegate :old_path, :new_path, :old_sha, :new_sha, to: :diff_file, prefix: :diff delegate :old_path, :new_path, :old_sha, :new_sha, to: :diff_file, prefix: :diff
...@@ -97,12 +99,12 @@ module Gitlab ...@@ -97,12 +99,12 @@ module Gitlab
rich_line = syntax_highlighter(diff_line).highlight( rich_line = syntax_highlighter(diff_line).highlight(
diff_line.text(prefix: false), diff_line.text(prefix: false),
context: { line_number: diff_line.line } context: { line_number: diff_line.line }
)&.html_safe )
# Only update text if line is found. This will prevent # Only update text if line is found. This will prevent
# issues with submodules given the line only exists in diff content. # issues with submodules given the line only exists in diff content.
if rich_line if rich_line
line_prefix = diff_line.text =~ /\A(.)/ ? Regexp.last_match(1) : ' ' line_prefix = diff_line.text =~ PREFIX_REGEXP ? Regexp.last_match(1) : ' '
rich_line.prepend(line_prefix).concat("\n") rich_line.prepend(line_prefix).concat("\n")
end end
end end
...@@ -131,7 +133,7 @@ module Gitlab ...@@ -131,7 +133,7 @@ module Gitlab
# Only update text if line is found. This will prevent # Only update text if line is found. This will prevent
# issues with submodules given the line only exists in diff content. # issues with submodules given the line only exists in diff content.
if rich_line if rich_line
line_prefix = diff_line.text =~ /\A(.)/ ? Regexp.last_match(1) : ' ' line_prefix = diff_line.text =~ PREFIX_REGEXP ? Regexp.last_match(1) : ' '
"#{line_prefix}#{rich_line}".html_safe "#{line_prefix}#{rich_line}".html_safe
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment