debian-mirror-gitlab/app/services/files/update_service.rb

44 lines
1.2 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
module Files
2015-09-25 12:07:36 +05:30
class UpdateService < Files::BaseService
2017-08-17 22:00:37 +05:30
FileChangedError = Class.new(StandardError)
def initialize(*args)
super
@last_commit_sha = params[:last_commit_sha]
end
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
def create_commit!
2016-08-24 12:49:21 +05:30
repository.update_file(current_user, @file_path, @file_content,
2016-09-29 09:46:39 +05:30
message: @commit_message,
2017-08-17 22:00:37 +05:30
branch_name: @branch_name,
previous_path: @previous_path,
2016-09-29 09:46:39 +05:30
author_email: @author_email,
2017-08-17 22:00:37 +05:30
author_name: @author_name,
start_project: @start_project,
start_branch_name: @start_branch)
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
private
2017-08-17 22:00:37 +05:30
def file_has_changed?
return false unless @last_commit_sha && last_commit
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
@last_commit_sha != last_commit.sha
2016-09-13 17:45:13 +05:30
end
def last_commit
2017-09-10 17:25:29 +05:30
@last_commit ||= Gitlab::Git::Commit
.last_for_path(@start_project.repository, @start_branch, @file_path)
2017-08-17 22:00:37 +05:30
end
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
2016-09-13 17:45:13 +05:30
end
2014-09-02 18:07:02 +05:30
end
end