2012-10-26 00:33:53 +02:00
|
|
|
module Gitlab
|
|
|
|
module Satellite
|
2012-10-26 02:50:24 +02:00
|
|
|
# GitLab server-side file update and commit
|
2012-10-26 00:33:53 +02:00
|
|
|
class EditFileAction < Action
|
2012-10-26 01:59:50 +02:00
|
|
|
attr_accessor :file_path, :ref
|
2012-10-26 00:33:53 +02:00
|
|
|
|
2012-10-26 01:59:50 +02:00
|
|
|
def initialize(user, project, ref, file_path)
|
|
|
|
super user, project, git_timeout: 10.seconds
|
|
|
|
@file_path = file_path
|
2012-10-26 00:33:53 +02:00
|
|
|
@ref = ref
|
|
|
|
end
|
|
|
|
|
2012-10-26 02:50:24 +02:00
|
|
|
# Updates the files content and creates a new commit for it
|
|
|
|
#
|
|
|
|
# Returns false if the ref has been updated while editing the file
|
|
|
|
# Returns false if commiting the change fails
|
|
|
|
# Returns false if pushing from the satellite to Gitolite failed or was rejected
|
|
|
|
# Returns true otherwise
|
2012-10-26 01:59:50 +02:00
|
|
|
def commit!(content, commit_message, last_commit)
|
2012-10-26 00:39:23 +02:00
|
|
|
return false unless can_edit?(last_commit)
|
2012-10-26 00:33:53 +02:00
|
|
|
|
|
|
|
in_locked_and_timed_satellite do |repo|
|
|
|
|
prepare_satellite!(repo)
|
|
|
|
|
2012-10-26 01:59:50 +02:00
|
|
|
# create target branch in satellite at the corresponding commit from Gitolite
|
|
|
|
repo.git.checkout({raise: true, timeout: true, b: true}, ref, "origin/#{ref}")
|
|
|
|
|
|
|
|
# update the file in the satellite's working dir
|
|
|
|
file_path_in_satellite = File.join(repo.working_dir, file_path)
|
|
|
|
File.open(file_path_in_satellite, 'w') { |f| f.write(content) }
|
|
|
|
|
|
|
|
# commit the changes
|
|
|
|
# will raise CommandFailed when commit fails
|
|
|
|
repo.git.commit(raise: true, timeout: true, a: true, m: commit_message)
|
|
|
|
|
|
|
|
|
|
|
|
# push commit back to Gitolite
|
|
|
|
# will raise CommandFailed when push fails
|
|
|
|
repo.git.push({raise: true, timeout: true}, :origin, ref)
|
2012-10-26 00:33:53 +02:00
|
|
|
|
|
|
|
# everything worked
|
|
|
|
true
|
|
|
|
end
|
|
|
|
rescue Grit::Git::CommandFailed => ex
|
|
|
|
Gitlab::GitLogger.error(ex.message)
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2012-10-26 00:39:23 +02:00
|
|
|
def can_edit?(last_commit)
|
2013-01-16 17:31:11 +01:00
|
|
|
current_last_commit = @project.repository.last_commit_for(ref, file_path).sha
|
2012-10-26 00:33:53 +02:00
|
|
|
last_commit == current_last_commit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|