Skip to content

Commit 66f4f77

Browse files
committed
Unclutter #command and #command_lines
These internal function start to have too many arguments, and in order to avoid chomping when not applicable, we will need to add one more boolean. Pass a hash of option instead of distinct arguments to make it easier to scan the source code. No functionnal change.
1 parent c8d1012 commit 66f4f77

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

lib/git/lib.rb

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def init(opts={})
3737
arr_opts = []
3838
arr_opts << '--bare' if opts[:bare]
3939

40-
command('init', arr_opts, false)
40+
command('init', arr_opts, chdir: false)
4141
end
4242

4343
# tries to clone the given repo
@@ -132,7 +132,7 @@ def log_commits(opts={})
132132

133133
arr_opts += log_path_options(opts)
134134

135-
command_lines('log', arr_opts, true).map { |l| l.split.first }
135+
command_lines('log', arr_opts, chdir: true).map { |l| l.split.first }
136136
end
137137

138138
def full_log_commits(opts={})
@@ -143,7 +143,7 @@ def full_log_commits(opts={})
143143

144144
arr_opts += log_path_options(opts)
145145

146-
full_log = command_lines('log', arr_opts, true)
146+
full_log = command_lines('log', arr_opts, chdir: true)
147147

148148
process_commit_log_data(full_log)
149149
end
@@ -411,7 +411,7 @@ def ls_files(location=nil)
411411
def ls_remote(location=nil)
412412
location ||= '.'
413413
Hash.new{ |h,k| h[k] = {} }.tap do |hsh|
414-
command_lines('ls-remote', [location], false).each do |line|
414+
command_lines('ls-remote', [location], chdir: false).each do |line|
415415
(sha, info) = line.split("\t")
416416
(ref, type, name) = info.split('/', 3)
417417
type ||= 'head'
@@ -450,7 +450,7 @@ def config_get(name)
450450
end
451451

452452
def global_config_get(name)
453-
command('config', ['--global', '--get', name], false)
453+
command('config', ['--global', '--get', name], chdir: false)
454454
end
455455

456456
def config_list
@@ -466,7 +466,7 @@ def config_list
466466
end
467467

468468
def global_config_list
469-
parse_config_list command_lines('config', ['--global', '--list'], false)
469+
parse_config_list command_lines('config', ['--global', '--list'], chdir: false)
470470
end
471471

472472
def parse_config_list(lines)
@@ -479,7 +479,7 @@ def parse_config_list(lines)
479479
end
480480

481481
def parse_config(file)
482-
parse_config_list command_lines('config', ['--list', '--file', file], false)
482+
parse_config_list command_lines('config', ['--list', '--file', file], chdir: false)
483483
end
484484

485485
# Shows objects
@@ -502,7 +502,7 @@ def config_set(name, value)
502502
end
503503

504504
def global_config_set(name, value)
505-
command('config', ['--global', name, value], false)
505+
command('config', ['--global', name, value], chdir: false)
506506
end
507507

508508
# updates the repository index using the working directory content
@@ -671,10 +671,10 @@ def unmerged
671671
def conflicts # :yields: file, your, their
672672
self.unmerged.each do |f|
673673
your = Tempfile.new("YOUR-#{File.basename(f)}").path
674-
command('show', ":2:#{f}", true, "> #{escape your}")
674+
command('show', ":2:#{f}", chdir: true, redirect: "> #{escape your}")
675675

676676
their = Tempfile.new("THEIR-#{File.basename(f)}").path
677-
command('show', ":3:#{f}", true, "> #{escape their}")
677+
command('show', ":3:#{f}", chdir: true, redirect: "> #{escape their}")
678678
yield(f, your, their)
679679
end
680680
end
@@ -799,7 +799,7 @@ def commit_tree(tree, opts = {})
799799
arr_opts << tree
800800
arr_opts << '-p' << opts[:parent] if opts[:parent]
801801
arr_opts += [opts[:parents]].map { |p| ['-p', p] }.flatten if opts[:parents]
802-
command('commit-tree', arr_opts, true, "< #{escape t.path}")
802+
command('commit-tree', arr_opts, chdir: true, redirect: "< #{escape t.path}")
803803
end
804804

805805
def update_ref(branch, commit)
@@ -845,13 +845,13 @@ def archive(sha, file = nil, opts = {})
845845
arr_opts << "--remote=#{opts[:remote]}" if opts[:remote]
846846
arr_opts << sha
847847
arr_opts << '--' << opts[:path] if opts[:path]
848-
command('archive', arr_opts, true, (opts[:add_gzip] ? '| gzip' : '') + " > #{escape file}")
848+
command('archive', arr_opts, chdir: true, redirect: (opts[:add_gzip] ? '| gzip' : '') + " > #{escape file}")
849849
return file
850850
end
851851

852852
# returns the current version of git, as an Array of Fixnums.
853853
def current_command_version
854-
output = command('version', [], false)
854+
output = command('version', [], chdir: false)
855855
version = output[/\d+\.\d+(\.\d+)+/]
856856
version.split('.').collect {|i| i.to_i}
857857
end
@@ -872,8 +872,8 @@ def meets_required_version?
872872
# @return [<String>] the names of the EVN variables involved in the git commands
873873
ENV_VARIABLE_NAMES = ['GIT_DIR', 'GIT_WORK_TREE', 'GIT_INDEX_FILE', 'GIT_SSH']
874874

875-
def command_lines(cmd, opts = [], chdir = true, redirect = '')
876-
cmd_op = command(cmd, opts, chdir)
875+
def command_lines(cmd, *opts)
876+
cmd_op = command(cmd, *opts)
877877
if cmd_op.encoding.name != "UTF-8"
878878
op = cmd_op.encode("UTF-8", "binary", {
879879
:invalid => :replace,
@@ -922,7 +922,18 @@ def with_custom_env_variables(&block)
922922
restore_git_system_env_variables()
923923
end
924924

925-
def command(cmd, opts = [], chdir = true, redirect = '', &block)
925+
def command(cmd, *opts, &block)
926+
command_opts = { chdir: true, redirect: '' }
927+
if opts.last.is_a?(Hash)
928+
command_opts.merge!(opts.pop)
929+
end
930+
command_opts.keys.each do |k|
931+
raise ArgumentError.new("Unsupported option: #{k}") unless %i[chdir redirect].include?(k)
932+
end
933+
934+
default_command_opts = { chdir: true, redirect: '' }
935+
command_opts = default_command_opts.merge(command_opts)
936+
926937
global_opts = []
927938
global_opts << "--git-dir=#{@git_dir}" if !@git_dir.nil?
928939
global_opts << "--work-tree=#{@git_work_dir}" if !@git_work_dir.nil?
@@ -931,7 +942,7 @@ def command(cmd, opts = [], chdir = true, redirect = '', &block)
931942

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

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

936947
output = nil
937948

0 commit comments

Comments
 (0)