Commit 6b9c730e authored by Rubén Dávila's avatar Rubén Dávila

More refactoring from last code review. #3945

* Use commit objects instead of IDs when generating diffs
* Use proper references when generating MR's source and target
* Update broken specs
parent 70bc3224
...@@ -72,7 +72,7 @@ class Projects::CommitController < Projects::ApplicationController ...@@ -72,7 +72,7 @@ class Projects::CommitController < Projects::ApplicationController
@diffs = commit.diffs @diffs = commit.diffs
end end
@diff_refs = [commit.parent_id || commit.id, commit.id] @diff_refs = [commit.parent || commit, commit]
@notes_count = commit.notes.count @notes_count = commit.notes.count
@statuses = ci_commit.statuses if ci_commit @statuses = ci_commit.statuses if ci_commit
......
...@@ -20,9 +20,9 @@ class Projects::CompareController < Projects::ApplicationController ...@@ -20,9 +20,9 @@ class Projects::CompareController < Projects::ApplicationController
if compare_result if compare_result
@commits = Commit.decorate(compare_result.commits, @project) @commits = Commit.decorate(compare_result.commits, @project)
@diffs = compare_result.diffs @diffs = compare_result.diffs
@diff_refs = [base_ref, head_ref]
@commit = @project.commit(head_ref) @commit = @project.commit(head_ref)
@first_commit = @project.commit(base_ref) @first_commit = @project.commit(base_ref)
@diff_refs = [@first_commit, @commit]
@line_notes = [] @line_notes = []
end end
end end
......
...@@ -59,7 +59,6 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -59,7 +59,6 @@ class Projects::MergeRequestsController < Projects::ApplicationController
def diffs def diffs
@commit = @merge_request.last_commit @commit = @merge_request.last_commit
@first_commit = @merge_request.first_commit @first_commit = @merge_request.first_commit
@diff_refs = [@merge_request.last_commit.parent_id, @merge_request.source_sha]
@comments_allowed = @reply_allowed = true @comments_allowed = @reply_allowed = true
@comments_target = { @comments_target = {
...@@ -104,8 +103,6 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -104,8 +103,6 @@ class Projects::MergeRequestsController < Projects::ApplicationController
@commit = @merge_request.last_commit @commit = @merge_request.last_commit
@first_commit = @merge_request.first_commit @first_commit = @merge_request.first_commit
@diffs = @merge_request.compare_diffs @diffs = @merge_request.compare_diffs
# We need to use #source_branch because #source_sha requires an existent MergeRequestDiff object.
@diff_refs = [@merge_request.target_sha, @merge_request.source_branch]
@ci_commit = @merge_request.ci_commit @ci_commit = @merge_request.ci_commit
@statuses = @ci_commit.statuses if @ci_commit @statuses = @ci_commit.statuses if @ci_commit
......
...@@ -19,13 +19,13 @@ module DiffHelper ...@@ -19,13 +19,13 @@ module DiffHelper
end end
end end
def safe_diff_files(diffs, diff_refs, repository) def safe_diff_files(diffs, diff_refs)
lines = 0 lines = 0
safe_files = [] safe_files = []
diffs.first(allowed_diff_size).each do |diff| diffs.first(allowed_diff_size).each do |diff|
lines += diff.diff.lines.count lines += diff.diff.lines.count
break if lines > allowed_diff_lines break if lines > allowed_diff_lines
safe_files << Gitlab::Diff::File.new(diff, diff_refs, repository) safe_files << Gitlab::Diff::File.new(diff, diff_refs)
end end
safe_files safe_files
end end
......
...@@ -511,4 +511,8 @@ class MergeRequest < ActiveRecord::Base ...@@ -511,4 +511,8 @@ class MergeRequest < ActiveRecord::Base
def broken? def broken?
self.commits.blank? || branch_missing? || cannot_be_merged? self.commits.blank? || branch_missing? || cannot_be_merged?
end end
def diff_range
[last_commit.parent, first_commit]
end
end end
- if diff_view == 'parallel' - if diff_view == 'parallel'
- fluid_layout true - fluid_layout true
- diff_files = safe_diff_files(diffs, diff_refs, repository) - diff_files = safe_diff_files(diffs, diff_refs)
.gray-content-block.middle-block.oneline-block .gray-content-block.middle-block.oneline-block
.inline-parallel-buttons .inline-parallel-buttons
......
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
= render "projects/merge_requests/show/commits" = render "projects/merge_requests/show/commits"
#diffs.diffs.tab-pane.active #diffs.diffs.tab-pane.active
- if @diffs.present? - if @diffs.present?
= render "projects/diffs/diffs", diffs: @diffs, project: @project, diff_refs: @diff_refs = render "projects/diffs/diffs", diffs: @diffs, project: @project, diff_refs: @merge_request.diff_range
- elsif @commits.size > MergeRequestDiff::COMMITS_SAFE_SIZE - elsif @commits.size > MergeRequestDiff::COMMITS_SAFE_SIZE
.alert.alert-danger .alert.alert-danger
%h4 This comparison includes more than #{MergeRequestDiff::COMMITS_SAFE_SIZE} commits. %h4 This comparison includes more than #{MergeRequestDiff::COMMITS_SAFE_SIZE} commits.
......
- if @merge_request_diff.collected? - if @merge_request_diff.collected?
= render "projects/diffs/diffs", diffs: params[:w] == '1' ? @merge_request.diffs_no_whitespace : @merge_request.diffs, project: @merge_request.project, diff_refs: @diff_refs = render "projects/diffs/diffs", diffs: params[:w] == '1' ? @merge_request.diffs_no_whitespace : @merge_request.diffs,
project: @merge_request.project, diff_refs: @merge_request.diff_range
- elsif @merge_request_diff.empty? - elsif @merge_request_diff.empty?
.nothing-here-block Nothing to merge from #{@merge_request.source_branch} into #{@merge_request.target_branch} .nothing-here-block Nothing to merge from #{@merge_request.source_branch} into #{@merge_request.target_branch}
- else - else
......
module Gitlab module Gitlab
module Diff module Diff
class File class File
attr_reader :diff, :repository, :new_ref, :old_ref attr_reader :diff, :new_ref, :old_ref
delegate :new_file, :deleted_file, :renamed_file, delegate :new_file, :deleted_file, :renamed_file,
:old_path, :new_path, to: :diff, prefix: false :old_path, :new_path, to: :diff, prefix: false
def initialize(diff, diff_refs, repository) def initialize(diff, diff_refs)
@diff = diff @diff = diff
@repository = repository
@old_ref, @new_ref = diff_refs @old_ref, @new_ref = diff_refs
end end
......
...@@ -3,8 +3,7 @@ module Gitlab ...@@ -3,8 +3,7 @@ module Gitlab
class Highlight class Highlight
attr_reader :diff_file attr_reader :diff_file
delegate :repository, :old_path, :new_path, :old_ref, :new_ref, delegate :old_path, :new_path, :old_ref, :new_ref, to: :diff_file, prefix: :diff
to: :diff_file, prefix: :diff
def initialize(diff_file) def initialize(diff_file)
@diff_file = diff_file @diff_file = diff_file
...@@ -141,11 +140,11 @@ module Gitlab ...@@ -141,11 +140,11 @@ module Gitlab
end end
def old_lines def old_lines
@old_lines ||= Gitlab::Highlight.highlight_lines(diff_repository, diff_old_ref, diff_old_path) @old_lines ||= self.class.process_file(*processing_args(:old))
end end
def new_lines def new_lines
@new_lines ||= Gitlab::Highlight.highlight_lines(diff_repository, diff_new_ref, diff_new_path) @new_lines ||= self.class.process_file(*processing_args(:new))
end end
def longest_common_suffix(a, b) def longest_common_suffix(a, b)
...@@ -200,6 +199,16 @@ module Gitlab ...@@ -200,6 +199,16 @@ module Gitlab
offset offset
end end
private
def processing_args(version)
ref = send("diff_#{version}_ref")
path = send("diff_#{version}_path")
[ref.project.repository, ref.id, path]
end
end end
end end
end end
This diff is collapsed.
...@@ -8,8 +8,8 @@ describe DiffHelper do ...@@ -8,8 +8,8 @@ describe DiffHelper do
let(:commit) { project.commit(sample_commit.id) } let(:commit) { project.commit(sample_commit.id) }
let(:diffs) { commit.diffs } let(:diffs) { commit.diffs }
let(:diff) { diffs.first } let(:diff) { diffs.first }
let(:diff_refs) { [commit.parent.id, commit.id] } let(:diff_refs) { [commit.parent, commit] }
let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs, repository) } let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs) }
describe 'diff_hard_limit_enabled?' do describe 'diff_hard_limit_enabled?' do
it 'should return true if param is provided' do it 'should return true if param is provided' do
...@@ -46,41 +46,41 @@ describe DiffHelper do ...@@ -46,41 +46,41 @@ describe DiffHelper do
describe 'safe_diff_files' do describe 'safe_diff_files' do
it 'should return all files from a commit that is smaller than safe limits' do it 'should return all files from a commit that is smaller than safe limits' do
expect(safe_diff_files(diffs, diff_refs, repository).length).to eq(2) expect(safe_diff_files(diffs, diff_refs).length).to eq(2)
end end
it 'should return only the first file if the diff line count in the 2nd file takes the total beyond safe limits' do it 'should return only the first file if the diff line count in the 2nd file takes the total beyond safe limits' do
allow(diffs[1].diff).to receive(:lines).and_return([""] * 4999) #simulate 4999 lines allow(diffs[1].diff).to receive(:lines).and_return([""] * 4999) #simulate 4999 lines
expect(safe_diff_files(diffs, diff_refs, repository).length).to eq(1) expect(safe_diff_files(diffs, diff_refs).length).to eq(1)
end end
it 'should return all files from a commit that is beyond safe limit for numbers of lines if force diff is true' do it 'should return all files from a commit that is beyond safe limit for numbers of lines if force diff is true' do
allow(controller).to receive(:params) { { force_show_diff: true } } allow(controller).to receive(:params) { { force_show_diff: true } }
allow(diffs[1].diff).to receive(:lines).and_return([""] * 4999) #simulate 4999 lines allow(diffs[1].diff).to receive(:lines).and_return([""] * 4999) #simulate 4999 lines
expect(safe_diff_files(diffs, diff_refs, repository).length).to eq(2) expect(safe_diff_files(diffs, diff_refs).length).to eq(2)
end end
it 'should return only the first file if the diff line count in the 2nd file takes the total beyond hard limits' do it 'should return only the first file if the diff line count in the 2nd file takes the total beyond hard limits' do
allow(controller).to receive(:params) { { force_show_diff: true } } allow(controller).to receive(:params) { { force_show_diff: true } }
allow(diffs[1].diff).to receive(:lines).and_return([""] * 49999) #simulate 49999 lines allow(diffs[1].diff).to receive(:lines).and_return([""] * 49999) #simulate 49999 lines
expect(safe_diff_files(diffs, diff_refs, repository).length).to eq(1) expect(safe_diff_files(diffs, diff_refs).length).to eq(1)
end end
it 'should return only a safe number of file diffs if a commit touches more files than the safe limits' do it 'should return only a safe number of file diffs if a commit touches more files than the safe limits' do
large_diffs = diffs * 100 #simulate 200 diffs large_diffs = diffs * 100 #simulate 200 diffs
expect(safe_diff_files(large_diffs, diff_refs, repository).length).to eq(100) expect(safe_diff_files(large_diffs, diff_refs).length).to eq(100)
end end
it 'should return all file diffs if a commit touches more files than the safe limits but force diff is true' do it 'should return all file diffs if a commit touches more files than the safe limits but force diff is true' do
allow(controller).to receive(:params) { { force_show_diff: true } } allow(controller).to receive(:params) { { force_show_diff: true } }
large_diffs = diffs * 100 #simulate 200 diffs large_diffs = diffs * 100 #simulate 200 diffs
expect(safe_diff_files(large_diffs, diff_refs, repository).length).to eq(200) expect(safe_diff_files(large_diffs, diff_refs).length).to eq(200)
end end
it 'should return a limited file diffs if a commit touches more files than the hard limits and force diff is true' do it 'should return a limited file diffs if a commit touches more files than the hard limits and force diff is true' do
allow(controller).to receive(:params) { { force_show_diff: true } } allow(controller).to receive(:params) { { force_show_diff: true } }
very_large_diffs = diffs * 1000 #simulate 2000 diffs very_large_diffs = diffs * 1000 #simulate 2000 diffs
expect(safe_diff_files(very_large_diffs, diff_refs, repository).length).to eq(1000) expect(safe_diff_files(very_large_diffs, diff_refs).length).to eq(1000)
end end
end end
...@@ -128,7 +128,11 @@ describe DiffHelper do ...@@ -128,7 +128,11 @@ describe DiffHelper do
expect(diff_line_content(diff_file.diff_lines.first.text)). expect(diff_line_content(diff_file.diff_lines.first.text)).
to eq('@@ -6,12 +6,18 @@ module Popen') to eq('@@ -6,12 +6,18 @@ module Popen')
expect(diff_line_content(diff_file.diff_lines.first.type)).to eq('match') expect(diff_line_content(diff_file.diff_lines.first.type)).to eq('match')
expect(diff_line_content(diff_file.diff_lines.first.new_pos)).to eq(6) expect(diff_file.diff_lines.first.new_pos).to eq(6)
end
it 'should return safe HTML' do
expect(diff_line_content(diff_file.diff_lines.first.text)).to be_html_safe
end end
end end
......
...@@ -6,7 +6,7 @@ describe Gitlab::Diff::File, lib: true do ...@@ -6,7 +6,7 @@ describe Gitlab::Diff::File, lib: true do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:commit) { project.commit(sample_commit.id) } let(:commit) { project.commit(sample_commit.id) }
let(:diff) { commit.diffs.first } let(:diff) { commit.diffs.first }
let(:diff_file) { Gitlab::Diff::File.new(diff, [commit.parent.id, commit.id], project.repository) } let(:diff_file) { Gitlab::Diff::File.new(diff, [commit.parent, commit]) }
describe :diff_lines do describe :diff_lines do
let(:diff_lines) { diff_file.diff_lines } let(:diff_lines) { diff_file.diff_lines }
......
...@@ -6,7 +6,7 @@ describe Gitlab::Diff::Highlight, lib: true do ...@@ -6,7 +6,7 @@ describe Gitlab::Diff::Highlight, lib: true do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:commit) { project.commit(sample_commit.id) } let(:commit) { project.commit(sample_commit.id) }
let(:diff) { commit.diffs.first } let(:diff) { commit.diffs.first }
let(:diff_file) { Gitlab::Diff::File.new(diff, [commit.parent.id, commit.id], project.repository) } let(:diff_file) { Gitlab::Diff::File.new(diff, [commit.parent, commit]) }
describe '.process_diff_lines' do describe '.process_diff_lines' do
context 'when processing Gitlab::Diff::Line objects' do context 'when processing Gitlab::Diff::Line objects' 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