[ruby/irb] History refactors (https://github.com/ruby/irb/pull/1013)
* Extract logic save_history in separate helper * Extract logic history_file in helper * Allow for readonly history https://github.com/ruby/irb/commit/52307f9026
This commit is contained in:
parent
2dab59933c
commit
eccfb6e60c
@ -14,6 +14,7 @@ require_relative "irb/default_commands"
|
|||||||
|
|
||||||
require_relative "irb/ruby-lex"
|
require_relative "irb/ruby-lex"
|
||||||
require_relative "irb/statement"
|
require_relative "irb/statement"
|
||||||
|
require_relative "irb/history"
|
||||||
require_relative "irb/input-method"
|
require_relative "irb/input-method"
|
||||||
require_relative "irb/locale"
|
require_relative "irb/locale"
|
||||||
require_relative "irb/color"
|
require_relative "irb/color"
|
||||||
@ -972,7 +973,7 @@ module IRB
|
|||||||
# debugger.
|
# debugger.
|
||||||
input = nil
|
input = nil
|
||||||
forced_exit = catch(:IRB_EXIT) do
|
forced_exit = catch(:IRB_EXIT) do
|
||||||
if IRB.conf[:SAVE_HISTORY] && context.io.support_history_saving?
|
if History.save_history? && context.io.support_history_saving?
|
||||||
# Previous IRB session's history has been saved when `Irb#run` is exited We need
|
# Previous IRB session's history has been saved when `Irb#run` is exited We need
|
||||||
# to make sure the saved history is not saved again by resetting the counter
|
# to make sure the saved history is not saved again by resetting the counter
|
||||||
context.io.reset_history_counter
|
context.io.reset_history_counter
|
||||||
@ -1003,9 +1004,10 @@ module IRB
|
|||||||
prev_context = conf[:MAIN_CONTEXT]
|
prev_context = conf[:MAIN_CONTEXT]
|
||||||
conf[:MAIN_CONTEXT] = context
|
conf[:MAIN_CONTEXT] = context
|
||||||
|
|
||||||
save_history = !in_nested_session && conf[:SAVE_HISTORY] && context.io.support_history_saving?
|
load_history = !in_nested_session && context.io.support_history_saving?
|
||||||
|
save_history = load_history && History.save_history?
|
||||||
|
|
||||||
if save_history
|
if load_history
|
||||||
context.io.load_history
|
context.io.load_history
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,6 +1,42 @@
|
|||||||
require "pathname"
|
require "pathname"
|
||||||
|
|
||||||
module IRB
|
module IRB
|
||||||
|
module History
|
||||||
|
class << self
|
||||||
|
# Integer representation of <code>IRB.conf[:HISTORY_FILE]</code>.
|
||||||
|
def save_history
|
||||||
|
num = IRB.conf[:SAVE_HISTORY].to_i
|
||||||
|
# Bignums cause RangeErrors when slicing arrays.
|
||||||
|
# Treat such values as 'infinite'.
|
||||||
|
(num > save_history_max) ? -1 : num
|
||||||
|
end
|
||||||
|
|
||||||
|
def save_history?
|
||||||
|
!save_history.zero?
|
||||||
|
end
|
||||||
|
|
||||||
|
def infinite?
|
||||||
|
save_history.negative?
|
||||||
|
end
|
||||||
|
|
||||||
|
# Might be nil when HOME and XDG_CONFIG_HOME are not available.
|
||||||
|
def history_file
|
||||||
|
if (history_file = IRB.conf[:HISTORY_FILE])
|
||||||
|
File.expand_path(history_file)
|
||||||
|
else
|
||||||
|
IRB.rc_file("_history")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def save_history_max
|
||||||
|
# Max fixnum (32-bit) that can be used without getting RangeError.
|
||||||
|
2**30 - 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
module HistorySavingAbility # :nodoc:
|
module HistorySavingAbility # :nodoc:
|
||||||
def support_history_saving?
|
def support_history_saving?
|
||||||
true
|
true
|
||||||
@ -11,76 +47,74 @@ module IRB
|
|||||||
end
|
end
|
||||||
|
|
||||||
def load_history
|
def load_history
|
||||||
|
history_file = History.history_file
|
||||||
|
return unless File.exist?(history_file.to_s)
|
||||||
|
|
||||||
history = self.class::HISTORY
|
history = self.class::HISTORY
|
||||||
|
|
||||||
if history_file = IRB.conf[:HISTORY_FILE]
|
File.open(history_file, "r:#{IRB.conf[:LC_MESSAGES].encoding}") do |f|
|
||||||
history_file = File.expand_path(history_file)
|
f.each { |l|
|
||||||
end
|
l = l.chomp
|
||||||
history_file = IRB.rc_file("_history") unless history_file
|
if self.class == RelineInputMethod and history.last&.end_with?("\\")
|
||||||
if history_file && File.exist?(history_file)
|
history.last.delete_suffix!("\\")
|
||||||
File.open(history_file, "r:#{IRB.conf[:LC_MESSAGES].encoding}") do |f|
|
history.last << "\n" << l
|
||||||
f.each { |l|
|
else
|
||||||
l = l.chomp
|
history << l
|
||||||
if self.class == RelineInputMethod and history.last&.end_with?("\\")
|
end
|
||||||
history.last.delete_suffix!("\\")
|
}
|
||||||
history.last << "\n" << l
|
|
||||||
else
|
|
||||||
history << l
|
|
||||||
end
|
|
||||||
}
|
|
||||||
end
|
|
||||||
@loaded_history_lines = history.size
|
|
||||||
@loaded_history_mtime = File.mtime(history_file)
|
|
||||||
end
|
end
|
||||||
|
@loaded_history_lines = history.size
|
||||||
|
@loaded_history_mtime = File.mtime(history_file)
|
||||||
end
|
end
|
||||||
|
|
||||||
def save_history
|
def save_history
|
||||||
|
return unless History.save_history?
|
||||||
|
return unless (history_file = History.history_file)
|
||||||
|
unless ensure_history_file_writable(history_file)
|
||||||
|
warn <<~WARN
|
||||||
|
Can't write history to #{History.history_file.inspect} due to insufficient permissions.
|
||||||
|
Please verify the value of `IRB.conf[:HISTORY_FILE]`. Ensure the folder exists and that both the folder and file (if it exists) are writable.
|
||||||
|
WARN
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
history = self.class::HISTORY.to_a
|
history = self.class::HISTORY.to_a
|
||||||
|
|
||||||
if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) != 0
|
if File.exist?(history_file) &&
|
||||||
if history_file = IRB.conf[:HISTORY_FILE]
|
File.mtime(history_file) != @loaded_history_mtime
|
||||||
history_file = File.expand_path(history_file)
|
history = history[@loaded_history_lines..-1] if @loaded_history_lines
|
||||||
end
|
append_history = true
|
||||||
history_file = IRB.rc_file("_history") unless history_file
|
end
|
||||||
|
|
||||||
# When HOME and XDG_CONFIG_HOME are not available, history_file might be nil
|
File.open(history_file, (append_history ? "a" : "w"), 0o600, encoding: IRB.conf[:LC_MESSAGES]&.encoding) do |f|
|
||||||
return unless history_file
|
hist = history.map { |l| l.scrub.split("\n").join("\\\n") }
|
||||||
|
|
||||||
# Change the permission of a file that already exists[BUG #7694]
|
unless append_history || History.infinite?
|
||||||
begin
|
hist = hist.last(History.save_history)
|
||||||
if File.stat(history_file).mode & 066 != 0
|
|
||||||
File.chmod(0600, history_file)
|
|
||||||
end
|
|
||||||
rescue Errno::ENOENT
|
|
||||||
rescue Errno::EPERM
|
|
||||||
return
|
|
||||||
rescue
|
|
||||||
raise
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if File.exist?(history_file) &&
|
f.puts(hist)
|
||||||
File.mtime(history_file) != @loaded_history_mtime
|
end
|
||||||
history = history[@loaded_history_lines..-1] if @loaded_history_lines
|
end
|
||||||
append_history = true
|
|
||||||
end
|
|
||||||
|
|
||||||
pathname = Pathname.new(history_file)
|
private
|
||||||
unless Dir.exist?(pathname.dirname)
|
|
||||||
warn "Warning: The directory to save IRB's history file does not exist. Please double check `IRB.conf[:HISTORY_FILE]`'s value."
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
File.open(history_file, (append_history ? 'a' : 'w'), 0o600, encoding: IRB.conf[:LC_MESSAGES]&.encoding) do |f|
|
# Returns boolean whether writing to +history_file+ will be possible.
|
||||||
hist = history.map{ |l| l.scrub.split("\n").join("\\\n") }
|
# Permissions of already existing +history_file+ are changed to
|
||||||
unless append_history
|
# owner-only-readable if necessary [BUG #7694].
|
||||||
begin
|
def ensure_history_file_writable(history_file)
|
||||||
hist = hist.last(num) if hist.size > num and num > 0
|
history_file = Pathname.new(history_file)
|
||||||
rescue RangeError # bignum too big to convert into `long'
|
|
||||||
# Do nothing because the bignum should be treated as infinity
|
return false unless history_file.dirname.writable?
|
||||||
end
|
return true unless history_file.exist?
|
||||||
end
|
|
||||||
f.puts(hist)
|
begin
|
||||||
|
if history_file.stat.mode & 0o66 != 0
|
||||||
|
history_file.chmod 0o600
|
||||||
end
|
end
|
||||||
|
true
|
||||||
|
rescue Errno::EPERM # no permissions
|
||||||
|
false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -39,6 +39,21 @@ module TestIRB
|
|||||||
include IRB::HistorySavingAbility
|
include IRB::HistorySavingAbility
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_history_dont_save
|
||||||
|
omit "Skip Editline" if /EditLine/n.match(Readline::VERSION)
|
||||||
|
IRB.conf[:SAVE_HISTORY] = nil
|
||||||
|
assert_history(<<~EXPECTED_HISTORY, <<~INITIAL_HISTORY, <<~INPUT)
|
||||||
|
1
|
||||||
|
2
|
||||||
|
EXPECTED_HISTORY
|
||||||
|
1
|
||||||
|
2
|
||||||
|
INITIAL_HISTORY
|
||||||
|
3
|
||||||
|
exit
|
||||||
|
INPUT
|
||||||
|
end
|
||||||
|
|
||||||
def test_history_save_1
|
def test_history_save_1
|
||||||
omit "Skip Editline" if /EditLine/n.match(Readline::VERSION)
|
omit "Skip Editline" if /EditLine/n.match(Readline::VERSION)
|
||||||
IRB.conf[:SAVE_HISTORY] = 1
|
IRB.conf[:SAVE_HISTORY] = 1
|
||||||
@ -166,7 +181,7 @@ module TestIRB
|
|||||||
IRB.conf[:HISTORY_FILE] = "fake/fake/fake/history_file"
|
IRB.conf[:HISTORY_FILE] = "fake/fake/fake/history_file"
|
||||||
io = TestInputMethodWithRelineHistory.new
|
io = TestInputMethodWithRelineHistory.new
|
||||||
|
|
||||||
assert_warn(/history file does not exist/) do
|
assert_warn(/ensure the folder exists/i) do
|
||||||
io.save_history
|
io.save_history
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user