Skip to content

Commit ca869fe

Browse files
committed
Do not change ENV variables of a current process
Instead pass relevant environment to the actual shell command being run Signed-off-by: Pavel Forkert <fxposter@gmail.com>
1 parent 5d269d8 commit ca869fe

File tree

1 file changed

+12
-52
lines changed

1 file changed

+12
-52
lines changed

lib/git/lib.rb

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
module Git
77
class Lib
88

9-
@@semaphore = Mutex.new
10-
119
# The path to the Git working copy. The default is '"./.git"'.
1210
#
1311
# @return [Pathname] the path to the Git working copy.
@@ -1142,41 +1140,14 @@ def command_lines(cmd, *opts, chdir: nil)
11421140
op.split("\n")
11431141
end
11441142

1145-
# Takes the current git's system ENV variables and store them.
1146-
def store_git_system_env_variables
1147-
@git_system_env_variables = {}
1148-
ENV_VARIABLE_NAMES.each do |env_variable_name|
1149-
@git_system_env_variables[env_variable_name] = ENV[env_variable_name]
1150-
end
1151-
end
1152-
1153-
# Takes the previously stored git's ENV variables and set them again on ENV.
1154-
def restore_git_system_env_variables
1155-
ENV_VARIABLE_NAMES.each do |env_variable_name|
1156-
ENV[env_variable_name] = @git_system_env_variables[env_variable_name]
1157-
end
1158-
end
1159-
1160-
# Sets git's ENV variables to the custom values for the current instance.
1161-
def set_custom_git_env_variables
1162-
ENV['GIT_DIR'] = @git_dir
1163-
ENV['GIT_WORK_TREE'] = @git_work_dir
1164-
ENV['GIT_INDEX_FILE'] = @git_index_file
1165-
ENV['GIT_SSH'] = Git::Base.config.git_ssh
1166-
end
1167-
1168-
# Runs a block inside an environment with customized ENV variables.
1169-
# It restores the ENV after execution.
1170-
#
1171-
# @param [Proc] block block to be executed within the customized environment
1172-
def with_custom_env_variables(&block)
1173-
@@semaphore.synchronize do
1174-
store_git_system_env_variables()
1175-
set_custom_git_env_variables()
1176-
return block.call()
1177-
end
1178-
ensure
1179-
restore_git_system_env_variables()
1143+
# git's ENV variables for the current instance.
1144+
def custom_git_env_variables
1145+
{
1146+
'GIT_DIR' => @git_dir,
1147+
'GIT_WORK_TREE' => @git_work_dir,
1148+
'GIT_INDEX_FILE' => @git_index_file,
1149+
'GIT_SSH' => Git::Base.config.git_ssh,
1150+
}
11801151
end
11811152

11821153
def command(*cmd, redirect: '', chomp: true, chdir: nil, &block)
@@ -1196,19 +1167,8 @@ def command(*cmd, redirect: '', chomp: true, chdir: nil, &block)
11961167

11971168
git_cmd = "#{Git::Base.config.binary_path} #{global_opts} #{escaped_cmd} #{redirect} 2>&1"
11981169

1199-
output = nil
1200-
1201-
command_thread = nil;
1202-
1203-
status = nil
1204-
1205-
with_custom_env_variables do
1206-
command_thread = Thread.new do
1207-
output = run_command(git_cmd, chdir, &block)
1208-
status = $?
1209-
end
1210-
command_thread.join
1211-
end
1170+
output = run_command(git_cmd, chdir, &block)
1171+
status = $?
12121172

12131173
@logger.info(git_cmd)
12141174
@logger.debug(output)
@@ -1293,9 +1253,9 @@ def run_command(git_cmd, chdir=nil, &block)
12931253
end
12941254

12951255
if chdir
1296-
IO.popen(git_cmd, chdir: chdir, &block)
1256+
IO.popen(custom_git_env_variables, git_cmd, chdir: chdir, &block)
12971257
else
1298-
IO.popen(git_cmd, &block)
1258+
IO.popen(custom_git_env_variables, git_cmd, &block)
12991259
end
13001260
end
13011261

0 commit comments

Comments
 (0)