Class: Compare

Inherits:
Object
  • Object
show all
Includes:
ActsAsPaginatedDiff, Gitlab::Utils::StrongMemoize
Defined in:
app/models/compare.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActsAsPaginatedDiff

#diffs_in_batch

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(compare, project, base_sha: nil, straight: false) ⇒ Compare

Returns a new instance of Compare.


21
22
23
24
25
26
# File 'app/models/compare.rb', line 21

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

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project


11
12
13
# File 'app/models/compare.rb', line 11

def project
  @project
end

Class Method Details

.decorate(compare, project) ⇒ Object


13
14
15
16
17
18
19
# File 'app/models/compare.rb', line 13

def self.decorate(compare, project)
  if compare.is_a?(Compare)
    compare
  else
    self.new(compare, project)
  end
end

Instance Method Details

#base_commit_shaObject


53
54
55
56
57
58
59
# File 'app/models/compare.rb', line 53

def base_commit_sha
  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
end

#commitsObject


28
29
30
# File 'app/models/compare.rb', line 28

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

#diff_refsObject


76
77
78
79
80
81
82
# File 'app/models/compare.rb', line 76

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

#diffs(diff_options = nil) ⇒ Object


69
70
71
72
73
74
# File 'app/models/compare.rb', line 69

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

#head_commitObject Also known as: commit


40
41
42
43
44
45
46
# File 'app/models/compare.rb', line 40

def head_commit
  strong_memoize(:head_commit) do
    commit = @compare.head

    ::Commit.new(commit, project) if commit
  end
end

#head_commit_shaObject


61
62
63
# File 'app/models/compare.rb', line 61

def head_commit_sha
  commit&.sha
end

#modified_pathsObject


84
85
86
87
88
89
90
91
# File 'app/models/compare.rb', line 84

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

#raw_diffs(*args) ⇒ Object


65
66
67
# File 'app/models/compare.rb', line 65

def raw_diffs(*args)
  @compare.diffs(*args)
end

#start_commitObject


32
33
34
35
36
37
38
# File 'app/models/compare.rb', line 32

def start_commit
  strong_memoize(:start_commit) do
    commit = @compare.base

    ::Commit.new(commit, project) if commit
  end
end

#start_commit_shaObject


49
50
51
# File 'app/models/compare.rb', line 49

def start_commit_sha
  start_commit&.sha
end