debian-mirror-gitlab/app/models/merge_request_diff.rb

193 lines
4.1 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
class MergeRequestDiff < ActiveRecord::Base
2015-04-26 12:48:37 +05:30
include Sortable
2015-09-25 12:07:36 +05:30
# Prevent store of diff if commits amount more then 500
2016-06-02 11:05:42 +05:30
COMMITS_SAFE_SIZE = 100
2014-09-02 18:07:02 +05:30
belongs_to :merge_request
2016-06-02 11:05:42 +05:30
delegate :head_source_sha, :target_branch, :source_branch, to: :merge_request, prefix: nil
2014-09-02 18:07:02 +05:30
state_machine :state, initial: :empty do
state :collected
2016-06-02 11:05:42 +05:30
state :overflow
# Deprecated states: these are no longer used but these values may still occur
# in the database.
2014-09-02 18:07:02 +05:30
state :timeout
state :overflow_commits_safe_size
state :overflow_diff_files_limit
state :overflow_diff_lines_limit
end
serialize :st_commits
serialize :st_diffs
after_create :reload_content
def reload_content
reload_commits
reload_diffs
end
2016-06-02 11:05:42 +05:30
def size
real_size.presence || diffs.size
2014-09-02 18:07:02 +05:30
end
2016-06-02 11:05:42 +05:30
def diffs(options={})
if options[:ignore_whitespace_change]
@diffs_no_whitespace ||= begin
compare = Gitlab::Git::Compare.new(
self.repository.raw_repository,
self.base,
self.head,
)
compare.diffs(options)
end
else
@diffs ||= load_diffs(st_diffs, options)
end
2015-11-26 14:37:03 +05:30
end
2014-09-02 18:07:02 +05:30
def commits
@commits ||= load_commits(st_commits || [])
end
def last_commit
commits.first
end
2015-10-24 18:46:33 +05:30
def first_commit
commits.last
end
2016-01-29 22:53:50 +05:30
def base_commit
return nil unless self.base_commit_sha
merge_request.target_project.commit(self.base_commit_sha)
end
2014-09-02 18:07:02 +05:30
def last_commit_short_sha
2015-04-26 12:48:37 +05:30
@last_commit_short_sha ||= last_commit.short_id
2014-09-02 18:07:02 +05:30
end
def dump_commits(commits)
commits.map(&:to_hash)
end
def load_commits(array)
2015-09-11 14:41:01 +05:30
array.map { |hash| Commit.new(Gitlab::Git::Commit.new(hash), merge_request.source_project) }
2014-09-02 18:07:02 +05:30
end
def dump_diffs(diffs)
if diffs.respond_to?(:map)
diffs.map(&:to_hash)
end
end
2016-06-02 11:05:42 +05:30
def load_diffs(raw, options)
if raw.respond_to?(:each)
Gitlab::Git::DiffCollection.new(raw, options)
else
Gitlab::Git::DiffCollection.new([])
2014-09-02 18:07:02 +05:30
end
end
# Collect array of Git::Commit objects
# between target and source branches
def unmerged_commits
2016-06-02 11:05:42 +05:30
commits = compare.commits
2014-09-02 18:07:02 +05:30
if commits.present?
2016-06-02 11:05:42 +05:30
commits = Commit.decorate(commits, merge_request.source_project).reverse
2014-09-02 18:07:02 +05:30
end
commits
end
# Reload all commits related to current merge request from repo
# and save it as array of hashes in st_commits db field
def reload_commits
commit_objects = unmerged_commits
if commit_objects.present?
self.st_commits = dump_commits(commit_objects)
end
save
end
# Reload diffs between branches related to current merge request from repo
# and save it as array of hashes in st_diffs db field
def reload_diffs
new_diffs = []
if commits.size.zero?
self.state = :empty
else
2016-06-02 11:05:42 +05:30
diff_collection = unmerged_diffs
2014-09-02 18:07:02 +05:30
2016-06-02 11:05:42 +05:30
if diff_collection.overflow?
# Set our state to 'overflow' to make the #empty? and #collected?
# methods (generated by StateMachine) return false.
self.state = :overflow
2014-09-02 18:07:02 +05:30
end
2016-06-02 11:05:42 +05:30
self.real_size = diff_collection.real_size
2014-09-02 18:07:02 +05:30
2016-06-02 11:05:42 +05:30
if diff_collection.any?
new_diffs = dump_diffs(diff_collection)
self.state = :collected
end
2014-09-02 18:07:02 +05:30
end
self.st_diffs = new_diffs
2016-01-29 22:53:50 +05:30
2016-06-02 11:05:42 +05:30
self.base_commit_sha = self.repository.merge_base(self.head, self.base)
2016-01-29 22:53:50 +05:30
2014-09-02 18:07:02 +05:30
self.save
end
# Collect array of Git::Diff objects
# between target and source branches
def unmerged_diffs
2016-06-02 11:05:42 +05:30
compare.diffs(Commit.max_diff_options)
2014-09-02 18:07:02 +05:30
end
def repository
merge_request.target_project.repository
end
2016-02-05 20:25:01 +05:30
def source_sha
2016-06-02 11:05:42 +05:30
return head_source_sha if head_source_sha.present?
2016-02-05 20:25:01 +05:30
source_commit = merge_request.source_project.commit(source_branch)
source_commit.try(:sha)
end
2014-09-02 18:07:02 +05:30
2016-06-02 11:05:42 +05:30
def target_sha
merge_request.target_sha
end
def base
self.target_sha || self.target_branch
end
def head
self.source_sha
end
def compare
@compare ||=
2015-09-25 12:07:36 +05:30
begin
# Update ref for merge request
merge_request.fetch_ref
2016-06-02 11:05:42 +05:30
Gitlab::Git::Compare.new(
self.repository.raw_repository,
self.base,
self.head
2015-09-25 12:07:36 +05:30
)
end
2014-09-02 18:07:02 +05:30
end
end