Fix regression causing missing automatic history saving after adding ephemeral items

Commit f36f757fa6 (Never rewrite history file when adding ephemeral
items, 2024-10-06) has a glaring bug: when adding to history ephemeral
items that has potential paths, we fail to re-enable automatic saving,
which effectively disables automatic saving for the entire session.

Only a call to history::save_all() at exit saves us from losing data.
But until exit, other fish will see our history (unless we run history
save/merge or similar).

Fix the imbalanced enable/disable and restructure the code a bit.
Also, extend the change from f36f757fa6 (never vacuum on ephemeral
items) to private mode.
This commit is contained in:
Johannes Altmanninger 2025-01-29 09:43:02 +01:00
parent 064d867873
commit acf9ba4195

View File

@ -1086,7 +1086,6 @@ impl HistoryImpl {
fn enable_automatic_saving(&mut self) { fn enable_automatic_saving(&mut self) {
assert!(self.disable_automatic_save_counter > 0); // negative overflow! assert!(self.disable_automatic_save_counter > 0); // negative overflow!
self.disable_automatic_save_counter -= 1; self.disable_automatic_save_counter -= 1;
self.save_unless_disabled();
} }
/// Irreversibly clears history. /// Irreversibly clears history.
@ -1620,7 +1619,7 @@ impl History {
let when = imp.timestamp_now(); let when = imp.timestamp_now();
let identifier = imp.next_identifier(); let identifier = imp.next_identifier();
let item = HistoryItem::new(s.to_owned(), when, identifier, persist_mode); let item = HistoryItem::new(s.to_owned(), when, identifier, persist_mode);
let do_save = persist_mode != PersistenceMode::Ephemeral; let to_disk = persist_mode == PersistenceMode::Disk;
if wants_file_detection { if wants_file_detection {
imp.disable_automatic_saving(); imp.disable_automatic_saving();
@ -1628,7 +1627,7 @@ impl History {
// Add the item. Then check for which paths are valid on a background thread, // Add the item. Then check for which paths are valid on a background thread,
// and unblock the item. // and unblock the item.
// Don't hold the lock while we perform this file detection. // Don't hold the lock while we perform this file detection.
imp.add(item, /*pending=*/ true, /*do_save=*/ true); imp.add(item, /*pending=*/ true, to_disk);
drop(imp); drop(imp);
let vars_snapshot = vars.snapshot(); let vars_snapshot = vars.snapshot();
iothread_perform(move || { iothread_perform(move || {
@ -1636,16 +1635,17 @@ impl History {
let validated_paths = expand_and_detect_paths(potential_paths, &vars_snapshot); let validated_paths = expand_and_detect_paths(potential_paths, &vars_snapshot);
let mut imp = self.imp(); let mut imp = self.imp();
imp.set_valid_file_paths(validated_paths, identifier); imp.set_valid_file_paths(validated_paths, identifier);
if do_save { imp.enable_automatic_saving();
imp.enable_automatic_saving(); if to_disk {
imp.save_unless_disabled();
} }
}); });
} else { } else {
// Add the item. // Add the item.
// If we think we're about to exit, save immediately, regardless of any disabling. This may // If we think we're about to exit, save immediately, regardless of any disabling. This may
// cause us to lose file hinting for some commands, but it beats losing history items. // cause us to lose file hinting for some commands, but it beats losing history items.
imp.add(item, /*pending=*/ true, do_save); imp.add(item, /*pending=*/ true, to_disk);
if do_save && needs_sync_write { if to_disk && needs_sync_write {
imp.save(false); imp.save(false);
} }
} }