From 847bba926929c4d64dcc5065b9b2762a62a87c87 Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Fri, 26 Oct 2012 00:10:40 +0200 Subject: [PATCH 01/14] Add Gitlab::Satellite::Action --- lib/gitlab/satellite/action.rb | 47 ++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 lib/gitlab/satellite/action.rb diff --git a/lib/gitlab/satellite/action.rb b/lib/gitlab/satellite/action.rb new file mode 100644 index 00000000..23f81856 --- /dev/null +++ b/lib/gitlab/satellite/action.rb @@ -0,0 +1,47 @@ +module Gitlab + module Satellite + class Action + DEFAULT_OPTIONS = { git_timeout: 30.seconds } + + attr_accessor :options, :project + + def initialize(project, options = {}) + @project = project + @options = DEFAULT_OPTIONS.merge(options) + end + + protected + + # * Sets a 30s timeout for Git + # * Locks the satellite repo + # * Yields the prepared satellite repo + def in_locked_and_timed_satellite + Grit::Git.with_timeout(options[:git_timeout]) do + File.open(lock_file, "w+") do |f| + f.flock(File::LOCK_EX) + + unless project.satellite.exists? + raise "Satellite doesn't exist" + end + + Dir.chdir(project.satellite.path) do + repo = Grit::Repo.new('.') + + return yield repo + end + end + end + rescue Errno::ENOMEM => ex + Gitlab::GitLogger.error(ex.message) + return false + rescue Grit::Git::GitTimeout => ex + Gitlab::GitLogger.error(ex.message) + return false + end + + def lock_file + Rails.root.join("tmp", "#{project.path}.lock") + end + end + end +end From 78235edda8009e7dcec3f15b3ec79a0c17d9d797 Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Fri, 26 Oct 2012 00:19:01 +0200 Subject: [PATCH 02/14] Renamed Gitlab::Merge to Gitlab::Satellite::MergeAction --- app/models/merge_request.rb | 4 +- lib/gitlab/merge.rb | 106 --------------------------- lib/gitlab/satellite/merge_action.rb | 83 +++++++++++++++++++++ 3 files changed, 85 insertions(+), 108 deletions(-) delete mode 100644 lib/gitlab/merge.rb create mode 100644 lib/gitlab/satellite/merge_action.rb diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index 70780b75..9a79be12 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -60,7 +60,7 @@ class MergeRequest < ActiveRecord::Base end def check_if_can_be_merged - self.state = if Gitlab::Merge.new(self, self.author).can_be_merged? + self.state = if Gitlab::Satellite::MergeAction.new(self, self.author).can_be_merged? CAN_BE_MERGED else CANNOT_BE_MERGED @@ -167,7 +167,7 @@ class MergeRequest < ActiveRecord::Base end def automerge!(current_user) - if Gitlab::Merge.new(self, current_user).merge! && self.unmerged_commits.empty? + if Gitlab::Satellite::MergeAction.new(self, current_user).merge! && self.unmerged_commits.empty? self.merge!(current_user.id) true end diff --git a/lib/gitlab/merge.rb b/lib/gitlab/merge.rb deleted file mode 100644 index 28c20689..00000000 --- a/lib/gitlab/merge.rb +++ /dev/null @@ -1,106 +0,0 @@ -module Gitlab - class Merge - attr_accessor :merge_request, :project, :user - - def initialize(merge_request, user) - @merge_request = merge_request - @project = merge_request.project - @user = user - end - - def can_be_merged? - in_locked_and_timed_satellite do |merge_repo| - merge_in_satellite!(merge_repo) - end - end - - # Merges the source branch into the target branch in the satellite and - # pushes it back to Gitolite. - # It also removes the source branch if requested in the merge request. - # - # Returns false if the merge produced conflicts - # Returns false if pushing from the satellite to Gitolite failed or was rejected - # Returns true otherwise - def merge! - in_locked_and_timed_satellite do |merge_repo| - if merge_in_satellite!(merge_repo) - # push merge back to Gitolite - # will raise CommandFailed when push fails - merge_repo.git.push({raise: true}, :origin, merge_request.target_branch) - - # remove source branch - if merge_request.should_remove_source_branch && !project.root_ref?(merge_request.source_branch) - # will raise CommandFailed when push fails - merge_repo.git.push({raise: true}, :origin, ":#{merge_request.source_branch}") - end - - # merge, push and branch removal successful - true - end - end - rescue Grit::Git::CommandFailed - false - end - - private - - # * Sets a 30s timeout for Git - # * Locks the satellite repo - # * Yields the prepared satellite repo - def in_locked_and_timed_satellite - Grit::Git.with_timeout(30.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 - - Dir.chdir(project.satellite.path) do - repo = Grit::Repo.new('.') - - return yield repo - end - end - end - rescue Errno::ENOMEM => ex - Gitlab::GitLogger.error(ex.message) - rescue Grit::Git::GitTimeout - return false - end - - # Merges the source_branch into the target_branch in the satellite. - # - # Note: it will clear out the satellite before doing anything - # - # Returns false if the merge produced conflicts - # Returns true otherwise - def merge_in_satellite!(repo) - prepare_satellite!(repo) - - # create target branch in satellite at the corresponding commit from Gitolite - repo.git.checkout({b: true}, merge_request.target_branch, "origin/#{merge_request.target_branch}") - - # merge the source branch from Gitolite into the satellite - # will raise CommandFailed when merge fails - repo.git.pull({no_ff: true, raise: true}, :origin, merge_request.source_branch) - rescue Grit::Git::CommandFailed - false - end - - # * Clears the satellite - # * Updates the satellite from Gitolite - # * Sets up Git variables for the user - def prepare_satellite!(repo) - project.satellite.clear - - repo.git.reset(hard: true) - repo.git.fetch({}, :origin) - - repo.git.config({}, "user.name", user.name) - repo.git.config({}, "user.email", user.email) - end - end -end diff --git a/lib/gitlab/satellite/merge_action.rb b/lib/gitlab/satellite/merge_action.rb new file mode 100644 index 00000000..067a9ce8 --- /dev/null +++ b/lib/gitlab/satellite/merge_action.rb @@ -0,0 +1,83 @@ +module Gitlab + module Satellite + class MergeAction < Action + attr_accessor :merge_request, :user + + def initialize(merge_request, user) + super merge_request.project + @merge_request = merge_request + @user = user + end + + def can_be_merged? + in_locked_and_timed_satellite do |merge_repo| + merge_in_satellite!(merge_repo) + end + end + + # Merges the source branch into the target branch in the satellite and + # pushes it back to Gitolite. + # It also removes the source branch if requested in the merge request. + # + # Returns false if the merge produced conflicts + # Returns false if pushing from the satellite to Gitolite failed or was rejected + # Returns true otherwise + def merge! + in_locked_and_timed_satellite do |merge_repo| + if merge_in_satellite!(merge_repo) + # push merge back to Gitolite + # will raise CommandFailed when push fails + merge_repo.git.push({raise: true}, :origin, merge_request.target_branch) + + # remove source branch + if merge_request.should_remove_source_branch && !project.root_ref?(merge_request.source_branch) + # will raise CommandFailed when push fails + merge_repo.git.push({raise: true}, :origin, ":#{merge_request.source_branch}") + end + + # merge, push and branch removal successful + true + end + end + rescue Grit::Git::CommandFailed => ex + Gitlab::GitLogger.error(ex.message) + false + end + + private + + # Merges the source_branch into the target_branch in the satellite. + # + # Note: it will clear out the satellite before doing anything + # + # Returns false if the merge produced conflicts + # Returns true otherwise + def merge_in_satellite!(repo) + prepare_satellite!(repo) + + # create target branch in satellite at the corresponding commit from Gitolite + repo.git.checkout({b: true}, merge_request.target_branch, "origin/#{merge_request.target_branch}") + + # merge the source branch from Gitolite into the satellite + # will raise CommandFailed when merge fails + repo.git.pull({no_ff: true, raise: true}, :origin, merge_request.source_branch) + rescue Grit::Git::CommandFailed => ex + Gitlab::GitLogger.error(ex.message) + false + end + + # * Clears the satellite + # * Updates the satellite from Gitolite + # * Sets up Git variables for the user + def prepare_satellite!(repo) + project.satellite.clear + + repo.git.reset(hard: true) + repo.git.fetch({}, :origin) + + repo.git.config({}, "user.name", user.name) + repo.git.config({}, "user.email", user.email) + end + end + end +end From 8778961d33c15938f4ed3de301fd9b865b9299d8 Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Fri, 26 Oct 2012 00:24:02 +0200 Subject: [PATCH 03/14] Move prepare_satellite! to Gitlab::Satelite::Action --- lib/gitlab/satellite/action.rb | 20 ++++++++++++++++++-- lib/gitlab/satellite/merge_action.rb | 18 ++---------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/lib/gitlab/satellite/action.rb b/lib/gitlab/satellite/action.rb index 23f81856..0d7eecdb 100644 --- a/lib/gitlab/satellite/action.rb +++ b/lib/gitlab/satellite/action.rb @@ -3,10 +3,11 @@ module Gitlab class Action DEFAULT_OPTIONS = { git_timeout: 30.seconds } - attr_accessor :options, :project + attr_accessor :options, :project, :user - def initialize(project, options = {}) + def initialize(project, user, options = {}) @project = project + @user = user @options = DEFAULT_OPTIONS.merge(options) end @@ -42,6 +43,21 @@ module Gitlab def lock_file Rails.root.join("tmp", "#{project.path}.lock") end + + # * Clears the satellite + # * Updates the satellite from Gitolite + # * Sets up Git variables for the user + # + # Note: use this within #in_locked_and_timed_satellite + def prepare_satellite!(repo) + project.satellite.clear + + repo.git.reset(hard: true) + repo.git.fetch({}, :origin) + + repo.git.config({}, "user.name", user.name) + repo.git.config({}, "user.email", user.email) + end end end end diff --git a/lib/gitlab/satellite/merge_action.rb b/lib/gitlab/satellite/merge_action.rb index 067a9ce8..fc4cd752 100644 --- a/lib/gitlab/satellite/merge_action.rb +++ b/lib/gitlab/satellite/merge_action.rb @@ -1,12 +1,11 @@ module Gitlab module Satellite class MergeAction < Action - attr_accessor :merge_request, :user + attr_accessor :merge_request def initialize(merge_request, user) - super merge_request.project + super merge_request.project, user @merge_request = merge_request - @user = user end def can_be_merged? @@ -65,19 +64,6 @@ module Gitlab Gitlab::GitLogger.error(ex.message) false end - - # * Clears the satellite - # * Updates the satellite from Gitolite - # * Sets up Git variables for the user - def prepare_satellite!(repo) - project.satellite.clear - - repo.git.reset(hard: true) - repo.git.fetch({}, :origin) - - repo.git.config({}, "user.name", user.name) - repo.git.config({}, "user.email", user.email) - end end end end From 35b7a5395524d25e3bb9c20b473f1a1eb6970b6f Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Fri, 26 Oct 2012 00:24:16 +0200 Subject: [PATCH 04/14] Reorder methods in Gitlab::Satellite --- lib/gitlab/satellite.rb | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/gitlab/satellite.rb b/lib/gitlab/satellite.rb index 9d8dfb8e..64875f3f 100644 --- a/lib/gitlab/satellite.rb +++ b/lib/gitlab/satellite.rb @@ -5,20 +5,8 @@ module Gitlab attr_accessor :project - def initialize project - self.project = project - end - - def create - `git clone #{project.url_to_repo} #{path}` - end - - def path - Rails.root.join("tmp", "repo_satellites", project.path) - end - - def exists? - File.exists? path + def initialize(project) + @project = project end #will be deleted all branches except PARKING_BRANCH @@ -37,5 +25,16 @@ module Gitlab end end + def create + `git clone #{project.url_to_repo} #{path}` + end + + def exists? + File.exists? path + end + + def path + Rails.root.join("tmp", "repo_satellites", project.path) + end end end From 8c89beb6f9bca14dd79fb1e0a81ebb65354edf73 Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Fri, 26 Oct 2012 00:26:47 +0200 Subject: [PATCH 05/14] Change argument order for satellite actions to always start with the user --- app/models/merge_request.rb | 4 ++-- lib/gitlab/satellite/action.rb | 4 ++-- lib/gitlab/satellite/merge_action.rb | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index 9a79be12..9238aa48 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -60,7 +60,7 @@ class MergeRequest < ActiveRecord::Base end def check_if_can_be_merged - self.state = if Gitlab::Satellite::MergeAction.new(self, self.author).can_be_merged? + self.state = if Gitlab::Satellite::MergeAction.new(self.author, self).can_be_merged? CAN_BE_MERGED else CANNOT_BE_MERGED @@ -167,7 +167,7 @@ class MergeRequest < ActiveRecord::Base end def automerge!(current_user) - if Gitlab::Satellite::MergeAction.new(self, current_user).merge! && self.unmerged_commits.empty? + if Gitlab::Satellite::MergeAction.new(current_user, self).merge! && self.unmerged_commits.empty? self.merge!(current_user.id) true end diff --git a/lib/gitlab/satellite/action.rb b/lib/gitlab/satellite/action.rb index 0d7eecdb..d50e28fa 100644 --- a/lib/gitlab/satellite/action.rb +++ b/lib/gitlab/satellite/action.rb @@ -5,10 +5,10 @@ module Gitlab attr_accessor :options, :project, :user - def initialize(project, user, options = {}) + def initialize(user, project, options = {}) + @options = DEFAULT_OPTIONS.merge(options) @project = project @user = user - @options = DEFAULT_OPTIONS.merge(options) end protected diff --git a/lib/gitlab/satellite/merge_action.rb b/lib/gitlab/satellite/merge_action.rb index fc4cd752..ffca6938 100644 --- a/lib/gitlab/satellite/merge_action.rb +++ b/lib/gitlab/satellite/merge_action.rb @@ -3,8 +3,8 @@ module Gitlab class MergeAction < Action attr_accessor :merge_request - def initialize(merge_request, user) - super merge_request.project, user + def initialize(user, merge_request) + super user, merge_request.project @merge_request = merge_request end From 55779b00ea695463056338af993c6332b710ea8f Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Fri, 26 Oct 2012 00:33:53 +0200 Subject: [PATCH 06/14] Rename Gitlab::FileEditor to Gitlab::Satellite::EditFileAction --- app/controllers/tree_controller.rb | 2 +- lib/gitlab/file_editor.rb | 58 ------------------------ lib/gitlab/satellite/edit_file_action.rb | 43 ++++++++++++++++++ lib/gitlab/satellite/merge_action.rb | 4 +- 4 files changed, 46 insertions(+), 61 deletions(-) delete mode 100644 lib/gitlab/file_editor.rb create mode 100644 lib/gitlab/satellite/edit_file_action.rb diff --git a/app/controllers/tree_controller.rb b/app/controllers/tree_controller.rb index e507fb51..60da719c 100644 --- a/app/controllers/tree_controller.rb +++ b/app/controllers/tree_controller.rb @@ -26,7 +26,7 @@ class TreeController < ProjectResourceController end def update - file_editor = Gitlab::FileEditor.new(current_user, @project, @ref) + file_editor = Gitlab::Satellite::EditFileAction.new(current_user, @project, @ref) update_status = file_editor.update( @path, params[:content], diff --git a/lib/gitlab/file_editor.rb b/lib/gitlab/file_editor.rb deleted file mode 100644 index dc3f9480..00000000 --- a/lib/gitlab/file_editor.rb +++ /dev/null @@ -1,58 +0,0 @@ -module Gitlab - # GitLab file editor - # - # It gives you ability to make changes to files - # & commit this changes from GitLab UI. - class FileEditor - attr_accessor :user, :project, :ref - - def initialize(user, project, ref) - self.user = user - self.project = project - self.ref = ref - 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) - current_last_commit = @project.last_commit_for(ref, path).sha - last_commit == current_last_commit - end - end -end diff --git a/lib/gitlab/satellite/edit_file_action.rb b/lib/gitlab/satellite/edit_file_action.rb new file mode 100644 index 00000000..8949f54c --- /dev/null +++ b/lib/gitlab/satellite/edit_file_action.rb @@ -0,0 +1,43 @@ +module Gitlab + module Satellite + # GitLab file editor + # + # It gives you ability to make changes to files + # & commit this changes from GitLab UI. + class EditFileAction < Action + attr_accessor :ref + + def initialize(user, project, ref) + super user, project + @ref = ref + end + + def update(path, content, commit_message, last_commit) + return false unless can_edit?(path, last_commit) + + in_locked_and_timed_satellite do |repo| + prepare_satellite!(repo) + + repo.git.sh "git checkout -b #{ref} origin/#{ref}" + File.open(path, 'w'){|f| f.write(content)} + repo.git.sh "git add ." + repo.git.sh "git commit -am '#{commit_message}'" + output = repo.git.sh "git push origin #{ref}" + + # everything worked + true + end + rescue Grit::Git::CommandFailed => ex + Gitlab::GitLogger.error(ex.message) + false + end + + protected + + def can_edit?(path, last_commit) + current_last_commit = @project.last_commit_for(ref, path).sha + last_commit == current_last_commit + end + end + end +end diff --git a/lib/gitlab/satellite/merge_action.rb b/lib/gitlab/satellite/merge_action.rb index ffca6938..edef8f6a 100644 --- a/lib/gitlab/satellite/merge_action.rb +++ b/lib/gitlab/satellite/merge_action.rb @@ -55,11 +55,11 @@ module Gitlab prepare_satellite!(repo) # create target branch in satellite at the corresponding commit from Gitolite - repo.git.checkout({b: true}, merge_request.target_branch, "origin/#{merge_request.target_branch}") + repo.git.checkout({raise: true, b: true}, merge_request.target_branch, "origin/#{merge_request.target_branch}") # merge the source branch from Gitolite into the satellite # will raise CommandFailed when merge fails - repo.git.pull({no_ff: true, raise: true}, :origin, merge_request.source_branch) + repo.git.pull({raise: true, no_ff: true}, :origin, merge_request.source_branch) rescue Grit::Git::CommandFailed => ex Gitlab::GitLogger.error(ex.message) false From 5f16687805b4daa98f7227849d064345e68210d2 Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Fri, 26 Oct 2012 00:39:23 +0200 Subject: [PATCH 07/14] Update arguments for Gitlab::Satellite::EditFileAction#initialize --- app/controllers/tree_controller.rb | 3 +-- lib/gitlab/satellite/edit_file_action.rb | 11 ++++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/controllers/tree_controller.rb b/app/controllers/tree_controller.rb index 60da719c..7b527041 100644 --- a/app/controllers/tree_controller.rb +++ b/app/controllers/tree_controller.rb @@ -26,9 +26,8 @@ class TreeController < ProjectResourceController end def update - file_editor = Gitlab::Satellite::EditFileAction.new(current_user, @project, @ref) + file_editor = Gitlab::Satellite::EditFileAction.new(current_user, @project, @ref, @path) update_status = file_editor.update( - @path, params[:content], params[:commit_message], params[:last_commit] diff --git a/lib/gitlab/satellite/edit_file_action.rb b/lib/gitlab/satellite/edit_file_action.rb index 8949f54c..0a5e753b 100644 --- a/lib/gitlab/satellite/edit_file_action.rb +++ b/lib/gitlab/satellite/edit_file_action.rb @@ -5,15 +5,16 @@ module Gitlab # It gives you ability to make changes to files # & commit this changes from GitLab UI. class EditFileAction < Action - attr_accessor :ref + attr_accessor :path, :ref - def initialize(user, project, ref) + def initialize(user, project, ref, path) super user, project + @path = path @ref = ref end - def update(path, content, commit_message, last_commit) - return false unless can_edit?(path, last_commit) + def update(content, commit_message, last_commit) + return false unless can_edit?(last_commit) in_locked_and_timed_satellite do |repo| prepare_satellite!(repo) @@ -34,7 +35,7 @@ module Gitlab protected - def can_edit?(path, last_commit) + def can_edit?(last_commit) current_last_commit = @project.last_commit_for(ref, path).sha last_commit == current_last_commit end From fba8ad560797500063df84089a99c24fdc5cacd7 Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Fri, 26 Oct 2012 00:56:24 +0200 Subject: [PATCH 08/14] Move Gitlab::Satellite into the Satellite module --- app/roles/repository.rb | 2 +- lib/gitlab/satellite.rb | 40 ------------------------------ lib/gitlab/satellite/satellite.rb | 41 +++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 41 deletions(-) delete mode 100644 lib/gitlab/satellite.rb create mode 100644 lib/gitlab/satellite/satellite.rb diff --git a/app/roles/repository.rb b/app/roles/repository.rb index 8942eaea..88468117 100644 --- a/app/roles/repository.rb +++ b/app/roles/repository.rb @@ -41,7 +41,7 @@ module Repository end def satellite - @satellite ||= Gitlab::Satellite.new(self) + @satellite ||= Gitlab::Satellite::Satellite.new(self) end def has_post_receive_file? diff --git a/lib/gitlab/satellite.rb b/lib/gitlab/satellite.rb deleted file mode 100644 index 64875f3f..00000000 --- a/lib/gitlab/satellite.rb +++ /dev/null @@ -1,40 +0,0 @@ -module Gitlab - class Satellite - - PARKING_BRANCH = "__parking_branch" - - attr_accessor :project - - def initialize(project) - @project = project - end - - #will be deleted all branches except PARKING_BRANCH - def clear - Dir.chdir(path) do - heads = Grit::Repo.new(".").heads.map{|head| head.name} - if heads.include? PARKING_BRANCH - `git checkout #{PARKING_BRANCH}` - else - `git checkout -b #{PARKING_BRANCH}` - end - heads.delete(PARKING_BRANCH) - heads.each do |head| - `git branch -D #{head}` - end - end - end - - def create - `git clone #{project.url_to_repo} #{path}` - end - - def exists? - File.exists? path - end - - def path - Rails.root.join("tmp", "repo_satellites", project.path) - end - end -end diff --git a/lib/gitlab/satellite/satellite.rb b/lib/gitlab/satellite/satellite.rb new file mode 100644 index 00000000..5137cd4c --- /dev/null +++ b/lib/gitlab/satellite/satellite.rb @@ -0,0 +1,41 @@ +module Gitlab + module Satellite + class Satellite + PARKING_BRANCH = "__parking_branch" + + attr_accessor :project + + def initialize(project) + @project = project + end + + #will be deleted all branches except PARKING_BRANCH + def clear + Dir.chdir(path) do + heads = Grit::Repo.new(".").heads.map{|head| head.name} + if heads.include? PARKING_BRANCH + `git checkout #{PARKING_BRANCH}` + else + `git checkout -b #{PARKING_BRANCH}` + end + heads.delete(PARKING_BRANCH) + heads.each do |head| + `git branch -D #{head}` + end + end + end + + def create + `git clone #{project.url_to_repo} #{path}` + end + + def exists? + File.exists? path + end + + def path + Rails.root.join("tmp", "repo_satellites", project.path) + end + end + end +end From 0e9d4f30f4961cc7ee768995a37c5bf9a123c08a Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Fri, 26 Oct 2012 01:32:33 +0200 Subject: [PATCH 09/14] Refactor Satellite#clear and rename it to clear_and_update! --- lib/gitlab/satellite/action.rb | 5 +-- lib/gitlab/satellite/satellite.rb | 56 +++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/lib/gitlab/satellite/action.rb b/lib/gitlab/satellite/action.rb index d50e28fa..9dd52b34 100644 --- a/lib/gitlab/satellite/action.rb +++ b/lib/gitlab/satellite/action.rb @@ -50,10 +50,7 @@ module Gitlab # # Note: use this within #in_locked_and_timed_satellite def prepare_satellite!(repo) - project.satellite.clear - - repo.git.reset(hard: true) - repo.git.fetch({}, :origin) + project.satellite.clear_and_update! repo.git.config({}, "user.name", user.name) repo.git.config({}, "user.email", user.email) diff --git a/lib/gitlab/satellite/satellite.rb b/lib/gitlab/satellite/satellite.rb index 5137cd4c..2147f40a 100644 --- a/lib/gitlab/satellite/satellite.rb +++ b/lib/gitlab/satellite/satellite.rb @@ -9,20 +9,10 @@ module Gitlab @project = project end - #will be deleted all branches except PARKING_BRANCH - def clear - Dir.chdir(path) do - heads = Grit::Repo.new(".").heads.map{|head| head.name} - if heads.include? PARKING_BRANCH - `git checkout #{PARKING_BRANCH}` - else - `git checkout -b #{PARKING_BRANCH}` - end - heads.delete(PARKING_BRANCH) - heads.each do |head| - `git branch -D #{head}` - end - end + def clear_and_update! + delete_heads! + clear_working_dir! + update_from_source! end def create @@ -36,6 +26,44 @@ module Gitlab def path Rails.root.join("tmp", "repo_satellites", project.path) end + + private + + # Clear the working directory + def clear_working_dir! + repo.git.reset(hard: true) + end + + # Deletes all branches except the parking branch + # + # This ensures we have no name clashes or issues updating branches when + # working with the satellite. + def delete_heads! + heads = repo.heads.map{|head| head.name} + + # update or create the parking branch + if heads.include? PARKING_BRANCH + repo.git.checkout({}, PARKING_BRANCH) + else + repo.git.checkout({b: true}, PARKING_BRANCH) + end + + # remove the parking branch from the list of heads ... + heads.delete(PARKING_BRANCH) + # ... and delete all others + heads.each { |head| repo.git.branch({D: true}, head) } + end + + def repo + @repo ||= Grit::Repo.new(path) + end + + # Updates the satellite from Gitolite + # + # Note: this will only update remote branches (i.e. origin/*) + def update_from_source! + repo.git.fetch({}, :origin) + end end end end From 0ebcc60a33f5aba9e7740f43a108b96461dd3fbf Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Fri, 26 Oct 2012 01:44:20 +0200 Subject: [PATCH 10/14] Move locking from Satellite::Action to Satellite and add checks --- lib/gitlab/satellite/action.rb | 18 ++---------------- lib/gitlab/satellite/satellite.rb | 31 +++++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/lib/gitlab/satellite/action.rb b/lib/gitlab/satellite/action.rb index 9dd52b34..ed2541f3 100644 --- a/lib/gitlab/satellite/action.rb +++ b/lib/gitlab/satellite/action.rb @@ -18,18 +18,8 @@ module Gitlab # * Yields the prepared satellite repo def in_locked_and_timed_satellite Grit::Git.with_timeout(options[:git_timeout]) do - File.open(lock_file, "w+") do |f| - f.flock(File::LOCK_EX) - - unless project.satellite.exists? - raise "Satellite doesn't exist" - end - - Dir.chdir(project.satellite.path) do - repo = Grit::Repo.new('.') - - return yield repo - end + project.satellite.lock do + return yield project.satellite.repo end end rescue Errno::ENOMEM => ex @@ -40,10 +30,6 @@ module Gitlab return false end - def lock_file - Rails.root.join("tmp", "#{project.path}.lock") - end - # * Clears the satellite # * Updates the satellite from Gitolite # * Sets up Git variables for the user diff --git a/lib/gitlab/satellite/satellite.rb b/lib/gitlab/satellite/satellite.rb index 2147f40a..0b8d42ae 100644 --- a/lib/gitlab/satellite/satellite.rb +++ b/lib/gitlab/satellite/satellite.rb @@ -10,6 +10,8 @@ module Gitlab end def clear_and_update! + raise "Satellite doesn't exist" unless exists? + delete_heads! clear_working_dir! update_from_source! @@ -23,10 +25,31 @@ module Gitlab File.exists? path end + # Locks the satellite and yields + def lock + raise "Satellite doesn't exist" unless exists? + + File.open(lock_file, "w+") do |f| + f.flock(File::LOCK_EX) + + return yield + end + end + + def lock_file + Rails.root.join("tmp", "#{project.path}.lock") + end + def path Rails.root.join("tmp", "repo_satellites", project.path) end + def repo + raise "Satellite doesn't exist" unless exists? + + @repo ||= Grit::Repo.new(path) + end + private # Clear the working directory @@ -39,7 +62,7 @@ module Gitlab # This ensures we have no name clashes or issues updating branches when # working with the satellite. def delete_heads! - heads = repo.heads.map{|head| head.name} + heads = repo.heads.map(&:name) # update or create the parking branch if heads.include? PARKING_BRANCH @@ -54,15 +77,11 @@ module Gitlab heads.each { |head| repo.git.branch({D: true}, head) } end - def repo - @repo ||= Grit::Repo.new(path) - end - # Updates the satellite from Gitolite # # Note: this will only update remote branches (i.e. origin/*) def update_from_source! - repo.git.fetch({}, :origin) + repo.git.fetch({timeout: true}, :origin) end end end From 3080127354a2c92fd3ceecc05fe93e091e3b5eec Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Fri, 26 Oct 2012 01:44:50 +0200 Subject: [PATCH 11/14] Fix timeouts in MergeAction --- lib/gitlab/satellite/merge_action.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/gitlab/satellite/merge_action.rb b/lib/gitlab/satellite/merge_action.rb index edef8f6a..6818e780 100644 --- a/lib/gitlab/satellite/merge_action.rb +++ b/lib/gitlab/satellite/merge_action.rb @@ -26,12 +26,12 @@ module Gitlab if merge_in_satellite!(merge_repo) # push merge back to Gitolite # will raise CommandFailed when push fails - merge_repo.git.push({raise: true}, :origin, merge_request.target_branch) + merge_repo.git.push({raise: true, timeout: true}, :origin, merge_request.target_branch) # remove source branch if merge_request.should_remove_source_branch && !project.root_ref?(merge_request.source_branch) # will raise CommandFailed when push fails - merge_repo.git.push({raise: true}, :origin, ":#{merge_request.source_branch}") + merge_repo.git.push({raise: true, timeout: true}, :origin, ":#{merge_request.source_branch}") end # merge, push and branch removal successful @@ -55,11 +55,11 @@ module Gitlab prepare_satellite!(repo) # create target branch in satellite at the corresponding commit from Gitolite - repo.git.checkout({raise: true, b: true}, merge_request.target_branch, "origin/#{merge_request.target_branch}") + repo.git.checkout({raise: true, timeout: true, b: true}, merge_request.target_branch, "origin/#{merge_request.target_branch}") # merge the source branch from Gitolite into the satellite # will raise CommandFailed when merge fails - repo.git.pull({raise: true, no_ff: true}, :origin, merge_request.source_branch) + repo.git.pull({raise: true, timeout: true, no_ff: true}, :origin, merge_request.source_branch) rescue Grit::Git::CommandFailed => ex Gitlab::GitLogger.error(ex.message) false From 700a784bc91c0ee24cbe2f1c36589e40b92ac28d Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Fri, 26 Oct 2012 01:59:50 +0200 Subject: [PATCH 12/14] Refactor EditFileAction#update and rename it to commit! --- app/controllers/tree_controller.rb | 6 ++--- lib/gitlab/satellite/edit_file_action.rb | 32 ++++++++++++++++-------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/app/controllers/tree_controller.rb b/app/controllers/tree_controller.rb index 7b527041..725f48fa 100644 --- a/app/controllers/tree_controller.rb +++ b/app/controllers/tree_controller.rb @@ -26,14 +26,14 @@ class TreeController < ProjectResourceController end def update - file_editor = Gitlab::Satellite::EditFileAction.new(current_user, @project, @ref, @path) - update_status = file_editor.update( + edit_file_action = Gitlab::Satellite::EditFileAction.new(current_user, @project, @ref, @path) + updated_successfully = edit_file_action.commit!( params[:content], params[:commit_message], params[:last_commit] ) - if update_status + if updated_successfully redirect_to project_tree_path(@project, @id), notice: "Your changes have been successfully commited" else flash[:notice] = "Your changes could not be commited, because the file has been changed" diff --git a/lib/gitlab/satellite/edit_file_action.rb b/lib/gitlab/satellite/edit_file_action.rb index 0a5e753b..1e2a2163 100644 --- a/lib/gitlab/satellite/edit_file_action.rb +++ b/lib/gitlab/satellite/edit_file_action.rb @@ -5,25 +5,35 @@ module Gitlab # It gives you ability to make changes to files # & commit this changes from GitLab UI. class EditFileAction < Action - attr_accessor :path, :ref + attr_accessor :file_path, :ref - def initialize(user, project, ref, path) - super user, project - @path = path + def initialize(user, project, ref, file_path) + super user, project, git_timeout: 10.seconds + @file_path = file_path @ref = ref end - def update(content, commit_message, last_commit) + def commit!(content, commit_message, last_commit) return false unless can_edit?(last_commit) in_locked_and_timed_satellite do |repo| prepare_satellite!(repo) - repo.git.sh "git checkout -b #{ref} origin/#{ref}" - File.open(path, 'w'){|f| f.write(content)} - repo.git.sh "git add ." - repo.git.sh "git commit -am '#{commit_message}'" - output = repo.git.sh "git push origin #{ref}" + # create target branch in satellite at the corresponding commit from Gitolite + repo.git.checkout({raise: true, timeout: true, b: true}, ref, "origin/#{ref}") + + # update the file in the satellite's working dir + file_path_in_satellite = File.join(repo.working_dir, file_path) + File.open(file_path_in_satellite, 'w') { |f| f.write(content) } + + # commit the changes + # will raise CommandFailed when commit fails + repo.git.commit(raise: true, timeout: true, a: true, m: commit_message) + + + # push commit back to Gitolite + # will raise CommandFailed when push fails + repo.git.push({raise: true, timeout: true}, :origin, ref) # everything worked true @@ -36,7 +46,7 @@ module Gitlab protected def can_edit?(last_commit) - current_last_commit = @project.last_commit_for(ref, path).sha + current_last_commit = @project.last_commit_for(ref, file_path).sha last_commit == current_last_commit end end From 7192f86ed0e2eec1bee94ee148253c1e65f925e3 Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Fri, 26 Oct 2012 02:32:17 +0200 Subject: [PATCH 13/14] Fix Satellite#lock --- lib/gitlab/satellite/satellite.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/gitlab/satellite/satellite.rb b/lib/gitlab/satellite/satellite.rb index 0b8d42ae..28b6f538 100644 --- a/lib/gitlab/satellite/satellite.rb +++ b/lib/gitlab/satellite/satellite.rb @@ -25,14 +25,18 @@ module Gitlab File.exists? path end - # Locks the satellite and yields + # * Locks the satellite + # * Changes the current directory to the satellite's working dir + # * Yields def lock raise "Satellite doesn't exist" unless exists? File.open(lock_file, "w+") do |f| f.flock(File::LOCK_EX) - return yield + Dir.chdir(path) do + return yield + end end end From 904615c1509b0923ba9b785d41cafcd264952d54 Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Fri, 26 Oct 2012 02:50:24 +0200 Subject: [PATCH 14/14] Update satellite action docs --- lib/gitlab/satellite/edit_file_action.rb | 11 +++++++---- lib/gitlab/satellite/merge_action.rb | 2 ++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/gitlab/satellite/edit_file_action.rb b/lib/gitlab/satellite/edit_file_action.rb index 1e2a2163..336afc88 100644 --- a/lib/gitlab/satellite/edit_file_action.rb +++ b/lib/gitlab/satellite/edit_file_action.rb @@ -1,9 +1,6 @@ module Gitlab module Satellite - # GitLab file editor - # - # It gives you ability to make changes to files - # & commit this changes from GitLab UI. + # GitLab server-side file update and commit class EditFileAction < Action attr_accessor :file_path, :ref @@ -13,6 +10,12 @@ module Gitlab @ref = ref end + # Updates the files content and creates a new commit for it + # + # Returns false if the ref has been updated while editing the file + # Returns false if commiting the change fails + # Returns false if pushing from the satellite to Gitolite failed or was rejected + # Returns true otherwise def commit!(content, commit_message, last_commit) return false unless can_edit?(last_commit) diff --git a/lib/gitlab/satellite/merge_action.rb b/lib/gitlab/satellite/merge_action.rb index 6818e780..832db662 100644 --- a/lib/gitlab/satellite/merge_action.rb +++ b/lib/gitlab/satellite/merge_action.rb @@ -1,5 +1,6 @@ module Gitlab module Satellite + # GitLab server-side merge class MergeAction < Action attr_accessor :merge_request @@ -8,6 +9,7 @@ module Gitlab @merge_request = merge_request end + # Checks if a merge request can be executed without user interaction def can_be_merged? in_locked_and_timed_satellite do |merge_repo| merge_in_satellite!(merge_repo)