Class: Compare

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Repositories::StreamableDiff

#diffs_for_streaming

Methods included from ActsAsPaginatedDiff

#diffs_in_batch

Constructor Details

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

Returns a new instance of Compare.



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

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.



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

def project
  @project
end

Class Method Details

.decorate(compare, project) ⇒ Object



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

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

Instance Method Details

#base_commit_shaObject



71
72
73
74
75
76
77
# File 'app/models/compare.rb', line 71

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

#cache_keyObject



39
40
41
# File 'app/models/compare.rb', line 39

def cache_key
  [@project, :compare, diff_refs.hash]
end

#changed_pathsObject



106
107
108
109
110
111
112
113
# File 'app/models/compare.rb', line 106

def changed_paths
  project
    .repository
    .find_changed_paths(
      [Gitlab::Git::DiffTree.new(diff_refs.base_sha, diff_refs.head_sha)],
      find_renames: true
    )
end

#commitsObject



43
44
45
46
47
48
# File 'app/models/compare.rb', line 43

def commits
  @commits ||= begin
    decorated_commits = Commit.decorate(@compare.commits, project)
    CommitCollection.new(project, decorated_commits)
  end
end

#diff_refsObject



98
99
100
101
102
103
104
# File 'app/models/compare.rb', line 98

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



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

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

#first_diffs_slice(limit, diff_options = {}) ⇒ Object



124
125
126
# File 'app/models/compare.rb', line 124

def first_diffs_slice(limit, diff_options = {})
  diffs(diff_options.merge(max_files: limit)).diff_files
end

#head_commitObject Also known as: commit



58
59
60
61
62
63
64
# File 'app/models/compare.rb', line 58

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

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

#head_commit_shaObject



79
80
81
# File 'app/models/compare.rb', line 79

def head_commit_sha
  commit&.sha
end

#modified_pathsObject



115
116
117
118
119
120
121
122
# File 'app/models/compare.rb', line 115

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_diffsObject



83
84
85
# File 'app/models/compare.rb', line 83

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

#repositoryObject



94
95
96
# File 'app/models/compare.rb', line 94

def repository
  project.repository
end

#start_commitObject



50
51
52
53
54
55
56
# File 'app/models/compare.rb', line 50

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

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

#start_commit_shaObject



67
68
69
# File 'app/models/compare.rb', line 67

def start_commit_sha
  start_commit&.sha
end

#to_paramObject

Return a Hash of parameters for passing to a URL helper

See namespace_project_compare_url



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

def to_param
  {
    from: @straight ? start_commit_sha : (base_commit_sha || start_commit_sha),
    to: head_commit_sha
  }.tap do |params|
    params[:straight] = true if @straight
  end
end