2013-11-05 05:21:11 -05:00
|
|
|
module Files
|
2015-08-11 08:33:31 -04:00
|
|
|
class UpdateService < Files::BaseService
|
2017-03-01 06:00:37 -05:00
|
|
|
FileChangedError = Class.new(StandardError)
|
2016-08-07 23:29:23 -04:00
|
|
|
|
2017-04-19 20:37:44 -04:00
|
|
|
def initialize(*args)
|
|
|
|
super
|
|
|
|
|
|
|
|
@last_commit_sha = params[:last_commit_sha]
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_commit!
|
2016-07-06 06:25:45 -04:00
|
|
|
repository.update_file(current_user, @file_path, @file_content,
|
2016-09-08 11:40:07 -04:00
|
|
|
message: @commit_message,
|
2017-04-19 20:37:44 -04:00
|
|
|
branch_name: @branch_name,
|
2016-12-08 04:08:25 -05:00
|
|
|
previous_path: @previous_path,
|
2016-09-08 11:40:07 -04:00
|
|
|
author_email: @author_email,
|
2016-11-14 15:02:10 -05:00
|
|
|
author_name: @author_name,
|
2017-01-06 10:29:13 -05:00
|
|
|
start_project: @start_project,
|
|
|
|
start_branch_name: @start_branch)
|
2013-11-05 05:21:11 -05:00
|
|
|
end
|
2016-08-07 23:29:23 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-04-19 20:37:44 -04:00
|
|
|
def file_has_changed?
|
|
|
|
return false unless @last_commit_sha && last_commit
|
2017-02-24 10:38:44 -05:00
|
|
|
|
2017-04-19 20:37:44 -04:00
|
|
|
@last_commit_sha != last_commit.sha
|
2016-08-07 23:29:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def last_commit
|
2017-06-21 09:48:12 -04:00
|
|
|
@last_commit ||= Gitlab::Git::Commit
|
|
|
|
.last_for_path(@start_project.repository, @start_branch, @file_path)
|
2016-08-07 23:29:23 -04:00
|
|
|
end
|
2017-04-19 20:37:44 -04:00
|
|
|
|
|
|
|
def validate!
|
|
|
|
super
|
|
|
|
|
|
|
|
if file_has_changed?
|
|
|
|
raise FileChangedError, "You are attempting to update a file that has changed since you started editing it."
|
|
|
|
end
|
|
|
|
end
|
2013-11-05 05:21:11 -05:00
|
|
|
end
|
|
|
|
end
|