Skip to content

Internal refactor of Git::Lib command #618

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

Merged
merged 4 commits into from
Feb 16, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Pass options to #command as keyword args instead of a hash
  • Loading branch information
jcouball committed Feb 15, 2023
commit 85f07c9eb0664de0e2a24d863a5e37e20df41b02
14 changes: 3 additions & 11 deletions lib/git/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1106,17 +1106,9 @@ def with_custom_env_variables(&block)
restore_git_system_env_variables()
end

def command(cmd, *opts, &block)
def command(cmd, *opts, redirect: '', chomp: true, &block)
Git::Lib.warn_if_old_command(self)

command_opts = { chomp: true, redirect: '' }
if opts.last.is_a?(Hash)
command_opts.merge!(opts.pop)
end
command_opts.keys.each do |k|
raise ArgumentError.new("Unsupported option: #{k}") unless [:chomp, :redirect].include?(k)
end

global_opts = []
global_opts << "--git-dir=#{@git_dir}" if !@git_dir.nil?
global_opts << "--work-tree=#{@git_work_dir}" if !@git_work_dir.nil?
Expand All @@ -1127,7 +1119,7 @@ def command(cmd, *opts, &block)

global_opts = global_opts.map { |s| escape(s) }.join(' ')

git_cmd = "#{Git::Base.config.binary_path} #{global_opts} #{cmd} #{opts} #{command_opts[:redirect]} 2>&1"
git_cmd = "#{Git::Base.config.binary_path} #{global_opts} #{cmd} #{opts} #{redirect} 2>&1"

output = nil

Expand All @@ -1151,7 +1143,7 @@ def command(cmd, *opts, &block)
raise Git::GitExecuteError, "#{git_cmd}:#{output}" if
exitstatus > 1 || (exitstatus == 1 && output != '')

output.chomp! if output && command_opts[:chomp] && !block_given?
output.chomp! if output && chomp && !block_given?

output
end
Expand Down