Skip to content

Naively inject env into backticks and IO.popen #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions lib/git/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -734,24 +734,18 @@ def command_lines(cmd, opts = [], chdir = true, redirect = '')
end

def command(cmd, opts = [], chdir = true, redirect = '', &block)
ENV['GIT_DIR'] = @git_dir
ENV['GIT_WORK_TREE'] = @git_work_dir
ENV['GIT_INDEX_FILE'] = @git_index_file
env = {}
env['GIT_DIR'] = @git_dir if @git_dir
env['GIT_WORK_TREE'] = @git_work_dir if @git_work_dir
env['GIT_INDEX_FILE'] = @git_index_file if @git_index_file

path = @git_work_dir || @git_dir || @path

opts = [opts].flatten.map {|s| escape(s) }.join(' ')

git_cmd = "git #{cmd} #{opts} #{redirect} 2>&1"

output = nil

if chdir && (Dir.getwd != path)
Dir.chdir(path) { output = run_command(git_cmd, &block) }
else
output = run_command(git_cmd, &block)
end

output = run_command(env, git_cmd, &block)

if @logger
@logger.info(git_cmd)
@logger.debug(output)
Expand Down Expand Up @@ -818,10 +812,11 @@ def log_path_options(opts)
arr_opts
end

def run_command(git_cmd, &block)
return IO.popen(git_cmd, &block) if block_given?
def run_command(env, git_cmd, &block)
return IO.popen(env, git_cmd, &block) if block_given?

`#{git_cmd}`.chomp
inline_env = env.map { |k, v| "#{k}=#{v}" }.join(' ')
`#{inline_env} #{git_cmd}`.chomp
end

def escape(s)
Expand Down