Skip to content

Fix version parsing #605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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