WebEditor: base implementation

This commit is contained in:
Valeriy Sizov 2012-10-12 17:14:52 +03:00
parent 0b3e9fd218
commit 42bdfd021b
4 changed files with 55 additions and 10 deletions

View file

@ -26,8 +26,14 @@ class TreeController < ProjectResourceController
def update def update
file_editor = Gitlab::FileEditor.new(current_user, @project, @ref) file_editor = Gitlab::FileEditor.new(current_user, @project, @ref)
if file_editor.can_edit?(@path, params[:last_commit]) update_status = file_editor.update(
file_editor.update(@path, params[:content]) @path,
params[:content],
params[:commit_message],
params[:last_commit]
)
if update_status
redirect_to project_tree_path(@project, @id), :notice => "File has been successfully changed" redirect_to project_tree_path(@project, @id), :notice => "File has been successfully changed"
else else
flash[:notice] = "You can't save file because it has been changed" flash[:notice] = "You can't save file because it has been changed"

View file

@ -13,10 +13,15 @@
= text_area_tag 'commit_message' = text_area_tag 'commit_message'
.form-actions .form-actions
= hidden_field_tag 'last_commit', @last_commit = hidden_field_tag 'last_commit', @last_commit
= hidden_field_tag 'content' = hidden_field_tag 'content', '', :id => :file_content
= submit_tag "Save", class: 'btn save-btn' = button_tag "Save", class: 'btn save-btn'
:javascript :javascript
var editor = ace.edit("editor"); var editor = ace.edit("editor");
editor.setTheme("ace/theme/twilight"); editor.setTheme("ace/theme/twilight");
editor.getSession().setMode("ace/mode/javascript"); editor.getSession().setMode("ace/mode/javascript");
$(".save-btn").click(function(){
$("#file_content").val(editor.getValue());
$(".form_editor form").submit();
});

View file

@ -9,14 +9,48 @@ module Gitlab
self.ref = ref self.ref = ref
end end
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
def can_edit?(path, last_commit) def can_edit?(path, last_commit)
current_last_commit = @project.commits(ref, path, 1).first.sha current_last_commit = @project.commits(ref, path, 1).first.sha
last_commit == current_last_commit last_commit == current_last_commit
end end
def update(path, content)
true
end
end end
end end

View file

@ -28,13 +28,13 @@ module Gitlab
def process def process
Grit::Git.with_timeout(30.seconds) do Grit::Git.with_timeout(30.seconds) do
lock_file = Rails.root.join("tmp", "merge_repo_#{project.path}.lock") lock_file = Rails.root.join("tmp", "#{project.path}.lock")
File.open(lock_file, "w+") do |f| File.open(lock_file, "w+") do |f|
f.flock(File::LOCK_EX) f.flock(File::LOCK_EX)
unless project.satellite.exists? unless project.satellite.exists?
raise "You should run: rake gitlab:app:enable_automerge" raise "Satellite doesn't exist"
end end
project.satellite.clear project.satellite.clear