summaryrefslogtreecommitdiff
path: root/app/models/compare.rb
blob: 3a8bbcb1acdf2ac8b24edd9e3dc915c5fe1c0668 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class Compare
  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

  def initialize(compare, project, straight: false)
    @compare = compare
    @project = project
    @straight = straight
  end

  def commits
    @commits ||= Commit.decorate(@compare.commits, project)
  end

  def start_commit
    return @start_commit if defined?(@start_commit)

    commit = @compare.base
    @start_commit = commit ? ::Commit.new(commit, project) : nil
  end

  def head_commit
    return @head_commit if defined?(@head_commit)

    commit = @compare.head
    @head_commit = commit ? ::Commit.new(commit, project) : nil
  end
  alias_method :commit, :head_commit

  def base_commit
    return @base_commit if defined?(@base_commit)

    @base_commit = if start_commit && head_commit
                     project.merge_base_commit(start_commit.id, head_commit.id)
                   else
                     nil
                   end
  end

  def start_commit_sha
    start_commit.try(:sha)
  end

  def base_commit_sha
    base_commit.try(:sha)
  end

  def head_commit_sha
    commit.try(:sha)
  end

  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(
      base_sha:  @straight ? start_commit_sha : base_commit_sha,
      start_sha: start_commit_sha,
      head_sha: head_commit_sha
    )
  end
end