2012-10-26 00:56:24 +02:00
|
|
|
module Gitlab
|
|
|
|
module Satellite
|
|
|
|
class Satellite
|
|
|
|
PARKING_BRANCH = "__parking_branch"
|
|
|
|
|
|
|
|
attr_accessor :project
|
|
|
|
|
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
|
|
|
end
|
|
|
|
|
2012-10-26 01:32:33 +02:00
|
|
|
def clear_and_update!
|
2012-10-26 01:44:20 +02:00
|
|
|
raise "Satellite doesn't exist" unless exists?
|
|
|
|
|
2012-10-26 01:32:33 +02:00
|
|
|
delete_heads!
|
|
|
|
clear_working_dir!
|
|
|
|
update_from_source!
|
2012-10-26 00:56:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
`git clone #{project.url_to_repo} #{path}`
|
|
|
|
end
|
|
|
|
|
|
|
|
def exists?
|
|
|
|
File.exists? path
|
|
|
|
end
|
|
|
|
|
2012-10-26 02:32:17 +02:00
|
|
|
# * Locks the satellite
|
|
|
|
# * Changes the current directory to the satellite's working dir
|
|
|
|
# * Yields
|
2012-10-26 01:44:20 +02:00
|
|
|
def lock
|
|
|
|
raise "Satellite doesn't exist" unless exists?
|
|
|
|
|
|
|
|
File.open(lock_file, "w+") do |f|
|
|
|
|
f.flock(File::LOCK_EX)
|
|
|
|
|
2012-10-26 02:32:17 +02:00
|
|
|
Dir.chdir(path) do
|
|
|
|
return yield
|
|
|
|
end
|
2012-10-26 01:44:20 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def lock_file
|
|
|
|
Rails.root.join("tmp", "#{project.path}.lock")
|
|
|
|
end
|
|
|
|
|
2012-10-26 00:56:24 +02:00
|
|
|
def path
|
|
|
|
Rails.root.join("tmp", "repo_satellites", project.path)
|
|
|
|
end
|
2012-10-26 01:32:33 +02:00
|
|
|
|
2012-10-26 01:44:20 +02:00
|
|
|
def repo
|
|
|
|
raise "Satellite doesn't exist" unless exists?
|
|
|
|
|
|
|
|
@repo ||= Grit::Repo.new(path)
|
|
|
|
end
|
|
|
|
|
2012-10-26 01:32:33 +02:00
|
|
|
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!
|
2012-10-26 01:44:20 +02:00
|
|
|
heads = repo.heads.map(&:name)
|
2012-10-26 01:32:33 +02:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# Updates the satellite from Gitolite
|
|
|
|
#
|
|
|
|
# Note: this will only update remote branches (i.e. origin/*)
|
|
|
|
def update_from_source!
|
2012-10-26 01:44:20 +02:00
|
|
|
repo.git.fetch({timeout: true}, :origin)
|
2012-10-26 01:32:33 +02:00
|
|
|
end
|
2012-10-26 00:56:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|