Skip to content

Fix deprecations and improve warning reporting #830

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 7 commits into from
Jul 8, 2025
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
2 changes: 1 addition & 1 deletion lib/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require 'active_support/deprecation'

module Git
Deprecation = ActiveSupport::Deprecation.new('3.0', 'Git')
Deprecation = ActiveSupport::Deprecation.new('5.0.0', 'Git')
end

require 'git/author'
Expand Down
15 changes: 4 additions & 11 deletions lib/git/diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,31 @@ def each(&)
@full_diff_files.map { |file| file[1] }.each(&)
end

def size
stats_provider.total[:files]
end

#
# DEPRECATED METHODS
#

def name_status
Git::Deprecation.warn('Git::Diff#name_status is deprecated. Use Git::Base#diff_path_status instead.')
path_status_provider.to_h
end

def size
Git::Deprecation.warn('Git::Diff#size is deprecated. Use Git::Base#diff_stats(...).total[:files] instead.')
stats_provider.total[:files]
end

def lines
Git::Deprecation.warn('Git::Diff#lines is deprecated. Use Git::Base#diff_stats(...).lines instead.')
stats_provider.lines
end

def deletions
Git::Deprecation.warn('Git::Diff#deletions is deprecated. Use Git::Base#diff_stats(...).deletions instead.')
stats_provider.deletions
end

def insertions
Git::Deprecation.warn('Git::Diff#insertions is deprecated. Use Git::Base#diff_stats(...).insertions instead.')
stats_provider.insertions
end

def stats
Git::Deprecation.warn('Git::Diff#stats is deprecated. Use Git::Base#diff_stats instead.')
# CORRECTED: Re-create the original hash structure for backward compatibility
{
files: stats_provider.files,
total: stats_provider.total
Expand Down
52 changes: 36 additions & 16 deletions lib/git/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,28 +90,56 @@ def execute

# @deprecated Use {#execute} and call `each` on the result.
def each(&)
deprecate_and_run
Git::Deprecation.warn(
'Calling Git::Log#each is deprecated. Call #execute and then #each on the result object.'
)
run_log_if_dirty
@commits.each(&)
end

# @deprecated Use {#execute} and call `size` on the result.
def size
deprecate_and_run
Git::Deprecation.warn(
'Calling Git::Log#size is deprecated. Call #execute and then #size on the result object.'
)
run_log_if_dirty
@commits&.size
end

# @deprecated Use {#execute} and call `to_s` on the result.
def to_s
deprecate_and_run
Git::Deprecation.warn(
'Calling Git::Log#to_s is deprecated. Call #execute and then #to_s on the result object.'
)
run_log_if_dirty
@commits&.map(&:to_s)&.join("\n")
end

# @deprecated Use {#execute} and call the method on the result.
%i[first last []].each do |method_name|
define_method(method_name) do |*args|
deprecate_and_run
@commits&.public_send(method_name, *args)
end
def first
Git::Deprecation.warn(
'Calling Git::Log#first is deprecated. Call #execute and then #first on the result object.'
)
run_log_if_dirty
@commits&.first
end

# @deprecated Use {#execute} and call the method on the result.
def last
Git::Deprecation.warn(
'Calling Git::Log#last is deprecated. Call #execute and then #last on the result object.'
)
run_log_if_dirty
@commits&.last
end

# @deprecated Use {#execute} and call the method on the result.
def [](index)
Git::Deprecation.warn(
'Calling Git::Log#[] is deprecated. Call #execute and then #[] on the result object.'
)
run_log_if_dirty
@commits&.[](index)
end

# @!endgroup
Expand All @@ -131,13 +159,5 @@ def run_log_if_dirty
@commits = log_data.map { |c| Git::Object::Commit.new(@base, c['sha'], c) }
@dirty = false
end

def deprecate_and_run(method = caller_locations(1, 1)[0].label)
Git::Deprecation.warn(
"Calling Git::Log##{method} is deprecated. " \
"Call #execute and then ##{method} on the result object."
)
run_log_if_dirty
end
end
end
2 changes: 1 addition & 1 deletion lib/git/stashes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(base)

@base.lib.stashes_all.each do |indexed_message|
_index, message = indexed_message
@stashes.unshift(Git::Stash.new(@base, message, true))
@stashes.unshift(Git::Stash.new(@base, message, save: true))
end
end

Expand Down
15 changes: 13 additions & 2 deletions tests/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,19 @@
$stdout.sync = true
$stderr.sync = true

# Silence deprecation warnings during tests
Git::Deprecation.behavior = :silence
# Make tests that emit a deprecation warning fail

# Deprecation warnings should not be ignored.

# This is important so that:
# * when a user sees a deprecation warning, they can be confident it is coming from
# their code and not this gem
# * test output is clean and does not contain noisey deprecation warnings

# Tests whose purpose is to test that a deprecation warning is issued in the right
# circumstance should mock Git::Deprecation#warn to avoid raising an error.
#
Git::Deprecation.behavior = :raise

module Test
module Unit
Expand Down
11 changes: 7 additions & 4 deletions tests/units/test_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,23 @@ def test_commit
git.add('test_file_1')
git.add('test_file_2')

base_commit_id = git.log[0].objectish
commits = git.log.execute
base_commit_id = commits[0].objectish

git.commit('Test Commit')

original_commit_id = git.log[0].objectish
commits = git.log.execute
original_commit_id = commits[0].objectish

create_file('test_commit/test_file_3', 'content test_file_3')

git.add('test_file_3')

git.commit(nil, amend: true)

assert(git.log[0].objectish != original_commit_id)
assert(git.log[1].objectish == base_commit_id)
commits = git.log.execute
assert(commits[0].objectish != original_commit_id)
assert(commits[1].objectish == base_commit_id)
end
end
end
3 changes: 2 additions & 1 deletion tests/units/test_commit_with_empty_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def test_with_allow_empty_message_option
Dir.mktmpdir do |dir|
git = Git.init(dir)
git.commit('', { allow_empty: true, allow_empty_message: true })
assert_equal(1, git.log.to_a.size)
commits = git.log.execute
assert_equal(1, commits.to_a.size)
end
end
end
6 changes: 4 additions & 2 deletions tests/units/test_git_dir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ def test_git_dir_outside_work_tree
# * the commit was added to the log
#
max_log_size = 100
assert_equal(64, git.log(max_log_size).size)
commits = git.log(max_log_size).execute
assert_equal(64, commits.size)
git.add(file)
git.commit('This is a new commit')
assert_equal(false, git.status.changed?(file))
assert_equal(65, git.log(max_log_size).size)
commits = git.log(max_log_size).execute
assert_equal(65, commits.size)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions tests/units/test_git_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ def setup
end

def test_initalize_with_good_path_and_check_path
path = Git::Path.new(@git.index.to_s, true)
path = Git::Path.new(@git.index.to_s, must_exist: true)
assert_equal @git.index.to_s, path.to_s
end

def test_initialize_with_bad_path_and_check_path
assert_raises ArgumentError do
Git::Path.new('/this path does not exist', true)
Git::Path.new('/this path does not exist', must_exist: true)
end
end

def test_initialize_with_bad_path_and_no_check
path = Git::Path.new('/this path does not exist', false)
path = Git::Path.new('/this path does not exist', must_exist: false)
assert path.to_s.end_with?('/this path does not exist')

assert(path.to_s.match(%r{^(?:[A-Z]:)?/this path does not exist$}))
Expand Down
5 changes: 3 additions & 2 deletions tests/units/test_index_ops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ def test_revert
g.commit('second-commit')
g.gcommit('HEAD')

commits = g.log(10_000).count
commits = g.log(10_000).execute
g.revert(first_commit.sha)
assert_equal(commits + 1, g.log(10_000).count)
commits_after_revert = g.log(10_000).execute
assert_equal(commits.count + 1, commits_after_revert.count)
assert(!File.exist?('test-file2'))
end
end
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy