diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 293f2878..b4d16152 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/tests/units/test_lib_meets_required_version.rb b/tests/units/test_lib_meets_required_version.rb index fce57241..1f572d31 100644 --- a/tests/units/test_lib_meets_required_version.rb +++ b/tests/units/test_lib_meets_required_version.rb @@ -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