Sha256: d5431b6cabdcb37f0e2e32b7a01235bf64f6bb8b5c73dc6464db89cc7d2f9cfb

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 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).first
    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

2 entries across 2 versions & 1 rubygems

Version Path
regrit-0.2.0 lib/regrit/remote_repo.rb
regrit-0.1.0 lib/regrit/remote_repo.rb