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