2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
class BlobPresenter < Gitlab::View::Presenter::Delegated
|
2021-06-08 01:23:25 +05:30
|
|
|
include ApplicationHelper
|
|
|
|
include BlobHelper
|
|
|
|
include DiffHelper
|
|
|
|
include TreeHelper
|
|
|
|
include ChecksCollaboration
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
presents ::Blob, as: :blob
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def highlight(to: nil, plain: nil)
|
2019-10-12 21:52:04 +05:30
|
|
|
load_all_blob_data
|
2018-12-13 13:39:08 +05:30
|
|
|
|
|
|
|
Gitlab::Highlight.highlight(
|
|
|
|
blob.path,
|
2022-01-26 12:08:38 +05:30
|
|
|
blob_data(to),
|
|
|
|
language: blob_language,
|
2021-12-11 22:18:48 +05:30
|
|
|
plain: plain
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
def plain_data
|
|
|
|
return if blob.binary?
|
|
|
|
|
|
|
|
highlight(plain: false)
|
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
def blob_data(to)
|
|
|
|
@_blob_data ||= Gitlab::Diff::CustomDiff.transformed_blob_data(blob) || limited_blob_data(to: to)
|
|
|
|
end
|
|
|
|
|
|
|
|
def blob_language
|
2022-04-04 11:22:00 +05:30
|
|
|
@_blob_language ||= Gitlab::Diff::CustomDiff.transformed_blob_language(blob) || gitattr_language || detect_language
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
def raw_plain_data
|
|
|
|
blob.data unless blob.binary?
|
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
def web_url
|
2021-06-08 01:23:25 +05:30
|
|
|
url_helpers.project_blob_url(project, ref_qualified_path)
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
def web_path
|
2021-06-08 01:23:25 +05:30
|
|
|
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
|
2022-05-07 20:08:51 +05:30
|
|
|
url_helpers.project_update_blob_path(project, ref_qualified_path)
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
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
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
def gitpod_blob_url
|
|
|
|
return unless Gitlab::CurrentSettings.gitpod_enabled && !current_user.nil? && current_user.gitpod_enabled
|
|
|
|
|
|
|
|
"#{Gitlab::CurrentSettings.gitpod_url}##{url_helpers.project_tree_url(project, tree_join(blob.commit_id, blob.path || ''))}"
|
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
def find_file_path
|
|
|
|
url_helpers.project_find_file_path(project, ref_qualified_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def blame_path
|
|
|
|
url_helpers.project_blame_path(project, ref_qualified_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def history_path
|
|
|
|
url_helpers.project_commits_path(project, ref_qualified_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def permalink_path
|
|
|
|
url_helpers.project_blob_path(project, File.join(project.repository.commit.sha, blob.path))
|
|
|
|
end
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
def environment_formatted_external_url
|
|
|
|
return unless environment
|
|
|
|
|
|
|
|
environment.formatted_external_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def environment_external_url_for_route_map
|
|
|
|
return unless environment
|
|
|
|
|
|
|
|
environment.external_url_for(blob.path, blob.commit_id)
|
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
# Will be overridden in EE
|
|
|
|
def code_owners
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
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
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
def fork_and_view_path
|
|
|
|
fork_path_for_current_user(project, web_path)
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
def can_modify_blob?
|
|
|
|
super(blob, project, blob.commit_id)
|
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
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
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
def archived?
|
|
|
|
project.archived
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
def ide_edit_path
|
|
|
|
super(project, blob.commit_id, blob.path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def external_storage_url
|
|
|
|
return unless static_objects_external_storage_enabled?
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
external_storage_url_or_path(url_helpers.project_raw_url(project, ref_qualified_path), project)
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
def code_navigation_path
|
|
|
|
Gitlab::CodeNavigationPath.new(project, blob.commit_id).full_json_path_for(blob.path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_blob_path_root
|
|
|
|
project_blob_path(project, blob.commit_id)
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
private
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
def url_helpers
|
|
|
|
Gitlab::Routing.url_helpers
|
|
|
|
end
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
def environment
|
|
|
|
environment_params = project.repository.branch_exists?(blob.commit_id) ? { ref: blob.commit_id } : { sha: blob.commit_id }
|
|
|
|
environment_params[:find_latest] = true
|
|
|
|
::Environments::EnvironmentsByDeploymentsFinder.new(project, current_user, environment_params).execute.last
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
def project
|
|
|
|
blob.repository.project
|
|
|
|
end
|
|
|
|
|
|
|
|
def ref_qualified_path
|
|
|
|
File.join(blob.commit_id, blob.path)
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def load_all_blob_data
|
|
|
|
blob.load_all_data! if blob.respond_to?(:load_all_data!)
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
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-03-13 15:44:24 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
def gitattr_language
|
2020-03-13 15:44:24 +05:30
|
|
|
blob.language_from_gitattributes
|
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
def detect_language
|
|
|
|
return if blob.binary?
|
|
|
|
|
|
|
|
Rouge::Lexer.guess(filename: blob.path, source: blob_data(nil)) { |lex| lex.min_by(&:tag) }.tag
|
|
|
|
end
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
BlobPresenter.prepend_mod_with('BlobPresenter')
|