diff options
Diffstat (limited to 'app/services/files/update_service.rb')
-rw-r--r-- | app/services/files/update_service.rb | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/app/services/files/update_service.rb b/app/services/files/update_service.rb index 1960dc7d949..4fc3b640799 100644 --- a/app/services/files/update_service.rb +++ b/app/services/files/update_service.rb @@ -2,8 +2,34 @@ require_relative "base_service" module Files class UpdateService < Files::BaseService + class FileChangedError < StandardError; end + def commit - repository.commit_file(current_user, @file_path, @file_content, @commit_message, @target_branch, true) + repository.update_file(current_user, @file_path, @file_content, + branch: @target_branch, + previous_path: @previous_path, + message: @commit_message) + end + + 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 end end |