Commit 9ea883fb authored by Sean McGivern's avatar Sean McGivern

Merge branch 'dm-highlight-diff-file-blob' into 'master'

Use Diff::File blob methods from diff highlighter

See merge request !11978
parents b4972c4b e6e29f92
...@@ -93,9 +93,11 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -93,9 +93,11 @@ class Projects::BlobController < Projects::ApplicationController
def diff def diff
apply_diff_view_cookie! apply_diff_view_cookie!
@form = UnfoldForm.new(params) @blob.load_all_data!
@lines = Gitlab::Highlight.highlight_lines(repository, @ref, @path) @lines = Gitlab::Highlight.highlight(@blob.path, @blob.data, repository: @repository).lines
@lines = @lines[@form.since - 1..@form.to - 1]
@form = UnfoldForm.new(params)
@lines = @lines[@form.since - 1..@form.to - 1].map(&:html_safe)
if @form.bottom? if @form.bottom?
@match_line = '' @match_line = ''
......
...@@ -58,19 +58,19 @@ module Gitlab ...@@ -58,19 +58,19 @@ module Gitlab
diff_refs&.head_sha diff_refs&.head_sha
end end
def content_sha def new_content_sha
return old_content_sha if deleted_file? return if deleted_file?
return @content_sha if defined?(@content_sha) return @new_content_sha if defined?(@new_content_sha)
refs = diff_refs || fallback_diff_refs refs = diff_refs || fallback_diff_refs
@content_sha = refs&.head_sha @new_content_sha = refs&.head_sha
end end
def content_commit def new_content_commit
return @content_commit if defined?(@content_commit) return @new_content_commit if defined?(@new_content_commit)
sha = content_sha sha = new_content_commit
@content_commit = repository.commit(sha) if sha @new_content_commit = repository.commit(sha) if sha
end end
def old_content_sha def old_content_sha
...@@ -88,13 +88,13 @@ module Gitlab ...@@ -88,13 +88,13 @@ module Gitlab
@old_content_commit = repository.commit(sha) if sha @old_content_commit = repository.commit(sha) if sha
end end
def blob def new_blob
return @blob if defined?(@blob) return @new_blob if defined?(@new_blob)
sha = content_sha sha = new_content_sha
return @blob = nil unless sha return @new_blob = nil unless sha
repository.blob_at(sha, file_path) @new_blob = repository.blob_at(sha, file_path)
end end
def old_blob def old_blob
...@@ -106,6 +106,18 @@ module Gitlab ...@@ -106,6 +106,18 @@ module Gitlab
@old_blob = repository.blob_at(sha, old_path) @old_blob = repository.blob_at(sha, old_path)
end end
def content_sha
new_content_sha || old_content_sha
end
def content_commit
new_content_commit || old_content_commit
end
def blob
new_blob || old_blob
end
attr_writer :highlighted_diff_lines attr_writer :highlighted_diff_lines
# Array of Gitlab::Diff::Line objects # Array of Gitlab::Diff::Line objects
......
...@@ -42,9 +42,9 @@ module Gitlab ...@@ -42,9 +42,9 @@ module Gitlab
rich_line = rich_line =
if diff_line.unchanged? || diff_line.added? if diff_line.unchanged? || diff_line.added?
new_lines[diff_line.new_pos - 1] new_lines[diff_line.new_pos - 1]&.html_safe
elsif diff_line.removed? elsif diff_line.removed?
old_lines[diff_line.old_pos - 1] old_lines[diff_line.old_pos - 1]&.html_safe
end end
# Only update text if line is found. This will prevent # Only update text if line is found. This will prevent
...@@ -60,13 +60,18 @@ module Gitlab ...@@ -60,13 +60,18 @@ module Gitlab
end end
def old_lines def old_lines
return unless diff_file @old_lines ||= highlighted_blob_lines(diff_file.old_blob)
@old_lines ||= Gitlab::Highlight.highlight_lines(self.repository, diff_old_sha, diff_old_path)
end end
def new_lines def new_lines
return unless diff_file @new_lines ||= highlighted_blob_lines(diff_file.new_blob)
@new_lines ||= Gitlab::Highlight.highlight_lines(self.repository, diff_new_sha, diff_new_path) end
def highlighted_blob_lines(blob)
return [] unless blob
blob.load_all_data!
Gitlab::Highlight.highlight(blob.path, blob.data, repository: repository).lines
end end
end end
end end
......
...@@ -5,14 +5,6 @@ module Gitlab ...@@ -5,14 +5,6 @@ module Gitlab
highlight(blob_content, continue: false, plain: plain) highlight(blob_content, continue: false, plain: plain)
end end
def self.highlight_lines(repository, ref, file_name)
blob = repository.blob_at(ref, file_name)
return [] unless blob
blob.load_all_data!
highlight(file_name, blob.data, repository: repository).lines.map!(&:html_safe)
end
attr_reader :blob_name attr_reader :blob_name
def initialize(blob_name, blob_content, repository: nil) def initialize(blob_name, blob_content, repository: nil)
......
...@@ -7,30 +7,6 @@ describe Gitlab::Highlight, lib: true do ...@@ -7,30 +7,6 @@ describe Gitlab::Highlight, lib: true do
let(:repository) { project.repository } let(:repository) { project.repository }
let(:commit) { project.commit(sample_commit.id) } let(:commit) { project.commit(sample_commit.id) }
describe '.highlight_lines' do
let(:lines) do
Gitlab::Highlight.highlight_lines(project.repository, commit.id, 'files/ruby/popen.rb')
end
it 'highlights all the lines properly' do
expect(lines[4]).to eq(%Q{<span id="LC5" class="line" lang="ruby"> <span class="kp">extend</span> <span class="nb">self</span></span>\n})
expect(lines[21]).to eq(%Q{<span id="LC22" class="line" lang="ruby"> <span class="k">unless</span> <span class="no">File</span><span class="p">.</span><span class="nf">directory?</span><span class="p">(</span><span class="n">path</span><span class="p">)</span></span>\n})
expect(lines[26]).to eq(%Q{<span id="LC27" class="line" lang="ruby"> <span class="vi">@cmd_status</span> <span class="o">=</span> <span class="mi">0</span></span>\n})
end
describe 'with CRLF' do
let(:branch) { 'crlf-diff' }
let(:blob) { repository.blob_at_branch(branch, path) }
let(:lines) do
Gitlab::Highlight.highlight_lines(project.repository, 'crlf-diff', 'files/whitespace')
end
it 'strips extra LFs' do
expect(lines[0]).to eq("<span id=\"LC1\" class=\"line\" lang=\"plaintext\">test </span>")
end
end
end
describe 'custom highlighting from .gitattributes' do describe 'custom highlighting from .gitattributes' do
let(:branch) { 'gitattributes' } let(:branch) { 'gitattributes' }
let(:blob) { repository.blob_at_branch(branch, path) } let(:blob) { repository.blob_at_branch(branch, path) }
...@@ -59,6 +35,19 @@ describe Gitlab::Highlight, lib: true do ...@@ -59,6 +35,19 @@ describe Gitlab::Highlight, lib: true do
end end
describe '#highlight' do describe '#highlight' do
describe 'with CRLF' do
let(:branch) { 'crlf-diff' }
let(:path) { 'files/whitespace' }
let(:blob) { repository.blob_at_branch(branch, path) }
let(:lines) do
Gitlab::Highlight.highlight(blob.path, blob.data, repository: repository).lines
end
it 'strips extra LFs' do
expect(lines[0]).to eq("<span id=\"LC1\" class=\"line\" lang=\"plaintext\">test </span>")
end
end
it 'links dependencies via DependencyLinker' do it 'links dependencies via DependencyLinker' do
expect(Gitlab::DependencyLinker).to receive(:link). expect(Gitlab::DependencyLinker).to receive(:link).
with('file.name', 'Contents', anything).and_call_original with('file.name', 'Contents', anything).and_call_original
......
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