Commit 700a784bc91c0ee24cbe2f1c36589e40b92ac28d
1 parent
30801273
Exists in
master
and in
4 other branches
Refactor EditFileAction#update and rename it to commit!
Showing
2 changed files
with
24 additions
and
14 deletions
Show diff stats
app/controllers/tree_controller.rb
| ... | ... | @@ -26,14 +26,14 @@ class TreeController < ProjectResourceController |
| 26 | 26 | end |
| 27 | 27 | |
| 28 | 28 | def update |
| 29 | - file_editor = Gitlab::Satellite::EditFileAction.new(current_user, @project, @ref, @path) | |
| 30 | - update_status = file_editor.update( | |
| 29 | + edit_file_action = Gitlab::Satellite::EditFileAction.new(current_user, @project, @ref, @path) | |
| 30 | + updated_successfully = edit_file_action.commit!( | |
| 31 | 31 | params[:content], |
| 32 | 32 | params[:commit_message], |
| 33 | 33 | params[:last_commit] |
| 34 | 34 | ) |
| 35 | 35 | |
| 36 | - if update_status | |
| 36 | + if updated_successfully | |
| 37 | 37 | redirect_to project_tree_path(@project, @id), notice: "Your changes have been successfully commited" |
| 38 | 38 | else |
| 39 | 39 | flash[:notice] = "Your changes could not be commited, because the file has been changed" | ... | ... |
lib/gitlab/satellite/edit_file_action.rb
| ... | ... | @@ -5,25 +5,35 @@ module Gitlab |
| 5 | 5 | # It gives you ability to make changes to files |
| 6 | 6 | # & commit this changes from GitLab UI. |
| 7 | 7 | class EditFileAction < Action |
| 8 | - attr_accessor :path, :ref | |
| 8 | + attr_accessor :file_path, :ref | |
| 9 | 9 | |
| 10 | - def initialize(user, project, ref, path) | |
| 11 | - super user, project | |
| 12 | - @path = path | |
| 10 | + def initialize(user, project, ref, file_path) | |
| 11 | + super user, project, git_timeout: 10.seconds | |
| 12 | + @file_path = file_path | |
| 13 | 13 | @ref = ref |
| 14 | 14 | end |
| 15 | 15 | |
| 16 | - def update(content, commit_message, last_commit) | |
| 16 | + def commit!(content, commit_message, last_commit) | |
| 17 | 17 | return false unless can_edit?(last_commit) |
| 18 | 18 | |
| 19 | 19 | in_locked_and_timed_satellite do |repo| |
| 20 | 20 | prepare_satellite!(repo) |
| 21 | 21 | |
| 22 | - repo.git.sh "git checkout -b #{ref} origin/#{ref}" | |
| 23 | - File.open(path, 'w'){|f| f.write(content)} | |
| 24 | - repo.git.sh "git add ." | |
| 25 | - repo.git.sh "git commit -am '#{commit_message}'" | |
| 26 | - output = repo.git.sh "git push origin #{ref}" | |
| 22 | + # create target branch in satellite at the corresponding commit from Gitolite | |
| 23 | + repo.git.checkout({raise: true, timeout: true, b: true}, ref, "origin/#{ref}") | |
| 24 | + | |
| 25 | + # update the file in the satellite's working dir | |
| 26 | + file_path_in_satellite = File.join(repo.working_dir, file_path) | |
| 27 | + File.open(file_path_in_satellite, 'w') { |f| f.write(content) } | |
| 28 | + | |
| 29 | + # commit the changes | |
| 30 | + # will raise CommandFailed when commit fails | |
| 31 | + repo.git.commit(raise: true, timeout: true, a: true, m: commit_message) | |
| 32 | + | |
| 33 | + | |
| 34 | + # push commit back to Gitolite | |
| 35 | + # will raise CommandFailed when push fails | |
| 36 | + repo.git.push({raise: true, timeout: true}, :origin, ref) | |
| 27 | 37 | |
| 28 | 38 | # everything worked |
| 29 | 39 | true |
| ... | ... | @@ -36,7 +46,7 @@ module Gitlab |
| 36 | 46 | protected |
| 37 | 47 | |
| 38 | 48 | def can_edit?(last_commit) |
| 39 | - current_last_commit = @project.last_commit_for(ref, path).sha | |
| 49 | + current_last_commit = @project.last_commit_for(ref, file_path).sha | |
| 40 | 50 | last_commit == current_last_commit |
| 41 | 51 | end |
| 42 | 52 | end | ... | ... |