Sha256: 80cc02509e1b6d856bbab555ba02b12187f014756c0cd7a0ee326fa945ff45c9

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

require 'fileutils'
require_relative 'null_logger'

module GitDuplicator
  class Duplicator
    attr_accessor :from, :to, :logger, :clone_path, :force_create_destination

    # @param [GitDuplicator::Repository, GitDuplicator::ServiceRepository]
    #  from repository to mirror
    # @param [GitDuplicator::Repository, GitDuplicator::ServiceRepository]
    #  to repository to mirror to
    # @param [Hash] options
    # @option options [String] :clone_path
    #   path to clone the repository to
    # @option options [Boolean] :force_create_destination
    #  delete the exisiting service repo then create it
    # @option options [#info] :logger log what's going on
    def initialize(from, to, options = {})
      self.from = from
      self.to = to
      self.clone_path = options.fetch(:clone_path) { '/tmp' }
      self.force_create_destination =
        options.fetch(:force_create_destination) { false }
      self.logger = options.fetch(:logger) { NullLogger.new }
    end

    # Perform the duplication
    def perform
      recreate_destination
      clone_source
      mirror
    ensure
      clean_up
    end

    private

    attr_accessor :source_cloned

    def recreate_destination
      return unless force_create_destination
      logger.info("Deleting existing destination repo: #{to.url}")
      to.delete
      logger.info("Creating destination repo: #{to.url}")
      to.create
    end

    def clone_source
      logger.info("Cloning bare Gitorious repo: #{from.url}")
      from.bare_clone(clone_path)
      self.source_cloned = true
    end

    def mirror
      logger.info("Mirroring Gitorious to Bitbucket: #{from.url}")
      from.mirror(to.url)
    end

    def clean_up
      return unless source_cloned
      logger.info 'Clean local source repo'
      FileUtils.rm_rf("#{clone_path}/#{from.name}")
      @cloned = nil
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
git_duplicator-0.0.1 lib/git_duplicator/duplicator.rb