Commit c86c1905 authored by Paco Guzman's avatar Paco Guzman

switch from diff_file_collection to diffs

So we have raw_diffs too
parent 1d0c7b74
...@@ -28,7 +28,7 @@ class Projects::CommitController < Projects::ApplicationController ...@@ -28,7 +28,7 @@ class Projects::CommitController < Projects::ApplicationController
end end
def diff_for_path def diff_for_path
render_diff_for_path(@commit.diff_file_collection(diff_options)) render_diff_for_path(@commit.diffs(diff_options))
end end
def builds def builds
...@@ -110,7 +110,7 @@ class Projects::CommitController < Projects::ApplicationController ...@@ -110,7 +110,7 @@ class Projects::CommitController < Projects::ApplicationController
opts = diff_options opts = diff_options
opts[:ignore_whitespace_change] = true if params[:format] == 'diff' opts[:ignore_whitespace_change] = true if params[:format] == 'diff'
@diffs = commit.diff_file_collection(opts) @diffs = commit.diffs(opts)
@notes_count = commit.notes.count @notes_count = commit.notes.count
end end
......
...@@ -21,7 +21,7 @@ class Projects::CompareController < Projects::ApplicationController ...@@ -21,7 +21,7 @@ class Projects::CompareController < Projects::ApplicationController
def diff_for_path def diff_for_path
return render_404 unless @compare return render_404 unless @compare
render_diff_for_path(@compare.diff_file_collection(diff_options: diff_options)) render_diff_for_path(@compare.diffs(diff_options: diff_options))
end end
def create def create
...@@ -45,7 +45,7 @@ class Projects::CompareController < Projects::ApplicationController ...@@ -45,7 +45,7 @@ class Projects::CompareController < Projects::ApplicationController
@commit = @compare.commit @commit = @compare.commit
@base_commit = @compare.base_commit @base_commit = @compare.base_commit
@diffs = @compare.diff_file_collection(diff_options: diff_options) @diffs = @compare.diffs(diff_options: diff_options)
@diff_notes_disabled = true @diff_notes_disabled = true
@grouped_diff_discussions = {} @grouped_diff_discussions = {}
......
...@@ -86,7 +86,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -86,7 +86,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
respond_to do |format| respond_to do |format|
format.html { define_discussion_vars } format.html { define_discussion_vars }
format.json do format.json do
@diffs = @merge_request.diff_file_collection(diff_options) @diffs = @merge_request.diffs(diff_options)
render json: { html: view_to_html_string("projects/merge_requests/show/_diffs") } render json: { html: view_to_html_string("projects/merge_requests/show/_diffs") }
end end
...@@ -108,7 +108,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -108,7 +108,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
define_commit_vars define_commit_vars
render_diff_for_path(@merge_request.diff_file_collection(diff_options)) render_diff_for_path(@merge_request.diffs(diff_options))
end end
def commits def commits
...@@ -156,9 +156,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -156,9 +156,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
@commits = @merge_request.compare_commits.reverse @commits = @merge_request.compare_commits.reverse
@commit = @merge_request.diff_head_commit @commit = @merge_request.diff_head_commit
@base_commit = @merge_request.diff_base_commit @base_commit = @merge_request.diff_base_commit
if @merge_request.compare @diffs = @merge_request.diffs(diff_options) if @merge_request.compare
@diffs = @merge_request.diff_file_collection(diff_options)
end
@diff_notes_disabled = true @diff_notes_disabled = true
@pipeline = @merge_request.pipeline @pipeline = @merge_request.pipeline
......
...@@ -23,10 +23,7 @@ module DiffHelper ...@@ -23,10 +23,7 @@ module DiffHelper
end end
def diff_options def diff_options
options = Gitlab::Diff::FileCollection.default_options.merge( options = { ignore_whitespace_change: hide_whitespace?, no_collapse: expand_all_diffs? }
ignore_whitespace_change: hide_whitespace?,
no_collapse: expand_all_diffs?
)
if action_name == 'diff_for_path' if action_name == 'diff_for_path'
options[:no_collapse] = true options[:no_collapse] = true
......
...@@ -104,7 +104,7 @@ class Commit ...@@ -104,7 +104,7 @@ class Commit
end end
def diff_line_count def diff_line_count
@diff_line_count ||= Commit::diff_line_count(self.diffs) @diff_line_count ||= Commit::diff_line_count(raw_diffs)
@diff_line_count @diff_line_count
end end
...@@ -317,7 +317,11 @@ class Commit ...@@ -317,7 +317,11 @@ class Commit
nil nil
end end
def diff_file_collection(diff_options = nil) def raw_diffs(*args)
raw.diffs(*args)
end
def diffs(diff_options = nil)
Gitlab::Diff::FileCollection::Commit.new(self, diff_options: diff_options) Gitlab::Diff::FileCollection::Commit.new(self, diff_options: diff_options)
end end
...@@ -330,7 +334,7 @@ class Commit ...@@ -330,7 +334,7 @@ class Commit
def repo_changes def repo_changes
changes = { added: [], modified: [], removed: [] } changes = { added: [], modified: [], removed: [] }
diffs.each do |diff| raw_diffs.each do |diff|
if diff.deleted_file if diff.deleted_file
changes[:removed] << diff.old_path changes[:removed] << diff.old_path
elsif diff.renamed_file || diff.new_file elsif diff.renamed_file || diff.new_file
......
class Compare class Compare
delegate :same, :head, :base, to: :@compare delegate :same, :head, :base, to: :@compare
attr_reader :project
def self.decorate(compare, project) def self.decorate(compare, project)
if compare.is_a?(Compare) if compare.is_a?(Compare)
compare compare
...@@ -15,49 +17,47 @@ class Compare ...@@ -15,49 +17,47 @@ class Compare
end end
def commits def commits
@commits ||= Commit.decorate(@compare.commits, @project) @commits ||= Commit.decorate(@compare.commits, project)
end end
def start_commit def start_commit
return @start_commit if defined?(@start_commit) return @start_commit if defined?(@start_commit)
commit = @compare.base commit = @compare.base
@start_commit = commit ? ::Commit.new(commit, @project) : nil @start_commit = commit ? ::Commit.new(commit, project) : nil
end end
def commit def head_commit
return @commit if defined?(@commit) return @head_commit if defined?(@head_commit)
commit = @compare.head commit = @compare.head
@commit = commit ? ::Commit.new(commit, @project) : nil @head_commit = commit ? ::Commit.new(commit, project) : nil
end
alias_method :head_commit, :commit
# Used only on emails_on_push_worker.rb
def base_commit=(commit)
@base_commit = commit
end end
alias_method :commit, :head_commit
def base_commit def base_commit
return @base_commit if defined?(@base_commit) return @base_commit if defined?(@base_commit)
@base_commit = if start_commit && commit @base_commit = if start_commit && head_commit
@project.merge_base_commit(start_commit.id, commit.id) project.merge_base_commit(start_commit.id, head_commit.id)
else else
nil nil
end end
end end
# keyword args until we get ride of diff_refs as argument def raw_diffs(*args)
def diff_file_collection(diff_options:, diff_refs: self.diff_refs) @compare.diffs(*args)
Gitlab::Diff::FileCollection::Compare.new(@compare, end
project: @project,
def diffs(diff_options:)
Gitlab::Diff::FileCollection::Compare.new(self,
project: project,
diff_options: diff_options, diff_options: diff_options,
diff_refs: diff_refs) diff_refs: diff_refs)
end end
def diff_refs def diff_refs
@diff_refs ||= Gitlab::Diff::DiffRefs.new( Gitlab::Diff::DiffRefs.new(
base_sha: base_commit.try(:sha), base_sha: base_commit.try(:sha),
start_sha: start_commit.try(:sha), start_sha: start_commit.try(:sha),
head_sha: commit.try(:sha) head_sha: commit.try(:sha)
......
...@@ -85,7 +85,7 @@ class LegacyDiffNote < Note ...@@ -85,7 +85,7 @@ class LegacyDiffNote < Note
return nil unless noteable return nil unless noteable
return @diff if defined?(@diff) return @diff if defined?(@diff)
@diff = noteable.diffs(Commit.max_diff_options).find do |d| @diff = noteable.raw_diffs(Commit.max_diff_options).find do |d|
d.new_path && Digest::SHA1.hexdigest(d.new_path) == diff_file_hash d.new_path && Digest::SHA1.hexdigest(d.new_path) == diff_file_hash
end end
end end
...@@ -116,7 +116,7 @@ class LegacyDiffNote < Note ...@@ -116,7 +116,7 @@ class LegacyDiffNote < Note
# Find the diff on noteable that matches our own # Find the diff on noteable that matches our own
def find_noteable_diff def find_noteable_diff
diffs = noteable.diffs(Commit.max_diff_options) diffs = noteable.raw_diffs(Commit.max_diff_options)
diffs.find { |d| d.new_path == self.diff.new_path } diffs.find { |d| d.new_path == self.diff.new_path }
end end
end end
...@@ -164,13 +164,13 @@ class MergeRequest < ActiveRecord::Base ...@@ -164,13 +164,13 @@ class MergeRequest < ActiveRecord::Base
merge_request_diff ? merge_request_diff.first_commit : compare_commits.first merge_request_diff ? merge_request_diff.first_commit : compare_commits.first
end end
def diffs(*args) def raw_diffs(*args)
merge_request_diff ? merge_request_diff.diffs(*args) : compare.diffs(*args) merge_request_diff ? merge_request_diff.diffs(*args) : compare.raw_diffs(*args)
end end
def diff_file_collection(diff_options = nil) def diffs(diff_options = nil)
if self.compare if self.compare
self.compare.diff_file_collection(diff_options: diff_options, diff_refs: diff_refs) self.compare.diffs(diff_options: diff_options)
else else
Gitlab::Diff::FileCollection::MergeRequest.new(self, diff_options: diff_options) Gitlab::Diff::FileCollection::MergeRequest.new(self, diff_options: diff_options)
end end
......
...@@ -372,7 +372,7 @@ class Repository ...@@ -372,7 +372,7 @@ class Repository
# We don't want to flush the cache if the commit didn't actually make any # We don't want to flush the cache if the commit didn't actually make any
# changes to any of the possible avatar files. # changes to any of the possible avatar files.
if revision && commit = self.commit(revision) if revision && commit = self.commit(revision)
return unless commit.diffs. return unless commit.raw_diffs.
any? { |diff| AVATAR_FILES.include?(diff.new_path) } any? { |diff| AVATAR_FILES.include?(diff.new_path) }
end end
......
...@@ -26,7 +26,6 @@ class CompareService ...@@ -26,7 +26,6 @@ class CompareService
source_sha source_sha
) )
# REVIEW be sure if it's target_project or source_project
Compare.new(raw_compare, target_project) Compare.new(raw_compare, target_project)
end end
end end
...@@ -2,7 +2,7 @@ module MergeRequests ...@@ -2,7 +2,7 @@ module MergeRequests
class MergeRequestDiffCacheService class MergeRequestDiffCacheService
def execute(merge_request) def execute(merge_request)
# Executing the iteration we cache all the highlighted diff information # Executing the iteration we cache all the highlighted diff information
merge_request.diff_file_collection.diff_files.to_a merge_request.diffs.diff_files.to_a
end end
end end
end end
...@@ -9,11 +9,11 @@ ...@@ -9,11 +9,11 @@
= link_to 'Expand all', url_for(params.merge(expand_all_diffs: 1, format: nil)), class: 'btn btn-default' = link_to 'Expand all', url_for(params.merge(expand_all_diffs: 1, format: nil)), class: 'btn btn-default'
- if show_whitespace_toggle - if show_whitespace_toggle
- if current_controller?(:commit) - if current_controller?(:commit)
= commit_diff_whitespace_link(@project, @commit, class: 'hidden-xs') = commit_diff_whitespace_link(diffs.project, @commit, class: 'hidden-xs')
- elsif current_controller?(:merge_requests) - elsif current_controller?(:merge_requests)
= diff_merge_request_whitespace_link(@project, @merge_request, class: 'hidden-xs') = diff_merge_request_whitespace_link(diffs.project, @merge_request, class: 'hidden-xs')
- elsif current_controller?(:compare) - elsif current_controller?(:compare)
= diff_compare_whitespace_link(@project, params[:from], params[:to], class: 'hidden-xs') = diff_compare_whitespace_link(diffs.project, params[:from], params[:to], class: 'hidden-xs')
.btn-group .btn-group
= inline_diff_btn = inline_diff_btn
= parallel_diff_btn = parallel_diff_btn
...@@ -22,12 +22,12 @@ ...@@ -22,12 +22,12 @@
- if diff_files.overflow? - if diff_files.overflow?
= render 'projects/diffs/warning', diff_files: diff_files = render 'projects/diffs/warning', diff_files: diff_files
.files{data: {can_create_note: (!@diff_notes_disabled && can?(current_user, :create_note, @project))}} .files{data: {can_create_note: (!@diff_notes_disabled && can?(current_user, :create_note, diffs.project))}}
- diff_files.each_with_index do |diff_file, index| - diff_files.each_with_index do |diff_file, index|
- diff_commit = commit_for_diff(diff_file) - diff_commit = commit_for_diff(diff_file)
- blob = diff_file.blob(diff_commit) - blob = diff_file.blob(diff_commit)
- next unless blob - next unless blob
- blob.load_all_data!(@project.repository) unless blob.only_display_raw? - blob.load_all_data!(diffs.project.repository) unless blob.only_display_raw?
= render 'projects/diffs/file', i: index, project: @project, = render 'projects/diffs/file', i: index, project: diffs.project,
diff_file: diff_file, diff_commit: diff_commit, blob: blob, diff_refs: diffs.diff_refs diff_file: diff_file, diff_commit: diff_commit, blob: blob
- if @merge_request_diff.collected? - if @merge_request_diff.collected?
= render "projects/diffs/diffs", diffs: @diffs, = render "projects/diffs/diffs", diffs: @diffs
diff_refs: @diffs.diff_refs, project: @diffs.project
- 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
......
...@@ -33,18 +33,13 @@ class EmailsOnPushWorker ...@@ -33,18 +33,13 @@ class EmailsOnPushWorker
reverse_compare = false reverse_compare = false
if action == :push if action == :push
base_commit = project.merge_base_commit(before_sha, after_sha) compare = CompareService.new.execute(project, before_sha, project, after_sha)
compare = Gitlab::Git::Compare.new(project.repository.raw_repository, before_sha, after_sha)
compare = Compare.decorate(compare, project)
compare.base_commit = base_commit
diff_refs = compare.diff_refs diff_refs = compare.diff_refs
return false if compare.same return false if compare.same
if compare.commits.empty? if compare.commits.empty?
compare = Gitlab::Git::Compare.new(project.repository.raw_repository, after_sha, before_sha) compare = CompareService.new.execute(project, after_sha, project, before_sha)
compare = Compare.decorate(compare, project)
compare.base_commit = base_commit
diff_refs = compare.diff_refs diff_refs = compare.diff_refs
reverse_compare = true reverse_compare = true
......
...@@ -141,8 +141,10 @@ class IrkerWorker ...@@ -141,8 +141,10 @@ class IrkerWorker
end end
def files_count(commit) def files_count(commit)
files = "#{commit.diffs.real_size} file" diffs = commit.raw_diffs
files += 's' if commit.diffs.size > 1
files = "#{diffs.real_size} file"
files += 's' if diffs.size > 1
files files
end end
......
...@@ -54,7 +54,7 @@ module API ...@@ -54,7 +54,7 @@ module API
sha = params[:sha] sha = params[:sha]
commit = user_project.commit(sha) commit = user_project.commit(sha)
not_found! "Commit" unless commit not_found! "Commit" unless commit
commit.diffs.to_a commit.raw_diffs.to_a
end end
# Get a commit's comments # Get a commit's comments
...@@ -96,7 +96,7 @@ module API ...@@ -96,7 +96,7 @@ module API
} }
if params[:path] && params[:line] && params[:line_type] if params[:path] && params[:line] && params[:line_type]
commit.diffs(all_diffs: true).each do |diff| commit.raw_diffs(all_diffs: true).each do |diff|
next unless diff.new_path == params[:path] next unless diff.new_path == params[:path]
lines = Gitlab::Diff::Parser.new.parse(diff.diff.each_line) lines = Gitlab::Diff::Parser.new.parse(diff.diff.each_line)
......
...@@ -224,7 +224,7 @@ module API ...@@ -224,7 +224,7 @@ module API
class MergeRequestChanges < MergeRequest class MergeRequestChanges < MergeRequest
expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _| expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _|
compare.diffs(all_diffs: true).to_a compare.raw_diffs(all_diffs: true).to_a
end end
end end
......
module Gitlab
module Diff
module FileCollection
def self.default_options
::Commit.max_diff_options.merge(ignore_whitespace_change: false, no_collapse: false)
end
end
end
end
module Gitlab module Gitlab
module Diff module Diff
module FileCollection module FileCollection
class Base class Base
attr_reader :project, :diff_options, :diff_view, :diff_refs attr_reader :project, :diff_options, :diff_view, :diff_refs
delegate :count, :size, :real_size, to: :diff_files delegate :count, :size, :real_size, to: :diff_files
def initialize(diffs, project:, diff_options: nil, diff_refs: nil) def self.default_options
@diffs = diffs ::Commit.max_diff_options.merge(ignore_whitespace_change: false, no_collapse: false)
end
def initialize(diffable, project:, diff_options: nil, diff_refs: nil)
diff_options = self.class.default_options.merge(diff_options || {})
@diffable = diffable
@diffs = diffable.raw_diffs(diff_options)
@project = project @project = project
@diff_options = diff_options @diff_options = diff_options
@diff_refs = diff_refs @diff_refs = diff_refs
end end
def diff_files def diff_files
@diffs.decorate! { |diff| decorate_diff!(diff) } @diff_files ||= @diffs.decorate! { |diff| decorate_diff!(diff) }
end end
private private
def decorate_diff!(diff) def decorate_diff!(diff)
return diff if diff.is_a?(Gitlab::Diff::File) Gitlab::Diff::File.new(diff, repository: project.repository, diff_refs: diff_refs)
Gitlab::Diff::File.new(diff, diff_refs: @diff_refs, repository: @project.repository)
end end
end end
end end
......
...@@ -3,10 +3,7 @@ module Gitlab ...@@ -3,10 +3,7 @@ module Gitlab
module FileCollection module FileCollection
class Commit < Base class Commit < Base
def initialize(commit, diff_options:) def initialize(commit, diff_options:)
# Not merge just set defaults super(commit,
diff_options = diff_options || Gitlab::Diff::FileCollection.default_options
super(commit.diffs(diff_options),
project: commit.project, project: commit.project,
diff_options: diff_options, diff_options: diff_options,
diff_refs: commit.diff_refs) diff_refs: commit.diff_refs)
......
...@@ -3,10 +3,7 @@ module Gitlab ...@@ -3,10 +3,7 @@ module Gitlab
module FileCollection module FileCollection
class Compare < Base class Compare < Base
def initialize(compare, project:, diff_options:, diff_refs: nil) def initialize(compare, project:, diff_options:, diff_refs: nil)
# Not merge just set defaults super(compare,
diff_options = diff_options || Gitlab::Diff::FileCollection.default_options
super(compare.diffs(diff_options),
project: project, project: project,
diff_options: diff_options, diff_options: diff_options,
diff_refs: diff_refs) diff_refs: diff_refs)
......
...@@ -4,10 +4,8 @@ module Gitlab ...@@ -4,10 +4,8 @@ module Gitlab
class MergeRequest < Base class MergeRequest < Base
def initialize(merge_request, diff_options:) def initialize(merge_request, diff_options:)
@merge_request = merge_request @merge_request = merge_request
# Not merge just set defaults
diff_options = diff_options || Gitlab::Diff::FileCollection.default_options
super(merge_request.diffs(diff_options), super(merge_request,
project: merge_request.project, project: merge_request.project,
diff_options: diff_options, diff_options: diff_options,
diff_refs: merge_request.diff_refs) diff_refs: merge_request.diff_refs)
...@@ -19,18 +17,11 @@ module Gitlab ...@@ -19,18 +17,11 @@ module Gitlab
private private
# Extracted method to highlight in the same iteration to the diff_collection. Iteration in the DiffCollections # Extracted method to highlight in the same iteration to the diff_collection.
# seems particularly slow on big diffs (event when already populated).
def decorate_diff!(diff) def decorate_diff!(diff)
highlight! super diff_file = super
end cache_highlight!(diff_file) if cacheable?
diff_file
def highlight!(diff_file)
if cacheable?
cache_highlight!(diff_file)
else
diff_file # Don't need to eager load highlighted diff lines
end
end end
def highlight_diff_file_from_cache!(diff_file, cache_diff_lines) def highlight_diff_file_from_cache!(diff_file, cache_diff_lines)
...@@ -55,15 +46,13 @@ module Gitlab ...@@ -55,15 +46,13 @@ module Gitlab
else else
highlight_cache[file_path] = diff_file.highlighted_diff_lines.map(&:to_hash) highlight_cache[file_path] = diff_file.highlighted_diff_lines.map(&:to_hash)
end end
diff_file
end end
def highlight_cache def highlight_cache
return @highlight_cache if defined?(@highlight_cache) return @highlight_cache if defined?(@highlight_cache)
@highlight_cache = Rails.cache.read(cache_key) || {} @highlight_cache = Rails.cache.read(cache_key) || {}
@highlight_cache_was_empty = highlight_cache.empty? @highlight_cache_was_empty = @highlight_cache.empty?
@highlight_cache @highlight_cache
end end
......
...@@ -41,7 +41,8 @@ module Gitlab ...@@ -41,7 +41,8 @@ module Gitlab
def diffs def diffs
return unless compare return unless compare
@diffs ||= compare.diff_file_collection(diff_options: { max_files: 30 }).diff_files # This diff is more moderated in number of files and lines
@diffs ||= compare.diffs(diff_options: { max_files: 30, max_lines: 5000, no_collapse: true }).diff_files
end end
def diffs_count def diffs_count
......
...@@ -6,7 +6,7 @@ describe DiffHelper do ...@@ -6,7 +6,7 @@ describe DiffHelper do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:repository) { project.repository } let(:repository) { project.repository }
let(:commit) { project.commit(sample_commit.id) } let(:commit) { project.commit(sample_commit.id) }
let(:diffs) { commit.diffs } let(:diffs) { commit.raw_diffs }
let(:diff) { diffs.first } let(:diff) { diffs.first }
let(:diff_refs) { [commit.parent, commit] } let(:diff_refs) { [commit.parent, commit] }
let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: diff_refs, repository: repository) } let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: diff_refs, repository: repository) }
...@@ -32,16 +32,6 @@ describe DiffHelper do ...@@ -32,16 +32,6 @@ describe DiffHelper do
end end
describe 'diff_options' do describe 'diff_options' do
it 'should return hard limit for a diff if force diff is true' do
allow(controller).to receive(:params) { { force_show_diff: true } }
expect(diff_options).to include(Commit.max_diff_options)
end
it 'should return hard limit for a diff if expand_all_diffs is true' do
allow(controller).to receive(:params) { { expand_all_diffs: true } }
expect(diff_options).to include(Commit.max_diff_options)
end
it 'should return no collapse false' do it 'should return no collapse false' do
expect(diff_options).to include(no_collapse: false) expect(diff_options).to include(no_collapse: false)
end end
...@@ -55,6 +45,18 @@ describe DiffHelper do ...@@ -55,6 +45,18 @@ describe DiffHelper do
allow(controller).to receive(:action_name) { 'diff_for_path' } allow(controller).to receive(:action_name) { 'diff_for_path' }
expect(diff_options).to include(no_collapse: true) expect(diff_options).to include(no_collapse: true)
end end
it 'should return paths if action name diff_for_path and param old path' do
allow(controller).to receive(:params) { { old_path: 'lib/wadus.rb' } }
allow(controller).to receive(:action_name) { 'diff_for_path' }
expect(diff_options[:paths]).to include('lib/wadus.rb')
end
it 'should return paths if action name diff_for_path and param new path' do
allow(controller).to receive(:params) { { new_path: 'lib/wadus.rb' } }
allow(controller).to receive(:action_name) { 'diff_for_path' }
expect(diff_options[:paths]).to include('lib/wadus.rb')
end
end end
describe 'unfold_bottom_class' do describe 'unfold_bottom_class' do
......
...@@ -5,7 +5,7 @@ describe Gitlab::Diff::File, lib: true do ...@@ -5,7 +5,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.raw_diffs.first }
let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: commit.diff_refs, repository: project.repository) } let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: commit.diff_refs, repository: project.repository) }
describe '#diff_lines' do describe '#diff_lines' do
......
...@@ -5,7 +5,7 @@ describe Gitlab::Diff::Highlight, lib: true do ...@@ -5,7 +5,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.raw_diffs.first }
let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: commit.diff_refs, repository: project.repository) } let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: commit.diff_refs, repository: project.repository) }
describe '#highlight' do describe '#highlight' do
......
...@@ -6,7 +6,7 @@ describe Gitlab::Diff::LineMapper, lib: true do ...@@ -6,7 +6,7 @@ describe Gitlab::Diff::LineMapper, lib: true do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:repository) { project.repository } let(:repository) { project.repository }
let(:commit) { project.commit(sample_commit.id) } let(:commit) { project.commit(sample_commit.id) }
let(:diffs) { commit.diffs } let(:diffs) { commit.raw_diffs }
let(:diff) { diffs.first } let(:diff) { diffs.first }
let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: commit.diff_refs, repository: repository) } let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: commit.diff_refs, repository: repository) }
subject { described_class.new(diff_file) } subject { described_class.new(diff_file) }
......
...@@ -6,7 +6,7 @@ describe Gitlab::Diff::ParallelDiff, lib: true do ...@@ -6,7 +6,7 @@ describe Gitlab::Diff::ParallelDiff, lib: true do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:repository) { project.repository } let(:repository) { project.repository }
let(:commit) { project.commit(sample_commit.id) } let(:commit) { project.commit(sample_commit.id) }
let(:diffs) { commit.diffs } let(:diffs) { commit.raw_diffs }
let(:diff) { diffs.first } let(:diff) { diffs.first }
let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: commit.diff_refs, repository: repository) } let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: commit.diff_refs, repository: repository) }
subject { described_class.new(diff_file) } subject { described_class.new(diff_file) }
......
...@@ -5,7 +5,7 @@ describe Gitlab::Diff::Parser, lib: true do ...@@ -5,7 +5,7 @@ describe Gitlab::Diff::Parser, 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.raw_diffs.first }
let(:parser) { Gitlab::Diff::Parser.new } let(:parser) { Gitlab::Diff::Parser.new }
describe '#parse' do describe '#parse' do
......
require 'spec_helper'
describe Compare, models: true do
include RepoHelpers
let(:project) { create(:project, :public) }
let(:commit) { project.commit }
let(:start_commit) { sample_image_commit }
let(:head_commit) { sample_commit }
let(:raw_compare) { Gitlab::Git::Compare.new(project.repository.raw_repository, start_commit.id, head_commit.id) }
subject { described_class.new(raw_compare, project) }
describe '#start_commit' do
it 'returns raw compare base commit' do
expect(subject.start_commit.id).to eq(start_commit.id)
end
it 'returns nil if compare base commit is nil' do
expect(raw_compare).to receive(:base).and_return(nil)
expect(subject.start_commit).to eq(nil)
end
end
describe '#commit' do
it 'returns raw compare head commit' do
expect(subject.commit.id).to eq(head_commit.id)
end
it 'returns nil if compare head commit is nil' do
expect(raw_compare).to receive(:head).and_return(nil)
expect(subject.commit).to eq(nil)
end
end
describe '#base_commit' do
let(:base_commit) { Commit.new(another_sample_commit, project) }
it 'returns project merge base commit' do
expect(project).to receive(:merge_base_commit).with(start_commit.id, head_commit.id).and_return(base_commit)
expect(subject.base_commit).to eq(base_commit)
end
it 'returns nil if there is no start_commit' do
expect(subject).to receive(:start_commit).and_return(nil)
expect(subject.base_commit).to eq(nil)
end
it 'returns nil if there is no head commit' do
expect(subject).to receive(:head_commit).and_return(nil)
expect(subject.base_commit).to eq(nil)
end
end
describe '#diff_refs' do
it 'uses base_commit sha as base_sha' do
expect(subject).to receive(:base_commit).at_least(:once).and_call_original
expect(subject.diff_refs.base_sha).to eq(subject.base_commit.id)
end
it 'uses start_commit sha as start_sha' do
expect(subject.diff_refs.start_sha).to eq(start_commit.id)
end
it 'uses commit sha as head sha' do
expect(subject.diff_refs.head_sha).to eq(head_commit.id)
end
end
end
...@@ -58,7 +58,7 @@ describe LegacyDiffNote, models: true do ...@@ -58,7 +58,7 @@ describe LegacyDiffNote, models: true do
# Generate a real line_code value so we know it will match. We use a # Generate a real line_code value so we know it will match. We use a
# random line from a random diff just for funsies. # random line from a random diff just for funsies.
diff = merge.diffs.to_a.sample diff = merge.raw_diffs.to_a.sample
line = Gitlab::Diff::Parser.new.parse(diff.diff.each_line).to_a.sample line = Gitlab::Diff::Parser.new.parse(diff.diff.each_line).to_a.sample
code = Gitlab::Diff::LineCode.generate(diff.new_path, line.new_pos, line.old_pos) code = Gitlab::Diff::LineCode.generate(diff.new_path, line.new_pos, line.old_pos)
......
...@@ -128,7 +128,7 @@ describe MergeRequest, models: true do ...@@ -128,7 +128,7 @@ describe MergeRequest, models: true do
end end
end end
describe '#diffs' do describe '#raw_diffs' do
let(:merge_request) { build(:merge_request) } let(:merge_request) { build(:merge_request) }
let(:options) { { paths: ['a/b', 'b/a', 'c/*'] } } let(:options) { { paths: ['a/b', 'b/a', 'c/*'] } }
...@@ -138,6 +138,31 @@ describe MergeRequest, models: true do ...@@ -138,6 +138,31 @@ describe MergeRequest, models: true do
expect(merge_request.merge_request_diff).to receive(:diffs).with(options) expect(merge_request.merge_request_diff).to receive(:diffs).with(options)
merge_request.raw_diffs(options)
end
end
context 'when there are no MR diffs' do
it 'delegates to the compare object' do
merge_request.compare = double(:compare)
expect(merge_request.compare).to receive(:raw_diffs).with(options)
merge_request.raw_diffs(options)
end
end
end
describe '#diffs' do
let(:merge_request) { build(:merge_request) }
let(:options) { { paths: ['a/b', 'b/a', 'c/*'] } }
context 'when there are MR diffs' do
it 'delegates to the MR diffs' do
merge_request.merge_request_diff = MergeRequestDiff.new
expect(merge_request.merge_request_diff).to receive(:diffs).with(hash_including(options))
merge_request.diffs(options) merge_request.diffs(options)
end end
end end
...@@ -146,7 +171,7 @@ describe MergeRequest, models: true do ...@@ -146,7 +171,7 @@ describe MergeRequest, models: true do
it 'delegates to the compare object' do it 'delegates to the compare object' do
merge_request.compare = double(:compare) merge_request.compare = double(:compare)
expect(merge_request.compare).to receive(:diffs).with(options) expect(merge_request.compare).to receive(:diffs).with(diff_options: options)
merge_request.diffs(options) merge_request.diffs(options)
end end
......
...@@ -1154,7 +1154,7 @@ describe Repository, models: true do ...@@ -1154,7 +1154,7 @@ describe Repository, models: true do
it 'does not flush the cache if the commit does not change any logos' do it 'does not flush the cache if the commit does not change any logos' do
diff = double(:diff, new_path: 'test.txt') diff = double(:diff, new_path: 'test.txt')
expect(commit).to receive(:diffs).and_return([diff]) expect(commit).to receive(:raw_diffs).and_return([diff])
expect(cache).not_to receive(:expire) expect(cache).not_to receive(:expire)
repository.expire_avatar_cache(repository.root_ref, '123') repository.expire_avatar_cache(repository.root_ref, '123')
...@@ -1163,7 +1163,7 @@ describe Repository, models: true do ...@@ -1163,7 +1163,7 @@ describe Repository, models: true do
it 'flushes the cache if the commit changes any of the logos' do it 'flushes the cache if the commit changes any of the logos' do
diff = double(:diff, new_path: Repository::AVATAR_FILES[0]) diff = double(:diff, new_path: Repository::AVATAR_FILES[0])
expect(commit).to receive(:diffs).and_return([diff]) expect(commit).to receive(:raw_diffs).and_return([diff])
expect(cache).to receive(:expire).with(:avatar) expect(cache).to receive(:expire).with(:avatar)
repository.expire_avatar_cache(repository.root_ref, '123') repository.expire_avatar_cache(repository.root_ref, '123')
......
...@@ -173,10 +173,10 @@ describe API::API, api: true do ...@@ -173,10 +173,10 @@ describe API::API, api: true do
end end
it 'should return the inline comment' do it 'should return the inline comment' do
post api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/comments", user), note: 'My comment', path: project.repository.commit.diffs.first.new_path, line: 7, line_type: 'new' post api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/comments", user), note: 'My comment', path: project.repository.commit.raw_diffs.first.new_path, line: 7, line_type: 'new'
expect(response).to have_http_status(201) expect(response).to have_http_status(201)
expect(json_response['note']).to eq('My comment') expect(json_response['note']).to eq('My comment')
expect(json_response['path']).to eq(project.repository.commit.diffs.first.new_path) expect(json_response['path']).to eq(project.repository.commit.raw_diffs.first.new_path)
expect(json_response['line']).to eq(7) expect(json_response['line']).to eq(7)
expect(json_response['line_type']).to eq('new') expect(json_response['line_type']).to eq('new')
end end
......
...@@ -5,9 +5,9 @@ describe MergeRequests::MergeRequestDiffCacheService do ...@@ -5,9 +5,9 @@ describe MergeRequests::MergeRequestDiffCacheService do
let(:subject) { MergeRequests::MergeRequestDiffCacheService.new } let(:subject) { MergeRequests::MergeRequestDiffCacheService.new }
describe '#execute' do describe '#execute' do
it 'retrieve the diff files to cache the highlighted result' do it 'retrieves the diff files to cache the highlighted result' do
merge_request = create(:merge_request) merge_request = create(:merge_request)
cache_key = [merge_request.merge_request_diff, 'highlighted-diff-files', Gitlab::Diff::FileCollection.default_options] cache_key = [merge_request.merge_request_diff, 'highlighted-diff-files', Gitlab::Diff::FileCollection::MergeRequest.default_options]
expect(Rails.cache).to receive(:read).with(cache_key).and_return({}) expect(Rails.cache).to receive(:read).with(cache_key).and_return({})
expect(Rails.cache).to receive(:write).with(cache_key, anything) expect(Rails.cache).to receive(:write).with(cache_key, anything)
......
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