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.
This commit is contained in:
Johannes Altmanninger 2024-10-06 08:26:06 +02:00
parent 33bcf4d0ce
commit f36f757fa6

View file

@ -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<usize>, // -1
countdown_to_vacuum: Option<usize>,
/// 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);
}
}