Skip to content

Commit c7946b0

Browse files
committed
fix: fix Rubocop Metrics/ParameterLists offense
1 parent 9c856ba commit c7946b0

File tree

3 files changed

+105
-66
lines changed

3 files changed

+105
-66
lines changed

.rubocop_todo.yml

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,6 @@
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 68
10-
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
11-
Metrics/AbcSize:
12-
Max: 109
13-
14-
# Offense count: 21
15-
# Configuration parameters: CountComments, CountAsOne.
16-
Metrics/ClassLength:
17-
Max: 898
18-
19-
# Offense count: 14
20-
# Configuration parameters: AllowedMethods, AllowedPatterns.
21-
Metrics/CyclomaticComplexity:
22-
Max: 21
23-
24-
# Offense count: 111
25-
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
26-
Metrics/MethodLength:
27-
Max: 51
28-
29-
# Offense count: 2
30-
# Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
31-
Metrics/ParameterLists:
32-
Max: 8
33-
349
# Offense count: 12
3510
# Configuration parameters: AllowedMethods, AllowedPatterns.
3611
Metrics/PerceivedComplexity:
@@ -115,3 +90,23 @@ Style/OptionalBooleanParameter:
11590
# URISchemes: http, https
11691
Layout/LineLength:
11792
Max: 346
93+
94+
# Offense count: 68
95+
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
96+
Metrics/AbcSize:
97+
Max: 109
98+
99+
# Offense count: 21
100+
# Configuration parameters: CountComments, CountAsOne.
101+
Metrics/ClassLength:
102+
Max: 925
103+
104+
# Offense count: 14
105+
# Configuration parameters: AllowedMethods, AllowedPatterns.
106+
Metrics/CyclomaticComplexity:
107+
Max: 21
108+
109+
# Offense count: 111
110+
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
111+
Metrics/MethodLength:
112+
Max: 51

lib/git/command_line.rb

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ def initialize(env, binary_path, global_opts, logger)
9797

9898
# Execute a git command, wait for it to finish, and return the result
9999
#
100+
# Non-option the command line arguements to pass to git. If you collect
101+
# the command line arguments in an array, make sure you splat the array
102+
# into the parameter list.
103+
#
100104
# NORMALIZATION
101105
#
102106
# The command output is returned as a Unicde string containing the binary output
@@ -142,32 +146,30 @@ def initialize(env, binary_path, global_opts, logger)
142146
# stderr.string #=> "unknown revision or path not in the working tree.\n"
143147
# end
144148
#
145-
# @param args [Array<String>] the command line arguements to pass to git
146-
#
147-
# This array should be splatted into the parameter list.
149+
# @param options_hash [Hash] the options to pass to the command
148150
#
149-
# @param out [#write, nil] the object to write stdout to or nil to ignore stdout
151+
# @option options_hash [#write, nil] :out the object to write stdout to or nil to ignore stdout
150152
#
151153
# If this is a 'StringIO' object, then `stdout_writer.string` will be returned.
152154
#
153155
# In general, only specify a `stdout_writer` object when you want to redirect
154156
# stdout to a file or some other object that responds to `#write`. The default
155157
# behavior will return the output of the command.
156158
#
157-
# @param err [#write] the object to write stderr to or nil to ignore stderr
159+
# @option options_hash [#write, nil] :err the object to write stderr to or nil to ignore stderr
158160
#
159161
# If this is a 'StringIO' object and `merged_output` is `true`, then
160162
# `stderr_writer.string` will be merged into the output returned by this method.
161163
#
162-
# @param normalize [Boolean] whether to normalize the output to a valid encoding
164+
# @option options_hash [Boolean] :normalize whether to normalize the output of stdout and stderr
163165
#
164-
# @param chomp [Boolean] whether to chomp the output
166+
# @option options_hash [Boolean] :chomp whether to chomp both stdout and stderr output
165167
#
166-
# @param merge [Boolean] whether to merge stdout and stderr in the string returned
168+
# @option options_hash [Boolean] :merge whether to merge stdout and stderr in the string returned
167169
#
168-
# @param chdir [String] the directory to run the command in
170+
# @option options_hash [String, nil] :chdir the directory to run the command in
169171
#
170-
# @param timeout [Numeric, nil] the maximum seconds to wait for the command to complete
172+
# @option options_hash [Numeric, nil] :timeout the maximum seconds to wait for the command to complete
171173
#
172174
# If timeout is zero, the timeout will not be enforced.
173175
#
@@ -189,21 +191,50 @@ def initialize(env, binary_path, global_opts, logger)
189191
#
190192
# @raise [Git::TimeoutError] if the command times out
191193
#
192-
def run(*args, normalize:, chomp:, merge:, out: nil, err: nil, chdir: nil, timeout: nil)
194+
def run(*, **options_hash)
195+
options_hash = RUN_ARGS.merge(options_hash)
196+
extra_options = options_hash.keys - RUN_ARGS.keys
197+
raise ArgumentError, "Unknown options: #{extra_options.join(', ')}" if extra_options.any?
198+
199+
result = run_with_capture(*, **options_hash)
200+
process_result(result, options_hash[:normalize], options_hash[:chomp], options_hash[:timeout])
201+
end
202+
203+
# @return [Git::CommandLineResult] the result of running the command
204+
#
205+
# @api private
206+
#
207+
def run_with_capture(*args, **options_hash)
193208
git_cmd = build_git_cmd(args)
194-
begin
195-
options = { chdir: chdir || :not_set, timeout_after: timeout, raise_errors: false }
209+
options = run_with_capture_options(**options_hash)
210+
ProcessExecuter.run_with_capture(env, *git_cmd, **options)
211+
rescue ProcessExecuter::ProcessIOError => e
212+
raise Git::ProcessIOError.new(e.message), cause: e.exception.cause
213+
end
214+
215+
def run_with_capture_options(**options_hash)
216+
chdir = options_hash[:chdir] || :not_set
217+
timeout_after = options_hash[:timeout]
218+
out = options_hash[:out]
219+
err = options_hash[:err]
220+
merge_output = options_hash[:merge] || false
221+
222+
{ chdir:, timeout_after:, merge_output:, raise_errors: false }.tap do |options|
196223
options[:out] = out unless out.nil?
197224
options[:err] = err unless err.nil?
198-
options[:merge_output] = merge unless merge.nil?
199-
200-
result = ProcessExecuter.run_with_capture(env, *git_cmd, **options)
201-
rescue ProcessExecuter::ProcessIOError => e
202-
raise Git::ProcessIOError.new(e.message), cause: e.exception.cause
203225
end
204-
process_result(result, normalize, chomp, timeout)
205226
end
206227

228+
RUN_ARGS = {
229+
normalize: false,
230+
chomp: false,
231+
merge: false,
232+
out: nil,
233+
err: nil,
234+
chdir: nil,
235+
timeout: nil
236+
}.freeze
237+
207238
private
208239

209240
# Build the git command line from the available sources to send to `Process.spawn`

lib/git/lib.rb

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,16 @@ def self.warn_if_old_command(lib)
15221522
true
15231523
end
15241524

1525+
COMMAND_ARG_DEFAULTS = {
1526+
out: nil,
1527+
err: nil,
1528+
normalize: true,
1529+
chomp: true,
1530+
merge: false,
1531+
chdir: nil,
1532+
timeout: nil # Don't set to Git.config.timeout here since it is mutable
1533+
}.freeze
1534+
15251535
private
15261536

15271537
def command_lines(cmd, *opts, chdir: nil)
@@ -1569,26 +1579,20 @@ def command_line
15691579
# Runs a git command and returns the output
15701580
#
15711581
# Additional args are passed to the command line. They should exclude the 'git'
1572-
# command itself and global options.
1573-
#
1574-
# For example, to run `git log --pretty=oneline`, you would pass `['log',
1575-
# '--pretty=oneline']`
1576-
#
1577-
# @param out [String, nil] the path to a file or an IO to write the command's
1578-
# stdout to
1579-
#
1580-
# @param err [String, nil] the path to a file or an IO to write the command's
1581-
# stdout to
1582-
#
1583-
# @param normalize [Boolean] true to normalize the output encoding
1584-
#
1585-
# @param chomp [Boolean] true to remove trailing newlines from the output
1586-
#
1587-
# @param merge [Boolean] true to merge stdout and stderr
1582+
# command itself and global options. Remember to splat the the arguments if given
1583+
# as an array.
15881584
#
1589-
# @param chdir [String, nil] the directory to run the command in
1585+
# For example, to run `git log --pretty=oneline`, you would create the array
1586+
# `args = ['log', '--pretty=oneline']` and call `command(*args)`.
15901587
#
1591-
# @param timeout [Numeric, nil] the maximum seconds to wait for the command to complete
1588+
# @param options_hash [Hash] the options to pass to the command
1589+
# @option options_hash [IO, String, #write, nil] :out the destination for captured stdout
1590+
# @option options_hash [IO, String, #write, nil] :err the destination for captured stderr
1591+
# @option options_hash [Boolean] :normalize true to normalize the output encoding to UTF-8
1592+
# @option options_hash [Boolean] :chomp true to remove trailing newlines from the output
1593+
# @option options_hash [Boolean] :merge true to merge stdout and stderr into a single output
1594+
# @option options_hash [String, nil] :chdir the directory to run the command in
1595+
# @option options_hash [Numeric, nil] :timeout the maximum seconds to wait for the command to complete
15921596
#
15931597
# If timeout is nil, the global timeout from {Git::Config} is used.
15941598
#
@@ -1603,9 +1607,14 @@ def command_line
16031607
# @return [String] the command's stdout (or merged stdout and stderr if `merge`
16041608
# is true)
16051609
#
1610+
# @raise [ArgumentError] if an unknown option is passed
1611+
#
16061612
# @raise [Git::FailedError] if the command failed
1613+
#
16071614
# @raise [Git::SignaledError] if the command was signaled
1615+
#
16081616
# @raise [Git::TimeoutError] if the command times out
1617+
#
16091618
# @raise [Git::ProcessIOError] if an exception was raised while collecting subprocess output
16101619
#
16111620
# The exception's `result` attribute is a {Git::CommandLineResult} which will
@@ -1614,10 +1623,14 @@ def command_line
16141623
#
16151624
# @api private
16161625
#
1617-
def command(*, out: nil, err: nil, normalize: true, chomp: true, merge: false, chdir: nil, timeout: nil)
1618-
timeout ||= Git.config.timeout
1619-
result = command_line.run(*, out: out, err: err, normalize: normalize, chomp: chomp, merge: merge,
1620-
chdir: chdir, timeout: timeout)
1626+
def command(*, **options_hash)
1627+
options_hash = COMMAND_ARG_DEFAULTS.merge(options_hash)
1628+
options_hash[:timeout] ||= Git.config.timeout
1629+
1630+
extra_options = options_hash.keys - COMMAND_ARG_DEFAULTS.keys
1631+
raise ArgumentError, "Unknown options: #{extra_options.join(', ')}" if extra_options.any?
1632+
1633+
result = command_line.run(*, **options_hash)
16211634
result.stdout
16221635
end
16231636

0 commit comments

Comments
 (0)