diff options
Diffstat (limited to 'lib/gitlab/diff/file.rb')
-rw-r--r-- | lib/gitlab/diff/file.rb | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/gitlab/diff/file.rb b/lib/gitlab/diff/file.rb index 1e6d80e1100..83f242ff902 100644 --- a/lib/gitlab/diff/file.rb +++ b/lib/gitlab/diff/file.rb @@ -43,6 +43,8 @@ module Gitlab # Ensure items are collected in the the batch new_blob_lazy old_blob_lazy + + preprocess_before_diff(diff) if Feature.enabled?(:jupyter_clean_diffs, repository.project, default_enabled: true) end def position(position_marker, position_type: :text) @@ -448,6 +450,33 @@ module Gitlab find_renderable_viewer_class(classes) end + def preprocess_before_diff(diff) + return unless diff.new_path.ends_with? '.ipynb' + + from = old_blob_lazy&.data + to = new_blob_lazy&.data + + transformed_diff = IpynbDiff.diff(from, to, + diff_opts: { context: 5, include_diff_info: true }, + transform_options: { cell_decorator: :percent }, + raise_if_invalid_notebook: true) + new_diff = strip_diff_frontmatter(transformed_diff) + + if new_diff + diff.diff = new_diff + new_blob_lazy.transformed_for_diff = true if new_blob_lazy + old_blob_lazy.transformed_for_diff = true if old_blob_lazy + end + + Gitlab::AppLogger.info({ message: new_diff ? 'IPYNB_DIFF_GENERATED' : 'IPYNB_DIFF_NIL' }) + rescue IpynbDiff::InvalidNotebookError => e + Gitlab::ErrorTracking.log_exception(e) + end + + def strip_diff_frontmatter(diff_content) + diff_content.scan(/.*\n/)[2..-1]&.join('') if diff_content.present? + end + def alternate_viewer_class return unless viewer.instance_of?(DiffViewer::Renamed) |