2018-09-06 05:48:57 -04:00
|
|
|
# frozen_string_literal: true
|
2021-11-01 14:12:42 -04:00
|
|
|
require 'ipynbdiff'
|
2018-09-06 05:48:57 -04:00
|
|
|
|
2019-06-10 04:05:16 -04:00
|
|
|
class BlobPresenter < Gitlab::View::Presenter::Delegated
|
2021-05-11 14:10:36 -04:00
|
|
|
include ApplicationHelper
|
|
|
|
include BlobHelper
|
|
|
|
include DiffHelper
|
|
|
|
include TreeHelper
|
|
|
|
include ChecksCollaboration
|
|
|
|
|
2021-09-21 05:12:21 -04:00
|
|
|
presents ::Blob, as: :blob
|
2018-09-06 05:48:57 -04:00
|
|
|
|
2019-09-01 23:19:32 -04:00
|
|
|
def highlight(to: nil, plain: nil)
|
2019-07-22 01:25:24 -04:00
|
|
|
load_all_blob_data
|
2018-10-04 00:04:13 -04:00
|
|
|
|
2018-09-06 05:48:57 -04:00
|
|
|
Gitlab::Highlight.highlight(
|
|
|
|
blob.path,
|
2021-12-10 13:14:42 -05:00
|
|
|
blob_data(to),
|
|
|
|
language: blob_language,
|
2021-11-01 14:12:42 -04:00
|
|
|
plain: plain
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-05-07 08:10:27 -04:00
|
|
|
def plain_data
|
|
|
|
return if blob.binary?
|
|
|
|
|
|
|
|
highlight(plain: false)
|
|
|
|
end
|
|
|
|
|
2021-12-10 13:14:42 -05:00
|
|
|
def blob_data(to)
|
|
|
|
@_blob_data ||= Gitlab::Diff::CustomDiff.transformed_blob_data(blob) || limited_blob_data(to: to)
|
|
|
|
end
|
|
|
|
|
|
|
|
def blob_language
|
|
|
|
@_blob_language ||= Gitlab::Diff::CustomDiff.transformed_blob_language(blob) || language
|
|
|
|
end
|
|
|
|
|
2021-06-28 17:10:13 -04:00
|
|
|
def raw_plain_data
|
|
|
|
blob.data unless blob.binary?
|
|
|
|
end
|
|
|
|
|
2019-06-04 09:38:18 -04:00
|
|
|
def web_url
|
2021-04-23 08:09:52 -04:00
|
|
|
url_helpers.project_blob_url(project, ref_qualified_path)
|
2019-06-04 09:38:18 -04:00
|
|
|
end
|
2019-07-22 01:25:24 -04:00
|
|
|
|
2020-07-23 11:09:28 -04:00
|
|
|
def web_path
|
2021-04-23 08:09:52 -04:00
|
|
|
url_helpers.project_blob_path(project, ref_qualified_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit_blob_path
|
|
|
|
url_helpers.project_edit_blob_path(project, ref_qualified_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw_path
|
|
|
|
url_helpers.project_raw_path(project, ref_qualified_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def replace_path
|
|
|
|
url_helpers.project_create_blob_path(project, ref_qualified_path)
|
2020-07-23 11:09:28 -04:00
|
|
|
end
|
|
|
|
|
2021-11-12 04:10:03 -05:00
|
|
|
def pipeline_editor_path
|
|
|
|
project_ci_pipeline_editor_path(project, branch_name: blob.commit_id) if can_collaborate_with_project?(project) && blob.path == project.ci_config_path_or_default
|
|
|
|
end
|
|
|
|
|
2021-12-06 13:14:09 -05:00
|
|
|
# Will be overridden in EE
|
|
|
|
def code_owners
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2021-05-11 14:10:36 -04:00
|
|
|
def fork_and_edit_path
|
|
|
|
fork_path_for_current_user(project, edit_blob_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def ide_fork_and_edit_path
|
|
|
|
fork_path_for_current_user(project, ide_edit_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_modify_blob?
|
|
|
|
super(blob, project, blob.commit_id)
|
|
|
|
end
|
|
|
|
|
2021-11-26 07:12:49 -05:00
|
|
|
def can_current_user_push_to_branch?
|
|
|
|
return false unless current_user && project.repository.branch_exists?(blob.commit_id)
|
|
|
|
|
|
|
|
user_access(project).can_push_to_branch?(blob.commit_id)
|
|
|
|
end
|
|
|
|
|
2021-05-11 14:10:36 -04:00
|
|
|
def ide_edit_path
|
|
|
|
super(project, blob.commit_id, blob.path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def external_storage_url
|
|
|
|
return unless static_objects_external_storage_enabled?
|
|
|
|
|
|
|
|
external_storage_url_or_path(url_helpers.project_raw_url(project, ref_qualified_path))
|
|
|
|
end
|
|
|
|
|
2019-07-22 01:25:24 -04:00
|
|
|
private
|
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
def url_helpers
|
|
|
|
Gitlab::Routing.url_helpers
|
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
|
|
|
blob.repository.project
|
|
|
|
end
|
|
|
|
|
|
|
|
def ref_qualified_path
|
|
|
|
File.join(blob.commit_id, blob.path)
|
|
|
|
end
|
|
|
|
|
2019-07-22 01:25:24 -04:00
|
|
|
def load_all_blob_data
|
|
|
|
blob.load_all_data! if blob.respond_to?(:load_all_data!)
|
|
|
|
end
|
2019-09-01 23:19:32 -04:00
|
|
|
|
|
|
|
def limited_blob_data(to: nil)
|
|
|
|
return blob.data if to.blank?
|
|
|
|
|
|
|
|
# Even though we don't need all the lines at the start of the file (e.g
|
|
|
|
# viewing the middle part of a file), they still need to be highlighted
|
|
|
|
# to ensure that the succeeding lines can be formatted correctly (e.g.
|
|
|
|
# multi-line comments).
|
|
|
|
all_lines[0..to - 1].join
|
|
|
|
end
|
|
|
|
|
|
|
|
def all_lines
|
|
|
|
@all_lines ||= blob.data.lines
|
|
|
|
end
|
2020-01-22 07:08:40 -05:00
|
|
|
|
|
|
|
def language
|
|
|
|
blob.language_from_gitattributes
|
|
|
|
end
|
2018-09-06 05:48:57 -04:00
|
|
|
end
|
2021-12-06 13:14:09 -05:00
|
|
|
|
|
|
|
BlobPresenter.prepend_mod_with('BlobPresenter')
|