Sha256: 24c1111832e7cd5c90ed53cd035dda5fcefd66e3cc7a0a140496b953ee740222

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

require 'gitable'
require 'regrit/ref'

module Regrit
  class RemoteRepo

    REFS_REGEXP = /^[0-9a-f]{40}\t\w/i

    attr_reader :uri

    def initialize(uri, options={})
      if uri.nil? || uri.empty?
        raise InvalidURIError
      end

      begin
        @uri = Gitable::URI.parse(uri)
      rescue TypeError, Gitable::URI::InvalidURIError
        raise InvalidURIError
      end

      if @uri.interactive_authenticated?
        raise InvalidURIError
      end

      @options = options
    end

    # Decide if the URI is likely to require authentication
    # @return [Boolean] Does the repo require auth?
    def private_key_required?
      @uri.ssh?
    end

    # Attempt to grab refs. If the repository is auth required and a private key
    # is passed, use ssh to attempt access to the repository.
    #
    # @return [Boolean] can the repository be accessed?
    def accessible?
      !!refs
    rescue Inaccessible
      false
    end

    # Use a git ls-remote to load all repository refs
    #
    # @return [Array] An Array of Ref objects
    def refs
      @refs ||= load_refs
    end

    # Use a git ls-remote to find a single ref
    #
    # @return [Ref, nil] A Ref object or nil
    def ref(named)
      load_refs(named).detect { |ref| ref.match?(named) }
    end

    private

    def provider
      @provider ||= Provider.new(@uri, @options)
    end

    def load_refs(named=nil)
      raw_refs = provider.ls_remote(named)

      return [] if raw_refs.empty?

      unless raw_refs =~ REFS_REGEXP
        raise InvalidRefsFormat.new(raw_refs)
      end

      raw_refs.split(/\n/).map { |ref| Ref.new(self, ref) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
regrit-0.2.1 lib/regrit/remote_repo.rb