Skip to content

Commit 1bcd2e2

Browse files
committed
Fix file based history when capacity is set to zero
Signed-off-by: Andrei Stan <[email protected]>
1 parent 973dbb5 commit 1bcd2e2

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/history/base.rs

+13
Original file line numberDiff line numberDiff line change
@@ -427,4 +427,17 @@ mod test {
427427

428428
Ok(())
429429
}
430+
431+
#[test]
432+
fn history_size_zero() -> Result<()> {
433+
#[cfg(not(any(feature = "sqlite", feature = "sqlite-dynlib")))]
434+
let mut history = crate::FileBackedHistory::new(0);
435+
history.save(create_item(1, "/home/me", "cd ~/Downloads", 0))?;
436+
assert_eq!(history.count_all()?, 0);
437+
let _ = history.sync();
438+
history.clear()?;
439+
drop(history);
440+
441+
Ok(())
442+
}
430443
}

src/history/file_backed.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl History for FileBackedHistory {
5959
.entries
6060
.back()
6161
.map_or(true, |previous| previous != &entry)
62-
&& !entry.is_empty()
62+
&& !entry.is_empty() && self.capacity > 0
6363
{
6464
if self.entries.len() == self.capacity {
6565
// History is "full", so we delete the oldest entry first,
@@ -234,7 +234,7 @@ impl History for FileBackedHistory {
234234
.collect::<std::io::Result<VecDeque<_>>>()?;
235235
if from_file.len() + own_entries.len() > self.capacity {
236236
(
237-
from_file.split_off(from_file.len() - (self.capacity - own_entries.len())),
237+
from_file.split_off(from_file.len() - (self.capacity.saturating_sub(own_entries.len()))),
238238
true,
239239
)
240240
} else {

0 commit comments

Comments
 (0)