2014-01-16 13:36:51 -05:00
|
|
|
require_relative "base_service"
|
2013-12-10 07:15:08 -05:00
|
|
|
|
2013-11-05 05:21:11 -05:00
|
|
|
module Files
|
2015-08-11 08:33:31 -04:00
|
|
|
class UpdateService < Files::BaseService
|
2016-08-07 23:29:23 -04:00
|
|
|
class FileChangedError < StandardError; end
|
|
|
|
|
2015-08-11 08:33:31 -04:00
|
|
|
def commit
|
2016-07-06 06:25:45 -04:00
|
|
|
repository.update_file(current_user, @file_path, @file_content,
|
2016-07-06 13:51:02 -04:00
|
|
|
branch: @target_branch,
|
|
|
|
previous_path: @previous_path,
|
|
|
|
message: @commit_message)
|
2013-11-05 05:21:11 -05:00
|
|
|
end
|
2016-08-07 23:29:23 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def validate
|
|
|
|
super
|
|
|
|
|
|
|
|
if file_has_changed?
|
|
|
|
raise FileChangedError.new("You are attempting to update a file that has changed since you started editing it.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def file_has_changed?
|
|
|
|
return false unless @last_commit_sha && last_commit
|
|
|
|
|
|
|
|
@last_commit_sha != last_commit.sha
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_commit
|
|
|
|
@last_commit ||= Gitlab::Git::Commit.
|
|
|
|
last_for_path(@source_project.repository, @source_branch, @file_path)
|
|
|
|
end
|
2013-11-05 05:21:11 -05:00
|
|
|
end
|
|
|
|
end
|