2014-09-02 18:07:02 +05:30
|
|
|
# Controller for viewing a file's blame
|
|
|
|
class Projects::BlobController < Projects::ApplicationController
|
|
|
|
include ExtractsPath
|
2016-01-14 18:37:52 +05:30
|
|
|
include CreatesCommit
|
2017-08-17 22:00:37 +05:30
|
|
|
include RendersBlob
|
2015-04-26 12:48:37 +05:30
|
|
|
include ActionView::Helpers::SanitizeHelper
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
# Raised when given an invalid file path
|
2017-08-17 22:00:37 +05:30
|
|
|
InvalidPathError = Class.new(StandardError)
|
|
|
|
|
|
|
|
prepend_before_action :authenticate_user!, only: [:edit]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :require_non_empty_project, except: [:new, :create]
|
|
|
|
before_action :authorize_download_code!
|
2017-08-17 22:00:37 +05:30
|
|
|
before_action :authorize_edit_tree!, only: [:new, :create, :update, :destroy]
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :assign_blob_vars
|
|
|
|
before_action :commit, except: [:new, :create]
|
|
|
|
before_action :blob, except: [:new, :create]
|
|
|
|
before_action :require_branch_head, only: [:edit, :update]
|
2015-09-25 12:07:36 +05:30
|
|
|
before_action :editor_variables, except: [:show, :preview, :diff]
|
2016-08-24 12:49:21 +05:30
|
|
|
before_action :validate_diff_params, only: :diff
|
2016-09-13 17:45:13 +05:30
|
|
|
before_action :set_last_commit_sha, only: [:edit, :update]
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
def new
|
|
|
|
commit unless @repository.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2017-08-17 22:00:37 +05:30
|
|
|
set_start_branch_to_branch_name
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
create_commit(Files::CreateService, success_notice: "The file has been successfully created.",
|
2017-08-17 22:00:37 +05:30
|
|
|
success_path: -> { namespace_project_blob_path(@project.namespace, @project, File.join(@branch_name, @file_path)) },
|
2015-11-26 14:37:03 +05:30
|
|
|
failure_view: :new,
|
|
|
|
failure_path: namespace_project_new_blob_path(@project.namespace, @project, @ref))
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
def show
|
2017-08-17 22:00:37 +05:30
|
|
|
override_max_blob_size(@blob)
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
environment_params = @repository.branch_exists?(@ref) ? { ref: @ref } : { commit: @commit }
|
|
|
|
@environment = EnvironmentsFinder.new(@project, current_user, environment_params).execute.last
|
|
|
|
|
|
|
|
render 'show'
|
|
|
|
end
|
|
|
|
|
|
|
|
format.json do
|
|
|
|
render_blob_json(@blob)
|
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def edit
|
2017-08-17 22:00:37 +05:30
|
|
|
if can_collaborate_with_project?
|
|
|
|
blob.load_all_data!(@repository)
|
|
|
|
else
|
|
|
|
redirect_to action: 'show'
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2016-09-29 09:46:39 +05:30
|
|
|
@path = params[:file_path] if params[:file_path].present?
|
2017-08-17 22:00:37 +05:30
|
|
|
create_commit(Files::UpdateService, success_path: -> { after_edit_path },
|
2015-11-26 14:37:03 +05:30
|
|
|
failure_view: :edit,
|
|
|
|
failure_path: namespace_project_blob_path(@project.namespace, @project, @id))
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
rescue Files::UpdateService::FileChangedError
|
|
|
|
@conflict = true
|
|
|
|
render :edit
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def preview
|
|
|
|
@content = params[:content]
|
2016-04-02 18:10:28 +05:30
|
|
|
@blob.load_all_data!(@repository)
|
2015-04-26 12:48:37 +05:30
|
|
|
diffy = Diffy::Diff.new(@blob.data, @content, diff: '-U 3', include_diff_info: true)
|
2016-01-29 22:53:50 +05:30
|
|
|
diff_lines = diffy.diff.scan(/.*\n/)[2..-1]
|
|
|
|
diff_lines = Gitlab::Diff::Parser.new.parse(diff_lines)
|
2016-08-24 12:49:21 +05:30
|
|
|
@diff_lines = Gitlab::Diff::Highlight.new(diff_lines, repository: @repository).highlight
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
render layout: false
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def destroy
|
2016-01-14 18:37:52 +05:30
|
|
|
create_commit(Files::DeleteService, success_notice: "The file has been successfully deleted.",
|
2017-08-17 22:00:37 +05:30
|
|
|
success_path: -> { namespace_project_tree_path(@project.namespace, @project, @branch_name) },
|
2016-01-14 18:37:52 +05:30
|
|
|
failure_view: :show,
|
|
|
|
failure_path: namespace_project_blob_path(@project.namespace, @project, @id))
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def diff
|
2016-09-13 17:45:13 +05:30
|
|
|
apply_diff_view_cookie!
|
|
|
|
|
2016-01-29 22:53:50 +05:30
|
|
|
@form = UnfoldForm.new(params)
|
|
|
|
@lines = Gitlab::Highlight.highlight_lines(repository, @ref, @path)
|
|
|
|
@lines = @lines[@form.since - 1..@form.to - 1]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
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
|
2017-08-17 22:00:37 +05:30
|
|
|
@blob ||= Blob.decorate(@repository.blob_at(@commit.id, @path), @project)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
if @blob
|
|
|
|
@blob
|
|
|
|
else
|
2015-04-26 12:48:37 +05:30
|
|
|
if tree = @repository.tree(@commit.id, @path)
|
|
|
|
if tree.entries.any?
|
2017-08-17 22:00:37 +05:30
|
|
|
return redirect_to namespace_project_tree_path(@project.namespace, @project, File.join(@ref, @path))
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
return render_404
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
def commit
|
|
|
|
@commit = @repository.commit(@ref)
|
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
return render_404 unless @commit
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def assign_blob_vars
|
|
|
|
@id = params[:id]
|
|
|
|
@ref, @path = extract_ref(@id)
|
|
|
|
|
|
|
|
rescue InvalidPathError
|
2015-10-24 18:46:33 +05:30
|
|
|
render_404
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2017-01-15 13:20:01 +05:30
|
|
|
def after_edit_path
|
|
|
|
from_merge_request = MergeRequestsFinder.new(current_user, project_id: @project.id).execute.find_by(iid: params[:from_merge_request_iid])
|
2017-08-17 22:00:37 +05:30
|
|
|
if from_merge_request && @branch_name == @ref
|
2017-01-15 13:20:01 +05:30
|
|
|
diffs_namespace_project_merge_request_path(from_merge_request.target_project.namespace, from_merge_request.target_project, from_merge_request) +
|
2017-08-17 22:00:37 +05:30
|
|
|
"##{hexdigest(@path)}"
|
2017-01-15 13:20:01 +05:30
|
|
|
else
|
2017-08-17 22:00:37 +05:30
|
|
|
namespace_project_blob_path(@project.namespace, @project, File.join(@branch_name, @path))
|
2017-01-15 13:20:01 +05:30
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
def editor_variables
|
2017-08-17 22:00:37 +05:30
|
|
|
@branch_name = params[:branch_name]
|
2015-09-25 12:07:36 +05:30
|
|
|
|
|
|
|
@file_path =
|
|
|
|
if action_name.to_s == 'create'
|
|
|
|
if params[:file].present?
|
|
|
|
params[:file_name] = params[:file].original_filename
|
|
|
|
end
|
2015-11-26 14:37:03 +05:30
|
|
|
File.join(@path, params[:file_name])
|
2016-09-29 09:46:39 +05:30
|
|
|
elsif params[:file_path].present?
|
|
|
|
params[:file_path]
|
2015-09-25 12:07:36 +05:30
|
|
|
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],
|
2016-09-29 09:46:39 +05:30
|
|
|
previous_path: @path,
|
2015-09-25 12:07:36 +05:30
|
|
|
file_content: params[:content],
|
2016-09-13 17:45:13 +05:30
|
|
|
file_content_encoding: params[:encoding],
|
|
|
|
last_commit_sha: params[:last_commit_sha]
|
2015-09-25 12:07:36 +05:30
|
|
|
}
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
|
|
|
def validate_diff_params
|
|
|
|
if [:since, :to, :offset].any? { |key| params[key].blank? }
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
def set_last_commit_sha
|
|
|
|
@last_commit_sha = Gitlab::Git::Commit.
|
|
|
|
last_for_path(@repository, @ref, @path).sha
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|