Class: Ssh2http

Inherits:
Object
  • Object
show all
Defined in:
lib/ssh2http.rb,
lib/ssh2http/version.rb

Constant Summary collapse

VERSION =
"1.0.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(destination, *cmds) ⇒ Ssh2http

Returns a new instance of Ssh2http.



5
6
7
8
# File 'lib/ssh2http.rb', line 5

def initialize(destination, *cmds)
  @destination = destination
  @cmds = cmds
end

Class Method Details

.die(msg) ⇒ Object



106
107
108
109
# File 'lib/ssh2http.rb', line 106

def self.die(msg)
  STDERR.puts msg
  exit 1
end

Instance Method Details

#cmdObject



76
77
78
# File 'lib/ssh2http.rb', line 76

def cmd
  @cmds[0]
end

#debugObject



88
89
90
91
92
93
94
95
96
# 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) ⇒ Object



102
103
104
# File 'lib/ssh2http.rb', line 102

def die(msg)
  self.class.die("#{@destination} #{@cmds} #{msg}")
end

#keyObject



80
81
82
# File 'lib/ssh2http.rb', line 80

def key
  @cmds[1].gsub(/['"]/, '')
end

#receive!Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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!Object



10
11
12
13
14
15
16
17
18
19
# 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!Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



84
85
86
# File 'lib/ssh2http.rb', line 84

def url(part)
  "#{@destination}#{key}#{part}"
end

#var(var) ⇒ Object



98
99
100
# File 'lib/ssh2http.rb', line 98

def var(var)
  STDERR.puts "#{var}=#{eval(var).inspect}"
end