2013-11-05 10:06:52 +00:00
|
|
|
module Files
|
2015-08-11 12:33:31 +00:00
|
|
|
class CreateService < Files::BaseService
|
|
|
|
def commit
|
2017-02-15 23:28:29 +00:00
|
|
|
repository.create_file(
|
2016-11-14 20:02:10 +00:00
|
|
|
current_user,
|
|
|
|
@file_path,
|
|
|
|
@file_content,
|
2016-12-08 09:08:25 +00:00
|
|
|
message: @commit_message,
|
|
|
|
branch_name: @target_branch,
|
2016-11-14 20:02:10 +00:00
|
|
|
author_email: @author_email,
|
|
|
|
author_name: @author_name,
|
2017-01-06 15:29:13 +00:00
|
|
|
start_project: @start_project,
|
|
|
|
start_branch_name: @start_branch)
|
2015-08-11 12:33:31 +00:00
|
|
|
end
|
2013-11-05 10:06:52 +00:00
|
|
|
|
2015-08-11 12:33:31 +00:00
|
|
|
def validate
|
|
|
|
super
|
2013-11-05 10:06:52 +00:00
|
|
|
|
2015-10-19 21:52:46 +00:00
|
|
|
if @file_path =~ Gitlab::Regex.directory_traversal_regex
|
|
|
|
raise_error(
|
|
|
|
'Your changes could not be committed, because the file name ' +
|
|
|
|
Gitlab::Regex.directory_traversal_regex_message
|
|
|
|
)
|
|
|
|
end
|
2013-11-05 10:06:52 +00:00
|
|
|
|
2015-10-19 21:52:46 +00:00
|
|
|
unless @file_path =~ Gitlab::Regex.file_path_regex
|
2015-08-11 12:33:31 +00:00
|
|
|
raise_error(
|
2014-06-26 07:53:01 +00:00
|
|
|
'Your changes could not be committed, because the file name ' +
|
2015-10-19 21:52:46 +00:00
|
|
|
Gitlab::Regex.file_path_regex_message
|
2014-06-26 07:53:01 +00:00
|
|
|
)
|
2013-11-05 10:06:52 +00:00
|
|
|
end
|
|
|
|
|
2015-08-11 12:33:31 +00:00
|
|
|
unless project.empty_repo?
|
2015-09-10 14:18:40 +00:00
|
|
|
@file_path.slice!(0) if @file_path.start_with?('/')
|
|
|
|
|
2017-01-06 15:29:13 +00:00
|
|
|
blob = repository.blob_at_branch(@start_branch, @file_path)
|
2015-01-26 21:31:20 +00:00
|
|
|
|
|
|
|
if blob
|
2016-07-06 13:26:59 +00:00
|
|
|
raise_error('Your changes could not be committed because a file with the same name already exists')
|
2015-08-11 09:49:09 +00:00
|
|
|
end
|
|
|
|
end
|
2013-11-05 10:06:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|