summaryrefslogtreecommitdiff
path: root/app/models/context_commits_diff.rb
blob: fe1a72b79f2bcbff318a7eb00aad92503fec33d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# frozen_string_literal: true

class ContextCommitsDiff
  include ActsAsPaginatedDiff

  attr_reader :merge_request

  def initialize(merge_request)
    @merge_request = merge_request
  end

  def empty?
    commits.empty?
  end

  def commits_count
    merge_request.context_commits_count
  end

  def diffs(diff_options = nil)
    Gitlab::Diff::FileCollection::Compare.new(
      self,
      project: merge_request.project,
      diff_options: diff_options,
      diff_refs: diff_refs
    )
  end

  def raw_diffs(options = {})
    compare.diffs(options.merge(paths: paths))
  end

  def diff_refs
    Gitlab::Diff::DiffRefs.new(
      base_sha: commits.last&.diff_refs&.base_sha,
      head_sha: commits.first&.diff_refs&.head_sha
    )
  end

  private

  def compare
    @compare ||=
      Gitlab::Git::Compare.new(
        merge_request.project.repository.raw_repository,
        commits.last&.diff_refs&.base_sha,
        commits.first&.diff_refs&.head_sha
      )
  end

  def commits
    @commits ||= merge_request.project.repository.commits_by(oids: merge_request.recent_context_commits.map(&:id))
  end

  def paths
    merge_request.merge_request_context_commit_diff_files.map(&:path)
  end
end