gitlabhq/lib/gitlab/file_editor.rb

57 lines
1.5 KiB
Ruby
Raw Normal View History

2012-10-11 23:15:24 +02:00
module Gitlab
class FileEditor
2012-10-12 13:06:12 +02:00
attr_accessor :user, :project, :ref
2012-10-11 23:15:24 +02:00
2012-10-12 13:06:12 +02:00
def initialize(user, project, ref)
2012-10-11 23:15:24 +02:00
self.user = user
self.project = project
2012-10-12 13:06:12 +02:00
self.ref = ref
2012-10-11 23:15:24 +02:00
end
2012-10-12 16:14:52 +02:00
def update(path, content, commit_message, last_commit)
return false unless can_edit?(path, last_commit)
Grit::Git.with_timeout(10.seconds) do
lock_file = Rails.root.join("tmp", "#{project.path}.lock")
File.open(lock_file, "w+") do |f|
f.flock(File::LOCK_EX)
unless project.satellite.exists?
raise "Satellite doesn't exist"
end
project.satellite.clear
Dir.chdir(project.satellite.path) do
r = Grit::Repo.new('.')
r.git.sh "git reset --hard"
r.git.sh "git fetch origin"
r.git.sh "git config user.name \"#{user.name}\""
r.git.sh "git config user.email \"#{user.email}\""
r.git.sh "git checkout -b #{ref} origin/#{ref}"
File.open(path, 'w'){|f| f.write(content)}
r.git.sh "git add ."
r.git.sh "git commit -am '#{commit_message}'"
output = r.git.sh "git push origin #{ref}"
if output =~ /reject/
return false
end
end
end
end
true
end
protected
2012-10-11 23:15:24 +02:00
def can_edit?(path, last_commit)
2012-10-12 13:06:12 +02:00
current_last_commit = @project.commits(ref, path, 1).first.sha
last_commit == current_last_commit
2012-10-11 23:15:24 +02:00
end
end
end