-
Notifications
You must be signed in to change notification settings - Fork 514
/
command_line.rb
376 lines (358 loc) · 14.1 KB
/
command_line.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# frozen_string_literal: true
require 'git/base'
require 'git/command_line_result'
require 'git/failed_error'
require 'git/signaled_error'
require 'stringio'
module Git
# Runs a git command and returns the result
#
# @api public
#
class CommandLine
# Create a Git::CommandLine object
#
# @example
# env = { 'GIT_DIR' => '/path/to/git/dir' }
# binary_path = '/usr/bin/git'
# global_opts = %w[--git-dir /path/to/git/dir]
# logger = Logger.new(STDOUT)
# cli = CommandLine.new(env, binary_path, global_opts, logger)
# cli.run('version') #=> #<Git::CommandLineResult:0x00007f9b0c0b0e00
#
# @param env [Hash<String, String>] environment variables to set
# @param global_opts [Array<String>] global options to pass to git
# @param logger [Logger] the logger to use
#
def initialize(env, binary_path, global_opts, logger)
@env = env
@binary_path = binary_path
@global_opts = global_opts
@logger = logger
end
# @attribute [r] env
#
# Variables to set (or unset) in the git command's environment
#
# @example
# env = { 'GIT_DIR' => '/path/to/git/dir' }
# command_line = Git::CommandLine.new(env, '/usr/bin/git', [], Logger.new(STDOUT))
# command_line.env #=> { 'GIT_DIR' => '/path/to/git/dir' }
#
# @return [Hash<String, String>]
#
# @see https://ruby-doc.org/3.2.1/Process.html#method-c-spawn Process.spawn
# for details on how to set environment variables using the `env` parameter
#
attr_reader :env
# @attribute [r] binary_path
#
# The path to the command line binary to run
#
# @example
# binary_path = '/usr/bin/git'
# command_line = Git::CommandLine.new({}, binary_path, ['version'], Logger.new(STDOUT))
# command_line.binary_path #=> '/usr/bin/git'
#
# @return [String]
#
attr_reader :binary_path
# @attribute [r] global_opts
#
# The global options to pass to git
#
# These are options that are passed to git before the command name and
# arguments. For example, in `git --git-dir /path/to/git/dir version`, the
# global options are %w[--git-dir /path/to/git/dir].
#
# @example
# env = {}
# global_opts = %w[--git-dir /path/to/git/dir]
# logger = Logger.new(nil)
# cli = CommandLine.new(env, '/usr/bin/git', global_opts, logger)
# cli.global_opts #=> %w[--git-dir /path/to/git/dir]
#
# @return [Array<String>]
#
attr_reader :global_opts
# @attribute [r] logger
#
# The logger to use for logging git commands and results
#
# @example
# env = {}
# global_opts = %w[]
# logger = Logger.new(STDOUT)
# cli = CommandLine.new(env, '/usr/bin/git', global_opts, logger)
# cli.logger == logger #=> true
#
# @return [Logger]
#
attr_reader :logger
# Execute a git command, wait for it to finish, and return the result
#
# NORMALIZATION
#
# The command output is returned as a Unicde string containing the binary output
# from the command. If the binary output is not valid UTF-8, the output will
# cause problems because the encoding will be invalid.
#
# Normalization is a process that trys to convert the binary output to a valid
# UTF-8 string. It uses the `rchardet` gem to detect the encoding of the binary
# output and then converts it to UTF-8.
#
# Normalization is not enabled by default. Pass `normalize: true` to Git::CommandLine#run
# to enable it. Normalization will only be performed on stdout and only if the `out:`` option
# is nil or is a StringIO object. If the out: option is set to a file or other IO object,
# the normalize option will be ignored.
#
# @example Run a command and return the output
# cli.run('version') #=> "git version 2.39.1\n"
#
# @example The args array should be splatted into the parameter list
# args = %w[log -n 1 --oneline]
# cli.run(*args) #=> "f5baa11 beginning of Ruby/Git project\n"
#
# @example Run a command and return the chomped output
# cli.run('version', chomp: true) #=> "git version 2.39.1"
#
# @example Run a command and without normalizing the output
# cli.run('version', normalize: false) #=> "git version 2.39.1\n"
#
# @example Capture stdout in a temporary file
# require 'tempfile'
# tempfile = Tempfile.create('git') do |file|
# cli.run('version', out: file)
# file.rewind
# file.read #=> "git version 2.39.1\n"
# end
#
# @example Capture stderr in a StringIO object
# require 'stringio'
# stderr = StringIO.new
# begin
# cli.run('log', 'nonexistent-branch', err: stderr)
# rescue Git::FailedError => e
# stderr.string #=> "unknown revision or path not in the working tree.\n"
# end
#
# @param args [Array<String>] the command line arguements to pass to git
#
# This array should be splatted into the parameter list.
#
# @param out [#write, nil] the object to write stdout to or nil to ignore stdout
#
# If this is a 'StringIO' object, then `stdout_writer.string` will be returned.
#
# In general, only specify a `stdout_writer` object when you want to redirect
# stdout to a file or some other object that responds to `#write`. The default
# behavior will return the output of the command.
#
# @param err [#write] the object to write stderr to or nil to ignore stderr
#
# If this is a 'StringIO' object and `merged_output` is `true`, then
# `stderr_writer.string` will be merged into the output returned by this method.
#
# @param normalize [Boolean] whether to normalize the output to a valid encoding
#
# @param chomp [Boolean] whether to chomp the output
#
# @param merge [Boolean] whether to merge stdout and stderr in the string returned
#
# @param chdir [String] the directory to run the command in
#
# @param timeout [Numeric, nil] the maximum seconds to wait for the command to complete
#
# If timeout is zero, the timeout will not be enforced.
#
# If the command times out, it is killed via a `SIGKILL` signal and `Git::TimeoutError` is raised.
#
# If the command does not respond to SIGKILL, it will hang this method.
#
# @return [Git::CommandLineResult] the output of the command
#
# This result of running the command.
#
# @raise [ArgumentError] if `args` is not an array of strings
#
# @raise [Git::SignaledError] if the command was terminated because of an uncaught signal
#
# @raise [Git::FailedError] if the command returned a non-zero exitstatus
#
# @raise [Git::GitExecuteError] if an exception was raised while collecting subprocess output
#
# @raise [Git::TimeoutError] if the command times out
#
def run(*args, out:, err:, normalize:, chomp:, merge:, chdir: nil, timeout: nil)
git_cmd = build_git_cmd(args)
out ||= StringIO.new
err ||= (merge ? out : StringIO.new)
status = execute(git_cmd, out, err, chdir: (chdir || :not_set), timeout: timeout)
process_result(git_cmd, status, out, err, normalize, chomp, timeout)
end
private
# Build the git command line from the available sources to send to `Process.spawn`
# @return [Array<String>]
# @api private
#
def build_git_cmd(args)
raise ArgumentError.new('The args array can not contain an array') if args.any? { |a| a.is_a?(Array) }
[binary_path, *global_opts, *args].map { |e| e.to_s }
end
# Determine the output to return in the `CommandLineResult`
#
# If the writer can return the output by calling `#string` (such as a StringIO),
# then return the result of normalizing the encoding and chomping the output
# as requested.
#
# If the writer does not support `#string`, then return nil. The output is
# assumed to be collected by the writer itself such as when the writer
# is a file instead of a StringIO.
#
# @param writer [#string] the writer to post-process
#
# @return [String, nil]
#
# @api private
#
def post_process(writer, normalize, chomp)
if writer.respond_to?(:string)
output = writer.string.dup
output = output.lines.map { |l| Git::EncodingUtils.normalize_encoding(l) }.join if normalize
output.chomp! if chomp
output
else
nil
end
end
# Post-process all writers and return an array of the results
#
# @param writers [Array<#write>] the writers to post-process
# @param normalize [Boolean] whether to normalize the output of each writer
# @param chomp [Boolean] whether to chomp the output of each writer
#
# @return [Array<String, nil>] the output of each writer that supports `#string`
#
# @api private
#
def post_process_all(writers, normalize, chomp)
Array.new.tap do |result|
writers.each { |writer| result << post_process(writer, normalize, chomp) }
end
end
# Raise an error when there was exception while collecting the subprocess output
#
# @param git_cmd [Array<String>] the git command that was executed
# @param pipe_name [Symbol] the name of the pipe that raised the exception
# @param pipe [ProcessExecuter::MonitoredPipe] the pipe that raised the exception
#
# @raise [Git::GitExecuteError]
#
# @return [void] this method always raises an error
#
# @api private
#
def raise_pipe_error(git_cmd, pipe_name, pipe)
raise Git::GitExecuteError.new("Pipe Exception for #{git_cmd}: #{pipe_name}"), cause: pipe.exception
end
# Execute the git command and collect the output
#
# @param cmd [Array<String>] the git command to execute
# @param chdir [String] the directory to run the command in
# @param timeout [Numeric, nil] the maximum seconds to wait for the command to complete
#
# If timeout is zero of nil, the command will not time out. If the command
# times out, it is killed via a SIGKILL signal and `Git::TimeoutError` is raised.
#
# If the command does not respond to SIGKILL, it will hang this method.
#
# @raise [Git::GitExecuteError] if an exception was raised while collecting subprocess output
# @raise [Git::TimeoutError] if the command times out
#
# @return [ProcessExecuter::Status] the status of the completed subprocess
#
# @api private
#
def spawn(cmd, out_writers, err_writers, chdir:, timeout:)
out_pipe = ProcessExecuter::MonitoredPipe.new(*out_writers, chunk_size: 10_000)
err_pipe = ProcessExecuter::MonitoredPipe.new(*err_writers, chunk_size: 10_000)
ProcessExecuter.spawn(env, *cmd, out: out_pipe, err: err_pipe, chdir: chdir, timeout: timeout)
ensure
out_pipe.close
err_pipe.close
raise_pipe_error(cmd, :stdout, out_pipe) if out_pipe.exception
raise_pipe_error(cmd, :stderr, err_pipe) if err_pipe.exception
end
# The writers that will be used to collect stdout and stderr
#
# Additional writers could be added here if you wanted to tee output
# or send output to the terminal.
#
# @param out [#write] the object to write stdout to
# @param err [#write] the object to write stderr to
#
# @return [Array<Array<#write>, Array<#write>>] the writers for stdout and stderr
#
# @api private
#
def writers(out, err)
out_writers = [out]
err_writers = [err]
[out_writers, err_writers]
end
# Process the result of the command and return a Git::CommandLineResult
#
# Post process output, log the command and result, and raise an error if the
# command failed.
#
# @param git_cmd [Array<String>] the git command that was executed
# @param status [Process::Status] the status of the completed subprocess
# @param out [#write] the object that stdout was written to
# @param err [#write] the object that stderr was written to
# @param normalize [Boolean] whether to normalize the output of each writer
# @param chomp [Boolean] whether to chomp the output of each writer
# @param timeout [Numeric, nil] the maximum seconds to wait for the command to complete
#
# @return [Git::CommandLineResult] the result of the command to return to the caller
#
# @raise [Git::FailedError] if the command failed
# @raise [Git::SignaledError] if the command was signaled
#
# @api private
#
def process_result(git_cmd, status, out, err, normalize, chomp, timeout)
out_str, err_str = post_process_all([out, err], normalize, chomp)
logger.info { "#{git_cmd} exited with status #{status}" }
logger.debug { "stdout:\n#{out_str.inspect}\nstderr:\n#{err_str.inspect}" }
Git::CommandLineResult.new(git_cmd, status, out_str, err_str).tap do |result|
raise Git::TimeoutError.new(result, timeout) if status.timeout?
raise Git::SignaledError.new(result) if status.signaled?
raise Git::FailedError.new(result) unless status.success?
end
end
# Execute the git command and write the command output to out and err
#
# @param git_cmd [Array<String>] the git command to execute
# @param out [#write] the object to write stdout to
# @param err [#write] the object to write stderr to
# @param chdir [String] the directory to run the command in
# @param timeout [Numeric, nil] the maximum seconds to wait for the command to complete
#
# If timeout is zero of nil, the command will not time out. If the command
# times out, it is killed via a SIGKILL signal and `Git::TimeoutError` is raised.
#
# If the command does not respond to SIGKILL, it will hang this method.
#
# @raise [Git::GitExecuteError] if an exception was raised while collecting subprocess output
# @raise [Git::TimeoutError] if the command times out
#
# @return [Git::CommandLineResult] the result of the command to return to the caller
#
# @api private
#
def execute(git_cmd, out, err, chdir:, timeout:)
out_writers, err_writers = writers(out, err)
spawn(git_cmd, out_writers, err_writers, chdir: chdir, timeout: timeout)
end
end
end