Commit 55779b00ea695463056338af993c6332b710ea8f
1 parent
8c89beb6
Exists in
master
and in
4 other branches
Rename Gitlab::FileEditor to Gitlab::Satellite::EditFileAction
Showing
4 changed files
with
46 additions
and
61 deletions
Show diff stats
app/controllers/tree_controller.rb
| ... | ... | @@ -26,7 +26,7 @@ class TreeController < ProjectResourceController |
| 26 | 26 | end |
| 27 | 27 | |
| 28 | 28 | def update |
| 29 | - file_editor = Gitlab::FileEditor.new(current_user, @project, @ref) | |
| 29 | + file_editor = Gitlab::Satellite::EditFileAction.new(current_user, @project, @ref) | |
| 30 | 30 | update_status = file_editor.update( |
| 31 | 31 | @path, |
| 32 | 32 | params[:content], | ... | ... |
lib/gitlab/file_editor.rb
| ... | ... | @@ -1,58 +0,0 @@ |
| 1 | -module Gitlab | |
| 2 | - # GitLab file editor | |
| 3 | - # | |
| 4 | - # It gives you ability to make changes to files | |
| 5 | - # & commit this changes from GitLab UI. | |
| 6 | - class FileEditor | |
| 7 | - attr_accessor :user, :project, :ref | |
| 8 | - | |
| 9 | - def initialize(user, project, ref) | |
| 10 | - self.user = user | |
| 11 | - self.project = project | |
| 12 | - self.ref = ref | |
| 13 | - end | |
| 14 | - | |
| 15 | - def update(path, content, commit_message, last_commit) | |
| 16 | - return false unless can_edit?(path, last_commit) | |
| 17 | - | |
| 18 | - Grit::Git.with_timeout(10.seconds) do | |
| 19 | - lock_file = Rails.root.join("tmp", "#{project.path}.lock") | |
| 20 | - | |
| 21 | - File.open(lock_file, "w+") do |f| | |
| 22 | - f.flock(File::LOCK_EX) | |
| 23 | - | |
| 24 | - unless project.satellite.exists? | |
| 25 | - raise "Satellite doesn't exist" | |
| 26 | - end | |
| 27 | - | |
| 28 | - project.satellite.clear | |
| 29 | - | |
| 30 | - Dir.chdir(project.satellite.path) do | |
| 31 | - r = Grit::Repo.new('.') | |
| 32 | - r.git.sh "git reset --hard" | |
| 33 | - r.git.sh "git fetch origin" | |
| 34 | - r.git.sh "git config user.name \"#{user.name}\"" | |
| 35 | - r.git.sh "git config user.email \"#{user.email}\"" | |
| 36 | - r.git.sh "git checkout -b #{ref} origin/#{ref}" | |
| 37 | - File.open(path, 'w'){|f| f.write(content)} | |
| 38 | - r.git.sh "git add ." | |
| 39 | - r.git.sh "git commit -am '#{commit_message}'" | |
| 40 | - output = r.git.sh "git push origin #{ref}" | |
| 41 | - | |
| 42 | - if output =~ /reject/ | |
| 43 | - return false | |
| 44 | - end | |
| 45 | - end | |
| 46 | - end | |
| 47 | - end | |
| 48 | - true | |
| 49 | - end | |
| 50 | - | |
| 51 | - protected | |
| 52 | - | |
| 53 | - def can_edit?(path, last_commit) | |
| 54 | - current_last_commit = @project.last_commit_for(ref, path).sha | |
| 55 | - last_commit == current_last_commit | |
| 56 | - end | |
| 57 | - end | |
| 58 | -end |
| ... | ... | @@ -0,0 +1,43 @@ |
| 1 | +module Gitlab | |
| 2 | + module Satellite | |
| 3 | + # GitLab file editor | |
| 4 | + # | |
| 5 | + # It gives you ability to make changes to files | |
| 6 | + # & commit this changes from GitLab UI. | |
| 7 | + class EditFileAction < Action | |
| 8 | + attr_accessor :ref | |
| 9 | + | |
| 10 | + def initialize(user, project, ref) | |
| 11 | + super user, project | |
| 12 | + @ref = ref | |
| 13 | + end | |
| 14 | + | |
| 15 | + def update(path, content, commit_message, last_commit) | |
| 16 | + return false unless can_edit?(path, last_commit) | |
| 17 | + | |
| 18 | + in_locked_and_timed_satellite do |repo| | |
| 19 | + prepare_satellite!(repo) | |
| 20 | + | |
| 21 | + repo.git.sh "git checkout -b #{ref} origin/#{ref}" | |
| 22 | + File.open(path, 'w'){|f| f.write(content)} | |
| 23 | + repo.git.sh "git add ." | |
| 24 | + repo.git.sh "git commit -am '#{commit_message}'" | |
| 25 | + output = repo.git.sh "git push origin #{ref}" | |
| 26 | + | |
| 27 | + # everything worked | |
| 28 | + true | |
| 29 | + end | |
| 30 | + rescue Grit::Git::CommandFailed => ex | |
| 31 | + Gitlab::GitLogger.error(ex.message) | |
| 32 | + false | |
| 33 | + end | |
| 34 | + | |
| 35 | + protected | |
| 36 | + | |
| 37 | + def can_edit?(path, last_commit) | |
| 38 | + current_last_commit = @project.last_commit_for(ref, path).sha | |
| 39 | + last_commit == current_last_commit | |
| 40 | + end | |
| 41 | + end | |
| 42 | + end | |
| 43 | +end | ... | ... |
lib/gitlab/satellite/merge_action.rb
| ... | ... | @@ -55,11 +55,11 @@ module Gitlab |
| 55 | 55 | prepare_satellite!(repo) |
| 56 | 56 | |
| 57 | 57 | # create target branch in satellite at the corresponding commit from Gitolite |
| 58 | - repo.git.checkout({b: true}, merge_request.target_branch, "origin/#{merge_request.target_branch}") | |
| 58 | + repo.git.checkout({raise: true, b: true}, merge_request.target_branch, "origin/#{merge_request.target_branch}") | |
| 59 | 59 | |
| 60 | 60 | # merge the source branch from Gitolite into the satellite |
| 61 | 61 | # will raise CommandFailed when merge fails |
| 62 | - repo.git.pull({no_ff: true, raise: true}, :origin, merge_request.source_branch) | |
| 62 | + repo.git.pull({raise: true, no_ff: true}, :origin, merge_request.source_branch) | |
| 63 | 63 | rescue Grit::Git::CommandFailed => ex |
| 64 | 64 | Gitlab::GitLogger.error(ex.message) |
| 65 | 65 | false | ... | ... |