debian-mirror-gitlab/app/controllers/projects/blob_controller.rb

260 lines
7.6 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
# Controller for viewing a file's blame
class Projects::BlobController < Projects::ApplicationController
include ExtractsPath
include CreatesCommit
2017-08-17 22:00:37 +05:30
include RendersBlob
2018-11-08 19:23:39 +05:30
include NotesHelper
2015-04-26 12:48:37 +05:30
include ActionView::Helpers::SanitizeHelper
2019-12-26 22:10:19 +05:30
include RedirectsForMissingPathOnTree
2020-03-13 15:44:24 +05:30
include SourcegraphDecorator
2020-07-28 23:09:34 +05:30
include DiffHelper
2019-12-26 22:10:19 +05:30
2017-08-17 22:00:37 +05:30
prepend_before_action :authenticate_user!, only: [:edit]
2014-09-02 18:07:02 +05:30
2019-07-07 11:18:12 +05:30
around_action :allow_gitaly_ref_name_caching, only: [:show]
2015-09-11 14:41:01 +05:30
before_action :require_non_empty_project, except: [:new, :create]
before_action :authorize_download_code!
2018-03-27 19:54:05 +05:30
# We need to assign the blob vars before `authorize_edit_tree!` so we can
# validate access to a specific ref.
2015-09-11 14:41:01 +05:30
before_action :assign_blob_vars
2018-03-27 19:54:05 +05:30
before_action :authorize_edit_tree!, only: [:new, :create, :update, :destroy]
2015-09-11 14:41:01 +05:30
before_action :commit, except: [:new, :create]
before_action :blob, except: [:new, :create]
before_action :require_branch_head, only: [:edit, :update]
2015-09-25 12:07:36 +05:30
before_action :editor_variables, except: [:show, :preview, :diff]
2016-08-24 12:49:21 +05:30
before_action :validate_diff_params, only: :diff
2016-09-13 17:45:13 +05:30
before_action :set_last_commit_sha, only: [:edit, :update]
2015-04-26 12:48:37 +05:30
2020-03-13 15:44:24 +05:30
before_action only: :show do
2020-07-10 23:44:40 +05:30
push_frontend_feature_flag(:code_navigation, @project, default_enabled: true)
2020-04-08 14:13:33 +05:30
push_frontend_feature_flag(:suggest_pipeline) if experiment_enabled?(:suggest_pipeline)
2020-03-13 15:44:24 +05:30
end
2015-04-26 12:48:37 +05:30
def new
commit unless @repository.empty?
end
def create
2019-07-07 11:18:12 +05:30
create_commit(Files::CreateService, success_notice: _("The file has been successfully created."),
2017-09-10 17:25:29 +05:30
success_path: -> { project_blob_path(@project, File.join(@branch_name, @file_path)) },
2015-11-26 14:37:03 +05:30
failure_view: :new,
2017-09-10 17:25:29 +05:30
failure_path: project_new_blob_path(@project, @ref))
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
def show
2017-09-10 17:25:29 +05:30
conditionally_expand_blob(@blob)
2017-08-17 22:00:37 +05:30
respond_to do |format|
format.html do
2017-09-10 17:25:29 +05:30
show_html
2017-08-17 22:00:37 +05:30
end
format.json do
2018-03-27 19:54:05 +05:30
page_title @blob.path, @ref, @project.full_name
2018-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
show_json
2017-08-17 22:00:37 +05:30
end
end
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
def edit
2018-03-27 19:54:05 +05:30
if can_collaborate_with_project?(project, ref: @ref)
2017-09-10 17:25:29 +05:30
blob.load_all_data!
2017-08-17 22:00:37 +05:30
else
redirect_to action: 'show'
end
2015-04-26 12:48:37 +05:30
end
def update
2016-09-29 09:46:39 +05:30
@path = params[:file_path] if params[:file_path].present?
2020-06-23 00:09:42 +05:30
2017-08-17 22:00:37 +05:30
create_commit(Files::UpdateService, success_path: -> { after_edit_path },
2015-11-26 14:37:03 +05:30
failure_view: :edit,
2017-09-10 17:25:29 +05:30
failure_path: project_blob_path(@project, @id))
2016-09-13 17:45:13 +05:30
rescue Files::UpdateService::FileChangedError
@conflict = true
render :edit
2015-04-26 12:48:37 +05:30
end
def preview
@content = params[:content]
2017-09-10 17:25:29 +05:30
@blob.load_all_data!
2015-04-26 12:48:37 +05:30
diffy = Diffy::Diff.new(@blob.data, @content, diff: '-U 3', include_diff_info: true)
2016-01-29 22:53:50 +05:30
diff_lines = diffy.diff.scan(/.*\n/)[2..-1]
diff_lines = Gitlab::Diff::Parser.new.parse(diff_lines)
2016-08-24 12:49:21 +05:30
@diff_lines = Gitlab::Diff::Highlight.new(diff_lines, repository: @repository).highlight
2015-04-26 12:48:37 +05:30
render layout: false
end
2014-09-02 18:07:02 +05:30
def destroy
2019-07-07 11:18:12 +05:30
create_commit(Files::DeleteService, success_notice: _("The file has been successfully deleted."),
2018-12-13 13:39:08 +05:30
success_path: -> { after_delete_path },
2017-09-10 17:25:29 +05:30
failure_path: project_blob_path(@project, @id))
2014-09-02 18:07:02 +05:30
end
def diff
2016-09-13 17:45:13 +05:30
apply_diff_view_cookie!
2019-12-04 20:38:33 +05:30
@form = Blobs::UnfoldPresenter.new(blob, diff_params)
2014-09-02 18:07:02 +05:30
2019-07-07 11:18:12 +05:30
# keep only json rendering when
2019-12-04 20:38:33 +05:30
# https://gitlab.com/gitlab-org/gitlab-foss/issues/44988 is done
2018-11-08 19:23:39 +05:30
if rendered_for_merge_request?
2019-07-07 11:18:12 +05:30
render json: DiffLineSerializer.new.represent(@form.diff_lines)
2018-11-08 19:23:39 +05:30
else
2019-07-07 11:18:12 +05:30
@lines = @form.lines
@match_line = @form.match_line_text
2018-11-08 19:23:39 +05:30
render layout: false
end
2014-09-02 18:07:02 +05:30
end
private
2020-06-23 00:09:42 +05:30
attr_reader :branch_name
2014-09-02 18:07:02 +05:30
def blob
2017-09-10 17:25:29 +05:30
@blob ||= @repository.blob_at(@commit.id, @path)
2014-09-02 18:07:02 +05:30
if @blob
@blob
else
2015-04-26 12:48:37 +05:30
if tree = @repository.tree(@commit.id, @path)
if tree.entries.any?
2017-09-10 17:25:29 +05:30
return redirect_to project_tree_path(@project, File.join(@ref, @path))
2015-04-26 12:48:37 +05:30
end
end
2020-07-28 23:09:34 +05:30
redirect_to_tree_root_for_missing_path(@project, @ref, @path)
2014-09-02 18:07:02 +05:30
end
end
2015-04-26 12:48:37 +05:30
def commit
@commit = @repository.commit(@ref)
2015-10-24 18:46:33 +05:30
return render_404 unless @commit
2015-04-26 12:48:37 +05:30
end
def assign_blob_vars
@id = params[:id]
@ref, @path = extract_ref(@id)
rescue InvalidPathError
2015-10-24 18:46:33 +05:30
render_404
2015-04-26 12:48:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-01-15 13:20:01 +05:30
def after_edit_path
2018-03-27 19:54:05 +05:30
from_merge_request = MergeRequestsFinder.new(current_user, project_id: @project.id).find_by(iid: params[:from_merge_request_iid])
2017-08-17 22:00:37 +05:30
if from_merge_request && @branch_name == @ref
2017-09-10 17:25:29 +05:30
diffs_project_merge_request_path(from_merge_request.target_project, from_merge_request) +
2017-08-17 22:00:37 +05:30
"##{hexdigest(@path)}"
2017-01-15 13:20:01 +05:30
else
2017-09-10 17:25:29 +05:30
project_blob_path(@project, File.join(@branch_name, @path))
2017-01-15 13:20:01 +05:30
end
2015-04-26 12:48:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2015-04-26 12:48:37 +05:30
2018-12-13 13:39:08 +05:30
def after_delete_path
branch = BranchesFinder.new(@repository, search: @ref).execute.first
if @repository.tree(branch.target, tree_path).entries.empty?
project_tree_path(@project, @ref)
else
project_tree_path(@project, File.join(@ref, tree_path))
end
end
2015-09-25 12:07:36 +05:30
def editor_variables
2017-08-17 22:00:37 +05:30
@branch_name = params[:branch_name]
2015-09-25 12:07:36 +05:30
@file_path =
if action_name.to_s == 'create'
if params[:file].present?
params[:file_name] = params[:file].original_filename
end
2018-03-17 18:26:18 +05:30
2015-11-26 14:37:03 +05:30
File.join(@path, params[:file_name])
2016-09-29 09:46:39 +05:30
elsif params[:file_path].present?
params[:file_path]
2015-09-25 12:07:36 +05:30
else
@path
end
if params[:file].present?
2019-07-07 11:18:12 +05:30
params[:content] = params[:file]
2015-09-25 12:07:36 +05:30
end
@commit_params = {
file_path: @file_path,
commit_message: params[:commit_message],
2016-09-29 09:46:39 +05:30
previous_path: @path,
2015-09-25 12:07:36 +05:30
file_content: params[:content],
2016-09-13 17:45:13 +05:30
file_content_encoding: params[:encoding],
last_commit_sha: params[:last_commit_sha]
2015-09-25 12:07:36 +05:30
}
end
2016-08-24 12:49:21 +05:30
def validate_diff_params
2019-07-07 11:18:12 +05:30
return if params[:full]
2016-08-24 12:49:21 +05:30
if [:since, :to, :offset].any? { |key| params[key].blank? }
2019-02-15 15:39:39 +05:30
head :ok
2016-08-24 12:49:21 +05:30
end
end
2016-09-13 17:45:13 +05:30
def set_last_commit_sha
2017-09-10 17:25:29 +05:30
@last_commit_sha = Gitlab::Git::Commit
2020-07-28 23:09:34 +05:30
.last_for_path(@repository, @ref, @path, literal_pathspec: true).sha
2017-09-10 17:25:29 +05:30
end
def show_html
environment_params = @repository.branch_exists?(@ref) ? { ref: @ref } : { commit: @commit }
2020-03-13 15:44:24 +05:30
environment_params[:find_latest] = true
2017-09-10 17:25:29 +05:30
@environment = EnvironmentsFinder.new(@project, current_user, environment_params).execute.last
2020-07-28 23:09:34 +05:30
@last_commit = @repository.last_commit_for_path(@commit.id, @blob.path, literal_pathspec: true)
2020-04-22 19:07:51 +05:30
@code_navigation_path = Gitlab::CodeNavigationPath.new(@project, @blob.commit_id).full_json_path_for(@blob.path)
2017-09-10 17:25:29 +05:30
render 'show'
end
def show_json
2018-11-08 19:23:39 +05:30
set_last_commit_sha
2017-09-10 17:25:29 +05:30
2018-11-08 19:23:39 +05:30
json = {
2018-03-17 18:26:18 +05:30
id: @blob.id,
2018-11-08 19:23:39 +05:30
last_commit_sha: @last_commit_sha,
2017-09-10 17:25:29 +05:30
path: blob.path,
name: blob.name,
extension: blob.extension,
size: blob.raw_size,
mime_type: blob.mime_type,
2019-02-15 15:39:39 +05:30
binary: blob.binary?,
2017-09-10 17:25:29 +05:30
simple_viewer: blob.simple_viewer&.class&.partial_name,
rich_viewer: blob.rich_viewer&.class&.partial_name,
show_viewer_switcher: !!blob.show_viewer_switcher?,
render_error: blob.simple_viewer&.render_error || blob.rich_viewer&.render_error,
raw_path: project_raw_path(project, @id),
blame_path: project_blame_path(project, @id),
commits_path: project_commits_path(project, @id),
tree_path: project_tree_path(project, File.join(@ref, tree_path)),
permalink: project_blob_path(project, File.join(@commit.id, @path))
2018-11-08 19:23:39 +05:30
}
json.merge!(blob_json(@blob) || {}) unless params[:viewer] == 'none'
render json: json
2016-09-13 17:45:13 +05:30
end
2018-12-13 13:39:08 +05:30
def tree_path
@path.rpartition('/').first
end
2019-12-04 20:38:33 +05:30
def diff_params
params.permit(:full, :since, :to, :bottom, :unfold, :offset, :indent)
end
2014-09-02 18:07:02 +05:30
end