2012-09-17 13:49:57 -04:00
|
|
|
# Controller for viewing a file's blame
|
2013-06-23 12:47:22 -04:00
|
|
|
class Projects::BlobController < Projects::ApplicationController
|
2012-09-20 13:55:14 -04:00
|
|
|
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
|
|
|
|
|
2015-04-16 08:03:37 -04:00
|
|
|
before_action :require_non_empty_project, except: [:new, :create]
|
|
|
|
before_action :authorize_download_code!
|
2015-09-17 01:45:22 -04:00
|
|
|
before_action :authorize_push_code!, only: [:destroy, :create]
|
2015-04-16 08:03:37 -04:00
|
|
|
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]
|
2015-08-11 05:49:09 -04:00
|
|
|
before_action :require_branch_head, only: [:edit, :update]
|
2015-08-11 08:33:31 -04:00
|
|
|
before_action :editor_variables, except: [:show, :preview, :diff]
|
|
|
|
before_action :after_edit_path, 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
|
2015-08-11 08:33:31 -04:00
|
|
|
result = Files::CreateService.new(@project, current_user, @commit_params).execute
|
2015-01-26 18:02:28 -05:00
|
|
|
|
|
|
|
if result[:status] == :success
|
2015-09-17 01:45:22 -04:00
|
|
|
flash[:notice] = "The changes have been successfully committed"
|
2015-09-10 10:18:40 -04:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to namespace_project_blob_path(@project.namespace, @project, File.join(@target_branch, @file_path)) }
|
|
|
|
format.json { render json: { message: "success", filePath: namespace_project_blob_path(@project.namespace, @project, File.join(@target_branch, @file_path)) } }
|
|
|
|
end
|
2015-01-26 18:02:28 -05:00
|
|
|
else
|
|
|
|
flash[:alert] = result[:message]
|
2015-09-10 10:18:40 -04:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :new }
|
2015-09-17 01:45:22 -04:00
|
|
|
format.json { render json: { message: "failed", filePath: namespace_project_blob_path(@project.namespace, @project, @id) } }
|
2015-09-10 10:18:40 -04:00
|
|
|
end
|
2015-01-26 18:02:28 -05:00
|
|
|
end
|
|
|
|
end
|
2013-11-19 09:14:48 -05:00
|
|
|
|
2012-09-17 13:49:57 -04:00
|
|
|
def show
|
2013-11-19 09:14:48 -05:00
|
|
|
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
|
2015-08-11 08:33:31 -04:00
|
|
|
result = Files::UpdateService.new(@project, current_user, @commit_params).execute
|
2015-01-26 18:02:28 -05:00
|
|
|
|
|
|
|
if result[:status] == :success
|
|
|
|
flash[:notice] = "Your changes have been successfully committed"
|
2015-09-10 10:18:40 -04:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to after_edit_path }
|
|
|
|
format.json { render json: { message: "success", filePath: after_edit_path } }
|
|
|
|
end
|
2015-01-26 18:02:28 -05:00
|
|
|
else
|
|
|
|
flash[:alert] = result[:message]
|
2015-09-10 10:18:40 -04:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :edit }
|
|
|
|
format.json { render json: { message: "failed", filePath: namespace_project_new_blob_path(@project.namespace, @project, @id) } }
|
|
|
|
end
|
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
|
|
|
|
|
2013-11-19 09:14:48 -05:00
|
|
|
def destroy
|
2015-08-11 08:33:31 -04:00
|
|
|
result = Files::DeleteService.new(@project, current_user, @commit_params).execute
|
2013-11-19 09:14:48 -05:00
|
|
|
|
|
|
|
if result[:status] == :success
|
2013-12-16 03:57:50 -05:00
|
|
|
flash[:notice] = "Your changes have been successfully committed"
|
2015-08-11 08:33:31 -04:00
|
|
|
redirect_to namespace_project_tree_path(@project.namespace, @project, @target_branch)
|
2013-11-19 09:14:48 -05:00
|
|
|
else
|
2014-09-25 10:43:23 -04:00
|
|
|
flash[:alert] = result[:message]
|
2013-11-19 09:14:48 -05:00
|
|
|
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
|
|
|
|
|
2013-11-19 09:14:48 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def blob
|
|
|
|
@blob ||= @repository.blob_at(@commit.id, @path)
|
|
|
|
|
2014-07-07 09:46:57 -04:00
|
|
|
if @blob
|
|
|
|
@blob
|
|
|
|
else
|
2015-01-26 18:02:28 -05:00
|
|
|
if tree = @repository.tree(@commit.id, @path)
|
|
|
|
if tree.entries.any?
|
2015-01-24 13:02:58 -05:00
|
|
|
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
|
2014-07-07 09:46:57 -04:00
|
|
|
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 after_edit_path
|
|
|
|
@after_edit_path ||=
|
|
|
|
if from_merge_request
|
2015-01-24 13:02:58 -05:00
|
|
|
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-08-11 08:33:31 -04:00
|
|
|
elsif @target_branch.present?
|
|
|
|
namespace_project_blob_path(@project.namespace, @project, File.join(@target_branch, @path))
|
2015-01-26 18:02:28 -05:00
|
|
|
else
|
2015-01-24 13:02:58 -05:00
|
|
|
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
|
2015-08-11 08:33:31 -04:00
|
|
|
|
|
|
|
def editor_variables
|
|
|
|
@current_branch = @ref
|
2015-09-17 01:45:22 -04:00
|
|
|
@target_branch = params[:new_branch].present? ? sanitized_new_branch_name : @ref
|
2015-08-11 08:33:31 -04:00
|
|
|
|
|
|
|
@file_path =
|
|
|
|
if action_name.to_s == 'create'
|
2015-09-10 10:18:40 -04:00
|
|
|
if params[:file].present?
|
|
|
|
params[:file_name] = params[:file].original_filename
|
|
|
|
end
|
2015-08-11 08:33:31 -04:00
|
|
|
File.join(@path, File.basename(params[:file_name]))
|
|
|
|
else
|
|
|
|
@path
|
|
|
|
end
|
|
|
|
|
2015-09-10 10:18:40 -04:00
|
|
|
if params[:file].present?
|
|
|
|
params[:content] = Base64.encode64(params[:file].read)
|
|
|
|
params[:encoding] = 'base64'
|
|
|
|
end
|
|
|
|
|
2015-08-11 08:33:31 -04:00
|
|
|
@commit_params = {
|
|
|
|
file_path: @file_path,
|
|
|
|
current_branch: @current_branch,
|
|
|
|
target_branch: @target_branch,
|
|
|
|
commit_message: params[:commit_message],
|
|
|
|
file_content: params[:content],
|
|
|
|
file_content_encoding: params[:encoding]
|
|
|
|
}
|
|
|
|
end
|
2012-09-17 13:49:57 -04:00
|
|
|
end
|