Commit 42bdfd021ba8cc8b634a23e3c5c2ac605f4c602c

Authored by Valeriy Sizov
1 parent 0b3e9fd2

WebEditor: base implementation

app/controllers/tree_controller.rb
@@ -26,8 +26,14 @@ class TreeController < ProjectResourceController @@ -26,8 +26,14 @@ class TreeController < ProjectResourceController
26 26
27 def update 27 def update
28 file_editor = Gitlab::FileEditor.new(current_user, @project, @ref) 28 file_editor = Gitlab::FileEditor.new(current_user, @project, @ref)
29 - if file_editor.can_edit?(@path, params[:last_commit])  
30 - file_editor.update(@path, params[:content]) 29 + update_status = file_editor.update(
  30 + @path,
  31 + params[:content],
  32 + params[:commit_message],
  33 + params[:last_commit]
  34 + )
  35 +
  36 + if update_status
31 redirect_to project_tree_path(@project, @id), :notice => "File has been successfully changed" 37 redirect_to project_tree_path(@project, @id), :notice => "File has been successfully changed"
32 else 38 else
33 flash[:notice] = "You can't save file because it has been changed" 39 flash[:notice] = "You can't save file because it has been changed"
app/views/tree/edit.html.haml
@@ -13,10 +13,15 @@ @@ -13,10 +13,15 @@
13 = text_area_tag 'commit_message' 13 = text_area_tag 'commit_message'
14 .form-actions 14 .form-actions
15 = hidden_field_tag 'last_commit', @last_commit 15 = hidden_field_tag 'last_commit', @last_commit
16 - = hidden_field_tag 'content'  
17 - = submit_tag "Save", class: 'btn save-btn' 16 + = hidden_field_tag 'content', '', :id => :file_content
  17 + = button_tag "Save", class: 'btn save-btn'
18 18
19 :javascript 19 :javascript
20 var editor = ace.edit("editor"); 20 var editor = ace.edit("editor");
21 editor.setTheme("ace/theme/twilight"); 21 editor.setTheme("ace/theme/twilight");
22 editor.getSession().setMode("ace/mode/javascript"); 22 editor.getSession().setMode("ace/mode/javascript");
  23 +
  24 + $(".save-btn").click(function(){
  25 + $("#file_content").val(editor.getValue());
  26 + $(".form_editor form").submit();
  27 + });
lib/gitlab/file_editor.rb
@@ -9,14 +9,48 @@ module Gitlab @@ -9,14 +9,48 @@ module Gitlab
9 self.ref = ref 9 self.ref = ref
10 end 10 end
11 11
  12 + def update(path, content, commit_message, last_commit)
  13 + return false unless can_edit?(path, last_commit)
  14 +
  15 + Grit::Git.with_timeout(10.seconds) do
  16 + lock_file = Rails.root.join("tmp", "#{project.path}.lock")
  17 +
  18 + File.open(lock_file, "w+") do |f|
  19 + f.flock(File::LOCK_EX)
  20 +
  21 + unless project.satellite.exists?
  22 + raise "Satellite doesn't exist"
  23 + end
  24 +
  25 + project.satellite.clear
  26 +
  27 + Dir.chdir(project.satellite.path) do
  28 + r = Grit::Repo.new('.')
  29 + r.git.sh "git reset --hard"
  30 + r.git.sh "git fetch origin"
  31 + r.git.sh "git config user.name \"#{user.name}\""
  32 + r.git.sh "git config user.email \"#{user.email}\""
  33 + r.git.sh "git checkout -b #{ref} origin/#{ref}"
  34 + File.open(path, 'w'){|f| f.write(content)}
  35 + r.git.sh "git add ."
  36 + r.git.sh "git commit -am '#{commit_message}'"
  37 + output = r.git.sh "git push origin #{ref}"
  38 + if output =~ /reject/
  39 + return false
  40 + end
  41 + end
  42 + end
  43 + end
  44 +
  45 + true
  46 +
  47 + end
  48 +
  49 + protected
12 def can_edit?(path, last_commit) 50 def can_edit?(path, last_commit)
13 current_last_commit = @project.commits(ref, path, 1).first.sha 51 current_last_commit = @project.commits(ref, path, 1).first.sha
14 last_commit == current_last_commit 52 last_commit == current_last_commit
15 end 53 end
16 54
17 - def update(path, content)  
18 - true  
19 - end  
20 -  
21 end 55 end
22 end 56 end
lib/gitlab/merge.rb
@@ -28,13 +28,13 @@ module Gitlab @@ -28,13 +28,13 @@ module Gitlab
28 28
29 def process 29 def process
30 Grit::Git.with_timeout(30.seconds) do 30 Grit::Git.with_timeout(30.seconds) do
31 - lock_file = Rails.root.join("tmp", "merge_repo_#{project.path}.lock") 31 + lock_file = Rails.root.join("tmp", "#{project.path}.lock")
32 32
33 File.open(lock_file, "w+") do |f| 33 File.open(lock_file, "w+") do |f|
34 f.flock(File::LOCK_EX) 34 f.flock(File::LOCK_EX)
35 35
36 unless project.satellite.exists? 36 unless project.satellite.exists?
37 - raise "You should run: rake gitlab:app:enable_automerge" 37 + raise "Satellite doesn't exist"
38 end 38 end
39 39
40 project.satellite.clear 40 project.satellite.clear