gitlab-org--gitlab-foss/app/controllers/projects/blob_controller.rb

284 lines
8.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2012-09-17 13:49:57 -04:00
# Controller for viewing a file's blame
class Projects::BlobController < Projects::ApplicationController
include ExtractsPath
include CreatesCommit
include RendersBlob
2018-06-21 08:22:40 -04:00
include NotesHelper
2015-02-14 11:18:05 -05:00
include ActionView::Helpers::SanitizeHelper
2017-04-06 12:36:38 -04:00
prepend_before_action :authenticate_user!, only: [:edit]
before_action :require_non_empty_project, except: [:new, :create]
before_action :authorize_download_code!
# We need to assign the blob vars before `authorize_edit_tree!` so we can
# validate access to a specific ref.
before_action :assign_blob_vars
before_action :authorize_edit_tree!, only: [:new, :create, :update, :destroy]
before_action :commit, except: [:new, :create]
before_action :blob, except: [:new, :create]
before_action :require_branch_head, only: [:edit, :update]
before_action :editor_variables, except: [:show, :preview, :diff]
before_action :validate_diff_params, only: :diff
before_action :set_last_commit_sha, only: [:edit, :update]
2015-01-26 18:02:28 -05:00
def new
commit unless @repository.empty?
end
2012-09-17 13:49:57 -04:00
2015-01-26 18:02:28 -05:00
def create
create_commit(Files::CreateService, success_notice: "The file has been successfully created.",
success_path: -> { project_blob_path(@project, File.join(@branch_name, @file_path)) },
2015-11-18 11:10:06 -05:00
failure_view: :new,
failure_path: project_new_blob_path(@project, @ref))
2015-01-26 18:02:28 -05:00
end
2012-09-17 13:49:57 -04:00
def show
conditionally_expand_blob(@blob)
2017-04-21 14:59:34 -04:00
respond_to do |format|
format.html do
2017-08-04 08:21:02 -04:00
show_html
end
format.json do
page_title @blob.path, @ref, @project.full_name
2017-08-04 08:21:02 -04:00
show_json
end
end
end
2015-01-26 18:02:28 -05:00
def edit
if can_collaborate_with_project?(project, ref: @ref)
blob.load_all_data!
2017-04-06 12:36:38 -04:00
else
redirect_to action: 'show'
end
2015-01-26 18:02:28 -05:00
end
def update
@path = params[:file_path] if params[:file_path].present?
create_commit(Files::UpdateService, success_path: -> { after_edit_path },
2015-11-18 11:10:06 -05:00
failure_view: :edit,
failure_path: project_blob_path(@project, @id))
rescue Files::UpdateService::FileChangedError
@conflict = true
render :edit
2015-01-26 18:02:28 -05:00
end
def preview
@content = params[:content]
@blob.load_all_data!
2015-02-03 00:15:44 -05:00
diffy = Diffy::Diff.new(@blob.data, @content, diff: '-U 3', include_diff_info: true)
diff_lines = diffy.diff.scan(/.*\n/)[2..-1]
diff_lines = Gitlab::Diff::Parser.new.parse(diff_lines)
@diff_lines = Gitlab::Diff::Highlight.new(diff_lines, repository: @repository).highlight
2015-01-26 18:02:28 -05:00
render layout: false
end
def destroy
2017-04-19 20:37:44 -04:00
create_commit(Files::DeleteService, success_notice: "The file has been successfully deleted.",
success_path: -> { after_delete_path },
2017-04-19 20:37:44 -04:00
failure_view: :show,
failure_path: project_blob_path(@project, @id))
end
2014-08-02 11:12:01 -04:00
def diff
2016-07-21 14:44:12 -04:00
apply_diff_view_cookie!
@blob.load_all_data!
@lines = @blob.present.highlight.lines
@form = UnfoldForm.new(params.to_unsafe_h)
2018-06-21 08:22:40 -04:00
@lines = @lines[@form.since - 1..@form.to - 1].map(&:html_safe)
2014-08-02 11:12:01 -04:00
if @form.bottom?
@match_line = ''
else
lines_length = @lines.length - 1
line = [@form.since, lines_length].join(',')
@match_line = "@@ -#{line}+#{line} @@"
end
2018-06-21 08:22:40 -04:00
# We can keep only 'render_diff_lines' from this conditional when
# https://gitlab.com/gitlab-org/gitlab-ce/issues/44988 is done
if rendered_for_merge_request?
render_diff_lines
else
render layout: false
end
2014-08-02 11:12:01 -04:00
end
private
2018-06-21 08:22:40 -04:00
# Converts a String array to Gitlab::Diff::Line array
def render_diff_lines
@lines.map! do |line|
# These are marked as context lines but are loaded from blobs.
# We also have context lines loaded from diffs in other places.
diff_line = Gitlab::Diff::Line.new(line, nil, nil, nil, nil)
2018-06-21 08:22:40 -04:00
diff_line.rich_text = line
diff_line
end
add_match_line
2018-08-16 14:53:33 -04:00
render json: DiffLineSerializer.new.represent(@lines)
2018-06-21 08:22:40 -04:00
end
def add_match_line
return unless @form.unfold?
if @form.bottom? && @form.to < @blob.lines.size
old_pos = @form.to - @form.offset
new_pos = @form.to
elsif @form.since != 1
old_pos = new_pos = @form.since
end
# Match line is not needed when it reaches the top limit or bottom limit of the file.
return unless new_pos
@match_line = Gitlab::Diff::Line.new(@match_line, 'match', nil, old_pos, new_pos)
@form.bottom? ? @lines.push(@match_line) : @lines.unshift(@match_line)
end
def blob
@blob ||= @repository.blob_at(@commit.id, @path)
if @blob
@blob
else
2015-01-26 18:02:28 -05:00
if tree = @repository.tree(@commit.id, @path)
if tree.entries.any?
return redirect_to project_tree_path(@project, File.join(@ref, @path))
2015-01-26 18:02:28 -05:00
end
end
2015-10-09 13:07:29 -04:00
return render_404
end
2012-09-17 13:49:57 -04:00
end
2015-01-26 18:02:28 -05:00
def commit
@commit = @repository.commit(@ref)
2015-10-09 13:07:29 -04:00
return render_404 unless @commit
2015-01-26 18:02:28 -05:00
end
def assign_blob_vars
@id = params[:id]
@ref, @path = extract_ref(@id)
rescue InvalidPathError
2015-10-09 13:07:29 -04:00
render_404
2015-01-26 18:02:28 -05:00
end
# rubocop: disable CodeReuse/ActiveRecord
def after_edit_path
from_merge_request = MergeRequestsFinder.new(current_user, project_id: @project.id).find_by(iid: params[:from_merge_request_iid])
2017-04-19 20:37:44 -04:00
if from_merge_request && @branch_name == @ref
diffs_project_merge_request_path(from_merge_request.target_project, from_merge_request) +
"##{hexdigest(@path)}"
else
project_blob_path(@project, File.join(@branch_name, @path))
end
2015-01-26 18:02:28 -05:00
end
# rubocop: enable CodeReuse/ActiveRecord
2015-02-14 11:18:05 -05: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
def editor_variables
2017-04-19 20:37:44 -04:00
@branch_name = params[:branch_name]
@file_path =
if action_name.to_s == 'create'
if params[:file].present?
params[:file_name] = params[:file].original_filename
end
File.join(@path, params[:file_name])
elsif params[:file_path].present?
params[:file_path]
else
@path
end
if params[:file].present?
params[:content] = Base64.encode64(params[:file].read)
params[:encoding] = 'base64'
end
@commit_params = {
file_path: @file_path,
commit_message: params[:commit_message],
previous_path: @path,
file_content: params[:content],
file_content_encoding: params[:encoding],
last_commit_sha: params[:last_commit_sha]
}
end
def validate_diff_params
if [:since, :to, :offset].any? { |key| params[key].blank? }
head :ok
end
end
def set_last_commit_sha
2017-06-21 09:48:12 -04:00
@last_commit_sha = Gitlab::Git::Commit
.last_for_path(@repository, @ref, @path).sha
end
2017-08-04 08:21:02 -04:00
def show_html
environment_params = @repository.branch_exists?(@ref) ? { ref: @ref } : { commit: @commit }
@environment = EnvironmentsFinder.new(@project, current_user, environment_params).execute.last
@last_commit = @repository.last_commit_for_path(@commit.id, @blob.path)
render 'show'
end
def show_json
set_last_commit_sha
2017-08-15 15:53:41 -04:00
json = {
id: @blob.id,
last_commit_sha: @last_commit_sha,
2017-08-04 08:21:02 -04:00
path: blob.path,
name: blob.name,
extension: blob.extension,
size: blob.raw_size,
mime_type: blob.mime_type,
binary: blob.binary?,
2017-08-04 08:21:02 -04: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 15:53:41 -04:00
tree_path: project_tree_path(project, File.join(@ref, tree_path)),
2017-08-04 08:21:02 -04:00
permalink: project_blob_path(project, File.join(@commit.id, @path))
}
json.merge!(blob_json(@blob) || {}) unless params[:viewer] == 'none'
render json: json
2017-08-04 08:21:02 -04:00
end
def tree_path
@path.rpartition('/').first
end
2012-09-17 13:49:57 -04:00
end