Skip to content

Commit

Permalink
Extract logic history_file in helper
Browse files Browse the repository at this point in the history
  • Loading branch information
eval committed Oct 8, 2024
1 parent f0820ce commit f64eaea
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 42 deletions.
102 changes: 61 additions & 41 deletions lib/irb/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@ 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

def history_file_present
history_file if history_file && File.exist?(history_file)
end

def folder_writable?
history_file && Pathname.new(history_file).dirname.writable?
end

def append?(history_file, loaded_mtime)
File.exist?(history_file) &&
File.mtime(history_file) != loaded_mtime
end

def file_encoding
IRB.conf[:LC_MESSAGES].encoding
end

private

def save_history_max
Expand All @@ -40,12 +66,8 @@ def reset_history_counter
def load_history
history = self.class::HISTORY

if history_file = IRB.conf[:HISTORY_FILE]
history_file = File.expand_path(history_file)
end
history_file = IRB.rc_file("_history") unless history_file
if history_file && File.exist?(history_file)
File.open(history_file, "r:#{IRB.conf[:LC_MESSAGES].encoding}") do |f|
if (history_file = History.history_file_present)
File.open(history_file, "r:#{History.file_encoding}") do |f|
f.each { |l|
l = l.chomp
if self.class == RelineInputMethod and history.last&.end_with?("\\")
Expand All @@ -62,50 +84,48 @@ def load_history
end

def save_history
return unless History.save_history?
return unless (history_file = History.history_file)
unless History.folder_writable? && correct_permissions!(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

if History.save_history?
if history_file = IRB.conf[:HISTORY_FILE]
history_file = File.expand_path(history_file)
end
history_file = IRB.rc_file("_history") unless history_file

# When HOME and XDG_CONFIG_HOME are not available, history_file might be nil
return unless history_file

# Change the permission of a file that already exists[BUG #7694]
begin
if File.stat(history_file).mode & 066 != 0
File.chmod(0600, history_file)
end
rescue Errno::ENOENT
rescue Errno::EPERM
return
rescue
raise
end
if History.append?(history_file, @loaded_history_mtime)
history = history[@loaded_history_lines..-1] if @loaded_history_lines
append_history = true
end

if File.exist?(history_file) &&
File.mtime(history_file) != @loaded_history_mtime
history = history[@loaded_history_lines..-1] if @loaded_history_lines
append_history = true
end
File.open(history_file, (append_history ? "a" : "w"), 0o600, encoding: History.file_encoding) do |f|
hist = history.map { |l| l.scrub.split("\n").join("\\\n") }

pathname = Pathname.new(history_file)
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
unless append_history || History.infinite?
hist = hist.last(History.save_history)
end

File.open(history_file, (append_history ? 'a' : 'w'), 0o600, encoding: IRB.conf[:LC_MESSAGES]&.encoding) do |f|
hist = history.map{ |l| l.scrub.split("\n").join("\\\n") }
f.puts(hist)
end
end

private

unless append_history || History.infinite?
hist = hist.last(History.save_history)
end
# Ensure existing +history_file+ is owner-only-readable [BUG #7694].
# Yields boolean whether chmod succeeded. `true` when file is absent.
def correct_permissions!(history_file)
return true unless File.exist?(history_file)

f.puts(hist)
begin
if File.stat(history_file).mode & 0o66 != 0
File.chmod(0o600, history_file)
end
true
rescue Errno::EPERM # no permissions
false
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/irb/test_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def test_history_does_not_raise_when_history_file_directory_does_not_exist
IRB.conf[:HISTORY_FILE] = "fake/fake/fake/history_file"
io = TestInputMethodWithRelineHistory.new

assert_warn(/history file does not exist/) do
assert_warn(/ensure the folder exists/i) do
io.save_history
end

Expand Down

0 comments on commit f64eaea

Please sign in to comment.