Sha256: ef050987d93a7f7228895bd3cd3a38ee8ab996708e33b3b137c3d73a110d9cff

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require 'pathname'

module Spaarti
  ##
  # Repo object, handles individual repo syncing and state
  class Repo
    def initialize(data, client, params)
      @raw = data
      @client = client
      @params = params
      @path = @params[:format] % @raw
    end

    def sync!
      return log("#{@raw[:full_name]} already cloned") if Dir.exist?(@path)
      clone
      Dir.chdir(@path) { config && add_upstream }
    end

    def parent_of(repo)
      repo.relative_path_from(Pathname.new(@path)).each_filename.first != '..'
    end

    private

    def log(msg)
      puts msg unless @params[:quiet]
    end

    def err(msg)
      STDERR.puts msg
    end

    def run(cmd, error_msg)
      res = system "#{cmd} &>/dev/null"
      err(error_msg) unless res
    end

    def clone
      log "Cloning #{@raw[:ssh_url]} to #{@path}"
      run(
        "git clone '#{@raw[:ssh_url]}' '#{@path}' &>/dev/null",
        "Failed to clone #{@raw[:ssh_url]}"
      )
    end

    def config
      @params[:git_config].each do |k, v|
        run("git config '#{k}' '#{v}'", "Failed to set config for #{@path}")
      end
    end

    def add_upstream
      return unless @raw[:fork]
      upstream = @client.repo(@raw[:id]).source.git_url
      run(
        "git remote add upstream '#{upstream}'",
        "Failed to add upstrema for #{@path}"
      )
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spaarti-0.0.2 lib/spaarti/repo.rb