Use Gitlab::Git::DiffCollections

This commit is contained in:
Jacob Vosmaer 2016-03-03 18:38:44 +01:00
parent e1bc808746
commit 1764e1b7cb
36 changed files with 192 additions and 294 deletions

View file

@ -50,7 +50,7 @@ gem "browser", '~> 1.0.0'
# Extracting information from a git repository # Extracting information from a git repository
# Provide access to Gitlab::Git library # Provide access to Gitlab::Git library
gem "gitlab_git", '~> 8.2' gem "gitlab_git", '~> 9.0'
# LDAP Auth # LDAP Auth
# GitLab fork with several improvements to original library. For full list of changes # GitLab fork with several improvements to original library. For full list of changes

View file

@ -357,7 +357,7 @@ GEM
posix-spawn (~> 0.3) posix-spawn (~> 0.3)
gitlab_emoji (0.3.1) gitlab_emoji (0.3.1)
gemojione (~> 2.2, >= 2.2.1) gemojione (~> 2.2, >= 2.2.1)
gitlab_git (8.2.0) gitlab_git (9.0.0)
activesupport (~> 4.0) activesupport (~> 4.0)
charlock_holmes (~> 0.7.3) charlock_holmes (~> 0.7.3)
github-linguist (~> 4.7.0) github-linguist (~> 4.7.0)
@ -929,7 +929,7 @@ DEPENDENCIES
github-markup (~> 1.3.1) github-markup (~> 1.3.1)
gitlab-flowdock-git-hook (~> 1.0.1) gitlab-flowdock-git-hook (~> 1.0.1)
gitlab_emoji (~> 0.3.0) gitlab_emoji (~> 0.3.0)
gitlab_git (~> 8.2) gitlab_git (~> 9.0)
gitlab_meta (= 7.0) gitlab_meta (= 7.0)
gitlab_omniauth-ldap (~> 1.2.1) gitlab_omniauth-ldap (~> 1.2.1)
gollum-lib (~> 4.1.0) gollum-lib (~> 4.1.0)

View file

@ -3,6 +3,7 @@
# Not to be confused with CommitsController, plural. # Not to be confused with CommitsController, plural.
class Projects::CommitController < Projects::ApplicationController class Projects::CommitController < Projects::ApplicationController
include CreatesCommit include CreatesCommit
include DiffHelper
# Authorize # Authorize
before_action :require_non_empty_project before_action :require_non_empty_project
@ -100,12 +101,10 @@ class Projects::CommitController < Projects::ApplicationController
def define_show_vars def define_show_vars
return git_not_found! unless commit return git_not_found! unless commit
if params[:w].to_i == 1 opts = diff_options
@diffs = commit.diffs({ ignore_whitespace_change: true }) opts[:ignore_whitespace_change] = true if params[:format] == 'diff'
else
@diffs = commit.diffs
end
@diffs = commit.diffs(opts)
@diff_refs = [commit.parent || commit, commit] @diff_refs = [commit.parent || commit, commit]
@notes_count = commit.notes.count @notes_count = commit.notes.count

View file

@ -1,6 +1,8 @@
require 'addressable/uri' require 'addressable/uri'
class Projects::CompareController < Projects::ApplicationController class Projects::CompareController < Projects::ApplicationController
include DiffHelper
# Authorize # Authorize
before_action :require_non_empty_project before_action :require_non_empty_project
before_action :authorize_download_code! before_action :authorize_download_code!
@ -11,16 +13,14 @@ class Projects::CompareController < Projects::ApplicationController
end end
def show def show
diff_options = { ignore_whitespace_change: true } if params[:w] == '1' compare = CompareService.new.
compare_result = CompareService.new.
execute(@project, @head_ref, @project, @base_ref, diff_options) execute(@project, @head_ref, @project, @base_ref, diff_options)
if compare_result if compare
@commits = Commit.decorate(compare_result.commits, @project) @commits = Commit.decorate(compare.commits, @project)
@diffs = compare_result.diffs
@commit = @project.commit(@head_ref) @commit = @project.commit(@head_ref)
@base_commit = @project.merge_base_commit(@base_ref, @head_ref) @base_commit = @project.merge_base_commit(@base_ref, @head_ref)
@diffs = compare.diffs(diff_options)
@diff_refs = [@base_commit, @commit] @diff_refs = [@base_commit, @commit]
@line_notes = [] @line_notes = []
end end

View file

@ -1,4 +1,6 @@
class Projects::MergeRequestsController < Projects::ApplicationController class Projects::MergeRequestsController < Projects::ApplicationController
include DiffHelper
before_action :module_enabled before_action :module_enabled
before_action :merge_request, only: [ before_action :merge_request, only: [
:edit, :update, :show, :diffs, :commits, :builds, :merge, :merge_check, :edit, :update, :show, :diffs, :commits, :builds, :merge, :merge_check,
@ -111,7 +113,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
@commits = @merge_request.compare_commits.reverse @commits = @merge_request.compare_commits.reverse
@commit = @merge_request.last_commit @commit = @merge_request.last_commit
@base_commit = @merge_request.diff_base_commit @base_commit = @merge_request.diff_base_commit
@diffs = @merge_request.compare_diffs @diffs = @merge_request.compare.diffs(diff_options) if @merge_request.compare
@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

View file

@ -12,40 +12,20 @@ module DiffHelper
params[:view] == 'parallel' ? 'parallel' : 'inline' params[:view] == 'parallel' ? 'parallel' : 'inline'
end end
def allowed_diff_size def diff_hard_limit_enabled?
if diff_hard_limit_enabled? params[:force_show_diff].present?
Commit::DIFF_HARD_LIMIT_FILES
else
Commit::DIFF_SAFE_FILES
end
end end
def allowed_diff_lines def diff_options
options = { ignore_whitespace_change: params[:w] == '1' }
if diff_hard_limit_enabled? if diff_hard_limit_enabled?
Commit::DIFF_HARD_LIMIT_LINES options.merge!(Commit.max_diff_options)
else
Commit::DIFF_SAFE_LINES
end end
options
end end
def safe_diff_files(diffs, diff_refs) def safe_diff_files(diffs, diff_refs)
lines = 0 diffs.decorate! { |diff| Gitlab::Diff::File.new(diff, diff_refs) }
safe_files = []
diffs.first(allowed_diff_size).each do |diff|
lines += diff.diff.lines.count
break if lines > allowed_diff_lines
safe_files << Gitlab::Diff::File.new(diff, diff_refs)
end
safe_files
end
def diff_hard_limit_enabled?
# Enabling hard limit allows user to see more diff information
if params[:force_show_diff].present?
true
else
false
end
end end
def generate_line_code(file_path, line) def generate_line_code(file_path, line)

View file

@ -12,12 +12,7 @@ class Commit
attr_accessor :project attr_accessor :project
# Safe amount of changes (files and lines) in one commit to render DIFF_SAFE_LINES = Gitlab::Git::DiffCollection::DEFAULT_LIMITS[:max_lines]
# Used to prevent 500 error on huge commits by suppressing diff
#
# User can force display of diff above this size
DIFF_SAFE_FILES = 100 unless defined?(DIFF_SAFE_FILES)
DIFF_SAFE_LINES = 5000 unless defined?(DIFF_SAFE_LINES)
# Commits above this size will not be rendered in HTML # Commits above this size will not be rendered in HTML
DIFF_HARD_LIMIT_FILES = 1000 unless defined?(DIFF_HARD_LIMIT_FILES) DIFF_HARD_LIMIT_FILES = 1000 unless defined?(DIFF_HARD_LIMIT_FILES)
@ -36,13 +31,20 @@ class Commit
# Calculate number of lines to render for diffs # Calculate number of lines to render for diffs
def diff_line_count(diffs) def diff_line_count(diffs)
diffs.reduce(0) { |sum, d| sum + d.diff.lines.count } diffs.reduce(0) { |sum, d| sum + Gitlab::Git::Util.count_lines(d.diff) }
end end
# Truncate sha to 8 characters # Truncate sha to 8 characters
def truncate_sha(sha) def truncate_sha(sha)
sha[0..7] sha[0..7]
end end
def max_diff_options
{
max_files: DIFF_HARD_LIMIT_FILES,
max_lines: DIFF_HARD_LIMIT_LINES,
}
end
end end
attr_accessor :raw attr_accessor :raw

View file

@ -48,7 +48,7 @@ class MergeRequest < ActiveRecord::Base
after_create :create_merge_request_diff after_create :create_merge_request_diff
after_update :update_merge_request_diff after_update :update_merge_request_diff
delegate :commits, :diffs, :diffs_no_whitespace, to: :merge_request_diff, prefix: nil delegate :commits, :diffs, :real_size, to: :merge_request_diff, prefix: nil
# When this attribute is true some MR validation is ignored # When this attribute is true some MR validation is ignored
# It allows us to close or modify broken merge requests # It allows us to close or modify broken merge requests
@ -56,8 +56,7 @@ class MergeRequest < ActiveRecord::Base
# Temporary fields to store compare vars # Temporary fields to store compare vars
# when creating new merge request # when creating new merge request
attr_accessor :can_be_created, :compare_failed, attr_accessor :can_be_created, :compare_commits, :compare
:compare_commits, :compare_diffs
state_machine :state, initial: :opened do state_machine :state, initial: :opened do
event :close do event :close do
@ -182,6 +181,10 @@ 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 diff_size
merge_request_diff.size
end
def diff_base_commit def diff_base_commit
if merge_request_diff if merge_request_diff
merge_request_diff.base_commit merge_request_diff.base_commit

View file

@ -19,14 +19,15 @@ class MergeRequestDiff < ActiveRecord::Base
# Prevent store of diff if commits amount more then 500 # Prevent store of diff if commits amount more then 500
COMMITS_SAFE_SIZE = 500 COMMITS_SAFE_SIZE = 500
attr_reader :commits, :diffs, :diffs_no_whitespace
belongs_to :merge_request belongs_to :merge_request
delegate :target_branch, :source_branch, to: :merge_request, prefix: nil delegate :target_branch, :source_branch, to: :merge_request, prefix: nil
state_machine :state, initial: :empty do state_machine :state, initial: :empty do
state :collected state :collected
state :overflow
# Deprecated states: these are no longer used but these values may still occur
# in the database.
state :timeout state :timeout
state :overflow_commits_safe_size state :overflow_commits_safe_size
state :overflow_diff_files_limit state :overflow_diff_files_limit
@ -43,19 +44,23 @@ class MergeRequestDiff < ActiveRecord::Base
reload_diffs reload_diffs
end end
def diffs def size
@diffs ||= (load_diffs(st_diffs) || []) real_size.presence || diffs.size
end end
def diffs_no_whitespace def diffs(options={})
compare_result = Gitlab::CompareResult.new( if options[:ignore_whitespace_change]
Gitlab::Git::Compare.new( @diffs_no_whitespace ||= begin
self.repository.raw_repository, compare = Gitlab::Git::Compare.new(
self.target_branch, self.repository.raw_repository,
self.source_sha, self.target_branch,
), { ignore_whitespace_change: true } self.source_sha,
) )
@diffs_no_whitespace ||= load_diffs(dump_commits(compare_result.diffs)) compare.diffs(options)
end
else
@diffs ||= load_diffs(st_diffs, options)
end
end end
def commits def commits
@ -94,16 +99,18 @@ class MergeRequestDiff < ActiveRecord::Base
end end
end end
def load_diffs(raw) def load_diffs(raw, options)
if raw.respond_to?(:map) if raw.respond_to?(:each)
raw.map { |hash| Gitlab::Git::Diff.new(hash) } Gitlab::Git::DiffCollection.new(raw, options)
else
Gitlab::Git::DiffCollection.new([])
end end
end end
# Collect array of Git::Commit objects # Collect array of Git::Commit objects
# between target and source branches # between target and source branches
def unmerged_commits def unmerged_commits
commits = compare_result.commits commits = compare.commits
if commits.present? if commits.present?
commits = Commit.decorate(commits, merge_request.source_project). commits = Commit.decorate(commits, merge_request.source_project).
@ -133,27 +140,21 @@ class MergeRequestDiff < ActiveRecord::Base
if commits.size.zero? if commits.size.zero?
self.state = :empty self.state = :empty
elsif commits.size > COMMITS_SAFE_SIZE
self.state = :overflow_commits_safe_size
else else
new_diffs = unmerged_diffs diff_collection = unmerged_diffs
end
if new_diffs.any? if diff_collection.overflow?
if new_diffs.size > Commit::DIFF_HARD_LIMIT_FILES # Set our state to 'overflow' to make the #empty? and #collected?
self.state = :overflow_diff_files_limit # methods (generated by StateMachine) return false.
new_diffs = new_diffs.first(Commit::DIFF_HARD_LIMIT_LINES) self.state = :overflow
end end
if new_diffs.sum { |diff| diff.diff.lines.count } > Commit::DIFF_HARD_LIMIT_LINES self.real_size = diff_collection.real_size
self.state = :overflow_diff_lines_limit
new_diffs = new_diffs.first(Commit::DIFF_HARD_LIMIT_LINES)
end
end
if new_diffs.present? if diff_collection.any?
new_diffs = dump_commits(new_diffs) new_diffs = dump_diffs(diff_collection)
self.state = :collected self.state = :collected
end
end end
self.st_diffs = new_diffs self.st_diffs = new_diffs
@ -166,10 +167,7 @@ class MergeRequestDiff < ActiveRecord::Base
# Collect array of Git::Diff objects # Collect array of Git::Diff objects
# between target and source branches # between target and source branches
def unmerged_diffs def unmerged_diffs
compare_result.diffs || [] compare.diffs(Commit.max_diff_options)
rescue Gitlab::Git::Diff::TimeoutError
self.state = :timeout
[]
end end
def repository def repository
@ -181,18 +179,16 @@ class MergeRequestDiff < ActiveRecord::Base
source_commit.try(:sha) source_commit.try(:sha)
end end
def compare_result def compare
@compare_result ||= @compare ||=
begin begin
# Update ref for merge request # Update ref for merge request
merge_request.fetch_ref merge_request.fetch_ref
Gitlab::CompareResult.new( Gitlab::Git::Compare.new(
Gitlab::Git::Compare.new( self.repository.raw_repository,
self.repository.raw_repository, self.target_branch,
self.target_branch, self.source_sha
self.source_sha
)
) )
end end
end end

View file

@ -131,9 +131,11 @@ class Note < ActiveRecord::Base
end end
def find_diff def find_diff
return nil unless noteable && noteable.diffs.present? return nil unless noteable
return @diff if defined?(@diff)
@diff ||= noteable.diffs.find do |d| # Don't use ||= because nil is a valid value for @diff
@diff = noteable.diffs(Commit.max_diff_options).find do |d|
Digest::SHA1.hexdigest(d.new_path) == diff_file_index if d.new_path Digest::SHA1.hexdigest(d.new_path) == diff_file_index if d.new_path
end end
end end
@ -165,20 +167,16 @@ class Note < ActiveRecord::Base
def active? def active?
return true unless self.diff return true unless self.diff
return false unless noteable return false unless noteable
return @active if defined?(@active)
noteable.diffs.each do |mr_diff| diffs = noteable.diffs(Commit.max_diff_options)
next unless mr_diff.new_path == self.diff.new_path notable_diff = diffs.find { |d| d.new_path == self.diff.new_path }
lines = Gitlab::Diff::Parser.new.parse(mr_diff.diff.lines.to_a) return @active = false if notable_diff.nil?
lines.each do |line| parsed_lines = Gitlab::Diff::Parser.new.parse(notable_diff.diff.each_line)
if line.text == diff_line # We cannot use ||= because @active may be false
return true @active = parsed_lines.any? { |line_obj| line_obj.text == diff_line }
end
end
end
false
end end
def outdated? def outdated?
@ -263,7 +261,7 @@ class Note < ActiveRecord::Base
end end
def diff_lines def diff_lines
@diff_lines ||= Gitlab::Diff::Parser.new.parse(diff.diff.lines) @diff_lines ||= Gitlab::Diff::Parser.new.parse(diff.diff.each_line)
end end
def highlighted_diff_lines def highlighted_diff_lines

View file

@ -1,7 +1,7 @@
require 'securerandom' require 'securerandom'
# Compare 2 branches for one repo or between repositories # Compare 2 branches for one repo or between repositories
# and return Gitlab::CompareResult object that responds to commits and diffs # and return Gitlab::Git::Compare object that responds to commits and diffs
class CompareService class CompareService
def execute(source_project, source_branch, target_project, target_branch, diff_options = {}) def execute(source_project, source_branch, target_project, target_branch, diff_options = {})
source_commit = source_project.commit(source_branch) source_commit = source_project.commit(source_branch)
@ -20,12 +20,10 @@ class CompareService
) )
end end
Gitlab::CompareResult.new( Gitlab::Git::Compare.new(
Gitlab::Git::Compare.new( target_project.repository.raw_repository,
target_project.repository.raw_repository, target_branch,
target_branch, source_sha,
source_sha,
), diff_options
) )
end end
end end

View file

@ -5,9 +5,7 @@ module MergeRequests
# Set MR attributes # Set MR attributes
merge_request.can_be_created = false merge_request.can_be_created = false
merge_request.compare_failed = false
merge_request.compare_commits = [] merge_request.compare_commits = []
merge_request.compare_diffs = []
merge_request.source_project = project unless merge_request.source_project merge_request.source_project = project unless merge_request.source_project
merge_request.target_project ||= (project.forked_from_project || project) merge_request.target_project ||= (project.forked_from_project || project)
merge_request.target_branch ||= merge_request.target_project.default_branch merge_request.target_branch ||= merge_request.target_project.default_branch
@ -21,35 +19,23 @@ module MergeRequests
return build_failed(merge_request, message) return build_failed(merge_request, message)
end end
compare_result = CompareService.new.execute( compare = CompareService.new.execute(
merge_request.source_project, merge_request.source_project,
merge_request.source_branch, merge_request.source_branch,
merge_request.target_project, merge_request.target_project,
merge_request.target_branch, merge_request.target_branch,
) )
commits = compare_result.commits commits = compare.commits
# At this point we decide if merge request can be created # At this point we decide if merge request can be created
# If we have at least one commit to merge -> creation allowed # If we have at least one commit to merge -> creation allowed
if commits.present? if commits.present?
merge_request.compare_commits = Commit.decorate(commits, merge_request.source_project) merge_request.compare_commits = Commit.decorate(commits, merge_request.source_project)
merge_request.can_be_created = true merge_request.can_be_created = true
merge_request.compare_failed = false merge_request.compare = compare
# Try to collect diff for merge request.
diffs = compare_result.diffs
if diffs.present?
merge_request.compare_diffs = diffs
elsif diffs == false
merge_request.can_be_created = false
merge_request.compare_failed = true
end
else else
merge_request.can_be_created = false merge_request.can_be_created = false
merge_request.compare_failed = false
end end
commits = merge_request.compare_commits commits = merge_request.compare_commits

View file

@ -10,8 +10,8 @@
= parallel_diff_btn = parallel_diff_btn
= render 'projects/diffs/stats', diff_files: diff_files = render 'projects/diffs/stats', diff_files: diff_files
- if diff_files.count < diffs.size - if diff_files.overflow?
= render 'projects/diffs/warning', diffs: diffs, shown_files_count: diff_files.count = render 'projects/diffs/warning', diff_files: diff_files
.files .files
- diff_files.each_with_index do |diff_file, index| - diff_files.each_with_index do |diff_file, index|
@ -21,10 +21,3 @@
= render 'projects/diffs/file', i: index, project: project, = render 'projects/diffs/file', i: index, project: project,
diff_file: diff_file, diff_commit: diff_commit, blob: blob diff_file: diff_file, diff_commit: diff_commit, blob: blob
- if @diff_timeout
.alert.alert-danger
%h4
Failed to collect changes
%p
Maybe diff is really big and operation failed with timeout. Try to get diff locally

View file

@ -6,7 +6,7 @@
%table.text-file.code.js-syntax-highlight{ class: too_big ? 'hide' : '' } %table.text-file.code.js-syntax-highlight{ class: too_big ? 'hide' : '' }
- last_line = 0 - last_line = 0
- raw_diff_lines = diff_file.diff_lines - raw_diff_lines = diff_file.diff_lines.to_a
- diff_file.highlighted_diff_lines.each_with_index do |line, index| - diff_file.highlighted_diff_lines.each_with_index do |line, index|
- type = line.type - type = line.type
- last_line = line.new_pos - last_line = line.new_pos

View file

@ -14,5 +14,5 @@
= link_to "Email patch", merge_request_path(@merge_request, format: :patch), class: "btn btn-sm" = link_to "Email patch", merge_request_path(@merge_request, format: :patch), class: "btn btn-sm"
%p %p
To preserve performance only To preserve performance only
%strong #{shown_files_count} of #{diffs.size} %strong #{diff_files.count} of #{diff_files.real_size}
files are displayed. files are displayed.

View file

@ -33,23 +33,18 @@
%div= msg %div= msg
- elsif @merge_request.source_branch.present? && @merge_request.target_branch.present? - elsif @merge_request.source_branch.present? && @merge_request.target_branch.present?
- if @merge_request.compare_failed .light-well.append-bottom-default
.alert.alert-danger .center
%h4 Compare failed %h4
%p We can't compare selected branches. It may be because of huge diff. Please try again or select different branches. There isn't anything to merge.
- else %p.slead
.light-well.append-bottom-default - if @merge_request.source_branch == @merge_request.target_branch
.center You'll need to use different branch names to get a valid comparison.
%h4 - else
There isn't anything to merge. %span.label-branch #{@merge_request.source_branch}
%p.slead and
- if @merge_request.source_branch == @merge_request.target_branch %span.label-branch #{@merge_request.target_branch}
You'll need to use different branch names to get a valid comparison. are the same.
- else
%span.label-branch #{@merge_request.source_branch}
and
%span.label-branch #{@merge_request.target_branch}
are the same.
.form-actions .form-actions

View file

@ -31,22 +31,18 @@
%li.diffs-tab.active %li.diffs-tab.active
= link_to url_for(params), data: {target: 'div#diffs', action: 'diffs', toggle: 'tab'} do = link_to url_for(params), data: {target: 'div#diffs', action: 'diffs', toggle: 'tab'} do
Changes Changes
%span.badge= @diffs.size %span.badge= @diffs.real_size
.tab-content .tab-content
#commits.commits.tab-pane #commits.commits.tab-pane
= 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 @commits.size > MergeRequestDiff::COMMITS_SAFE_SIZE
= render "projects/diffs/diffs", diffs: @diffs, project: @project, diff_refs: @merge_request.diff_refs
- 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.
%p To preserve performance the line changes are not shown. %p To preserve performance the line changes are not shown.
- else - else
.alert.alert-danger = render "projects/diffs/diffs", diffs: @diffs, project: @project, diff_refs: @merge_request.diff_refs
%h4 This comparison includes a huge diff.
%p To preserve performance the line changes are not shown.
- if @ci_commit - if @ci_commit
#builds.builds.tab-pane #builds.builds.tab-pane
= render "projects/merge_requests/show/builds" = render "projects/merge_requests/show/builds"

View file

@ -62,7 +62,7 @@
%li.diffs-tab %li.diffs-tab
= link_to diffs_namespace_project_merge_request_path(@project.namespace, @project, @merge_request), data: {target: 'div#diffs', action: 'diffs', toggle: 'tab'} do = link_to diffs_namespace_project_merge_request_path(@project.namespace, @project, @merge_request), data: {target: 'div#diffs', action: 'diffs', toggle: 'tab'} do
Changes Changes
%span.badge= @merge_request.diffs.size %span.badge= @merge_request.diff_size
.tab-content .tab-content
#notes.notes.tab-pane.voting_notes #notes.notes.tab-pane.voting_notes

View file

@ -1,5 +1,5 @@
- 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, = render "projects/diffs/diffs", diffs: @merge_request.diffs(diff_options),
project: @merge_request.project, diff_refs: @merge_request.diff_refs project: @merge_request.project, diff_refs: @merge_request.diff_refs
- 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}

View file

@ -30,7 +30,7 @@
.line-numbers .line-numbers
- snippet_chunks.each do |chunk| - snippet_chunks.each do |chunk|
- unless chunk[:data].empty? - unless chunk[:data].empty?
- chunk[:data].lines.to_a.size.times do |index| - Gitlab::Git::Util.count_lines(chunk[:data]).times do |index|
- offset = defined?(chunk[:start_line]) ? chunk[:start_line] : 1 - offset = defined?(chunk[:start_line]) ? chunk[:start_line] : 1
- i = index + offset - i = index + offset
= link_to snippet_path+"#L#{i}", id: "L#{i}", rel: "#L#{i}", class: "diff-line-num" do = link_to snippet_path+"#L#{i}", id: "L#{i}", rel: "#L#{i}", class: "diff-line-num" do

View file

@ -141,7 +141,7 @@ class IrkerWorker
end end
def files_count(commit) def files_count(commit)
files = "#{commit.diffs.count} file" files = "#{commit.diffs.real_size} file"
files += 's' if commit.diffs.count > 1 files += 's' if commit.diffs.count > 1
files files
end end

View file

@ -0,0 +1,5 @@
class AddRealSizeToMergeRequestDiffs < ActiveRecord::Migration
def change
add_column :merge_request_diffs, :real_size, :string
end
end

View file

@ -509,6 +509,7 @@ ActiveRecord::Schema.define(version: 20160222153918) do
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.string "base_commit_sha" t.string "base_commit_sha"
t.string "real_size"
end end
add_index "merge_request_diffs", ["merge_request_id"], name: "index_merge_request_diffs_on_merge_request_id", unique: true, using: :btree add_index "merge_request_diffs", ["merge_request_id"], name: "index_merge_request_diffs_on_merge_request_id", unique: true, using: :btree

View file

@ -126,8 +126,11 @@ class Spinach::Features::ProjectCommits < Spinach::FeatureSteps
end end
step 'I visit big commit page' do step 'I visit big commit page' do
stub_const('Commit::DIFF_SAFE_FILES', 20) # Create a temporary scope to ensure that the stub_const is removed after user
visit namespace_project_commit_path(@project.namespace, @project, sample_big_commit.id) RSpec::Mocks.with_temporary_scope do
stub_const('Gitlab::Git::DiffCollection::DEFAULT_LIMITS', { max_lines: 1, max_files: 1 })
visit namespace_project_commit_path(@project.namespace, @project, sample_big_commit.id)
end
end end
step 'I see big commit warning' do step 'I see big commit warning' do

View file

@ -48,7 +48,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 commit.diffs.to_a
end end
# Get a commit's comments # Get a commit's comments
@ -90,9 +90,9 @@ module API
} }
if params[:path] && params[:line] && params[:line_type] if params[:path] && params[:line] && params[:line_type]
commit.diffs.each do |diff| commit.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.lines.to_a) lines = Gitlab::Diff::Parser.new.parse(diff.diff.each_line)
lines.each do |line| lines.each do |line|
next unless line.new_pos == params[:line].to_i && line.type == params[:line_type] next unless line.new_pos == params[:line].to_i && line.type == params[:line_type]

View file

@ -181,7 +181,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 compare.diffs(all_diffs: true).to_a
end end
end end
@ -295,11 +295,11 @@ module API
end end
expose :diffs, using: Entities::RepoDiff do |compare, options| expose :diffs, using: Entities::RepoDiff do |compare, options|
compare.diffs compare.diffs(all_diffs: true).to_a
end end
expose :compare_timeout do |compare, options| expose :compare_timeout do |compare, options|
compare.timeout compare.diffs.overflow?
end end
expose :same, as: :compare_same_ref expose :same, as: :compare_same_ref

View file

@ -1,9 +0,0 @@
module Gitlab
class CompareResult
attr_reader :commits, :diffs
def initialize(compare, diff_options = {})
@commits, @diffs = compare.commits, compare.diffs(nil, diff_options)
end
end
end

View file

@ -21,7 +21,7 @@ module Gitlab
# Array of Gitlab::DIff::Line objects # Array of Gitlab::DIff::Line objects
def diff_lines def diff_lines
@lines ||= parser.parse(raw_diff.lines) @lines ||= parser.parse(raw_diff.each_line).to_a
end end
def highlighted_diff_lines def highlighted_diff_lines

View file

@ -5,52 +5,53 @@ module Gitlab
def parse(lines) def parse(lines)
@lines = lines @lines = lines
lines_obj = []
line_obj_index = 0 line_obj_index = 0
line_old = 1 line_old = 1
line_new = 1 line_new = 1
type = nil type = nil
@lines.each do |line| # By returning an Enumerator we make it possible to search for a single line (with #find)
next if filename?(line) # without having to instantiate all the others that come after it.
Enumerator.new do |yielder|
@lines.each do |line|
next if filename?(line)
full_line = line.gsub(/\n/, '') full_line = line.gsub(/\n/, '')
if line.match(/^@@ -/) if line.match(/^@@ -/)
type = "match" type = "match"
line_old = line.match(/\-[0-9]*/)[0].to_i.abs rescue 0 line_old = line.match(/\-[0-9]*/)[0].to_i.abs rescue 0
line_new = line.match(/\+[0-9]*/)[0].to_i.abs rescue 0 line_new = line.match(/\+[0-9]*/)[0].to_i.abs rescue 0
next if line_old <= 1 && line_new <= 1 #top of file next if line_old <= 1 && line_new <= 1 #top of file
lines_obj << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new) yielder << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new)
line_obj_index += 1 line_obj_index += 1
next next
elsif line[0] == '\\' elsif line[0] == '\\'
type = 'nonewline' type = 'nonewline'
lines_obj << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new) yielder << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new)
line_obj_index += 1 line_obj_index += 1
else else
type = identification_type(line) type = identification_type(line)
lines_obj << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new) yielder << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new)
line_obj_index += 1 line_obj_index += 1
end end
case line[0] case line[0]
when "+" when "+"
line_new += 1 line_new += 1
when "-" when "-"
line_old += 1 line_old += 1
when "\\" when "\\"
# No increment # No increment
else else
line_new += 1 line_new += 1
line_old += 1 line_old += 1
end
end end
end end
lines_obj
end end
def empty? def empty?

View file

@ -50,7 +50,7 @@ module Gitlab
end end
def compare_timeout def compare_timeout
compare.timeout if compare diffs.overflow? if diffs
end end
def reverse_compare? def reverse_compare?

View file

@ -81,7 +81,7 @@ describe Projects::CommitController do
expect(response.body).to start_with("diff --git") expect(response.body).to start_with("diff --git")
# without whitespace option, there are more than 2 diff_splits # without whitespace option, there are more than 2 diff_splits
diff_splits = assigns(:diffs)[0].diff.split("\n") diff_splits = assigns(:diffs).first.diff.split("\n")
expect(diff_splits.length).to be <= 2 expect(diff_splits.length).to be <= 2
end end
end end

View file

@ -19,7 +19,7 @@ describe Projects::CompareController do
to: ref_to) to: ref_to)
expect(response).to be_success expect(response).to be_success
expect(assigns(:diffs).length).to be >= 1 expect(assigns(:diffs).first).to_not be_nil
expect(assigns(:commits).length).to be >= 1 expect(assigns(:commits).length).to be >= 1
end end
@ -32,10 +32,10 @@ describe Projects::CompareController do
w: 1) w: 1)
expect(response).to be_success expect(response).to be_success
expect(assigns(:diffs).length).to be >= 1 expect(assigns(:diffs).first).to_not be_nil
expect(assigns(:commits).length).to be >= 1 expect(assigns(:commits).length).to be >= 1
# without whitespace option, there are more than 2 diff_splits # without whitespace option, there are more than 2 diff_splits
diff_splits = assigns(:diffs)[0].diff.split("\n") diff_splits = assigns(:diffs).first.diff.split("\n")
expect(diff_splits.length).to be <= 2 expect(diff_splits.length).to be <= 2
end end
@ -48,7 +48,7 @@ describe Projects::CompareController do
to: ref_to) to: ref_to)
expect(response).to be_success expect(response).to be_success
expect(assigns(:diffs)).to eq([]) expect(assigns(:diffs).to_a).to eq([])
expect(assigns(:commits)).to eq([]) expect(assigns(:commits)).to eq([])
end end

View file

@ -22,65 +22,14 @@ describe DiffHelper do
end end
end end
describe 'allowed_diff_size' do describe 'diff_options' do
it 'should return hard limit for a diff if force diff is true' do it 'should return hard limit for a diff if force diff is true' do
allow(controller).to receive(:params) { { force_show_diff: true } } allow(controller).to receive(:params) { { force_show_diff: true } }
expect(allowed_diff_size).to eq(1000) expect(diff_options).to include(Commit.max_diff_options)
end end
it 'should return safe limit for a diff if force diff is false' do it 'should return safe limit for a diff if force diff is false' do
expect(allowed_diff_size).to eq(100) expect(diff_options).not_to include(:max_lines, :max_files)
end
end
describe 'allowed_diff_lines' do
it 'should return hard limit for number of lines in a diff if force diff is true' do
allow(controller).to receive(:params) { { force_show_diff: true } }
expect(allowed_diff_lines).to eq(50000)
end
it 'should return safe limit for numbers of lines a diff if force diff is false' do
expect(allowed_diff_lines).to eq(5000)
end
end
describe 'safe_diff_files' do
it 'should return all files from a commit that is smaller than safe limits' do
expect(safe_diff_files(diffs, diff_refs).length).to eq(2)
end
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
expect(safe_diff_files(diffs, diff_refs).length).to eq(1)
end
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(diffs[1].diff).to receive(:lines).and_return([""] * 4999) #simulate 4999 lines
expect(safe_diff_files(diffs, diff_refs).length).to eq(2)
end
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(diffs[1].diff).to receive(:lines).and_return([""] * 49999) #simulate 49999 lines
expect(safe_diff_files(diffs, diff_refs).length).to eq(1)
end
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
expect(safe_diff_files(large_diffs, diff_refs).length).to eq(100)
end
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 } }
large_diffs = diffs * 100 #simulate 200 diffs
expect(safe_diff_files(large_diffs, diff_refs).length).to eq(200)
end
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 } }
very_large_diffs = diffs * 1000 #simulate 2000 diffs
expect(safe_diff_files(very_large_diffs, diff_refs).length).to eq(1000)
end end
end end

View file

@ -47,7 +47,7 @@ eos
end end
before do before do
@lines = parser.parse(diff.lines) @lines = parser.parse(diff.lines).to_a
end end
it { expect(@lines.size).to eq(30) } it { expect(@lines.size).to eq(30) }

View file

@ -72,7 +72,7 @@ describe Gitlab::Email::Message::RepositoryPush do
describe '#compare_timeout' do describe '#compare_timeout' do
subject { message.compare_timeout } subject { message.compare_timeout }
it { is_expected.to eq compare.timeout } it { is_expected.to eq compare.diffs.overflow? }
end end
describe '#reverse_compare?' do describe '#reverse_compare?' do

View file

@ -79,7 +79,7 @@ describe MergeRequests::RefreshService, services: true do
it { expect(@merge_request.notes.last.note).to include('changed to merged') } it { expect(@merge_request.notes.last.note).to include('changed to merged') }
it { expect(@merge_request).to be_merged } it { expect(@merge_request).to be_merged }
it { expect(@merge_request.diffs.length).to be > 0 } it { expect(@merge_request.diffs.size).to be > 0 }
it { expect(@fork_merge_request).to be_merged } it { expect(@fork_merge_request).to be_merged }
it { expect(@fork_merge_request.notes.last.note).to include('changed to merged') } it { expect(@fork_merge_request.notes.last.note).to include('changed to merged') }
end end