ruby_git 0.2.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.commitlintrc.yml +16 -0
- data/.github/CODEOWNERS +4 -0
- data/.github/workflows/continuous_integration.yml +87 -0
- data/.github/workflows/enforce_conventional_commits.yml +21 -0
- data/.github/workflows/experimental_ruby_builds.yml +65 -0
- data/.gitignore +3 -0
- data/.husky/commit-msg +1 -0
- data/.markdownlint.yml +25 -0
- data/.rubocop.yml +13 -15
- data/.yardopts +6 -1
- data/CHANGELOG.md +58 -0
- data/CONTRIBUTING.md +5 -5
- data/{LICENSE.md → LICENSE.txt} +1 -1
- data/PLAN.md +67 -0
- data/README.md +58 -6
- data/RELEASING.md +5 -54
- data/Rakefile +31 -38
- data/RubyGit Class Diagram.svg +1 -0
- data/bin/command-line-test +189 -0
- data/bin/console +2 -0
- data/bin/setup +13 -2
- data/lib/ruby_git/command_line/options.rb +61 -0
- data/lib/ruby_git/command_line/result.rb +155 -0
- data/lib/ruby_git/command_line/runner.rb +296 -0
- data/lib/ruby_git/command_line.rb +95 -0
- data/lib/ruby_git/encoding_normalizer.rb +49 -0
- data/lib/ruby_git/errors.rb +169 -0
- data/lib/ruby_git/repository.rb +33 -0
- data/lib/ruby_git/status/branch.rb +92 -0
- data/lib/ruby_git/status/entry.rb +162 -0
- data/lib/ruby_git/status/ignored_entry.rb +44 -0
- data/lib/ruby_git/status/ordinary_entry.rb +207 -0
- data/lib/ruby_git/status/parser.rb +203 -0
- data/lib/ruby_git/status/renamed_entry.rb +257 -0
- data/lib/ruby_git/status/report.rb +143 -0
- data/lib/ruby_git/status/stash.rb +33 -0
- data/lib/ruby_git/status/submodule_status.rb +85 -0
- data/lib/ruby_git/status/unmerged_entry.rb +248 -0
- data/lib/ruby_git/status/untracked_entry.rb +52 -0
- data/lib/ruby_git/status.rb +33 -0
- data/lib/ruby_git/version.rb +1 -1
- data/lib/ruby_git/worktree.rb +175 -33
- data/lib/ruby_git.rb +91 -28
- data/package.json +11 -0
- data/ruby_git.gemspec +33 -23
- metadata +146 -50
- data/.travis.yml +0 -26
- data/CODEOWNERS +0 -3
- data/MAINTAINERS.md +0 -8
- data/lib/ruby_git/error.rb +0 -8
- data/lib/ruby_git/file_helpers.rb +0 -42
- data/lib/ruby_git/git_binary.rb +0 -106
- /data/{ISSUE_TEMPLATE.md → .github/ISSUE_TEMPLATE.md} +0 -0
- /data/{PULL_REQUEST_TEMPLATE.md → .github/PULL_REQUEST_TEMPLATE.md} +0 -0
@@ -0,0 +1,296 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'result'
|
4
|
+
require 'ruby_git/errors'
|
5
|
+
|
6
|
+
module RubyGit
|
7
|
+
# Runs a git command and returns the result
|
8
|
+
#
|
9
|
+
# @api public
|
10
|
+
#
|
11
|
+
module CommandLine
|
12
|
+
# Runs the git command line and returns the result
|
13
|
+
# @api public
|
14
|
+
class Runner
|
15
|
+
# Create a an object to run git commands via the command line
|
16
|
+
#
|
17
|
+
# @example
|
18
|
+
# env = { 'GIT_DIR' => '/path/to/git/dir' }
|
19
|
+
# binary_path = '/usr/bin/git'
|
20
|
+
# global_options = %w[--git-dir /path/to/git/dir]
|
21
|
+
# logger = Logger.new(STDOUT)
|
22
|
+
# cli = CommandLine.new(env, binary_path, global_options, logger)
|
23
|
+
# cli.run('version') #=> #<RubyGit::CommandLineResult:0x00007f9b0c0b0e00
|
24
|
+
#
|
25
|
+
# @param env [Hash<String, String>] environment variables to set
|
26
|
+
# @param binary_path [String] the path to the git binary
|
27
|
+
# @param global_options [Array<String>] global options to pass to git
|
28
|
+
# @param logger [Logger] the logger to use
|
29
|
+
#
|
30
|
+
def initialize(env, binary_path, global_options, logger)
|
31
|
+
@env = env
|
32
|
+
@binary_path = binary_path
|
33
|
+
@global_options = global_options
|
34
|
+
@logger = logger
|
35
|
+
end
|
36
|
+
|
37
|
+
# @attribute [r] env
|
38
|
+
#
|
39
|
+
# Variables to set (or unset) in the git command's environment
|
40
|
+
#
|
41
|
+
# @example
|
42
|
+
# env = { 'GIT_DIR' => '/path/to/git/dir' }
|
43
|
+
# command_line = RubyGit::CommandLine.new(env, '/usr/bin/git', [], Logger.new(STDOUT))
|
44
|
+
# command_line.env #=> { 'GIT_DIR' => '/path/to/git/dir' }
|
45
|
+
#
|
46
|
+
# @return [Hash<String, String>]
|
47
|
+
#
|
48
|
+
# @see https://ruby-doc.org/3.2.1/Process.html#method-c-spawn Process.spawn
|
49
|
+
# for details on how to set environment variables using the `env` parameter
|
50
|
+
#
|
51
|
+
attr_reader :env
|
52
|
+
|
53
|
+
# @attribute [r] binary_path
|
54
|
+
#
|
55
|
+
# The path to the command line binary to run
|
56
|
+
#
|
57
|
+
# @example
|
58
|
+
# binary_path = '/usr/bin/git'
|
59
|
+
# command_line = RubyGit::CommandLine.new({}, binary_path, ['version'], Logger.new(STDOUT))
|
60
|
+
# command_line.binary_path #=> '/usr/bin/git'
|
61
|
+
#
|
62
|
+
# @return [String]
|
63
|
+
#
|
64
|
+
attr_reader :binary_path
|
65
|
+
|
66
|
+
# @attribute [r] global_options
|
67
|
+
#
|
68
|
+
# The global options to pass to git
|
69
|
+
#
|
70
|
+
# These are options that are passed to git before the command name and
|
71
|
+
# arguments. For example, in `git --git-dir /path/to/git/dir version`, the
|
72
|
+
# global options are %w[--git-dir /path/to/git/dir].
|
73
|
+
#
|
74
|
+
# @example
|
75
|
+
# env = {}
|
76
|
+
# global_options = %w[--git-dir /path/to/git/dir]
|
77
|
+
# logger = Logger.new(nil)
|
78
|
+
# cli = CommandLine.new(env, '/usr/bin/git', global_options, logger)
|
79
|
+
# cli.global_options #=> %w[--git-dir /path/to/git/dir]
|
80
|
+
#
|
81
|
+
# @return [Array<String>]
|
82
|
+
#
|
83
|
+
attr_reader :global_options
|
84
|
+
|
85
|
+
# @attribute [r] logger
|
86
|
+
#
|
87
|
+
# The logger to use for logging git commands and results
|
88
|
+
#
|
89
|
+
# @example
|
90
|
+
# env = {}
|
91
|
+
# global_options = %w[]
|
92
|
+
# logger = Logger.new(STDOUT)
|
93
|
+
# cli = CommandLine.new(env, '/usr/bin/git', global_options, logger)
|
94
|
+
# cli.logger == logger #=> true
|
95
|
+
#
|
96
|
+
# @return [Logger]
|
97
|
+
#
|
98
|
+
attr_reader :logger
|
99
|
+
|
100
|
+
# Execute a git command, wait for it to finish, and return the result
|
101
|
+
#
|
102
|
+
# NORMALIZATION
|
103
|
+
#
|
104
|
+
# The command output is returned as a Unicde string containing the binary output
|
105
|
+
# from the command. If the binary output is not valid UTF-8, the output will
|
106
|
+
# cause problems because the encoding will be invalid.
|
107
|
+
#
|
108
|
+
# Normalization is a process that trys to convert the binary output to a valid
|
109
|
+
# UTF-8 string. It uses the `rchardet` gem to detect the encoding of the binary
|
110
|
+
# output and then converts it to UTF-8.
|
111
|
+
#
|
112
|
+
# Normalization is not enabled by default. Pass `normalize: true` to RubyGit::CommandLine#run
|
113
|
+
# to enable it. Normalization will only be performed on stdout and only if the `out:`` option
|
114
|
+
# is nil or is a StringIO object. If the out: option is set to a file or other IO object,
|
115
|
+
# the normalize option will be ignored.
|
116
|
+
#
|
117
|
+
# @example Run a command and return the output
|
118
|
+
# cli.run('version') #=> "git version 2.39.1\n"
|
119
|
+
#
|
120
|
+
# @example The args array should be splatted into the parameter list
|
121
|
+
# args = %w[log -n 1 --oneline]
|
122
|
+
# cli.run(*args) #=> "f5baa11 beginning of Ruby/Git project\n"
|
123
|
+
#
|
124
|
+
# @example Run a command and return the chomped output
|
125
|
+
# cli.run('version', chomp: true) #=> "git version 2.39.1"
|
126
|
+
#
|
127
|
+
# @example Run a command and without normalizing the output
|
128
|
+
# cli.run('version', normalize: false) #=> "git version 2.39.1\n"
|
129
|
+
#
|
130
|
+
# @example Capture stdout in a temporary file
|
131
|
+
# require 'tempfile'
|
132
|
+
# tempfile = Tempfile.create('git') do |file|
|
133
|
+
# cli.run('version', out: file)
|
134
|
+
# file.rewind
|
135
|
+
# file.read #=> "git version 2.39.1\n"
|
136
|
+
# end
|
137
|
+
#
|
138
|
+
# @example Capture stderr in a StringIO object
|
139
|
+
# require 'stringio'
|
140
|
+
# stderr = StringIO.new
|
141
|
+
# begin
|
142
|
+
# cli.run('log', 'nonexistent-branch', err: stderr)
|
143
|
+
# rescue RubyGit::FailedError => e
|
144
|
+
# stderr.string #=> "unknown revision or path not in the working tree.\n"
|
145
|
+
# end
|
146
|
+
#
|
147
|
+
# @param args [Array<String>] the command line arguements to pass to git
|
148
|
+
#
|
149
|
+
# This array should be splatted into the parameter list.
|
150
|
+
#
|
151
|
+
# @param options_hash [Hash] the options to initialize {RubyGit::CommandLine::Options}
|
152
|
+
#
|
153
|
+
# @return [RubyGit::CommandLine::Result] the result of the command
|
154
|
+
#
|
155
|
+
# @raise [ArgumentError] if `args` or `options_hash` are not valid
|
156
|
+
#
|
157
|
+
# @raise [RubyGit::FailedError] if the command returned a non-zero exitstatus
|
158
|
+
#
|
159
|
+
# @raise [RubyGit::SignaledError] if the command was terminated because of an uncaught signal
|
160
|
+
#
|
161
|
+
# @raise [RubyGit::TimeoutError] if the command timeed out
|
162
|
+
#
|
163
|
+
# @raise [RubyGit::ProcessIOError] if an exception was raised while collecting subprocess output
|
164
|
+
#
|
165
|
+
def call(*args, **options_hash)
|
166
|
+
options_hash[:raise_errors] = false
|
167
|
+
options = RubyGit::CommandLine::Options.new(logger: logger, **options_hash)
|
168
|
+
begin
|
169
|
+
result = run_with_chdir([env, *build_git_cmd(args)], options)
|
170
|
+
rescue ProcessExecuter::ProcessIOError => e
|
171
|
+
raise RubyGit::ProcessIOError.new(e.message), cause: e.exception.cause
|
172
|
+
end
|
173
|
+
process_result(result)
|
174
|
+
end
|
175
|
+
|
176
|
+
private
|
177
|
+
|
178
|
+
# Run command with options with special handling for the `chdir` option on JRuby
|
179
|
+
#
|
180
|
+
# JRuby does not support the `chdir` option in `Process.spawn`. Note that this
|
181
|
+
# workaround means that this library is not thread-safe when using JRuby.
|
182
|
+
#
|
183
|
+
# @param args [Array<String>] the command to run
|
184
|
+
# @param options [RubyGit::CommandLine::Options] the options to pass to `Process.spawn`
|
185
|
+
#
|
186
|
+
# @return [ProcessExecuter::Result] the result of the command
|
187
|
+
#
|
188
|
+
# @api private
|
189
|
+
#
|
190
|
+
def run_with_chdir(args, options)
|
191
|
+
return ProcessExecuter.run_with_options(args, options) unless jruby? && options.chdir != :not_set
|
192
|
+
|
193
|
+
# :nocov: Not executed in MRI Ruby
|
194
|
+
Dir.chdir(options.chdir) do
|
195
|
+
saved_chdir = options.chdir
|
196
|
+
options.merge!(chdir: :not_set)
|
197
|
+
ProcessExecuter.run_with_options(args, options).tap do
|
198
|
+
options.merge!(chdir: saved_chdir)
|
199
|
+
end
|
200
|
+
end
|
201
|
+
# :nocov:
|
202
|
+
end
|
203
|
+
|
204
|
+
# Returns true if running on JRuby
|
205
|
+
#
|
206
|
+
# @return [Boolean]
|
207
|
+
#
|
208
|
+
# @api private
|
209
|
+
def jruby? = RUBY_ENGINE == 'jruby'
|
210
|
+
|
211
|
+
# Build the git command line from the available sources to send to `Process.spawn`
|
212
|
+
# @return [Array<String>]
|
213
|
+
# @api private
|
214
|
+
#
|
215
|
+
def build_git_cmd(args)
|
216
|
+
raise ArgumentError, 'The args array can not contain an array' if args.any? { |a| a.is_a?(Array) }
|
217
|
+
|
218
|
+
[binary_path, *global_options, *args].map(&:to_s)
|
219
|
+
end
|
220
|
+
|
221
|
+
# Process the result of the command and return a RubyGit::CommandLineResult
|
222
|
+
#
|
223
|
+
# Post process output, log the command and result, and raise an error if the
|
224
|
+
# command failed.
|
225
|
+
#
|
226
|
+
# @param result [ProcessExecuter::Result] the result it is a Process::Status
|
227
|
+
# and include command, stdout, and stderr
|
228
|
+
#
|
229
|
+
# @return [RubyGit::CommandLineResult] the result of the command to return to the caller
|
230
|
+
#
|
231
|
+
# @raise [RubyGit::FailedError] if the command failed
|
232
|
+
# @raise [RubyGit::SignaledError] if the command was signaled
|
233
|
+
# @raise [RubyGit::TimeoutError] if the command times out
|
234
|
+
# @raise [RubyGit::ProcessIOError] if an exception was raised while collecting subprocess output
|
235
|
+
#
|
236
|
+
# @api private
|
237
|
+
#
|
238
|
+
def process_result(result)
|
239
|
+
RubyGit::CommandLine::Result.new(result).tap do |processed_result|
|
240
|
+
raise_any_errors(processed_result) if processed_result.options.raise_git_errors
|
241
|
+
|
242
|
+
processed_result.process_stdout { |s, r| process_output(s, r) }
|
243
|
+
processed_result.process_stderr { |s, r| process_output(s, r) }
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
# Raise an error if the command failed, was signaled, or timed out
|
248
|
+
#
|
249
|
+
# @param result [RubyGit::CommandLineResult] the result of the command
|
250
|
+
#
|
251
|
+
# @return [Void]
|
252
|
+
#
|
253
|
+
# @raise [RubyGit::FailedError] if the command failed
|
254
|
+
# @raise [RubyGit::SignaledError] if the command was signaled
|
255
|
+
# @raise [RubyGit::TimeoutError] if the command times out
|
256
|
+
#
|
257
|
+
# @api private
|
258
|
+
#
|
259
|
+
def raise_any_errors(result)
|
260
|
+
raise RubyGit::TimeoutError, result if result.timed_out?
|
261
|
+
|
262
|
+
raise RubyGit::SignaledError, result if result.signaled?
|
263
|
+
|
264
|
+
raise RubyGit::FailedError, result unless result.success?
|
265
|
+
end
|
266
|
+
|
267
|
+
# Determine the output to return in the `CommandLineResult`
|
268
|
+
#
|
269
|
+
# If the writer can return the output by calling `#string` (such as a StringIO),
|
270
|
+
# then return the result of normalizing the encoding and chomping the output
|
271
|
+
# as requested.
|
272
|
+
#
|
273
|
+
# If the writer does not support `#string`, then return nil. The output is
|
274
|
+
# assumed to be collected by the writer itself such as when the writer
|
275
|
+
# is a file instead of a StringIO.
|
276
|
+
#
|
277
|
+
# @param output [#string] the output to post-process
|
278
|
+
# @return [String, nil]
|
279
|
+
#
|
280
|
+
# @api private
|
281
|
+
#
|
282
|
+
def process_output(output, result)
|
283
|
+
return nil unless output
|
284
|
+
|
285
|
+
output =
|
286
|
+
if result.options.normalize_encoding
|
287
|
+
output.lines.map { |l| RubyGit::EncodingNormalizer.normalize(l) }.join
|
288
|
+
else
|
289
|
+
output.dup
|
290
|
+
end
|
291
|
+
|
292
|
+
output.tap { |o| o.chomp! if result.options.chomp }
|
293
|
+
end
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'command_line/options'
|
4
|
+
require_relative 'command_line/result'
|
5
|
+
require_relative 'command_line/runner'
|
6
|
+
|
7
|
+
module RubyGit
|
8
|
+
# Run git commands via the command line
|
9
|
+
#
|
10
|
+
# @api public
|
11
|
+
#
|
12
|
+
module CommandLine
|
13
|
+
# Run a git command
|
14
|
+
#
|
15
|
+
# @example A simple example
|
16
|
+
# RubyGit::CommandLine.run('version') #=> outputs "git version 2.30.2\n" to stdout
|
17
|
+
#
|
18
|
+
# @example Capture stdout
|
19
|
+
# command = %w[version]
|
20
|
+
# options = { out: StringIO.new }
|
21
|
+
# result = RubyGit::CommandLine.run(*command, **options) #=> #<Process::Status: pid 21742 exit 0>
|
22
|
+
# result.stdout #=> "git version 2.30.2\n"
|
23
|
+
#
|
24
|
+
# @example A more complex example
|
25
|
+
# command = %w[rev-parse --show-toplevel]
|
26
|
+
# options = { chdir: worktree_path, chomp: true, out: StringIO.new, err: StringIO.new }
|
27
|
+
# RubyGit::CommandLine.run(*command, **options).stdout #=> "/path/to/working/tree"
|
28
|
+
#
|
29
|
+
# @param args [Array<String>] the git command and it arguments
|
30
|
+
# @param repository_path [String] the path to the git repository
|
31
|
+
# @param worktree_path [String] the path to the working tree
|
32
|
+
# @param options [Hash<Symbol, Object>] options to pass to the command line runner
|
33
|
+
#
|
34
|
+
# @return [RubyGit::CommandLine::Result] the result of running the command
|
35
|
+
#
|
36
|
+
# @raise [RubyGit::Error] if the command fails for any of the following reasons
|
37
|
+
# @raise [RubyGit::FailedError] if the command returns with non-zero exitstatus
|
38
|
+
# @raise [RubyGit::TimeoutError] if the command times out
|
39
|
+
# @raise [RubyGit::SignaledError] if the command terminates due to an uncaught signal
|
40
|
+
# @raise [RubyGit::ProcessIOError] if an exception is raised while collecting subprocess output
|
41
|
+
#
|
42
|
+
def self.run(*args, repository_path: nil, worktree_path: nil, **options)
|
43
|
+
runner = RubyGit::CommandLine::Runner.new(
|
44
|
+
env,
|
45
|
+
binary_path,
|
46
|
+
global_options(repository_path:, worktree_path:),
|
47
|
+
logger
|
48
|
+
)
|
49
|
+
runner.call(*args, **options)
|
50
|
+
end
|
51
|
+
|
52
|
+
# The environment variables that will be set for all git commands
|
53
|
+
# @return [Hash<String, String>]
|
54
|
+
# @api private
|
55
|
+
def self.env
|
56
|
+
{
|
57
|
+
'GIT_DIR' => nil,
|
58
|
+
'GIT_WORK_TREE' => nil,
|
59
|
+
'GIT_INDEX_FILE' => nil,
|
60
|
+
# 'GIT_SSH' => Git::Base.config.git_ssh,
|
61
|
+
'LC_ALL' => 'en_US.UTF-8'
|
62
|
+
}
|
63
|
+
end
|
64
|
+
|
65
|
+
# The path to the git binary
|
66
|
+
# @return [String]
|
67
|
+
# @api private
|
68
|
+
def self.binary_path = RubyGit.binary_path
|
69
|
+
|
70
|
+
# The global options that will be set for all git commands
|
71
|
+
# @return [Array<String>]
|
72
|
+
# @api private
|
73
|
+
def self.global_options(repository_path:, worktree_path:) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
74
|
+
[].tap do |global_opts|
|
75
|
+
global_opts << "--git-dir=#{repository_path}" unless repository_path.nil?
|
76
|
+
global_opts << "--work-tree=#{worktree_path}" unless worktree_path.nil?
|
77
|
+
global_opts << '-c' << 'core.quotePath=true'
|
78
|
+
global_opts << '-c' << 'color.ui=false'
|
79
|
+
global_opts << '-c' << 'color.advice=false'
|
80
|
+
global_opts << '-c' << 'color.diff=false'
|
81
|
+
global_opts << '-c' << 'color.grep=false'
|
82
|
+
global_opts << '-c' << 'color.push=false'
|
83
|
+
global_opts << '-c' << 'color.remote=false'
|
84
|
+
global_opts << '-c' << 'color.showBranch=false'
|
85
|
+
global_opts << '-c' << 'color.status=false'
|
86
|
+
global_opts << '-c' << 'color.transport=false'
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
# The logger to use for logging git commands
|
91
|
+
# @return [Logger]
|
92
|
+
# @api private
|
93
|
+
def self.logger = RubyGit.logger
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rchardet'
|
4
|
+
|
5
|
+
module RubyGit
|
6
|
+
# Utility to normalize string encoding
|
7
|
+
# @api public
|
8
|
+
module EncodingNormalizer
|
9
|
+
# Detects the character encoding used to create a string or binary data
|
10
|
+
#
|
11
|
+
# Detects the encoding of a string or return binary if it cannot be detected
|
12
|
+
#
|
13
|
+
# @example
|
14
|
+
# RubyGit::EncodingNormalizer.detect_encoding("Hello, world!") #=> "ascii"
|
15
|
+
# RubyGit::EncodingNormalizer.detect_encoding("\xCB\xEF\xF1\xE5\xEC") #=> "ISO-8859-7"
|
16
|
+
# RubyGit::EncodingNormalizer.detect_encoding("\xC0\xCC\xB0\xCD\xC0\xBA") #=> "EUC-KR"
|
17
|
+
#
|
18
|
+
# @param str [String] the string to detect the encoding of
|
19
|
+
# @return [String] the detected encoding
|
20
|
+
#
|
21
|
+
def self.detect_encoding(str)
|
22
|
+
CharDet.detect(str)&.dig('encoding') || Encoding::BINARY.name
|
23
|
+
end
|
24
|
+
|
25
|
+
# Normalizes the encoding to normalize_to
|
26
|
+
#
|
27
|
+
# @example
|
28
|
+
# RubyGit::EncodingNormalizer.normalize("Hello, world!") #=> "Hello, world!"
|
29
|
+
# RubyGit::EncodingNormalizer.normalize("\xCB\xEF\xF1\xE5\xEC") #=> "Λορεμ"
|
30
|
+
# RubyGit::EncodingNormalizer.normalize("\xC0\xCC\xB0\xCD\xC0\xBA") #=> "이것은"
|
31
|
+
#
|
32
|
+
# @param str [String] the string to normalize
|
33
|
+
# @param normalize_to [String] the name of the encoding to normalize to
|
34
|
+
#
|
35
|
+
# @return [String] the string with encoding converted to normalize_to
|
36
|
+
#
|
37
|
+
# @raise [Encoding::UndefinedConversionError] if the string cannot be converted to the default encoding
|
38
|
+
#
|
39
|
+
def self.normalize(str, normalize_to: Encoding::UTF_8.name)
|
40
|
+
encoding_options = { invalid: :replace, undef: :replace }
|
41
|
+
|
42
|
+
detected_encoding = detect_encoding(str)
|
43
|
+
|
44
|
+
return str if str.valid_encoding? && detected_encoding == normalize_to
|
45
|
+
|
46
|
+
str.encode(normalize_to, detected_encoding, **encoding_options)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,169 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RubyGit
|
4
|
+
# rubocop:disable Layout/LineLength
|
5
|
+
|
6
|
+
# Base class for all custom git module errors
|
7
|
+
#
|
8
|
+
# The git gem will only raise an `ArgumentError` or an error that is a subclass of
|
9
|
+
# `RubyGit::Error`. It does not explicitly raise any other types of errors.
|
10
|
+
#
|
11
|
+
# It is recommended to rescue `RubyGit::Error` to catch any runtime error raised by
|
12
|
+
# this gem unless you need more specific error handling.
|
13
|
+
#
|
14
|
+
# Git's custom errors are arranged in the following class heirarchy:
|
15
|
+
#
|
16
|
+
# ```text
|
17
|
+
# StandardError
|
18
|
+
# └─> RubyGit::Error
|
19
|
+
# ├─> RubyGit::CommandLineError
|
20
|
+
# │ ├─> RubyGit::FailedError
|
21
|
+
# │ └─> RubyGit::SignaledError
|
22
|
+
# │ └─> RubyGit::TimeoutError
|
23
|
+
# ├─> RubyGit::ProcessIOError
|
24
|
+
# └─> RubyGit::UnexpectedResultError
|
25
|
+
# ```
|
26
|
+
#
|
27
|
+
# | Error Class | Description |
|
28
|
+
# | --- | --- |
|
29
|
+
# | `Error` | This catch-all error serves as the base class for other custom errors raised by the git gem. |
|
30
|
+
# | `CommandLineError` | A subclass of this error is raised when there is a problem executing the git command line. |
|
31
|
+
# | `FailedError` | This error is raised when the git command line exits with a non-zero status code that is not expected by the git gem. |
|
32
|
+
# | `SignaledError` | This error is raised when the git command line is terminated as a result of receiving a signal. This could happen if the process is forcibly terminated or if there is a serious system error. |
|
33
|
+
# | `TimeoutError` | This is a specific type of `SignaledError` that is raised when the git command line operation times out and is killed via the SIGKILL signal. This happens if the operation takes longer than the timeout duration configured in `Git.config.timeout` or via the `:timeout` parameter given in git methods that support timeouts. |
|
34
|
+
# | `ProcessIOError` | An error was encountered reading or writing to a subprocess. |
|
35
|
+
# | `UnexpectedResultError` | The command line ran without error but did not return the expected results. |
|
36
|
+
#
|
37
|
+
# @example Rescuing a generic error
|
38
|
+
# begin
|
39
|
+
# # some git operation
|
40
|
+
# rescue RubyGit::Error => e
|
41
|
+
# puts "An error occurred: #{e.message}"
|
42
|
+
# end
|
43
|
+
#
|
44
|
+
# @example Rescuing a timeout error
|
45
|
+
# begin
|
46
|
+
# timeout_duration = 0.001 # seconds
|
47
|
+
# repo = Git.clone('https://github.com/ruby-git/ruby-git', 'ruby-git-temp', timeout: timeout_duration)
|
48
|
+
# rescue RubyGit::TimeoutError => e # Catch the more specific error first!
|
49
|
+
# puts "Git clone took too long and timed out #{e}"
|
50
|
+
# rescue RubyGit::Error => e
|
51
|
+
# puts "Received the following error: #{e}"
|
52
|
+
# end
|
53
|
+
#
|
54
|
+
# @see RubyGit::CommandLineError
|
55
|
+
# @see RubyGit::FailedError
|
56
|
+
# @see RubyGit::SignaledError
|
57
|
+
# @see RubyGit::TimeoutError
|
58
|
+
# @see RubyGit::ProcessIOError
|
59
|
+
# @see RubyGit::UnexpectedResultError
|
60
|
+
#
|
61
|
+
# @api public
|
62
|
+
#
|
63
|
+
class Error < StandardError; end
|
64
|
+
|
65
|
+
# rubocop:enable Layout/LineLength
|
66
|
+
|
67
|
+
# Raised when a git command fails or exits because of an uncaught signal
|
68
|
+
#
|
69
|
+
# The git command executed, status, stdout, and stderr are available from this
|
70
|
+
# object.
|
71
|
+
#
|
72
|
+
# The Gem will raise a more specific error for each type of failure:
|
73
|
+
#
|
74
|
+
# * {RubyGit::FailedError}: when the git command exits with a non-zero status
|
75
|
+
# * {RubyGit::SignaledError}: when the git command exits because of an uncaught signal
|
76
|
+
# * {RubyGit::TimeoutError}: when the git command times out
|
77
|
+
#
|
78
|
+
# @api public
|
79
|
+
#
|
80
|
+
class CommandLineError < RubyGit::Error
|
81
|
+
# Create a CommandLineError object
|
82
|
+
#
|
83
|
+
# @example
|
84
|
+
# `exit 1` # set $? appropriately for this example
|
85
|
+
# result = RubyGit::CommandLineResult.new(%w[git status], $?, 'stdout', 'stderr')
|
86
|
+
# error = RubyGit::CommandLineError.new(result)
|
87
|
+
# error.to_s #=> '["git", "status"], status: pid 89784 exit 1, stderr: "stderr"'
|
88
|
+
#
|
89
|
+
# @param result [RubyGit::CommandLineResult] the result of the git command including
|
90
|
+
# the git command, status, stdout, and stderr
|
91
|
+
#
|
92
|
+
def initialize(result)
|
93
|
+
@result = result
|
94
|
+
super(error_message)
|
95
|
+
end
|
96
|
+
|
97
|
+
# The human readable representation of this error
|
98
|
+
#
|
99
|
+
# @example
|
100
|
+
# error.error_message #=> '["git", "status"], status: pid 89784 exit 1, stderr: "stderr"'
|
101
|
+
#
|
102
|
+
# @return [String]
|
103
|
+
#
|
104
|
+
def error_message = <<~MESSAGE.chomp
|
105
|
+
#{result.command}, status: #{result}, stderr: #{result.stderr.inspect}
|
106
|
+
MESSAGE
|
107
|
+
|
108
|
+
# @attribute [r] result
|
109
|
+
#
|
110
|
+
# The the git command result with the command and its status and output
|
111
|
+
#
|
112
|
+
# @example
|
113
|
+
# error.result #=> #<RubyGit::CommandLineResult:0x00000001046bd488 ...>
|
114
|
+
#
|
115
|
+
# @return [RubyGit::CommandLineResult]
|
116
|
+
#
|
117
|
+
attr_reader :result
|
118
|
+
end
|
119
|
+
|
120
|
+
# This error is raised when a git command returns a non-zero exitstatus
|
121
|
+
#
|
122
|
+
# The git command executed, status, stdout, and stderr are available from this
|
123
|
+
# object.
|
124
|
+
#
|
125
|
+
# @api public
|
126
|
+
#
|
127
|
+
class FailedError < RubyGit::CommandLineError; end
|
128
|
+
|
129
|
+
# This error is raised when a git command exits because of an uncaught signal
|
130
|
+
#
|
131
|
+
# @api public
|
132
|
+
#
|
133
|
+
class SignaledError < RubyGit::CommandLineError; end
|
134
|
+
|
135
|
+
# This error is raised when a git command takes longer than the configured timeout
|
136
|
+
#
|
137
|
+
# The git command executed, status, stdout, and stderr, and the timeout duration
|
138
|
+
# are available from this object.
|
139
|
+
#
|
140
|
+
# result.status.timeout? will be `true`
|
141
|
+
#
|
142
|
+
# @api public
|
143
|
+
#
|
144
|
+
class TimeoutError < RubyGit::SignaledError
|
145
|
+
# The human readable representation of this error
|
146
|
+
#
|
147
|
+
# @example
|
148
|
+
# error.error_message #=>
|
149
|
+
# '["sleep", "10"], status: pid 88811 SIGKILL (signal 9), stderr: "err output", timed out after 1s'
|
150
|
+
#
|
151
|
+
# @return [String]
|
152
|
+
#
|
153
|
+
def error_message = <<~MESSAGE.chomp
|
154
|
+
#{super}, timed out after #{result.options.timeout_after}s
|
155
|
+
MESSAGE
|
156
|
+
end
|
157
|
+
|
158
|
+
# Raised when the output of a git command can not be read
|
159
|
+
#
|
160
|
+
# @api public
|
161
|
+
#
|
162
|
+
class ProcessIOError < RubyGit::Error; end
|
163
|
+
|
164
|
+
# Raised when the git command result was not as expected
|
165
|
+
#
|
166
|
+
# @api public
|
167
|
+
#
|
168
|
+
class UnexpectedResultError < RubyGit::Error; end
|
169
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RubyGit
|
4
|
+
# The repository is the database of all the objects, refs, and other data that
|
5
|
+
# make up the history of a project.
|
6
|
+
#
|
7
|
+
# @api public
|
8
|
+
#
|
9
|
+
class Repository
|
10
|
+
# @attribute [r] path
|
11
|
+
#
|
12
|
+
# The absolute path to the repository
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
# repository = RubyGit::Repository.new('.git')
|
16
|
+
# repository.path = '/absolute/path/.git'
|
17
|
+
#
|
18
|
+
# @return [String]
|
19
|
+
#
|
20
|
+
attr_reader :path
|
21
|
+
|
22
|
+
# Create a new Repository object with the given repository path
|
23
|
+
#
|
24
|
+
# @example
|
25
|
+
# RubyGit::Repository.new('/path/to/repository') #=> #<RubyGit::Repository ...>
|
26
|
+
#
|
27
|
+
# @param [String] repository_path the path to the repository
|
28
|
+
#
|
29
|
+
def initialize(repository_path)
|
30
|
+
@path = File.realpath(repository_path)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|