summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorPaco Guzman <pacoguzmanp@gmail.com>2016-07-26 09:21:42 +0200
committerPaco Guzman <pacoguzmanp@gmail.com>2016-08-03 07:00:20 +0200
commit8f359ea9170b984ad43d126e17628c31ac3a1f14 (patch)
treeee47d5172537ddedabc2cc38ef3cb5bf966c89ee /app/models
parentcd7c2cb6ddd4d9c9f9bdae00c887c0022c121c17 (diff)
downloadgitlab-ce-8f359ea9170b984ad43d126e17628c31ac3a1f14.tar.gz
Move to Gitlab::Diff::FileCollection
Instead calling diff_collection.count use diff_collection.size which is cache on the diff_collection
Diffstat (limited to 'app/models')
-rw-r--r--app/models/commit.rb4
-rw-r--r--app/models/compare.rb23
-rw-r--r--app/models/merge_request.rb4
-rw-r--r--app/models/safe_diffs.rb5
-rw-r--r--app/models/safe_diffs/base.rb55
-rw-r--r--app/models/safe_diffs/commit.rb10
-rw-r--r--app/models/safe_diffs/compare.rb10
-rw-r--r--app/models/safe_diffs/merge_request.rb52
8 files changed, 31 insertions, 132 deletions
diff --git a/app/models/commit.rb b/app/models/commit.rb
index c52b4a051c2..d22ecb222e5 100644
--- a/app/models/commit.rb
+++ b/app/models/commit.rb
@@ -317,6 +317,10 @@ class Commit
nil
end
+ def diff_file_collection(diff_options)
+ Gitlab::Diff::FileCollection::Commit.new(self, diff_options: diff_options)
+ end
+
private
def find_author_by_any_email
diff --git a/app/models/compare.rb b/app/models/compare.rb
new file mode 100644
index 00000000000..6672d1bf059
--- /dev/null
+++ b/app/models/compare.rb
@@ -0,0 +1,23 @@
+class Compare
+ delegate :commits, :same, :head, :base, to: :@compare
+
+ def self.decorate(compare, project)
+ if compare.is_a?(Compare)
+ compare
+ else
+ self.new(compare, project)
+ end
+ end
+
+ def initialize(compare, project)
+ @compare = compare
+ @project = project
+ end
+
+ def diff_file_collection(diff_options:, diff_refs: nil)
+ Gitlab::Diff::FileCollection::Compare.new(@compare,
+ project: @project,
+ diff_options: diff_options,
+ diff_refs: diff_refs)
+ end
+end
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb
index 774851cc90f..abc8bacbe59 100644
--- a/app/models/merge_request.rb
+++ b/app/models/merge_request.rb
@@ -168,6 +168,10 @@ class MergeRequest < ActiveRecord::Base
merge_request_diff ? merge_request_diff.diffs(*args) : compare.diffs(*args)
end
+ def diff_file_collection(diff_options)
+ Gitlab::Diff::FileCollection::MergeRequest.new(self, diff_options: diff_options)
+ end
+
def diff_size
merge_request_diff.size
end
diff --git a/app/models/safe_diffs.rb b/app/models/safe_diffs.rb
deleted file mode 100644
index 8ca9ec4cc39..00000000000
--- a/app/models/safe_diffs.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-module SafeDiffs
- def self.default_options
- ::Commit.max_diff_options.merge(ignore_whitespace_change: false, no_collapse: false)
- end
-end
diff --git a/app/models/safe_diffs/base.rb b/app/models/safe_diffs/base.rb
deleted file mode 100644
index dfc4708e293..00000000000
--- a/app/models/safe_diffs/base.rb
+++ /dev/null
@@ -1,55 +0,0 @@
-module SafeDiffs
- class Base
- attr_reader :project, :diff_options, :diff_view, :diff_refs
-
- delegate :count, :real_size, to: :diff_files
-
- def initialize(diffs, project:, diff_options:, diff_refs: nil)
- @diffs = diffs
- @project = project
- @diff_options = diff_options
- @diff_refs = diff_refs
- end
-
- def diff_files
- @diff_files ||= begin
- diffs = @diffs.decorate! do |diff|
- Gitlab::Diff::File.new(diff, diff_refs: @diff_refs, repository: @project.repository)
- end
-
- highlight!(diffs)
- diffs
- end
- end
-
- private
-
- def highlight!(diff_files)
- if cacheable?
- cache_highlight!(diff_files)
- else
- diff_files.each { |diff_file| highlight_diff_file!(diff_file) }
- end
- end
-
- def cacheable?
- false
- end
-
- def cache_highlight!
- raise NotImplementedError
- end
-
- def highlight_diff_file_from_cache!(diff_file, cache_diff_lines)
- diff_file.diff_lines = cache_diff_lines.map do |line|
- Gitlab::Diff::Line.init_from_hash(line)
- end
- end
-
- def highlight_diff_file!(diff_file)
- diff_file.diff_lines = Gitlab::Diff::Highlight.new(diff_file, repository: diff_file.repository).highlight
- diff_file.highlighted_diff_lines = diff_file.diff_lines # To be used on parallel diff
- diff_file
- end
- end
-end
diff --git a/app/models/safe_diffs/commit.rb b/app/models/safe_diffs/commit.rb
deleted file mode 100644
index 338878f32e0..00000000000
--- a/app/models/safe_diffs/commit.rb
+++ /dev/null
@@ -1,10 +0,0 @@
-module SafeDiffs
- class Commit < Base
- def initialize(commit, diff_options:)
- super(commit.diffs(diff_options),
- project: commit.project,
- diff_options: diff_options,
- diff_refs: commit.diff_refs)
- end
- end
-end
diff --git a/app/models/safe_diffs/compare.rb b/app/models/safe_diffs/compare.rb
deleted file mode 100644
index 6b64b81137d..00000000000
--- a/app/models/safe_diffs/compare.rb
+++ /dev/null
@@ -1,10 +0,0 @@
-module SafeDiffs
- class Compare < Base
- def initialize(compare, project:, diff_options:, diff_refs: nil)
- super(compare.diffs(diff_options),
- project: project,
- diff_options: diff_options,
- diff_refs: diff_refs)
- end
- end
-end
diff --git a/app/models/safe_diffs/merge_request.rb b/app/models/safe_diffs/merge_request.rb
deleted file mode 100644
index 111b9a54f91..00000000000
--- a/app/models/safe_diffs/merge_request.rb
+++ /dev/null
@@ -1,52 +0,0 @@
-module SafeDiffs
- class MergeRequest < Base
- def initialize(merge_request, diff_options:)
- @merge_request = merge_request
-
- super(merge_request.diffs(diff_options),
- project: merge_request.project,
- diff_options: diff_options,
- diff_refs: merge_request.diff_refs)
- end
-
- private
-
- #
- # If we find the highlighted diff files lines on the cache we replace existing diff_files lines (no highlighted)
- # for the highlighted ones, so we just skip their execution.
- # If the highlighted diff files lines are not cached we calculate and cache them.
- #
- # The content of the cache is and Hash where the key correspond to the file_path and the values are Arrays of
- # hashes than represent serialized diff lines.
- #
- def cache_highlight!(diff_files)
- highlighted_cache = Rails.cache.read(cache_key) || {}
- highlighted_cache_was_empty = highlighted_cache.empty?
-
- diff_files.each do |diff_file|
- file_path = diff_file.file_path
-
- if highlighted_cache[file_path]
- highlight_diff_file_from_cache!(diff_file, highlighted_cache[file_path])
- else
- highlight_diff_file!(diff_file)
- highlighted_cache[file_path] = diff_file.diff_lines.map(&:to_hash)
- end
- end
-
- if highlighted_cache_was_empty
- Rails.cache.write(cache_key, highlighted_cache)
- end
-
- diff_files
- end
-
- def cacheable?
- @merge_request.merge_request_diff.present?
- end
-
- def cache_key
- [@merge_request.merge_request_diff, 'highlighted-safe-diff-files', diff_options]
- end
- end
-end