2014-01-16 13:36:51 -05:00
|
|
|
require_relative "base_service"
|
2013-12-10 07:15:08 -05:00
|
|
|
|
2013-11-05 05:06:52 -05:00
|
|
|
module Files
|
2014-01-16 12:03:42 -05:00
|
|
|
class CreateService < BaseService
|
2013-11-05 05:06:52 -05:00
|
|
|
def execute
|
2015-03-24 09:10:55 -04:00
|
|
|
allowed = Gitlab::GitAccess.new(current_user, project).can_push_to_branch?(ref)
|
2013-11-05 05:06:52 -05:00
|
|
|
|
|
|
|
unless allowed
|
|
|
|
return error("You are not allowed to create file in this branch")
|
|
|
|
end
|
|
|
|
|
2013-11-20 03:03:50 -05:00
|
|
|
file_name = File.basename(path)
|
|
|
|
file_path = path
|
2013-11-05 05:06:52 -05:00
|
|
|
|
2015-03-24 09:55:14 -04:00
|
|
|
unless file_name =~ Gitlab::Regex.file_name_regex
|
2014-06-26 03:53:01 -04:00
|
|
|
return error(
|
|
|
|
'Your changes could not be committed, because the file name ' +
|
2015-03-24 09:55:14 -04:00
|
|
|
Gitlab::Regex.file_name_regex_message
|
2014-06-26 03:53:01 -04:00
|
|
|
)
|
2013-11-05 05:06:52 -05:00
|
|
|
end
|
|
|
|
|
2015-01-26 16:31:20 -05:00
|
|
|
if project.empty_repo?
|
|
|
|
# everything is ok because repo does not have a commits yet
|
|
|
|
else
|
|
|
|
unless repository.branch_names.include?(ref)
|
|
|
|
return error("You can only create files if you are on top of a branch")
|
|
|
|
end
|
2013-11-05 05:06:52 -05:00
|
|
|
|
2015-01-26 16:31:20 -05:00
|
|
|
blob = repository.blob_at_branch(ref, file_path)
|
|
|
|
|
|
|
|
if blob
|
|
|
|
return error("Your changes could not be committed, because file with such name exists")
|
|
|
|
end
|
2013-11-05 05:06:52 -05:00
|
|
|
end
|
|
|
|
|
2015-01-26 16:31:20 -05:00
|
|
|
|
2013-11-11 07:58:39 -05:00
|
|
|
new_file_action = Gitlab::Satellite::NewFileAction.new(current_user, project, ref, file_path)
|
2013-11-05 05:06:52 -05:00
|
|
|
created_successfully = new_file_action.commit!(
|
|
|
|
params[:content],
|
2014-01-16 11:10:35 -05:00
|
|
|
params[:commit_message],
|
2015-02-14 11:18:05 -05:00
|
|
|
params[:encoding],
|
|
|
|
params[:new_branch]
|
2013-11-05 05:06:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
if created_successfully
|
|
|
|
success
|
|
|
|
else
|
2013-12-16 03:57:50 -05:00
|
|
|
error("Your changes could not be committed, because the file has been changed")
|
2013-11-05 05:06:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|