Skip to content

Commit 4059bcd

Browse files
committed
Support calling subprocesses with JRuby
1 parent 9d57386 commit 4059bcd

File tree

1 file changed

+64
-24
lines changed

1 file changed

+64
-24
lines changed

lib/git/lib.rb

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,52 @@ module Git
77
class GitExecuteError < StandardError
88
end
99

10+
class JRubySubprocessCommand
11+
@@semaphore = Mutex.new
12+
13+
def run(env_overrides, cmd, &block)
14+
saved_env = {}
15+
@@semaphore.synchronize do
16+
saved_env = ENV.slice(*env_overrides.keys)
17+
saved_env.each { |k, v| saved_env[k] = nil unless saved_env.key?(k) }
18+
ENV.merge!(env_overrides)
19+
stdout, stderr, exitstatus = run_command(env, cmd, &block)
20+
end
21+
ensure
22+
ENV.merge!(saved_env)
23+
end
24+
25+
private
26+
27+
def run_command(cmd, &block)
28+
if block_given?
29+
output = IO.popen(cmd, &block)
30+
exitstatus = $?.exitstatus
31+
[output, exitstatus]
32+
else
33+
encoded_output, status = Open3.capture2e(cmd)
34+
output = encoded_output.lines.map { |l| Git::EncodingUtils.normalize_encoding(l) }.join
35+
exitstatus = status.exitstatus
36+
[output, exitstatus]
37+
end
38+
end
39+
end
40+
41+
class SubprocessCommand
42+
def run(env_overrides, cmd, &block)
43+
if block_given?
44+
stdout = IO.popen(env_overrides, cmd, &block)
45+
exitstatus = $?.exitstatus
46+
[stdout, "", exitstatus]
47+
else
48+
encoded_output, status = Open3.capture2e(env_overrides, cmd)
49+
stdout = encoded_output.lines.map { |l| Git::EncodingUtils.normalize_encoding(l) }.join
50+
exitstatus = status.exitstatus
51+
[stdout, "", exitstatus]
52+
end
53+
end
54+
end
55+
1056
class Lib
1157
# The path to the Git working copy. The default is '"./.git"'.
1258
#
@@ -1047,13 +1093,13 @@ def command_lines(cmd, *opts)
10471093
op.split("\n")
10481094
end
10491095

1050-
def env
1051-
ENV.to_h.tap do |env|
1052-
env['GIT_DIR'] = @git_dir
1053-
env['GIT_WORK_TREE'] = @git_work_dir
1054-
env['GIT_INDEX_FILE'] = @git_index_file
1055-
env['GIT_SSH'] = Git::Base.config.git_ssh
1056-
end
1096+
def env_overrides
1097+
{
1098+
'GIT_DIR' => @git_dir,
1099+
'GIT_WORK_TREE' => @git_work_dir,
1100+
'GIT_INDEX_FILE' => @git_index_file,
1101+
'GIT_SSH' => Git::Base.config.git_ssh
1102+
}
10571103
end
10581104

10591105
DEFAULT_COMMAND_OPTS = {
@@ -1078,6 +1124,11 @@ def global_opts
10781124
end
10791125
end
10801126

1127+
def subprocess_command
1128+
return @subprocess_command if @subprocess_command
1129+
@subprocess_command = (RUBY_ENGINE == 'java' ? JRubySubprocessCommand.new : SubprocessCommand.new)
1130+
end
1131+
10811132
def command(cmd, *opts, &block)
10821133
given_command_opts = opts.last.is_a?(Hash) ? opts.pop : {}
10831134
command_opts = command_opts(given_command_opts)
@@ -1092,30 +1143,19 @@ def command(cmd, *opts, &block)
10921143

10931144
exitstatus = nil
10941145

1095-
output, exitstatus = begin
1096-
if block_given?
1097-
output = IO.popen(env, git_cmd, &block)
1098-
exitstatus = $?.exitstatus
1099-
[output, exitstatus]
1100-
else
1101-
encoded_output, status = Open3.capture2e(env, git_cmd)
1102-
output = encoded_output.lines.map { |l| Git::EncodingUtils.normalize_encoding(l) }.join
1103-
exitstatus = status.exitstatus
1104-
[output, exitstatus]
1105-
end
1106-
end
1146+
stdout, stderr, exitstatus = subprocess_command.run(env_overrides, git_cmd, &block)
11071147

11081148
if @logger
11091149
@logger.info(git_cmd)
1110-
@logger.debug(output)
1150+
@logger.debug(stdout)
11111151
end
11121152

1113-
raise Git::GitExecuteError, "#{git_cmd}:#{output}" if
1114-
exitstatus > 1 || (exitstatus == 1 && output != '')
1153+
raise Git::GitExecuteError, "#{git_cmd}:#{stdout}" if
1154+
exitstatus > 1 || (exitstatus == 1 && stdout != '')
11151155

1116-
output.chomp! if output && command_opts[:chomp] && !block_given?
1156+
stdout.chomp! if stdout && command_opts[:chomp] && !block_given?
11171157

1118-
output
1158+
stdout
11191159
end
11201160

11211161
# Takes the diff command line output (as Array) and parse it into a Hash

0 commit comments

Comments
 (0)