Sha256: 5069cc3258ee87460044f35e0f905f6dc45e5cec305109cd733ce0065927ca26

Contents?: true

Size: 1.7 KB

Versions: 2

Compression:

Stored size: 1.7 KB

Contents

require 'open4'
require 'escape'
require 'git-ssh-wrapper'
require 'system_timer'

module Regrit
  module Provider
    class System
      DEFAULT_TIMEOUT = 5

      def initialize(uri, options)
        @uri = uri
        @timeout = options[:timeout] || DEFAULT_TIMEOUT
        load_git_ssh_wrapper(options) if @uri.ssh?
      end

      def ls_remote(named=nil)
        git "ls-remote", @uri.to_s, named
      end

      def clone(*argv)
        git "clone", *argv
      end

      def fetch(*argv)
        git "fetch", *argv
      end

      def push(*argv)
        git "push", *argv
      end

      def pull(*argv)
        git "pull", *argv
      end

      def git(*argv)
        spawn "#{git_command} #{Escape.shell_command(argv.flatten.compact)}"
      end

      protected

      # Only require open4 here so as not to set a hard dependency.
      #
      # Might raise CommandError or TimeoutError
      def spawn(command)
        stdout, stderr = '', ''
        SystemTimer.timeout(@timeout) do
          Open4.spawn(command, :stdout => stdout, :stderr => stderr, :timeout => @timeout)
        end
        stdout
      rescue Open4::SpawnError => e
        raise CommandError.new(e.cmd, e.exitstatus, stdout, stderr)
      rescue Timeout::Error
        raise TimeoutError.new(command, @uri)
      end

      def git_command
        # GIT_ASKPASS='echo' keeps password prompts from stalling the proccess (they still fail)
        "env GIT_ASKPASS='echo' #{@uri.ssh? ? @git_ssh_wrapper.git_ssh : ''} git"
      end

      def load_git_ssh_wrapper(options)
        @git_ssh_wrapper = GitSSHWrapper.new(options)
      rescue GitSSHWrapper::PrivateKeyRequired
        raise Regrit::PrivateKeyRequired.new(@uri)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
regrit-0.0.6 lib/regrit/provider/system.rb
regrit-0.0.5 lib/regrit/provider/system.rb