From f36f757fa675a574ee9b860087967d6b5faf6edb Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sun, 6 Oct 2024 08:26:06 +0200 Subject: [PATCH] Never rewrite history file when adding ephemeral items When I run a command with leading space, it is not added to the on-disk history. However we still call History::save(). After 25 of such calls, we rewrite the history file (even though nothing was written by us). This is annoying when diagnosing #10300 where the history of the current shell (but not other shells) is broken; because the history rewrite will make the problem go away. Let's not save in this case, to make it easier to run commands to inspect the state of the history file. --- src/history.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/history.rs b/src/history.rs index 564c767bb..e7b490b9e 100644 --- a/src/history.rs +++ b/src/history.rs @@ -375,7 +375,7 @@ struct HistoryImpl { /// The most recent "unique" identifier for a history item. last_identifier: HistoryIdentifier, // 0 /// How many items we add until the next vacuum. Initially a random value. - countdown_to_vacuum: Option, // -1 + countdown_to_vacuum: Option, /// Whether we've loaded old items. loaded_old: bool, // false /// List of old items, as offsets into out mmap data. @@ -1581,6 +1581,7 @@ impl History { let when = imp.timestamp_now(); let identifier = imp.next_identifier(); let item = HistoryItem::new(s.to_owned(), when, identifier, persist_mode); + let do_save = persist_mode != PersistenceMode::Ephemeral; if wants_file_detection { imp.disable_automatic_saving(); @@ -1596,14 +1597,16 @@ impl History { let validated_paths = expand_and_detect_paths(potential_paths, &vars_snapshot); let mut imp = self.imp(); imp.set_valid_file_paths(validated_paths, identifier); - imp.enable_automatic_saving(); + if do_save { + imp.enable_automatic_saving(); + } }); } else { // Add the item. // 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. - imp.add(item, /*pending=*/ true, /*do_save=*/ true); - if needs_sync_write { + imp.add(item, /*pending=*/ true, do_save); + if do_save && needs_sync_write { imp.save(false); } }