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