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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

110 lines
2.2 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
2021-11-11 11:23:49 +05:30
# Return a Hash of parameters for passing to a URL helper
#
# See `namespace_project_compare_url`
def to_param
{
2023-06-20 00:43:36 +05:30
from: @straight ? start_commit_sha : (base_commit_sha || start_commit_sha),
2021-11-11 11:23:49 +05:30
to: head_commit_sha
}
end
2021-09-30 23:02:18 +05:30
def cache_key
[@project, :compare, diff_refs.hash]
end
2016-09-13 17:45:13 +05:30
def commits
2022-07-23 23:45:48 +05:30
@commits ||= begin
decorated_commits = Commit.decorate(@compare.commits, project)
CommitCollection.new(project, decorated_commits)
end
2016-09-13 17:45:13 +05:30
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
2023-04-23 21:23:45 +05:30
def raw_diffs(...)
@compare.diffs(...)
2016-09-13 17:45:13 +05:30
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(
2022-08-27 11:52:29 +05:30
base_sha: @straight ? start_commit_sha : base_commit_sha,
2016-11-03 12:29:30 +05:30
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