From 0e9d4f30f4961cc7ee768995a37c5bf9a123c08a Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Fri, 26 Oct 2012 01:32:33 +0200 Subject: [PATCH] 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 d50e28f..9dd52b3 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 5137cd4..2147f40 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 -- libgit2 0.21.2