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-12-18 04:03:34 -05:00
|
|
|
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
|
|
|
|
|
2015-04-16 08:03:37 -04:00
|
|
|
before_action :require_non_empty_project, except: [:new, :create]
|
|
|
|
before_action :authorize_download_code!
|
2015-12-18 04:03:34 -05:00
|
|
|
before_action :authorize_edit_tree!, only: [:new, :create, :edit, :update, :destroy]
|
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]
|
2016-06-24 17:20:53 -04:00
|
|
|
before_action :validate_diff_params, only: :diff
|
2016-08-07 23:29:23 -04:00
|
|
|
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
|
2015-12-18 04:03:34 -05:00
|
|
|
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
|
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
|
2016-02-01 04:41:52 -05:00
|
|
|
blob.load_all_data!(@repository)
|
2015-01-26 18:02:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2016-08-24 09:30:39 -04:00
|
|
|
@path = params[:file_path] if params[:file_path].present?
|
2015-12-18 04:03:34 -05:00
|
|
|
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) +
|
2016-07-12 06:49:21 -04:00
|
|
|
"#file-path-#{hexdigest(@path)}"
|
2015-12-18 04:03:34 -05:00
|
|
|
else
|
2016-07-12 06:49:21 -04:00
|
|
|
namespace_project_blob_path(@project.namespace, @project, File.join(@target_branch, @path))
|
2015-12-18 04:03:34 -05:00
|
|
|
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))
|
2016-08-07 23:29:23 -04:00
|
|
|
|
|
|
|
rescue Files::UpdateService::FileChangedError
|
|
|
|
@conflict = true
|
|
|
|
render :edit
|
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)
|
2016-01-21 19:43:06 -05:00
|
|
|
diff_lines = diffy.diff.scan(/.*\n/)[2..-1]
|
|
|
|
diff_lines = Gitlab::Diff::Parser.new.parse(diff_lines)
|
2016-06-20 12:51:48 -04:00
|
|
|
@diff_lines = Gitlab::Diff::Highlight.new(diff_lines, repository: @repository).highlight
|
2015-01-26 18:02:28 -05:00
|
|
|
|
|
|
|
render layout: false
|
|
|
|
end
|
|
|
|
|
2013-11-19 09:14:48 -05:00
|
|
|
def destroy
|
2015-12-18 04:03:34 -05:00
|
|
|
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))
|
2013-11-19 09:14:48 -05:00
|
|
|
end
|
|
|
|
|
2014-08-02 11:12:01 -04:00
|
|
|
def diff
|
2016-07-21 14:44:12 -04:00
|
|
|
apply_diff_view_cookie!
|
|
|
|
|
2016-01-07 22:37:01 -05:00
|
|
|
@form = UnfoldForm.new(params)
|
2016-01-14 16:28:07 -05:00
|
|
|
@lines = Gitlab::Highlight.highlight_lines(repository, @ref, @path)
|
2016-01-07 22:37:01 -05:00
|
|
|
@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
|
|
|
|
|
2013-11-19 09:14:48 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def blob
|
2016-02-18 18:19:43 -05:00
|
|
|
@blob ||= Blob.decorate(@repository.blob_at(@commit.id, @path))
|
2013-11-19 09:14:48 -05:00
|
|
|
|
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 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
|
|
|
|
2015-08-11 08:33:31 -04:00
|
|
|
def editor_variables
|
2015-12-18 04:03:34 -05:00
|
|
|
@target_branch = params[:target_branch]
|
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-10-19 17:52:46 -04:00
|
|
|
File.join(@path, params[:file_name])
|
2016-08-24 09:30:39 -04:00
|
|
|
elsif params[:file_path].present?
|
|
|
|
params[:file_path]
|
2015-08-11 08:33:31 -04:00
|
|
|
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,
|
|
|
|
commit_message: params[:commit_message],
|
2016-08-24 09:30:39 -04:00
|
|
|
previous_path: @path,
|
2015-08-11 08:33:31 -04:00
|
|
|
file_content: params[:content],
|
2016-08-07 23:29:23 -04:00
|
|
|
file_content_encoding: params[:encoding],
|
|
|
|
last_commit_sha: params[:last_commit_sha]
|
2015-08-11 08:33:31 -04:00
|
|
|
}
|
|
|
|
end
|
2016-06-24 17:20:53 -04:00
|
|
|
|
|
|
|
def validate_diff_params
|
|
|
|
if [:since, :to, :offset].any? { |key| params[key].blank? }
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
end
|
2016-08-07 23:29:23 -04:00
|
|
|
|
|
|
|
def set_last_commit_sha
|
|
|
|
@last_commit_sha = Gitlab::Git::Commit.
|
|
|
|
last_for_path(@repository, @ref, @path).sha
|
|
|
|
end
|
2012-09-17 13:49:57 -04:00
|
|
|
end
|