2014-09-02 18:07:02 +05:30
|
|
|
require_relative "base_service"
|
|
|
|
|
|
|
|
module Files
|
2015-09-25 12:07:36 +05:30
|
|
|
class UpdateService < Files::BaseService
|
2016-09-13 17:45:13 +05:30
|
|
|
class FileChangedError < StandardError; end
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
def commit
|
2016-08-24 12:49:21 +05:30
|
|
|
repository.update_file(current_user, @file_path, @file_content,
|
|
|
|
branch: @target_branch,
|
|
|
|
previous_path: @previous_path,
|
|
|
|
message: @commit_message)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
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
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|