Skip to content

Commit cca0deb

Browse files
committed
fix: report correct line number in deprecation warnings
ActiveSupport::Deprecation is designed to report the line where the deprecated method is called (one level up the call stack), not where the warning itself is defined. The previous implementation triggered warnings from an internal helper method, causing them to report the location with the Git gem. This commit moves the `warn` call into the public-facing deprecated method, ensuring the warning correctly points to the user's code that should be changed.
1 parent 761b6ff commit cca0deb

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

lib/git/log.rb

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,28 +90,56 @@ def execute
9090

9191
# @deprecated Use {#execute} and call `each` on the result.
9292
def each(&)
93-
deprecate_and_run
93+
Git::Deprecation.warn(
94+
'Calling Git::Log#each is deprecated. Call #execute and then #each on the result object.'
95+
)
96+
run_log_if_dirty
9497
@commits.each(&)
9598
end
9699

97100
# @deprecated Use {#execute} and call `size` on the result.
98101
def size
99-
deprecate_and_run
102+
Git::Deprecation.warn(
103+
'Calling Git::Log#size is deprecated. Call #execute and then #size on the result object.'
104+
)
105+
run_log_if_dirty
100106
@commits&.size
101107
end
102108

103109
# @deprecated Use {#execute} and call `to_s` on the result.
104110
def to_s
105-
deprecate_and_run
111+
Git::Deprecation.warn(
112+
'Calling Git::Log#to_s is deprecated. Call #execute and then #to_s on the result object.'
113+
)
114+
run_log_if_dirty
106115
@commits&.map(&:to_s)&.join("\n")
107116
end
108117

109118
# @deprecated Use {#execute} and call the method on the result.
110-
%i[first last []].each do |method_name|
111-
define_method(method_name) do |*args|
112-
deprecate_and_run
113-
@commits&.public_send(method_name, *args)
114-
end
119+
def first
120+
Git::Deprecation.warn(
121+
'Calling Git::Log#first is deprecated. Call #execute and then #first on the result object.'
122+
)
123+
run_log_if_dirty
124+
@commits&.first
125+
end
126+
127+
# @deprecated Use {#execute} and call the method on the result.
128+
def last
129+
Git::Deprecation.warn(
130+
'Calling Git::Log#last is deprecated. Call #execute and then #last on the result object.'
131+
)
132+
run_log_if_dirty
133+
@commits&.last
134+
end
135+
136+
# @deprecated Use {#execute} and call the method on the result.
137+
def [](index)
138+
Git::Deprecation.warn(
139+
'Calling Git::Log#[] is deprecated. Call #execute and then #[] on the result object.'
140+
)
141+
run_log_if_dirty
142+
@commits&.[](index)
115143
end
116144

117145
# @!endgroup
@@ -132,12 +160,5 @@ def run_log_if_dirty
132160
@dirty = false
133161
end
134162

135-
def deprecate_and_run(method = caller_locations(1, 1)[0].label)
136-
Git::Deprecation.warn(
137-
"Calling Git::Log##{method} is deprecated. " \
138-
"Call #execute and then ##{method} on the result object."
139-
)
140-
run_log_if_dirty
141-
end
142163
end
143164
end

0 commit comments

Comments
 (0)