Class: Gitlab::Diff::FileCollection::Base

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/diff/file_collection/base.rb

Direct Known Subclasses

Commit, Compare, MergeRequestDiffBase, WikiPage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(diffable, project:, diff_options: nil, diff_refs: nil, fallback_diff_refs: nil) ⇒ Base

Returns a new instance of Base.


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gitlab/diff/file_collection/base.rb', line 17

def initialize(diffable, project:, diff_options: nil, diff_refs: nil, fallback_diff_refs: nil)
  diff_options = self.class.default_options.merge(diff_options || {})

  @diffable = diffable
  @include_stats = diff_options.delete(:include_stats)
  @project = project
  @diff_options = diff_options
  @diff_refs = diff_refs
  @fallback_diff_refs = fallback_diff_refs
  @repository = project.repository
end

Instance Attribute Details

#diff_optionsObject (readonly)

Returns the value of attribute diff_options


9
10
11
# File 'lib/gitlab/diff/file_collection/base.rb', line 9

def diff_options
  @diff_options
end

#diff_refsObject (readonly)

Returns the value of attribute diff_refs


9
10
11
# File 'lib/gitlab/diff/file_collection/base.rb', line 9

def diff_refs
  @diff_refs
end

#diffableObject (readonly)

Returns the value of attribute diffable


9
10
11
# File 'lib/gitlab/diff/file_collection/base.rb', line 9

def diffable
  @diffable
end

#fallback_diff_refsObject (readonly)

Returns the value of attribute fallback_diff_refs


9
10
11
# File 'lib/gitlab/diff/file_collection/base.rb', line 9

def fallback_diff_refs
  @fallback_diff_refs
end

#projectObject (readonly)

Returns the value of attribute project


9
10
11
# File 'lib/gitlab/diff/file_collection/base.rb', line 9

def project
  @project
end

Class Method Details

.default_optionsObject


13
14
15
# File 'lib/gitlab/diff/file_collection/base.rb', line 13

def self.default_options
  ::Commit.max_diff_options.merge(ignore_whitespace_change: false, expanded: false, include_stats: true)
end

Instance Method Details

#clear_cacheObject


79
80
81
# File 'lib/gitlab/diff/file_collection/base.rb', line 79

def clear_cache
  # No-op
end

#diff_file_pathsObject


41
42
43
# File 'lib/gitlab/diff/file_collection/base.rb', line 41

def diff_file_paths
  diff_files.map(&:file_path)
end

#diff_file_with_new_path(new_path, b_mode = nil) ⇒ Object


71
72
73
74
75
76
77
# File 'lib/gitlab/diff/file_collection/base.rb', line 71

def diff_file_with_new_path(new_path, b_mode = nil)
  if Feature.enabled?(:file_identifier_hash) && b_mode.present?
    diff_files.find { |diff_file| diff_file.new_path == new_path && diff_file.b_mode == b_mode }
  else
    diff_files.find { |diff_file| diff_file.new_path == new_path }
  end
end

#diff_file_with_old_path(old_path, a_mode = nil) ⇒ Object


63
64
65
66
67
68
69
# File 'lib/gitlab/diff/file_collection/base.rb', line 63

def diff_file_with_old_path(old_path, a_mode = nil)
  if Feature.enabled?(:file_identifier_hash) && a_mode.present?
    diff_files.find { |diff_file| diff_file.old_path == old_path && diff_file.a_mode == a_mode }
  else
    diff_files.find { |diff_file| diff_file.old_path == old_path }
  end
end

#diff_filesObject


33
34
35
# File 'lib/gitlab/diff/file_collection/base.rb', line 33

def diff_files
  raw_diff_files
end

#diffsObject


29
30
31
# File 'lib/gitlab/diff/file_collection/base.rb', line 29

def diffs
  @diffs ||= diffable.raw_diffs(diff_options)
end

#pagination_dataObject


45
46
47
48
49
50
51
# File 'lib/gitlab/diff/file_collection/base.rb', line 45

def pagination_data
  {
    current_page: nil,
    next_page: nil,
    total_pages: nil
  }
end

#raw_diff_filesObject


37
38
39
# File 'lib/gitlab/diff/file_collection/base.rb', line 37

def raw_diff_files
  @raw_diff_files ||= diffs.decorate! { |diff| decorate_diff!(diff) }
end

#unfold_diff_files(positions) ⇒ Object

This mutates `diff_files` lines.


54
55
56
57
58
59
60
61
# File 'lib/gitlab/diff/file_collection/base.rb', line 54

def unfold_diff_files(positions)
  positions_grouped_by_path = positions.group_by { |position| position.file_path }

  diff_files.each do |diff_file|
    positions = positions_grouped_by_path.fetch(diff_file.file_path, [])
    positions.each { |position| diff_file.unfold_diff_lines(position) }
  end
end

#write_cacheObject


83
84
85
# File 'lib/gitlab/diff/file_collection/base.rb', line 83

def write_cache
  # No-op
end