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

150 lines
4.6 KiB
Ruby
Raw Normal View History

2012-09-17 13:49:57 -04:00
# Controller for viewing a file's blame
class Projects::BlobController < Projects::ApplicationController
include ExtractsPath
include CreatesCommit
2015-02-14 11:18:05 -05:00
include ActionView::Helpers::SanitizeHelper
2012-09-17 13:49:57 -04:00
2015-01-26 18:02:28 -05:00
# Raised when given an invalid file path
class InvalidPathError < StandardError; end
before_action :require_non_empty_project, except: [:new, :create]
before_action :authorize_download_code!
before_action :authorize_edit_tree!, only: [:new, :create, :edit, :update, :destroy]
before_action :assign_blob_vars
before_action :commit, except: [:new, :create]
before_action :blob, except: [:new, :create]
before_action :from_merge_request, only: [:edit, :update]
before_action :require_branch_head, only: [:edit, :update]
before_action :editor_variables, except: [:show, :preview, :diff]
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: namespace_project_blob_path(@project.namespace, @project, File.join(@target_branch, @file_path)),
2015-11-18 11:10:06 -05:00
failure_view: :new,
failure_path: namespace_project_new_blob_path(@project.namespace, @project, @ref))
2015-01-26 18:02:28 -05:00
end
2012-09-17 13:49:57 -04:00
def show
end
2015-01-26 18:02:28 -05:00
def edit
@last_commit = Gitlab::Git::Commit.last_for_path(@repository, @ref, @path).sha
2016-02-01 04:41:52 -05:00
blob.load_all_data!(@repository)
2015-01-26 18:02:28 -05:00
end
def update
after_edit_path =
if from_merge_request && @target_branch == @ref
diffs_namespace_project_merge_request_path(from_merge_request.target_project.namespace, from_merge_request.target_project, from_merge_request) +
"#file-path-#{hexdigest(@path)}"
else
namespace_project_blob_path(@project.namespace, @project, File.join(@target_branch, @path))
end
2015-11-18 11:10:06 -05:00
create_commit(Files::UpdateService, success_path: after_edit_path,
failure_view: :edit,
failure_path: namespace_project_blob_path(@project.namespace, @project, @id))
2015-01-26 18:02:28 -05:00
end
def preview
@content = params[:content]
2016-02-01 04:41:52 -05:00
@blob.load_all_data!(@repository)
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).highlight
2015-01-26 18:02:28 -05:00
render layout: false
end
def destroy
create_commit(Files::DeleteService, success_notice: "The file has been successfully deleted.",
success_path: namespace_project_tree_path(@project.namespace, @project, @target_branch),
failure_view: :show,
failure_path: namespace_project_blob_path(@project.namespace, @project, @id))
end
2014-08-02 11:12:01 -04:00
def diff
@form = UnfoldForm.new(params)
@lines = Gitlab::Highlight.highlight_lines(repository, @ref, @path)
@lines = @lines[@form.since - 1..@form.to - 1]
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
render layout: false
end
private
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?
redirect_to namespace_project_tree_path(@project.namespace, @project, File.join(@ref, @path)) and return
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
def from_merge_request
# If blob edit was initiated from merge request page
@from_merge_request ||= MergeRequest.find_by(id: params[:from_merge_request_id])
end
2015-02-14 11:18:05 -05:00
def editor_variables
@target_branch = params[:target_branch]
@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])
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],
file_content: params[:content],
file_content_encoding: params[:encoding]
}
end
2012-09-17 13:49:57 -04:00
end