class Ssh2http

Constants

VERSION

Public Class Methods

die(msg) click to toggle source
# File lib/ssh2http.rb, line 106
def self.die(msg)
  STDERR.puts msg
  exit 1
end
new(destination, *cmds) click to toggle source
# File lib/ssh2http.rb, line 5
def initialize(destination, *cmds)
  @destination = destination
  @cmds = cmds
end

Public Instance Methods

cmd() click to toggle source
# File lib/ssh2http.rb, line 76
def cmd
  @cmds[0]
end
debug() click to toggle source
# File lib/ssh2http.rb, line 88
def debug
  var('@cmds')
  var('@destination')
  var('RUBY_VERSION')
  var('::Ssh2http::VERSION')
  ENV.each do |k,v|
    var("ENV[#{k.inspect}]")
  end
end
die(msg) click to toggle source
# File lib/ssh2http.rb, line 102
def die(msg)
  self.class.die("#{@destination} #{@cmds} #{msg}")
end
key() click to toggle source
# File lib/ssh2http.rb, line 80
def key
  @cmds[1].gsub(/['"]/, '')
end
receive!() click to toggle source
# File lib/ssh2http.rb, line 53
def receive!
  open(url('/info/refs?service=git-receive-pack')) do |f|
    f.each_line do |line|
      if "001f# service=git-receive-pack\n" == line
        f.read(4) # skip "0000"
        next
      end
      print line
    end
  end
  STDOUT.flush

  input = STDIN.read
  url = URI.parse(url('/git-receive-pack'))
  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Post.new(url.path)
  request.body = input
  request['Content-Type'] = 'application/x-git-receive-pack-request'
  response = http.request(request)
  print response.body
  STDOUT.flush
end
run!() click to toggle source
# File lib/ssh2http.rb, line 10
def run!
  case cmd
  when 'git-upload-pack'
    upload!
  when 'git-receive-pack'
    receive!
  else
    die 'Unknown command'
  end
end
upload!() click to toggle source
# File lib/ssh2http.rb, line 21
def upload!
  open(url('/info/refs?service=git-upload-pack')) do |f|
    f.each_line do |line|
      if "001e# service=git-upload-pack\n" == line
        f.read(4) # skip "0000"
        next
      end
      print line
    end
  end
  STDOUT.flush

  input = ''
  while line = STDIN.gets
    input += line
    break if line =~ /0009done\n$/
  end

  url = URI.parse(url('/git-upload-pack'))
  Net::HTTP.start(url.host, url.port) do |http|
    request = Net::HTTP::Post.new url.path
    request.body = input
    request['Content-Type'] = 'application/x-git-upload-pack-request'
    http.request request do |response|
      response.read_body do |chunk|
        STDOUT.write chunk
        STDOUT.flush
      end
    end
  end
end
url(part) click to toggle source
# File lib/ssh2http.rb, line 84
def url(part)
  "#{@destination}#{key}#{part}"
end
var(var) click to toggle source
# File lib/ssh2http.rb, line 98
def var(var)
  STDERR.puts "#{var}=#{eval(var).inspect}"
end