Skip to content

Commit 0401cff

Browse files
committed
Added ruby 1.8.7 compatible backport of Open3::capture3; refs dergachev#8
1 parent a3142b5 commit 0401cff

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

app/models/repository/git_remote.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
require 'redmine/scm/adapters/git_adapter'
22
require 'pathname'
33
require 'fileutils'
4-
require 'open3'
4+
# require 'open3'
5+
require_dependency 'redmine_git_remote/poor_mans_capture3'
56

67
class Repository::GitRemote < Repository::Git
78

@@ -97,7 +98,7 @@ def ensure_possibly_empty_clone_exists
9798
end
9899

99100
if Dir.exists? clone_path
100-
existing_repo_remote, status = Open3::capture2("git", "--git-dir", clone_path, "config", "--get", "remote.origin.url")
101+
existing_repo_remote, status = RedmineGitRemote::PoorMansCapture3::capture2("git", "--git-dir", clone_path, "config", "--get", "remote.origin.url")
101102
return "Unable to run: git --git-dir #{clone_path} config --get remote.origin.url" unless status.success?
102103

103104
unless two_remotes_equal(existing_repo_remote, clone_url)
@@ -154,7 +155,7 @@ def fetch
154155
# Checks if host is in ~/.ssh/known_hosts, adds it if not present
155156
def self.add_known_host(host)
156157
# if not found...
157-
out, status = Open3::capture2("ssh-keygen", "-F", host)
158+
out, status = RedmineGitRemote::PoorMansCapture3::capture2("ssh-keygen", "-F", host)
158159
raise "Unable to run 'ssh-keygen -F #{host}" unless status
159160
unless out.match /found/
160161
# hack to work with 'docker exec' where HOME isn't set (or set to /)
@@ -167,7 +168,7 @@ def self.add_known_host(host)
167168
end
168169

169170
puts "Adding #{host} to #{ssh_known_hosts}"
170-
out, status = Open3::capture2("ssh-keyscan", host)
171+
out, status = RedmineGitRemote::PoorMansCapture3::capture2("ssh-keyscan", host)
171172
raise "Unable to run 'ssh-keyscan #{host}'" unless status
172173
Kernel::open(ssh_known_hosts, 'a') { |f| f.puts out}
173174
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Ruby 1.8.7-compatible backport of Open3::capture3
2+
#
3+
# via https://gist.github.com/vasi/8ffc21bc09ac8fe38f76
4+
module RedmineGitRemote
5+
module PoorMansCapture3
6+
7+
def self.capture3(*cmd)
8+
# Force no shell expansion, by using a non-plain string. See ruby docs:
9+
#
10+
# `If the first argument is a two-element array, the first element is the
11+
# command to be executed, and the second argument is used as the argv[0]
12+
# value, which may show up in process listings.'
13+
cmd[0] = [cmd[0], cmd[0]]
14+
15+
rout, wout = IO.pipe
16+
rerr, werr = IO.pipe
17+
18+
pid = fork do
19+
rerr.close
20+
rout.close
21+
STDERR.reopen(werr)
22+
STDOUT.reopen(wout)
23+
exec(*cmd)
24+
end
25+
26+
wout.close
27+
werr.close
28+
29+
out = rout.read
30+
err = rerr.read
31+
Process.wait(pid)
32+
rout.close
33+
rerr.close
34+
return [out, err, $?]
35+
end
36+
37+
def self.capture2(*cmd)
38+
out, err, stat = capture3(*cmd)
39+
STDERR.write err
40+
return out, stat
41+
end
42+
43+
def self.test(*cmd)
44+
st, err, out = capture3(*cmd)
45+
p st
46+
p err
47+
p out
48+
puts
49+
end
50+
51+
def self.run_tests
52+
test('ls', '/var')
53+
test('ls', '/foo')
54+
test('lfhlkhladfla')
55+
test('ls && ls')
56+
end
57+
end
58+
end

0 commit comments

Comments
 (0)