Commit e8525e10 authored by Sean McGivern's avatar Sean McGivern

Show un-highlighted diffs when blobs are the same

For some old merge requests, we don't have enough information to figure out the
old blob and the new blob for the file. This means that we can't highlight the
diff correctly, but we can still display it without highlighting.
parent b0f09406
---
title: Show un-highlighted text diffs when we do not have references to the correct
blobs
merge_request:
author:
type: fixed
...@@ -186,7 +186,10 @@ module Gitlab ...@@ -186,7 +186,10 @@ module Gitlab
end end
def content_changed? def content_changed?
old_blob && new_blob && old_blob.id != new_blob.id return blobs_changed? if diff_refs
return false if new_file? || deleted_file? || renamed_file?
text? && diff_lines.any?
end end
def different_type? def different_type?
...@@ -225,6 +228,10 @@ module Gitlab ...@@ -225,6 +228,10 @@ module Gitlab
private private
def blobs_changed?
old_blob && new_blob && old_blob.id != new_blob.id
end
def simple_viewer_class def simple_viewer_class
return DiffViewer::NotDiffable unless diffable? return DiffViewer::NotDiffable unless diffable?
......
...@@ -15,6 +15,17 @@ describe Gitlab::Diff::File do ...@@ -15,6 +15,17 @@ describe Gitlab::Diff::File do
it { expect(diff_lines.first).to be_kind_of(Gitlab::Diff::Line) } it { expect(diff_lines.first).to be_kind_of(Gitlab::Diff::Line) }
end end
describe '#highlighted_diff_lines' do
it 'highlights the diff and memoises the result' do
expect(Gitlab::Diff::Highlight).to receive(:new)
.with(diff_file, repository: project.repository)
.once
.and_call_original
diff_file.highlighted_diff_lines
end
end
describe '#mode_changed?' do describe '#mode_changed?' do
it { expect(diff_file.mode_changed?).to be_falsey } it { expect(diff_file.mode_changed?).to be_falsey }
end end
...@@ -122,19 +133,43 @@ describe Gitlab::Diff::File do ...@@ -122,19 +133,43 @@ describe Gitlab::Diff::File do
let(:commit) { project.commit('2f63565e7aac07bcdadb654e253078b727143ec4') } let(:commit) { project.commit('2f63565e7aac07bcdadb654e253078b727143ec4') }
let(:diff_file) { commit.diffs.diff_file_with_new_path('files/images/6049019_460s.jpg') } let(:diff_file) { commit.diffs.diff_file_with_new_path('files/images/6049019_460s.jpg') }
context 'when the blobs are different' do
it 'returns true' do it 'returns true' do
expect(diff_file.content_changed?).to be_truthy expect(diff_file.content_changed?).to be_truthy
end end
end end
context 'when there are no diff refs' do
before do
allow(diff_file).to receive(:diff_refs).and_return(nil)
end
it 'returns false' do
expect(diff_file.content_changed?).to be_falsey
end
end
end
context 'when not binary' do context 'when not binary' do
let(:commit) { project.commit('570e7b2abdd848b95f2f578043fc23bd6f6fd24d') } let(:commit) { project.commit('570e7b2abdd848b95f2f578043fc23bd6f6fd24d') }
let(:diff_file) { commit.diffs.diff_file_with_new_path('files/ruby/popen.rb') } let(:diff_file) { commit.diffs.diff_file_with_new_path('files/ruby/popen.rb') }
context 'when the blobs are different' do
it 'returns true' do it 'returns true' do
expect(diff_file.content_changed?).to be_truthy expect(diff_file.content_changed?).to be_truthy
end end
end end
context 'when there are no diff refs' do
before do
allow(diff_file).to receive(:diff_refs).and_return(nil)
end
it 'returns true' do
expect(diff_file.content_changed?).to be_truthy
end
end
end
end end
end end
...@@ -278,6 +313,7 @@ describe Gitlab::Diff::File do ...@@ -278,6 +313,7 @@ describe Gitlab::Diff::File do
allow(diff_file).to receive(:deleted_file?).and_return(false) allow(diff_file).to receive(:deleted_file?).and_return(false)
allow(diff_file).to receive(:renamed_file?).and_return(false) allow(diff_file).to receive(:renamed_file?).and_return(false)
allow(diff_file).to receive(:mode_changed?).and_return(false) allow(diff_file).to receive(:mode_changed?).and_return(false)
allow(diff_file).to receive(:raw_text?).and_return(false)
end end
it 'returns a No Preview viewer' do it 'returns a No Preview viewer' do
......
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