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

93 lines
1.8 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2018-12-13 13:39:08 +05:30
require 'set'
2016-09-13 17:45:13 +05:30
class Compare
2018-03-27 19:54:05 +05:30
include Gitlab::Utils::StrongMemoize
2020-01-01 13:55:28 +05:30
include ActsAsPaginatedDiff
2018-03-27 19:54:05 +05:30
2016-09-13 17:45:13 +05:30
delegate :same, :head, :base, to: :@compare
attr_reader :project
def self.decorate(compare, project)
if compare.is_a?(Compare)
compare
else
self.new(compare, project)
end
end
2018-03-27 19:54:05 +05:30
def initialize(compare, project, base_sha: nil, straight: false)
2016-09-13 17:45:13 +05:30
@compare = compare
@project = project
2018-03-27 19:54:05 +05:30
@base_sha = base_sha
2016-11-03 12:29:30 +05:30
@straight = straight
2016-09-13 17:45:13 +05:30
end
def commits
@commits ||= Commit.decorate(@compare.commits, project)
end
def start_commit
2018-03-27 19:54:05 +05:30
strong_memoize(:start_commit) do
commit = @compare.base
2016-09-13 17:45:13 +05:30
2018-03-27 19:54:05 +05:30
::Commit.new(commit, project) if commit
end
2016-09-13 17:45:13 +05:30
end
def head_commit
2018-03-27 19:54:05 +05:30
strong_memoize(:head_commit) do
commit = @compare.head
2016-09-13 17:45:13 +05:30
2018-03-27 19:54:05 +05:30
::Commit.new(commit, project) if commit
end
2016-09-13 17:45:13 +05:30
end
alias_method :commit, :head_commit
2016-11-03 12:29:30 +05:30
def start_commit_sha
2018-03-27 19:54:05 +05:30
start_commit&.sha
2016-11-03 12:29:30 +05:30
end
def base_commit_sha
2018-03-27 19:54:05 +05:30
strong_memoize(:base_commit) do
next unless start_commit && head_commit
@base_sha || project.merge_base_commit(start_commit.id, head_commit.id)&.sha
end
2016-11-03 12:29:30 +05:30
end
def head_commit_sha
2018-03-27 19:54:05 +05:30
commit&.sha
2016-11-03 12:29:30 +05:30
end
2016-09-13 17:45:13 +05:30
def raw_diffs(*args)
@compare.diffs(*args)
end
def diffs(diff_options = nil)
Gitlab::Diff::FileCollection::Compare.new(self,
project: project,
diff_options: diff_options,
diff_refs: diff_refs)
end
def diff_refs
Gitlab::Diff::DiffRefs.new(
2016-11-03 12:29:30 +05:30
base_sha: @straight ? start_commit_sha : base_commit_sha,
start_sha: start_commit_sha,
head_sha: head_commit_sha
2016-09-13 17:45:13 +05:30
)
end
2018-12-13 13:39:08 +05:30
def modified_paths
paths = Set.new
diffs.diff_files.each do |diff|
paths.add diff.old_path
paths.add diff.new_path
end
paths.to_a
end
2016-09-13 17:45:13 +05:30
end