Skip to content

Commit

Permalink
Fix version parsing (#605)
Browse files Browse the repository at this point in the history
Signed-off-by: James Couball <jcouball@yahoo.com>
  • Loading branch information
jcouball committed Jan 12, 2023
1 parent 429f0bb commit 23a0ac4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/git/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ def initialize(base = nil, logger = nil)
@git_work_dir = base[:working_directory]
end
@logger = logger

Git::Lib.warn_if_old_command(self)
end

# creates or reinitializes the repository
Expand Down Expand Up @@ -1029,8 +1027,9 @@ def archive(sha, file = nil, opts = {})
# returns the current version of git, as an Array of Fixnums.
def current_command_version
output = command('version')
version = output[/\d+\.\d+(\.\d+)+/]
version.split('.').collect {|i| i.to_i}
version = output[/\d+(\.\d+)+/]
version_parts = version.split('.').collect { |i| i.to_i }
version_parts.fill(0, version_parts.length...3)
end

def required_command_version
Expand All @@ -1043,10 +1042,11 @@ def meets_required_version?

def self.warn_if_old_command(lib)
return true if @version_checked
@version_checked = true
unless lib.meets_required_version?
$stderr.puts "[WARNING] The git gem requires git #{lib.required_command_version.join('.')} or later, but only found #{lib.current_command_version.join('.')}. You should probably upgrade."
end
@version_checked = true
true
end

private
Expand Down Expand Up @@ -1104,6 +1104,8 @@ def with_custom_env_variables(&block)
end

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

command_opts = { chomp: true, redirect: '' }
if opts.last.is_a?(Hash)
command_opts.merge!(opts.pop)
Expand Down
27 changes: 27 additions & 0 deletions tests/units/test_lib_meets_required_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,31 @@ def test_with_old_command_version
lib.define_singleton_method(:current_command_version) { [major_version, minor_version] }
assert !lib.meets_required_version?
end

def test_parse_version
lib = Git::Lib.new(nil, nil)

versions_to_test = [
{ version_string: 'git version 2.1', expected_result: [2, 1, 0] },
{ version_string: 'git version 2.28.4', expected_result: [2, 28, 4] },
{ version_string: 'git version 2.32.GIT', expected_result: [2, 32, 0] },
]

lib.instance_variable_set(:@next_version_index, 0)

lib.define_singleton_method(:command) do |cmd, *opts, &block|
raise ArgumentError unless cmd == 'version'
versions_to_test[@next_version_index][:version_string].tap { @next_version_index += 1 }
end

lib.define_singleton_method(:next_version_index) { @next_version_index }

expected_version = versions_to_test[lib.next_version_index][:expected_result]
actual_version = lib.current_command_version
assert_equal(expected_version, actual_version)

expected_version = versions_to_test[lib.next_version_index][:expected_result]
actual_version = lib.current_command_version
assert_equal(expected_version, actual_version)
end
end

0 comments on commit 23a0ac4

Please sign in to comment.