From 0ebcc60a33f5aba9e7740f43a108b96461dd3fbf Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Fri, 26 Oct 2012 01:44:20 +0200 Subject: [PATCH] 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