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

164 lines
4.3 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
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_filter :require_non_empty_project, except: [:new, :create]
before_filter :authorize_download_code!
before_filter :authorize_push_code!, only: [:destroy]
2015-01-26 18:02:28 -05:00
before_filter :assign_blob_vars
before_filter :commit, except: [:new, :create]
before_filter :blob, except: [:new, :create]
before_filter :from_merge_request, only: [:edit, :update]
before_filter :after_edit_path, only: [:edit, :update]
before_filter :require_branch_head, only: [:edit, :update]
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
file_path = File.join(@path, File.basename(params[:file_name]))
2015-02-14 11:18:05 -05:00
result = Files::CreateService.new(
@project,
current_user,
params.merge(new_branch: sanitized_new_branch_name),
@ref,
file_path
).execute
2015-01-26 18:02:28 -05:00
if result[:status] == :success
flash[:notice] = "Your changes have been successfully committed"
2015-02-14 11:18:05 -05:00
ref = sanitized_new_branch_name.presence || @ref
redirect_to namespace_project_blob_path(@project.namespace, @project, File.join(ref, file_path))
2015-01-26 18:02:28 -05:00
else
flash[:alert] = result[:message]
render :new
2015-01-26 18:02:28 -05:00
end
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
end
def update
result = Files::UpdateService.
2015-02-14 11:18:05 -05:00
new(
@project,
current_user,
params.merge(new_branch: sanitized_new_branch_name),
@ref,
@path
).execute
2015-01-26 18:02:28 -05:00
if result[:status] == :success
flash[:notice] = "Your changes have been successfully committed"
if from_merge_request
from_merge_request.reload_code
end
redirect_to after_edit_path
else
flash[:alert] = result[:message]
render :edit
2015-01-26 18:02:28 -05:00
end
end
def preview
@content = params[:content]
2015-02-03 00:15:44 -05:00
diffy = Diffy::Diff.new(@blob.data, @content, diff: '-U 3', include_diff_info: true)
2015-01-26 18:02:28 -05:00
@diff_lines = Gitlab::Diff::Parser.new.parse(diffy.diff.scan(/.*\n/))
render layout: false
end
def destroy
result = Files::DeleteService.new(@project, current_user, params, @ref, @path).execute
if result[:status] == :success
2013-12-16 03:57:50 -05:00
flash[:notice] = "Your changes have been successfully committed"
redirect_to namespace_project_tree_path(@project.namespace, @project,
@ref)
else
flash[:alert] = result[:message]
render :show
end
end
2014-08-02 11:12:01 -04:00
def diff
@form = UnfoldForm.new(params)
@lines = @blob.data.lines[@form.since - 1..@form.to - 1]
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
return not_found!
end
2012-09-17 13:49:57 -04:00
end
2015-01-26 18:02:28 -05:00
def commit
@commit = @repository.commit(@ref)
return not_found! unless @commit
end
def assign_blob_vars
@id = params[:id]
@ref, @path = extract_ref(@id)
rescue InvalidPathError
not_found!
end
def after_edit_path
@after_edit_path ||=
if from_merge_request
diffs_namespace_project_merge_request_path(from_merge_request.target_project.namespace, from_merge_request.target_project, from_merge_request) +
2015-01-26 18:02:28 -05:00
"#file-path-#{hexdigest(@path)}"
2015-02-14 11:18:05 -05:00
elsif sanitized_new_branch_name.present?
namespace_project_blob_path(@project.namespace, @project, File.join(sanitized_new_branch_name, @path))
2015-01-26 18:02:28 -05:00
else
namespace_project_blob_path(@project.namespace, @project, @id)
2015-01-26 18:02:28 -05:00
end
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 sanitized_new_branch_name
@new_branch ||= sanitize(strip_tags(params[:new_branch]))
end
2012-09-17 13:49:57 -04:00
end