Skip to content

Fix exception when Git is autoloaded #594

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
Aug 19, 2022
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
5 changes: 0 additions & 5 deletions lib/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@
require 'git/worktree'
require 'git/worktrees'

lib = Git::Lib.new(nil, nil)
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

# The Git module provides the basic functions to open a git
# reference to work with. You can open a working directory,
# open a bare repository, initialize a new repo or clone an
Expand Down
9 changes: 9 additions & 0 deletions lib/git/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ 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 @@ -1027,6 +1029,13 @@ def meets_required_version?
(self.current_command_version <=> self.required_command_version) >= 0
end

def self.warn_if_old_command(lib)
return true if @version_checked
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
end

private

Expand Down
7 changes: 5 additions & 2 deletions tests/all_tests.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Dir.chdir(File.dirname(__FILE__)) do
Dir.glob('**/test_*.rb') do |test_case|
require "#{File.expand_path(File.dirname(__FILE__))}/#{test_case}"
Dir.glob('**/test_*.rb') do |test_case|
require "#{File.expand_path(File.dirname(__FILE__))}/#{test_case}"
end
end

# To run a single test:
# require_relative 'units/test_lib_meets_required_version'
24 changes: 24 additions & 0 deletions tests/units/test_lib_meets_required_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../test_helper'

class TestLibMeetsRequiredVersion < Test::Unit::TestCase
def test_with_supported_command_version
lib = Git::Lib.new(nil, nil)
major_version, minor_version = lib.required_command_version
lib.define_singleton_method(:current_command_version) { [major_version, minor_version] }
assert lib.meets_required_version?
end

def test_with_old_command_version
lib = Git::Lib.new(nil, nil)
major_version, minor_version = lib.required_command_version

# Set the major version to be returned by #current_command_version to be an
# earlier version than required
major_version = major_version - 1

lib.define_singleton_method(:current_command_version) { [major_version, minor_version] }
assert !lib.meets_required_version?
end
end