diff --git a/crates/engine/src/buffers.rs b/crates/engine/src/buffers.rs index 6b8b7cd..e3f033d 100644 --- a/crates/engine/src/buffers.rs +++ b/crates/engine/src/buffers.rs @@ -1,7 +1,8 @@ //! Open buffers: the engine's copy of every file the user is editing. -use std::collections::{BTreeMap, BTreeSet}; +use std::collections::BTreeMap; use std::path::{Path, PathBuf}; +use std::time::SystemTime; use lutra_text::{Buffer, LineEnding}; use serde::{Deserialize, Serialize}; @@ -31,6 +32,36 @@ impl OpenBuffer { } } +/// How a buffer's file differs from what Lutra last read or wrote. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] +pub enum DiskConflict { + /// Another program wrote a different text while the buffer had unsaved + /// changes. + Changed, + /// The file disappeared (deleted, renamed away, or a checkout removed + /// it). + Removed, +} + +/// The modification time and length of a file, compared before a save to +/// skip reading a file nobody touched. +pub type DiskStamp = (SystemTime, u64); + +/// What a buffer's file held when Lutra last read or wrote it. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Written { + /// [`crate::journal::hex_hash`] of the text. + pub hash: String, + /// Whether the file has a byte order mark. + pub bom: bool, + /// The file's stamp right after, `None` when it could not be read. + pub stamp: Option, + /// The Local History holds this text (Lutra saved or reloaded it), so + /// the next save need not snapshot the file it overwrites. + pub in_history: bool, +} + /// The open buffers by id. #[derive(Debug, Default)] pub struct Buffers { @@ -42,12 +73,17 @@ pub struct Buffers { /// unsaved changes ([`crate::Event::FileChangedOnDisk`]). The user has to /// decide, so `file.saveAll {skipConflicts}` leaves them alone until an /// explicit save or a reload settles it. - conflicts: BTreeSet, - /// What each buffer's file held when Lutra last read or wrote it: the - /// hash ([`crate::journal::hex_hash`]) of the text and the byte order - /// mark. A change on disk that matches it (our own save reported late, a - /// touch) is not a conflict. - written: BTreeMap, + conflicts: BTreeMap, + /// What each buffer's file held when Lutra last read or wrote it. A + /// change on disk that matches it (our own save reported late, a touch) + /// is not a conflict, and a save over a file that no longer matches it + /// asks first (`file.save` fails with `disk_changed`). + written: BTreeMap, + /// Buffers that were clean when their file disappeared, with their + /// version then. Such a buffer is dirty only because the file is gone; + /// if the file comes back before the user edits it, it reloads like a + /// clean buffer. + clean_when_removed: BTreeMap, } impl Buffers { @@ -73,24 +109,50 @@ impl Buffers { pub fn remove(&mut self, id: BufferId) -> Option { self.conflicts.remove(&id); self.written.remove(&id); + self.clean_when_removed.remove(&id); self.by_id.remove(&id) } /// Marks buffer `id` as in conflict with its file on disk. - pub fn mark_conflict(&mut self, id: BufferId) { + pub fn mark_conflict(&mut self, id: BufferId, kind: DiskConflict) { if self.by_id.contains_key(&id) { - self.conflicts.insert(id); + self.conflicts.insert(id, kind); } } /// Clears the disk conflict of buffer `id` (saved or reloaded). - pub fn clear_conflict(&mut self, id: BufferId) { - self.conflicts.remove(&id); + /// + /// # Returns + /// Whether it was in conflict. + pub fn clear_conflict(&mut self, id: BufferId) -> bool { + self.clean_when_removed.remove(&id); + self.conflicts.remove(&id).is_some() + } + + /// Records that buffer `id` was clean at `version` when its file + /// disappeared. + pub fn note_clean_removal(&mut self, id: BufferId, version: u64) { + if self.by_id.contains_key(&id) { + self.clean_when_removed.insert(id, version); + } + } + + /// Whether buffer `id` is dirty only because its file disappeared: it + /// was clean then and has not been edited since. + pub fn dirty_only_by_removal(&self, id: BufferId) -> bool { + self.by_id + .get(&id) + .is_some_and(|open| self.clean_when_removed.get(&id) == Some(&open.version)) } /// Whether buffer `id` is in conflict with its file on disk. pub fn has_conflict(&self, id: BufferId) -> bool { - self.conflicts.contains(&id) + self.conflicts.contains_key(&id) + } + + /// The disk conflict of buffer `id`, if any. + pub fn conflict(&self, id: BufferId) -> Option { + self.conflicts.get(&id).copied() } /// Remembers what buffer `id`'s file holds after Lutra read or wrote it. @@ -98,18 +160,47 @@ impl Buffers { /// # Parameters /// - `hash`: [`crate::journal::hex_hash`] of the text. /// - `bom`: whether the file has a byte order mark. - pub fn note_written(&mut self, id: BufferId, hash: String, bom: bool) { + /// - `stamp`: the file's stamp right after ([`crate::workspace::stamp`]). + pub fn note_written( + &mut self, + id: BufferId, + hash: String, + bom: bool, + stamp: Option, + ) { if self.by_id.contains_key(&id) { - self.written.insert(id, (hash, bom)); + self.written.insert( + id, + Written { + hash, + bom, + stamp, + in_history: false, + }, + ); } } + /// Records that the Local History holds the text last noted with + /// [`Buffers::note_written`] for buffer `id`. + pub fn mark_in_history(&mut self, id: BufferId) { + if let Some(w) = self.written.get_mut(&id) { + w.in_history = true; + } + } + + /// What Lutra last read or wrote for buffer `id`; `None` for a buffer + /// restored from the recovery journal before any save. + pub fn written(&self, id: BufferId) -> Option<&Written> { + self.written.get(&id) + } + /// Whether the file content `hash`/`bom` is what Lutra last read or /// wrote for buffer `id`. pub fn is_own_write(&self, id: BufferId, hash: &str, bom: bool) -> bool { self.written .get(&id) - .is_some_and(|(h, b)| h == hash && *b == bom) + .is_some_and(|w| w.hash == hash && w.bom == bom) } /// The id of the buffer for the project-relative `path`, if open. @@ -148,6 +239,7 @@ impl Buffers { pub fn clear(&mut self) { self.conflicts.clear(); self.written.clear(); + self.clean_when_removed.clear(); self.by_id.clear(); } } diff --git a/crates/engine/src/cmd/buffer.rs b/crates/engine/src/cmd/buffer.rs index 8f4f949..08a6e6d 100644 --- a/crates/engine/src/cmd/buffer.rs +++ b/crates/engine/src/cmd/buffer.rs @@ -40,6 +40,7 @@ pub fn describe(state: &State, command: &str, id: BufferId) -> Result Result { +/// [`EngineError::Failed`] when the file cannot be read as text. +pub(super) fn open_buffer( + state: &mut State, + command: &str, + rel: &std::path::Path, +) -> Result { + let abs = absolute(state, command, rel)?; + let before = stamp(&abs); + let file = lutra_vfs::read_text(&abs).map_err(vfs_error)?; + let buffer = OpenBuffer { + buffer: lutra_text::Buffer::new(&file.text), + path: rel.to_path_buf(), + version: 1, + saved_version: 1, + bom: file.bom, + }; + let new_id = state.buffers.insert(buffer); + state.buffers.note_written( + new_id, + journal::hex_hash(file.text.as_bytes()), + file.bom, + before, + ); + Ok(new_id) +} + +/// Whether another program changed the file of buffer `id` at `abs` +/// since Lutra last read or wrote it. Not when the stamp is unchanged, the +/// text is the same (a touch), the file is gone, or the buffer has no +/// record (restored from the recovery journal). A file that exists but +/// cannot be read as text counts as changed. +fn newer_on_disk(state: &State, id: BufferId, abs: &std::path::Path) -> bool { + let Some(written) = state.buffers.written(id) else { + return false; + }; + let Some(now) = stamp(abs) else { + return false; + }; + if written.stamp == Some(now) { + return false; + } + match lutra_vfs::read_text(abs) { + Ok(file) => { + journal::hex_hash(file.text.as_bytes()) != written.hash || file.bom != written.bom + } + Err(lutra_vfs::VfsError::NotFound { .. }) => false, + Err(_) => true, + } +} + +/// Writes buffer `id` to its file, keeping its line ending and byte order +/// mark, marks it clean, settles a disk conflict (saving is the user's +/// answer to it), remembers what was written, so the watcher's report of +/// this write is not taken for an outside change, and records Local +/// History snapshots: the file's previous text when the history does not +/// have it yet ([`Reason::BeforeSave`]), then the saved text +/// ([`Reason::Save`]). +/// +/// # Parameters +/// - `overwrite`: write even if another program changed the file since +/// Lutra last read or wrote it. +/// +/// # Errors +/// [`EngineError::DiskChanged`] when the file is newer and `overwrite` is +/// false; [`EngineError::Failed`] for an unknown buffer or a failed write. +fn save( + state: &mut State, + command: &str, + id: BufferId, + overwrite: bool, +) -> Result { + let open = state + .buffers + .get(id) + .ok_or_else(|| EngineError::Failed(format!("no buffer {id}")))?; + let rel = open.path.clone(); + let abs = absolute(state, command, &rel)?; + if !overwrite && newer_on_disk(state, id, &abs) { + return Err(EngineError::DiskChanged(path_string(&rel))); + } + snapshot_before_save(state, id, &rel, &abs, overwrite); let open = state .buffers .get(id) .ok_or_else(|| EngineError::Failed(format!("no buffer {id}")))?; - let abs = absolute(state, command, &open.path)?; let text = open.buffer.save_text(); let bom = open.bom; lutra_vfs::write_text(&abs, &text, bom).map_err(vfs_error)?; state .buffers - .note_written(id, journal::hex_hash(text.as_bytes()), bom); - state.buffers.clear_conflict(id); + .note_written(id, journal::hex_hash(text.as_bytes()), bom, stamp(&abs)); + settle_conflict(state, id); + history::snapshot(state, &rel, &text, bom, Reason::Save); + state.buffers.mark_in_history(id); let open = state .buffers .get_mut(id) @@ -103,17 +183,41 @@ fn save(state: &mut State, command: &str, id: BufferId) -> Result Result { - save(state, command, id) + save(state, command, id, false) } /// Params of `file.save`. @@ -131,6 +235,10 @@ pub(super) fn save_buffer( struct SaveParams { /// Buffer id. id: BufferId, + /// Write even if another program changed the file since Lutra last + /// read or wrote it (the user agreed to overwrite it). + #[serde(default)] + overwrite: bool, } /// `file.save`: writes one buffer to disk. @@ -143,7 +251,7 @@ impl Command for Save { label: "Save", menu_path: Some("File/Save"), default_shortcut: Some("Mod+S"), - params: "{ id: number }; returns { id, version }", + params: "{ id: number, overwrite?: boolean (write even though another program changed the file) }; returns { id, version }; fails with disk_changed when the file on disk changed since Lutra last read or wrote it and overwrite is not set", } } @@ -153,7 +261,7 @@ impl Command for Save { fn run(&self, state: &mut State, params: Value) -> Result { let p: SaveParams = parse(self.spec().id, params)?; - let version = save(state, self.spec().id, p.id)?; + let version = save(state, self.spec().id, p.id, p.overwrite)?; Ok(Outcome::value(json!({ "id": p.id, "version": version }))) } } @@ -171,7 +279,9 @@ struct SaveAllParams { /// `file.saveAll`: writes every dirty buffer. A failure doesn't stop the /// others; failures are listed in the result. With `skipConflicts` a dirty /// buffer whose file changed or disappeared on disk is not written and is -/// listed under `skipped`. +/// listed under `skipped`. A buffer whose file another program changed is +/// never written by it and is listed under `diskChanged` (save it alone, +/// with `overwrite`, after asking). pub struct SaveAll; impl Command for SaveAll { @@ -181,7 +291,7 @@ impl Command for SaveAll { label: "Save All", menu_path: Some("File/Save All"), default_shortcut: Some("Mod+Alt+S"), - params: "{ skipConflicts?: boolean (leave buffers whose file changed on disk unsaved) }; returns { saved: [id], failed: [{ id, path, message }], skipped: [id] }", + params: "{ skipConflicts?: boolean (leave buffers whose file changed on disk unsaved) }; returns { saved: [id], failed: [{ id, path, message }], skipped: [id], diskChanged: [id] (files another program changed; not written) }", } } @@ -194,13 +304,15 @@ impl Command for SaveAll { let mut saved = Vec::new(); let mut failed = Vec::new(); let mut skipped = Vec::new(); + let mut disk_changed = Vec::new(); for id in state.buffers.dirty_ids() { if p.skip_conflicts && state.buffers.has_conflict(id) { skipped.push(id); continue; } - match save(state, self.spec().id, id) { + match save(state, self.spec().id, id, false) { Ok(_) => saved.push(id), + Err(EngineError::DiskChanged(_)) => disk_changed.push(id), Err(e) => { let path = state .buffers @@ -211,13 +323,118 @@ impl Command for SaveAll { } } } + Ok(Outcome::value(json!({ + "saved": saved, + "failed": failed, + "skipped": skipped, + "diskChanged": disk_changed, + }))) + } +} + +/// Params naming one buffer. +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct IdParams { + /// Buffer id. + id: BufferId, +} + +/// `file.reload`: replaces a buffer's text with its file's ("Reload, +/// discard mine"). The discarded text goes into the Local History first. +pub struct Reload; + +impl Command for Reload { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "file.reload", + label: "Reload from Disk", + menu_path: None, + default_shortcut: None, + params: "{ id: number }; replaces the buffer's text with the file's, discarding unsaved changes (they are kept in the Local History); returns { id, version, dirty: false }; failed when the file is gone or not text", + } + } + + fn enabled(&self, state: &State) -> bool { + state.has_project() + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let command = self.spec().id; + let p: IdParams = parse(command, params)?; + let open = state + .buffers + .get(p.id) + .ok_or_else(|| EngineError::Failed(format!("{command}: no buffer {}", p.id)))?; + let abs = absolute(state, command, &open.path)?; + let before = stamp(&abs); + let file = lutra_vfs::read_text(&abs).map_err(vfs_error)?; + crate::workspace::take_disk_text(state, p.id, file, before); + let version = state.buffers.get(p.id).map(|b| b.version).unwrap_or(0); Ok(Outcome::value( - json!({ "saved": saved, "failed": failed, "skipped": skipped }), + json!({ "id": p.id, "version": version, "dirty": false }), )) } } +/// Params of `file.readDisk`. +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct ReadDiskParams { + /// Project-relative or absolute path of the file. + path: String, +} + +/// `file.readDisk`: the text of a file as it is on disk now, whatever its +/// buffer holds; the left side of "Compare" in the disk-conflict banner. +/// Answers like `git.show`, so the diff viewer reads both alike. +pub struct ReadDisk; + +impl Command for ReadDisk { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "file.readDisk", + label: "Read File from Disk", + menu_path: None, + default_shortcut: None, + params: "{ path: string (project-relative or absolute inside the project) }; returns { text: string | null (null when the file does not exist), binary, tooLarge }", + } + } + + fn enabled(&self, state: &State) -> bool { + state.has_project() + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let command = self.spec().id; + let p: ReadDiskParams = parse(command, params)?; + let rel = project_path(state, command, &p.path)?; + let abs = absolute(state, command, &rel)?; + let value = match lutra_vfs::read_text(&abs) { + Ok(file) => json!({ "text": file.text, "binary": false, "tooLarge": false }), + Err(lutra_vfs::VfsError::NotFound { .. }) => { + json!({ "text": null, "binary": false, "tooLarge": false }) + } + Err(lutra_vfs::VfsError::TooLarge { .. }) => { + json!({ "text": null, "binary": false, "tooLarge": true }) + } + Err( + lutra_vfs::VfsError::InvalidUtf8 { .. } + | lutra_vfs::VfsError::UnsupportedEncoding { .. }, + ) => json!({ "text": null, "binary": true, "tooLarge": false }), + Err(e) => return Err(vfs_error(e)), + }; + Ok(Outcome::value(value)) + } +} + /// The ids of the commands this module registers, checked against the /// registry by the engine's listing test. Add a new command's id here. #[cfg(test)] -pub(crate) const COMMAND_IDS: &[&str] = &["file.open", "file.save", "file.saveAll"]; +pub(crate) const COMMAND_IDS: &[&str] = &[ + "file.open", + "file.readDisk", + "file.reload", + "file.save", + "file.saveAll", +]; diff --git a/crates/engine/src/cmd/git.rs b/crates/engine/src/cmd/git.rs index c34808e..5adf388 100644 --- a/crates/engine/src/cmd/git.rs +++ b/crates/engine/src/cmd/git.rs @@ -300,7 +300,9 @@ pub(super) fn start_then( start(state, command, Op::Custom { mutates, run }, Some(then)) } -/// Runs `op` on a new thread and returns the pending outcome. +/// Runs `op` on a new thread and returns the pending outcome. A command +/// that changes files first snapshots the open buffers into the Local +/// History ([`crate::history::before_git`]). /// /// # Errors /// [`EngineError::Disabled`] without a project, [`EngineError::Failed`] @@ -311,6 +313,7 @@ fn start( op: Op, then: Option, ) -> Result { + crate::history::before_git(state, command); let project = state .project .as_ref() diff --git a/crates/engine/src/cmd/git_remote.rs b/crates/engine/src/cmd/git_remote.rs index fe9f5ab..47639ce 100644 --- a/crates/engine/src/cmd/git_remote.rs +++ b/crates/engine/src/cmd/git_remote.rs @@ -156,12 +156,15 @@ fn engine_error(command: &str, error: VcsError) -> EngineError { } } -/// Runs `op` on a new thread and returns the pending outcome. +/// Runs `op` on a new thread and returns the pending outcome. A pull +/// first snapshots the open buffers into the Local History +/// ([`crate::history::before_git`]). /// /// # Errors /// [`EngineError::Disabled`] without a project (except for clone), /// [`EngineError::Failed`] while another network call runs. fn start(state: &mut State, command: &'static str, op: Op) -> Result { + crate::history::before_git(state, command); let project = state .project .as_ref() diff --git a/crates/engine/src/cmd/history.rs b/crates/engine/src/cmd/history.rs new file mode 100644 index 0000000..aa37755 --- /dev/null +++ b/crates/engine/src/cmd/history.rs @@ -0,0 +1,191 @@ +//! Local History commands (C10 A2): `history.list`, `history.read`, +//! `history.revert`. The store and when snapshots are taken: +//! [`crate::history`]. +//! +//! `history.read` answers like `git.show`, so the diff viewer compares a +//! snapshot with the working copy as it compares a ref. `history.revert` +//! changes the file's buffer through `buffer.edit` (opening the file +//! first if needed): the buffer becomes dirty and the editor can undo it; +//! nothing is written to disk until the user saves. + +use serde::Deserialize; +use serde_json::{Value, json}; + +use super::params::{parse, project_path}; +use crate::history::{self, Reason}; +use crate::workspace::path_string; +use crate::{Command, CommandSpec, EngineError, Event, Outcome, Registry, State}; + +/// Registers the history commands. +/// +/// # Errors +/// [`EngineError::Internal`] if an id is taken. +pub fn register(registry: &mut Registry) -> Result<(), EngineError> { + registry.register(Box::new(List))?; + registry.register(Box::new(Read))?; + registry.register(Box::new(Revert))?; + Ok(()) +} + +/// The history of the open project. +/// +/// # Errors +/// [`EngineError::Failed`] when the project has none (no persistence, or it +/// failed to open; the warning said why). +fn store<'a>(state: &'a State, command: &str) -> Result<&'a history::History, EngineError> { + state.history.as_ref().ok_or_else(|| { + EngineError::Failed(format!( + "{command}: Local History is not available for this project" + )) + }) +} + +/// Params of `history.list`. +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct ListParams { + /// Project-relative or absolute path of the file. + path: String, +} + +/// `history.list`: the snapshots of a file, newest first. +pub struct List; + +impl Command for List { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "history.list", + label: "List Local History", + menu_path: None, + default_shortcut: None, + params: "{ path: string }; returns { path, entries: [{ id, time (ms since the Unix epoch), reason: save|beforeSave|externalChange|beforeReload|deleted|refactoring|git|beforeRevert, size (bytes) }] } newest first; failed when the project has no Local History", + } + } + + fn enabled(&self, state: &State) -> bool { + state.has_project() + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let command = self.spec().id; + let p: ListParams = parse(command, params)?; + let rel = path_string(&project_path(state, command, &p.path)?); + let entries: Vec = store(state, command)? + .list(&rel) + .into_iter() + .map(|s| json!({ "id": s.id, "time": s.time, "reason": s.reason, "size": s.size })) + .collect(); + Ok(Outcome::value(json!({ "path": rel, "entries": entries }))) + } +} + +/// Params naming one snapshot. +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct EntryParams { + /// Project-relative or absolute path of the file. + path: String, + /// The snapshot's id from `history.list`. + id: u64, +} + +/// The text of snapshot `id` of the project-relative `rel`. +/// +/// # Errors +/// [`EngineError::Failed`] for an unknown snapshot or an unreadable text. +fn snapshot_text(state: &State, command: &str, rel: &str, id: u64) -> Result { + let store = store(state, command)?; + let snapshot = store.get(rel, id).ok_or_else(|| { + EngineError::Failed(format!("{command}: {rel} has no Local History entry {id}")) + })?; + store + .text(snapshot) + .map_err(|e| EngineError::Failed(format!("{command}: {e}"))) +} + +/// `history.read`: the text of one snapshot. +pub struct Read; + +impl Command for Read { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "history.read", + label: "Read Local History Entry", + menu_path: None, + default_shortcut: None, + params: "{ path: string, id: number (from history.list) }; returns { text, binary: false, tooLarge: false } like git.show; failed for an unknown or damaged entry", + } + } + + fn enabled(&self, state: &State) -> bool { + state.has_project() + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let command = self.spec().id; + let p: EntryParams = parse(command, params)?; + let rel = path_string(&project_path(state, command, &p.path)?); + let text = snapshot_text(state, command, &rel, p.id)?; + Ok(Outcome::value( + json!({ "text": text, "binary": false, "tooLarge": false }), + )) + } +} + +/// `history.revert`: puts a snapshot's text into the file's buffer. +pub struct Revert; + +impl Command for Revert { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "history.revert", + label: "Revert to Local History Entry", + menu_path: None, + default_shortcut: None, + params: "{ path: string, id: number (from history.list) }; replaces the text of the file's buffer (opened first if needed, emitting bufferOpened) with the entry's through buffer.edit, so it is undoable and unsaved; the text it replaces is kept as a beforeRevert entry; returns { id (buffer), version, dirty }", + } + } + + fn enabled(&self, state: &State) -> bool { + state.has_project() + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let command = self.spec().id; + let p: EntryParams = parse(command, params)?; + let rel_path = project_path(state, command, &p.path)?; + let rel = path_string(&rel_path); + let text = snapshot_text(state, command, &rel, p.id)?; + let id = match state.buffers.find(&rel_path) { + Some(id) => id, + None => { + let id = super::file::open_buffer(state, command, &rel_path)?; + state.emit(Event::BufferOpened { + id, + path: rel.clone(), + }); + id + } + }; + history::snapshot_buffer(state, id, Reason::BeforeRevert); + let open = state + .buffers + .get(id) + .ok_or_else(|| EngineError::Failed(format!("{command}: no buffer {id}")))?; + let (base, len) = (open.version, open.buffer.len_utf16()); + let out = super::buffer::Edit.run( + state, + json!({ + "id": id, + "baseVersion": base, + "edits": [{ "from": 0, "to": len, "insert": text }], + }), + )?; + Ok(out) + } +} + +/// The ids of the commands this module registers, checked against the +/// registry by the engine's listing test. Add a new command's id here. +#[cfg(test)] +pub(crate) const COMMAND_IDS: &[&str] = &["history.list", "history.read", "history.revert"]; diff --git a/crates/engine/src/cmd/lsp_nav/edit.rs b/crates/engine/src/cmd/lsp_nav/edit.rs index ef0e882..0121b9a 100644 --- a/crates/engine/src/cmd/lsp_nav/edit.rs +++ b/crates/engine/src/cmd/lsp_nav/edit.rs @@ -11,7 +11,8 @@ //! sent; a buffer the user changed meanwhile refuses the edit (the ranges //! would be stale). //! -//! Then closed files are written, each atomically through the VFS. If a +//! Before anything changes, every document's text goes into the Local +//! History (`crate::history`). Then closed files are written, each atomically through the VFS. If a //! write fails, the files written so far are restored to their old content //! (best effort; a failed restore is reported in the error) and no buffer //! is touched. Only after every file is written are the open buffers changed, @@ -109,6 +110,7 @@ pub fn apply( versions: &BTreeMap, ) -> Result { let plan = plan(state, root, edit, versions)?; + snapshot(state, &plan); let mut applied = Applied::default(); let mut written: Vec<(PathBuf, &str, bool)> = Vec::new(); for (rel, planned, count) in &plan { @@ -159,6 +161,21 @@ pub fn apply( Ok(applied) } +/// Puts the text of every document the edit is about to change into the +/// Local History ([`crate::history::Reason::Refactoring`]), once the whole +/// edit has been checked. +fn snapshot(state: &mut State, plan: &[(PathBuf, Planned, usize)]) { + use crate::history::{self, Reason}; + for (rel, planned, _) in plan { + match planned { + Planned::Buffer { id, .. } => history::snapshot_buffer(state, *id, Reason::Refactoring), + Planned::Disk { old, bom, .. } => { + history::snapshot(state, rel, old, *bom, Reason::Refactoring); + } + } + } +} + /// Writes the old contents back into files written by a failed edit. /// /// # Returns diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index bf221bd..3bf686b 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -15,6 +15,7 @@ pub mod git_merge; pub mod git_remote; pub mod git_stash; pub mod goto; +mod history; mod layout; mod lsp_assist; mod lsp_diagnostics; @@ -46,6 +47,8 @@ pub fn register_builtins(registry: &mut Registry) -> Result<(), EngineError> { registry.register(Box::new(file::Open))?; registry.register(Box::new(file::Save))?; registry.register(Box::new(file::SaveAll))?; + registry.register(Box::new(file::Reload))?; + registry.register(Box::new(file::ReadDisk))?; registry.register(Box::new(buffer::Get))?; registry.register(Box::new(buffer::Edit))?; registry.register(Box::new(buffer::Close))?; @@ -90,6 +93,7 @@ pub fn register_builtins(registry: &mut Registry) -> Result<(), EngineError> { git_ignore::register(registry)?; deploy::register(registry)?; layout::register(registry)?; + history::register(registry)?; Ok(()) } @@ -114,6 +118,7 @@ pub(crate) const BUILTIN_COMMAND_IDS: &[&[&str]] = &[ git_stash::COMMAND_IDS, goto::COMMAND_IDS, layout::COMMAND_IDS, + history::COMMAND_IDS, lsp_assist::COMMAND_IDS, lsp_diagnostics::COMMAND_IDS, lsp_nav::COMMAND_IDS, diff --git a/crates/engine/src/cmd/project.rs b/crates/engine/src/cmd/project.rs index 102129e..30e2b1e 100644 --- a/crates/engine/src/cmd/project.rs +++ b/crates/engine/src/cmd/project.rs @@ -57,6 +57,7 @@ pub fn close_project(state: &mut State, force: bool) -> Result<(), EngineError> state.emit(Event::BufferClosed { id }); } state.journal = None; + state.history = None; state.project = None; crate::cmd::goto::close(state); state.emit(Event::ProjectClosed); @@ -100,6 +101,7 @@ impl Command for Open { .as_ref() .map(|c| Journal::new(&c.data_dir, &root)); state.project = Some(workspace); + crate::history::open(state); let mut name = String::new(); for event in events { if let Event::ProjectOpened { name: n, .. } = &event { diff --git a/crates/engine/src/error.rs b/crates/engine/src/error.rs index 706f088..4a376e9 100644 --- a/crates/engine/src/error.rs +++ b/crates/engine/src/error.rs @@ -37,6 +37,11 @@ pub enum EngineError { /// discard them. #[error("{0} has unsaved changes")] UnsavedChanges(String), + /// A save would overwrite a file that another program changed since + /// Lutra last read or wrote it. Repeat it with `overwrite` after the + /// user agreed. + #[error("{0} changed on disk since it was last loaded or saved")] + DiskChanged(String), /// The command ran and failed, e.g. a file could not be read. #[error("{0}")] Failed(String), @@ -55,6 +60,7 @@ impl EngineError { Self::InvalidParams { .. } => "invalid_params", Self::StaleVersion { .. } => "stale_version", Self::UnsavedChanges(_) => "unsaved_changes", + Self::DiskChanged(_) => "disk_changed", Self::Failed(_) => "failed", Self::Internal(_) => "internal", } diff --git a/crates/engine/src/event.rs b/crates/engine/src/event.rs index f7a6901..9a73449 100644 --- a/crates/engine/src/event.rs +++ b/crates/engine/src/event.rs @@ -83,6 +83,13 @@ pub enum Event { /// The file was removed or renamed away. removed: bool, }, + /// A buffer's disk conflict ([`Event::FileChangedOnDisk`]) is settled: + /// it was saved or reloaded, or its file came back with the buffer's + /// text. Clients drop the banner. + DiskConflictResolved { + /// The open buffer. + id: u64, + }, /// File watching stopped; the project keeps working without it. WatchFailed { /// Why. diff --git a/crates/engine/src/history.rs b/crates/engine/src/history.rs new file mode 100644 index 0000000..e846f40 --- /dev/null +++ b/crates/engine/src/history.rs @@ -0,0 +1,717 @@ +//! Local History (C10 A2): snapshots of file texts taken by the engine, so +//! a save, an outside change, a refactoring or a git operation can always be +//! undone from Lutra, also for files git does not track. +//! +//! # Layout +//! `/history//`, the project id being the FNV-1a hash +//! of the canonical project root as in [`crate::journal`]. Never inside the +//! project: a data directory below the project root disables the history. +//! - `blobs//`: the text of a snapshot, as written to disk +//! (line endings kept, without the byte order mark), named by its SHA-256 +//! in hex. Content-addressed, so equal texts are stored once. +//! - `index.jsonl`: one [`Snapshot`] per line, oldest first. New snapshots +//! are appended; pruning rewrites the file atomically. +//! +//! # When snapshots are taken +//! See [`Reason`]. A snapshot whose text equals the newest snapshot of the +//! same path is not added, so repeated saves of the same text (autosave) +//! cost nothing. Texts over [`MAX_SNAPSHOT_BYTES`] are not kept. +//! +//! # Retention (owner decision D29) +//! Snapshots older than [`KEEP`] are dropped, and while the blobs of a +//! project take more than [`MAX_BYTES`] the oldest snapshots go first. Both +//! run when the project opens ([`History::open`], which also deletes blob +//! files no snapshot refers to), and the size cap again whenever a new +//! snapshot pushes the total over it. +//! +//! # Untrusted input +//! The index is read with a size cap ([`MAX_INDEX_BYTES`], at most +//! [`MAX_SNAPSHOTS`] lines); malformed lines and lines whose hash is not 64 +//! hex digits are skipped. A blob is checked against its hash when read. +//! Failures never stop a save or a reload: the engine reports them once as +//! a warning ([`KEY_FAILED`]) and carries on without that snapshot. + +use std::collections::{BTreeMap, BTreeSet}; +use std::io::Write; +use std::path::{Path, PathBuf}; +use std::time::{Duration, SystemTime, UNIX_EPOCH}; + +use serde::{Deserialize, Serialize}; + +use crate::buffers::BufferId; +use crate::workspace::path_string; +use crate::{NotificationLevel, State}; + +/// How long snapshots are kept (D29: 14 days). +pub const KEEP: Duration = Duration::from_secs(14 * 24 * 60 * 60); + +/// Most bytes the blobs of one project may take (D29: 200 MB). +pub const MAX_BYTES: u64 = 200 * 1024 * 1024; + +/// Largest text kept as a snapshot. +pub const MAX_SNAPSHOT_BYTES: usize = 16 * 1024 * 1024; + +/// Largest index file read. +pub const MAX_INDEX_BYTES: u64 = 64 * 1024 * 1024; + +/// Most snapshots kept per project; the oldest go first beyond it. +pub const MAX_SNAPSHOTS: usize = 100_000; + +/// Most blob files looked at when deleting unreferenced ones. +const MAX_BLOB_SCAN: usize = 200_000; + +/// File name of the index. +const INDEX: &str = "index.jsonl"; + +/// Directory of the blobs. +const BLOBS: &str = "blobs"; + +/// Catalog key of the warning for a history that cannot be read or written. +pub const KEY_FAILED: &str = "notification.history.failed"; + +/// Why a snapshot was taken. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum Reason { + /// The text Lutra saved (`file.save`, `file.saveAll`, autosave). + Save, + /// The file's text on disk before Lutra's first save over it. + BeforeSave, + /// A new text another program wrote, as Lutra read it. + ExternalChange, + /// The buffer's text before a reload from disk replaced it. + BeforeReload, + /// The buffer's text when its file disappeared from disk. + Deleted, + /// The text before a refactoring (`lsp.rename`) changed it. + Refactoring, + /// The text before a git operation that changes files (checkout, + /// stash, merge, pull, revert chunk). + Git, + /// The buffer's text before `history.revert` replaced it. + BeforeRevert, +} + +/// One snapshot as stored in the index. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Snapshot { + /// Unique within the project's history, increasing. + pub id: u64, + /// When it was taken, in milliseconds since the Unix epoch. + pub time: u64, + /// Project-relative path with `/` separators. + pub path: String, + /// SHA-256 of the text in hex: the blob's name. + pub hash: String, + /// Size of the text in bytes. + pub size: u64, + /// Why it was taken. + pub reason: Reason, + /// The file has a UTF-8 byte order mark. + pub bom: bool, +} + +/// The Local History of the open project. +#[derive(Debug)] +pub struct History { + /// `/history/`; created on the first snapshot. + dir: PathBuf, + /// Snapshots, oldest first. + snapshots: Vec, + /// Size of each blob that a snapshot refers to. + blobs: BTreeMap, + /// The id the next snapshot gets. + next_id: u64, + /// Most bytes the blobs may take: [`MAX_BYTES`], smaller in tests. + max_bytes: u64, +} + +/// Milliseconds since the Unix epoch; 0 before it. +pub fn millis(time: SystemTime) -> u64 { + time.duration_since(UNIX_EPOCH) + .map(|d| u64::try_from(d.as_millis()).unwrap_or(u64::MAX)) + .unwrap_or(0) +} + +/// SHA-256 of `bytes` in lowercase hex. +pub fn sha256_hex(bytes: &[u8]) -> String { + lutra_lsp::toolchain::download::Hash::of_bytes(bytes).to_hex() +} + +/// Whether `hash` is 64 lowercase hex digits, so it can name a blob file. +fn valid_hash(hash: &str) -> bool { + hash.len() == 64 + && hash + .bytes() + .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) +} + +impl History { + /// Opens the history of the project at the canonical `root` below + /// `data_dir`, reads its index and prunes it for `now` (see the module + /// docs). + /// + /// # Errors + /// A message when the history directory would lie inside the project, + /// or the index exists but cannot be read. + pub fn open(data_dir: &Path, root: &Path, now: SystemTime) -> Result { + let dir = data_dir + .join("history") + .join(crate::journal::hex_hash(root.to_string_lossy().as_bytes())); + if dir.starts_with(root) { + return Err(format!( + "the data directory {} lies inside the project; Local History is off", + data_dir.display() + )); + } + let mut history = Self { + dir, + snapshots: Vec::new(), + blobs: BTreeMap::new(), + next_id: 1, + max_bytes: MAX_BYTES, + }; + history.load()?; + let dropped = history.prune(now); + if dropped > 0 { + history.rewrite_index()?; + } + history.delete_unreferenced(); + Ok(history) + } + + /// Lowers the size cap, for tests that should not write 200 MB. + #[cfg(test)] + fn set_max_bytes(&mut self, max: u64) { + self.max_bytes = max; + } + + /// The history directory. + pub fn dir(&self) -> &Path { + &self.dir + } + + /// Bytes the referenced blobs take. + pub fn total_bytes(&self) -> u64 { + self.blobs.values().sum() + } + + /// Reads the index, skipping malformed lines. + fn load(&mut self) -> Result<(), String> { + let file = self.dir.join(INDEX); + let bytes = match lutra_vfs::read_bytes(&file, MAX_INDEX_BYTES) { + Ok(bytes) => bytes, + Err(lutra_vfs::VfsError::NotFound { .. }) => return Ok(()), + Err(e) => return Err(format!("cannot read {}: {e}", file.display())), + }; + let text = String::from_utf8_lossy(&bytes); + for line in text.lines().filter(|l| !l.trim().is_empty()) { + let Ok(snapshot) = serde_json::from_str::(line) else { + continue; + }; + if !valid_hash(&snapshot.hash) || snapshot.path.is_empty() { + continue; + } + self.next_id = self.next_id.max(snapshot.id.saturating_add(1)); + self.blobs.insert(snapshot.hash.clone(), snapshot.size); + self.snapshots.push(snapshot); + } + self.snapshots.sort_by_key(|s| (s.time, s.id)); + Ok(()) + } + + /// Drops snapshots older than [`KEEP`] at `now`, then the oldest ones + /// while the blobs take more than the cap ([`MAX_BYTES`]) or there are more than + /// [`MAX_SNAPSHOTS`], and deletes the blobs no snapshot refers to any + /// more. + /// + /// # Returns + /// How many snapshots were dropped. + fn prune(&mut self, now: SystemTime) -> usize { + let cutoff = millis(now.checked_sub(KEEP).unwrap_or(UNIX_EPOCH)); + let before = self.snapshots.len(); + let mut keep: Vec = std::mem::take(&mut self.snapshots) + .into_iter() + .filter(|s| s.time >= cutoff) + .collect(); + let mut sizes: BTreeMap = BTreeMap::new(); + let mut refs: BTreeMap = BTreeMap::new(); + for s in &keep { + sizes.insert(s.hash.clone(), s.size); + *refs.entry(s.hash.clone()).or_default() += 1; + } + let mut total: u64 = sizes.values().sum(); + let mut first = 0; + while first < keep.len() + && (total > self.max_bytes || keep.len().saturating_sub(first) > MAX_SNAPSHOTS) + { + if let Some(s) = keep.get(first) { + let left = refs.get_mut(&s.hash).map(|n| { + *n = n.saturating_sub(1); + *n + }); + if left == Some(0) { + refs.remove(&s.hash); + total = total.saturating_sub(sizes.remove(&s.hash).unwrap_or(0)); + } + } + first += 1; + } + keep.drain(..first); + let gone: Vec = self + .blobs + .keys() + .filter(|h| !sizes.contains_key(*h)) + .cloned() + .collect(); + for hash in &gone { + let _ = std::fs::remove_file(self.blob_path(hash)); + } + self.snapshots = keep; + self.blobs = sizes; + before.saturating_sub(self.snapshots.len()) + } + + /// Deletes blob files no snapshot refers to (left by a crash between + /// writing a blob and its index line, or by an older pruning). Best + /// effort: a file that cannot be deleted is tried again next time. + fn delete_unreferenced(&self) { + let Ok(shards) = std::fs::read_dir(self.dir.join(BLOBS)) else { + return; + }; + let mut seen = 0usize; + for shard in shards.flatten() { + let Ok(files) = std::fs::read_dir(shard.path()) else { + continue; + }; + for file in files.flatten() { + seen += 1; + if seen > MAX_BLOB_SCAN { + return; + } + let name = file.file_name().to_string_lossy().into_owned(); + if !self.blobs.contains_key(&name) { + let _ = std::fs::remove_file(file.path()); + } + } + } + } + + /// Writes the whole index again, atomically. + fn rewrite_index(&self) -> Result<(), String> { + let mut out = String::new(); + for s in &self.snapshots { + let line = serde_json::to_string(s).map_err(|e| e.to_string())?; + out.push_str(&line); + out.push('\n'); + } + std::fs::create_dir_all(&self.dir) + .map_err(|e| format!("cannot create {}: {e}", self.dir.display()))?; + lutra_vfs::write_atomic(&self.dir.join(INDEX), out.as_bytes()) + .map_err(|e| format!("cannot write the Local History index: {e}")) + } + + /// The file of the blob `hash`. + fn blob_path(&self, hash: &str) -> PathBuf { + let shard = hash.get(..2).unwrap_or("00"); + self.dir.join(BLOBS).join(shard).join(hash) + } + + /// The newest snapshot of `path`. + pub fn latest(&self, path: &str) -> Option<&Snapshot> { + self.snapshots.iter().rev().find(|s| s.path == path) + } + + /// Adds a snapshot of `text` for `path` at `now`, unless it equals the + /// newest snapshot of that path or is larger than + /// [`MAX_SNAPSHOT_BYTES`]. + /// + /// # Returns + /// The new snapshot's id, `None` when nothing was added. + /// + /// # Errors + /// A message when the blob or the index line cannot be written. + pub fn record( + &mut self, + path: &str, + text: &str, + bom: bool, + reason: Reason, + now: SystemTime, + ) -> Result, String> { + if text.len() > MAX_SNAPSHOT_BYTES { + return Ok(None); + } + let hash = sha256_hex(text.as_bytes()); + if self + .latest(path) + .is_some_and(|s| s.hash == hash && s.bom == bom) + { + return Ok(None); + } + let blob = self.blob_path(&hash); + if !blob.is_file() { + if let Some(parent) = blob.parent() { + std::fs::create_dir_all(parent) + .map_err(|e| format!("cannot create {}: {e}", parent.display()))?; + } + lutra_vfs::write_atomic(&blob, text.as_bytes()) + .map_err(|e| format!("cannot write a Local History snapshot: {e}"))?; + } + let snapshot = Snapshot { + id: self.next_id, + time: millis(now), + path: path.to_string(), + hash: hash.clone(), + size: u64::try_from(text.len()).unwrap_or(u64::MAX), + reason, + bom, + }; + let line = serde_json::to_string(&snapshot).map_err(|e| e.to_string())?; + let mut index = std::fs::OpenOptions::new() + .create(true) + .append(true) + .open(self.dir.join(INDEX)) + .map_err(|e| format!("cannot open the Local History index: {e}"))?; + index + .write_all(format!("{line}\n").as_bytes()) + .map_err(|e| format!("cannot write the Local History index: {e}"))?; + self.next_id = self.next_id.saturating_add(1); + let id = snapshot.id; + self.blobs.insert(hash, snapshot.size); + self.snapshots.push(snapshot); + if self.total_bytes() > self.max_bytes || self.snapshots.len() > MAX_SNAPSHOTS { + self.prune(now); + self.rewrite_index()?; + } + Ok(Some(id)) + } + + /// The snapshots of `path`, newest first. + pub fn list(&self, path: &str) -> Vec<&Snapshot> { + self.snapshots + .iter() + .rev() + .filter(|s| s.path == path) + .collect() + } + + /// The snapshot `id` of `path`. + pub fn get(&self, path: &str, id: u64) -> Option<&Snapshot> { + self.snapshots.iter().find(|s| s.id == id && s.path == path) + } + + /// The text of snapshot `snapshot`, checked against its hash. + /// + /// # Errors + /// A message when the blob is missing, too large, not UTF-8 or does not + /// match its hash. + pub fn text(&self, snapshot: &Snapshot) -> Result { + let blob = self.blob_path(&snapshot.hash); + let limit = u64::try_from(MAX_SNAPSHOT_BYTES).unwrap_or(u64::MAX); + let bytes = lutra_vfs::read_bytes(&blob, limit) + .map_err(|e| format!("the snapshot's text cannot be read: {e}"))?; + if sha256_hex(&bytes) != snapshot.hash { + return Err("the snapshot's text is damaged".into()); + } + String::from_utf8(bytes).map_err(|_| "the snapshot's text is not UTF-8".into()) + } +} + +/// Opens the history of the open project into `state.history` (on +/// `project.open`). Without persistence there is none; a failure is +/// reported as a warning and leaves the history off for this project. +pub fn open(state: &mut State) { + state.history = None; + let (Some(config), Some(project)) = (state.config.as_ref(), state.project.as_ref()) else { + return; + }; + let root = project.project.root().to_path_buf(); + match History::open(&config.data_dir, &root, SystemTime::now()) { + Ok(history) => state.history = Some(history), + Err(message) => warn(state, message), + } +} + +/// Reports a history failure as a warning notification. +fn warn(state: &mut State, message: String) { + let mut params = serde_json::Map::new(); + params.insert("reason".into(), serde_json::Value::String(message.clone())); + state.notify( + NotificationLevel::Warning, + Some(KEY_FAILED), + Some(format!("Local History: {message}")), + Some(params), + ); +} + +/// Takes a snapshot of `text` for the project-relative `path`. Best +/// effort: without a history nothing happens, and a failure is reported as +/// a warning instead of failing the caller, because a save or a reload must +/// not stop for the history. +pub fn snapshot(state: &mut State, path: &Path, text: &str, bom: bool, reason: Reason) { + let Some(history) = state.history.as_mut() else { + return; + }; + if let Err(message) = history.record(&path_string(path), text, bom, reason, SystemTime::now()) { + warn(state, message); + } +} + +/// Takes a [`Reason::ExternalChange`] snapshot of `text`, unless the +/// newest such snapshot of `path` already holds it (the watcher recorded the +/// text when it saw it, and "Reload" reads it again). +pub fn snapshot_external(state: &mut State, path: &Path, text: &str, bom: bool) { + let Some(history) = state.history.as_ref() else { + return; + }; + let shown = path_string(path); + let hash = sha256_hex(text.as_bytes()); + if history + .list(&shown) + .into_iter() + .find(|s| s.reason == Reason::ExternalChange) + .is_some_and(|s| s.hash == hash && s.bom == bom) + { + return; + } + snapshot(state, path, text, bom, Reason::ExternalChange); +} + +/// Takes a snapshot of buffer `id`'s text as it would be saved. +pub fn snapshot_buffer(state: &mut State, id: BufferId, reason: Reason) { + let Some(open) = state.buffers.get(id) else { + return; + }; + let (path, text, bom) = (open.path.clone(), open.buffer.save_text(), open.bom); + snapshot(state, &path, &text, bom, reason); +} + +/// The git commands that change files in the work tree; [`before_git`] +/// snapshots the open buffers before each. +pub const FILE_CHANGING_GIT: &[&str] = &[ + "git.checkout", + "git.createBranch", + "git.merge", + "git.mergeAbort", + "git.stash", + "git.stashApply", + "git.revertChunk", + "git.pull", +]; + +/// Snapshots every open buffer before the git command `command` runs, if +/// it changes files ([`FILE_CHANGING_GIT`]). Files that are not open are +/// left to git: checkout, merge and pull refuse to overwrite local changes, +/// and stash keeps them. +pub fn before_git(state: &mut State, command: &str) { + if !FILE_CHANGING_GIT.contains(&command) || state.history.is_none() { + return; + } + let ids: BTreeSet = state.buffers.ids().into_iter().collect(); + for id in ids { + snapshot_buffer(state, id, Reason::Git); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn at(ms: u64) -> SystemTime { + UNIX_EPOCH + Duration::from_millis(ms) + } + + const DAY: u64 = 24 * 60 * 60 * 1000; + + fn open(data: &Path, root: &Path, now: SystemTime) -> History { + History::open(data, root, now).unwrap() + } + + #[test] + fn snapshots_are_deduplicated_and_listed_newest_first() { + let data = tempfile::tempdir().unwrap(); + let root = tempfile::tempdir().unwrap(); + let mut h = open(data.path(), root.path(), at(100 * DAY)); + let now = at(100 * DAY); + assert_eq!( + h.record("a.php", "one", false, Reason::Save, now).unwrap(), + Some(1) + ); + assert_eq!( + h.record("a.php", "one", false, Reason::Save, now).unwrap(), + None + ); + assert_eq!( + h.record("a.php", "two", false, Reason::Save, now).unwrap(), + Some(2) + ); + assert_eq!( + h.record("b.php", "one", false, Reason::Save, now).unwrap(), + Some(3) + ); + let ids: Vec = h.list("a.php").iter().map(|s| s.id).collect(); + assert_eq!(ids, vec![2, 1]); + assert_eq!(h.total_bytes(), 6, "\"one\" is stored once"); + let blobs = walk(&h.dir().join(BLOBS)); + assert_eq!(blobs.len(), 2); + let first = h.get("a.php", 1).unwrap().clone(); + assert_eq!(h.text(&first).unwrap(), "one"); + assert!(h.get("b.php", 1).is_none(), "ids belong to their path"); + } + + fn walk(dir: &Path) -> Vec { + let mut out = Vec::new(); + for shard in std::fs::read_dir(dir).into_iter().flatten().flatten() { + for f in std::fs::read_dir(shard.path()).unwrap().flatten() { + out.push(f.path()); + } + } + out + } + + #[test] + fn the_index_survives_a_restart_and_ids_keep_increasing() { + let data = tempfile::tempdir().unwrap(); + let root = tempfile::tempdir().unwrap(); + let now = at(100 * DAY); + let mut h = open(data.path(), root.path(), now); + h.record("a.php", "one\r\n", true, Reason::Save, now) + .unwrap(); + h.record("a.php", "two", false, Reason::ExternalChange, now) + .unwrap(); + let mut again = open(data.path(), root.path(), now); + let list: Vec<(u64, Reason, bool)> = again + .list("a.php") + .iter() + .map(|s| (s.id, s.reason, s.bom)) + .collect(); + assert_eq!( + list, + vec![(2, Reason::ExternalChange, false), (1, Reason::Save, true)] + ); + let first = again.get("a.php", 1).unwrap().clone(); + assert_eq!(again.text(&first).unwrap(), "one\r\n", "line endings kept"); + assert_eq!( + again + .record("a.php", "three", false, Reason::Save, now) + .unwrap(), + Some(3) + ); + } + + #[test] + fn old_snapshots_are_pruned_on_open_with_their_blobs() { + let data = tempfile::tempdir().unwrap(); + let root = tempfile::tempdir().unwrap(); + let mut h = open(data.path(), root.path(), at(0)); + h.record("a.php", "old", false, Reason::Save, at(DAY)) + .unwrap(); + h.record("a.php", "new", false, Reason::Save, at(10 * DAY)) + .unwrap(); + let dir = h.dir().to_path_buf(); + let h = open(data.path(), root.path(), at(16 * DAY)); + let texts: Vec = h.list("a.php").iter().map(|s| h.text(s).unwrap()).collect(); + assert_eq!(texts, vec!["new".to_string()]); + assert_eq!(walk(&dir.join(BLOBS)).len(), 1, "the old blob is deleted"); + let index = std::fs::read_to_string(dir.join(INDEX)).unwrap(); + assert_eq!(index.lines().count(), 1, "the index is rewritten"); + } + + #[test] + fn the_size_cap_drops_the_oldest_snapshots_first() { + let data = tempfile::tempdir().unwrap(); + let root = tempfile::tempdir().unwrap(); + let now = at(100 * DAY); + let mut h = open(data.path(), root.path(), now); + h.set_max_bytes(1000); + for c in ['a', 'b', 'c', 'd', 'e'] { + h.record( + "big.txt", + &c.to_string().repeat(300), + false, + Reason::Save, + now, + ) + .unwrap(); + } + assert!(h.total_bytes() <= 1000); + let kept: Vec = h.list("big.txt").iter().map(|s| s.id).collect(); + assert_eq!(kept, vec![5, 4, 3], "the two oldest went first"); + assert_eq!(walk(&h.dir().join(BLOBS)).len(), 3); + let index = std::fs::read_to_string(h.dir().join(INDEX)).unwrap(); + assert_eq!(index.lines().count(), 3); + } + + #[test] + fn oversized_texts_are_not_kept() { + let data = tempfile::tempdir().unwrap(); + let root = tempfile::tempdir().unwrap(); + let now = at(100 * DAY); + let mut h = open(data.path(), root.path(), now); + let text = "x".repeat(MAX_SNAPSHOT_BYTES + 1); + assert_eq!( + h.record("a", &text, false, Reason::Save, now).unwrap(), + None + ); + assert!(h.list("a").is_empty()); + } + + #[test] + fn a_damaged_index_or_blob_never_fails_the_open() { + let data = tempfile::tempdir().unwrap(); + let root = tempfile::tempdir().unwrap(); + let now = at(100 * DAY); + let mut h = open(data.path(), root.path(), now); + h.record("a.php", "good", false, Reason::Save, now).unwrap(); + h.record("a.php", "other", false, Reason::Save, now) + .unwrap(); + let dir = h.dir().to_path_buf(); + let mut index = std::fs::read_to_string(dir.join(INDEX)).unwrap(); + index.push_str("{not json\n"); + index.push_str(&format!( + "{{\"id\":9,\"time\":{},\"path\":\"x\",\"hash\":\"../../evil\",\"size\":1,\"reason\":\"save\",\"bom\":false}}\n", + 100 * DAY + )); + std::fs::write(dir.join(INDEX), index).unwrap(); + let other = h.list("a.php")[0].clone(); + std::fs::write(h.blob_path(&other.hash), "tampered").unwrap(); + let h = open(data.path(), root.path(), now); + assert_eq!(h.list("a.php").len(), 2); + assert!( + h.list("x").is_empty(), + "a hash that is no file name is skipped" + ); + assert_eq!( + h.text(&other).unwrap_err(), + "the snapshot's text is damaged" + ); + let good = h.list("a.php")[1].clone(); + assert_eq!(h.text(&good).unwrap(), "good"); + } + + #[test] + fn unreferenced_blobs_are_deleted_on_open() { + let data = tempfile::tempdir().unwrap(); + let root = tempfile::tempdir().unwrap(); + let now = at(100 * DAY); + let mut h = open(data.path(), root.path(), now); + h.record("a.php", "kept", false, Reason::Save, now).unwrap(); + let stray = h.blob_path(&sha256_hex(b"stray")); + std::fs::create_dir_all(stray.parent().unwrap()).unwrap(); + std::fs::write(&stray, "stray").unwrap(); + let h = open(data.path(), root.path(), now); + assert!(!stray.exists()); + assert_eq!(walk(&h.dir().join(BLOBS)).len(), 1); + } + + #[test] + fn a_data_dir_inside_the_project_is_refused() { + let root = tempfile::tempdir().unwrap(); + let data = root.path().join(".lutra-data"); + let err = History::open(&data, root.path(), at(0)).unwrap_err(); + assert!(err.contains("inside the project"), "{err}"); + assert!(!data.exists(), "nothing is written into the project"); + } +} diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index cc00479..4bc1b7f 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -1,7 +1,7 @@ //! Engine of Lutra (layer L4). //! //! Owns the command registry, the workspace state, the panic guard around every -//! entry point and the crash-recovery journal. Every user-visible action is a +//! entry point, the crash-recovery journal and the Local History. Every user-visible action is a //! [`Command`]; menus, the palette, shortcuts, the CLI, the control channel and //! MCP all reach it through [`Engine::execute`]. @@ -12,6 +12,7 @@ mod engine; mod error; mod event; pub mod guard; +pub mod history; pub mod journal; pub mod lsp; mod registry; @@ -20,7 +21,7 @@ mod state; pub mod theme; pub mod workspace; -pub use buffers::{BufferId, LineEndingName, OpenBuffer}; +pub use buffers::{BufferId, DiskConflict, LineEndingName, OpenBuffer}; pub use command::{Answered, Command, CommandInfo, CommandSpec, Effect, Outcome, Pending, Reply}; pub use engine::{Engine, EngineConfig, run_waiting}; pub use error::{EngineError, ErrorPayload}; diff --git a/crates/engine/src/state.rs b/crates/engine/src/state.rs index 61715c3..468dbc6 100644 --- a/crates/engine/src/state.rs +++ b/crates/engine/src/state.rs @@ -34,6 +34,9 @@ pub struct State { /// The crash-recovery journal of the open project; `None` without a /// project or without persistence. pub journal: Option, + /// The Local History of the open project ([`crate::history`]); `None` + /// without a project, without persistence, or when it failed to open. + pub history: Option, /// Ids of the engine's own commands, which UI commands may not take. pub engine_commands: BTreeSet<&'static str>, /// UI commands the frontend registered (`ui.registerCommands`), by id. diff --git a/crates/engine/src/workspace.rs b/crates/engine/src/workspace.rs index 795b744..d09726d 100644 --- a/crates/engine/src/workspace.rs +++ b/crates/engine/src/workspace.rs @@ -28,7 +28,8 @@ use std::sync::mpsc::{Receiver, TryRecvError}; use lutra_text::Buffer; use lutra_vfs::{Project, VfsError, VfsEvent, Watcher}; -use crate::buffers::BufferId; +use crate::buffers::{BufferId, DiskConflict, DiskStamp}; +use crate::history::{self, Reason}; use crate::{EngineError, Event, State}; /// Directories watched besides the root with native notifications. @@ -245,9 +246,15 @@ fn is_ignore_file(path: &Path) -> bool { /// index ([`crate::cmd::goto::note_disk_changes`]), queues /// [`Event::TreeChanged`] for affected directories (also for the directory /// of an edited ignore file, whose entries may change their `ignored` -/// flag), reloads clean buffers -/// whose file changed, and queues [`Event::FileChangedOnDisk`] for dirty -/// buffers and for buffers whose file disappeared. +/// flag), and brings every open buffer whose file was touched in line with +/// the disk ([`check_buffer`]). +/// +/// Every event for a path (created, modified, removed, either side of a +/// rename) only says "look again": what counts is whether the file exists +/// now. So a delete followed by a create (a git checkout, an editor that +/// saves by replacing the file) reloads the buffer instead of reporting it +/// removed, and a rename away is a removal of the old path; the buffer is +/// never lost. pub fn pump(state: &mut State) { let Some(workspace) = state.project.as_mut() else { return; @@ -263,25 +270,29 @@ pub fn pump(state: &mut State) { dirs.push(dir); } }; - let mut modified = Vec::new(); - let mut gone = Vec::new(); + let mut touched: Vec = Vec::new(); + let mut touch = |path: PathBuf| { + if !touched.contains(&path) { + touched.push(path); + } + }; for change in changes { match change { - VfsEvent::Created { path, .. } => add_dir(&path), - VfsEvent::Removed { path } => { + VfsEvent::Created { path, .. } | VfsEvent::Removed { path } => { add_dir(&path); - gone.push(path); + touch(path); } VfsEvent::Renamed { from, to } => { add_dir(&from); add_dir(&to); - gone.push(from); + touch(from); + touch(to); } VfsEvent::Modified { path } => { if is_ignore_file(&path) { add_dir(&path); } - modified.push(path); + touch(path); } VfsEvent::Failed(_) => {} } @@ -291,19 +302,9 @@ pub fn pump(state: &mut State) { dir: path_string(&dir), }); } - for path in gone { + for path in touched { if let Some(id) = state.buffers.find(&path) { - state.buffers.mark_conflict(id); - state.emit(Event::FileChangedOnDisk { - path: path_string(&path), - id, - removed: true, - }); - } - } - for path in modified { - if let Some(id) = state.buffers.find(&path) { - reload(state, id, &root.join(&path)); + check_buffer(state, id, &root.join(&path)); } } if let Some(message) = failure { @@ -311,41 +312,120 @@ pub fn pump(state: &mut State) { } } -/// Brings buffer `id` in line with its file after the file changed: a clean -/// buffer takes the new text (unless it already matches, as after our own -/// save), a dirty one keeps its text and the client is told, and the buffer -/// is marked as in conflict ([`crate::buffers::Buffers::mark_conflict`]). -/// A dirty buffer whose file holds exactly what Lutra last wrote is left -/// alone: that is our own save reported after the user typed again (an -/// autosave followed by a keystroke). A file that cannot be read is reported +/// The stamp of the file at `abs`: its modification time and length. +/// `None` when it cannot be read. +pub fn stamp(abs: &Path) -> Option { + let meta = std::fs::metadata(abs).ok()?; + Some((meta.modified().ok()?, meta.len())) +} + +/// Brings buffer `id` in line with its file at `abs` after something +/// touched it: [`file_gone`] when no file is there any more, else +/// [`reload`]. +pub fn check_buffer(state: &mut State, id: BufferId, abs: &Path) { + if abs.is_file() { + reload(state, id, abs); + } else { + file_gone(state, id); + } +} + +/// Clears the disk conflict of buffer `id` and, if it had one, queues +/// [`Event::DiskConflictResolved`] so clients drop their banner. +pub fn settle_conflict(state: &mut State, id: BufferId) { + if state.buffers.clear_conflict(id) { + state.emit(Event::DiskConflictResolved { id }); + } +} + +/// Handles the disappearance of buffer `id`'s file: the buffer keeps its +/// text and becomes dirty (so closing it asks, and the recovery journal +/// keeps it), its text goes into the Local History ([`Reason::Deleted`]), +/// it is marked [`DiskConflict::Removed`] and the client is told with +/// [`Event::FileChangedOnDisk`] `{removed: true}`. Reported once until the +/// file comes back or the buffer is saved. +fn file_gone(state: &mut State, id: BufferId) { + if state.buffers.conflict(id) == Some(DiskConflict::Removed) { + return; + } + history::snapshot_buffer(state, id, Reason::Deleted); + let Some(open) = state.buffers.get_mut(id) else { + return; + }; + let was_dirty = open.dirty(); + let version = open.version; + if !was_dirty { + open.saved_version = 0; + } + let path = path_string(&open.path); + state.buffers.mark_conflict(id, DiskConflict::Removed); + if !was_dirty { + state.buffers.note_clean_removal(id, version); + } + if !was_dirty { + state.emit(Event::DirtyChanged { id, dirty: true }); + crate::journal::mark_edited(state, id, std::time::Instant::now()); + } + state.emit(Event::FileChangedOnDisk { + path, + id, + removed: true, + }); +} + +/// Brings buffer `id` in line with its file after the file changed: +/// - A file that holds exactly the buffer's text (and byte order mark) +/// makes the buffer clean and settles its conflict: our own save +/// reported late, a touch, or a file that came back. +/// - A clean buffer takes the new text ([`take_disk_text`]); the old text +/// and the new one go into the Local History first. So does a buffer that +/// is dirty only because its file was gone for a while +/// ([`crate::buffers::Buffers::dirty_only_by_removal`]). +/// - A dirty buffer keeps its text; the new file text goes into the Local +/// History ([`Reason::ExternalChange`]), the buffer is marked +/// [`DiskConflict::Changed`] and the client is told once. A dirty buffer +/// whose file holds exactly what Lutra last wrote is left alone: that is +/// our own save reported after the user typed again (an autosave followed +/// by a keystroke). +/// +/// A file that cannot be read (gone meanwhile, no longer text) is reported /// like a removed one. fn reload(state: &mut State, id: BufferId, abs: &Path) { let Some(open) = state.buffers.get(id) else { return; }; let path = path_string(&open.path); - let dirty = open.dirty(); - let file = match lutra_vfs::read_text(abs) { - Ok(file) => file, - Err(_) => { - state.buffers.mark_conflict(id); - state.emit(Event::FileChangedOnDisk { - path, - id, - removed: true, - }); - return; - } + let rel = open.path.clone(); + let Ok(file) = lutra_vfs::read_text(abs) else { + file_gone(state, id); + return; }; - if dirty { - if state.buffers.is_own_write( - id, - &crate::journal::hex_hash(file.text.as_bytes()), - file.bom, - ) { + let Some(open) = state.buffers.get(id) else { + return; + }; + let hash = crate::journal::hex_hash(file.text.as_bytes()); + if file.text == open.buffer.save_text() && file.bom == open.bom { + let was_dirty = open.dirty(); + if let Some(open) = state.buffers.get_mut(id) { + open.saved_version = open.version; + } + state.buffers.note_written(id, hash, file.bom, stamp(abs)); + settle_conflict(state, id); + if was_dirty { + crate::journal::forget(state, id); + state.emit(Event::DirtyChanged { id, dirty: false }); + } + return; + } + if open.dirty() && !state.buffers.dirty_only_by_removal(id) { + if state.buffers.is_own_write(id, &hash, file.bom) { return; } - state.buffers.mark_conflict(id); + history::snapshot_external(state, &rel, &file.text, file.bom); + if state.buffers.conflict(id) == Some(DiskConflict::Changed) { + return; + } + state.buffers.mark_conflict(id, DiskConflict::Changed); state.emit(Event::FileChangedOnDisk { path, id, @@ -353,25 +433,50 @@ fn reload(state: &mut State, id: BufferId, abs: &Path) { }); return; } + take_disk_text(state, id, file, stamp(abs)); +} + +/// Replaces buffer `id`'s text with `file` as read from disk and makes it +/// clean: the old text goes into the Local History +/// ([`Reason::BeforeReload`]) and so does the new one +/// ([`Reason::ExternalChange`]); the conflict is settled, the recovery +/// entry dropped and [`Event::BufferChanged`] (plus +/// [`Event::DirtyChanged`] for a dirty buffer) queued. Clients move the +/// editor onto the new text with a minimal change, so the caret and the +/// scroll position stay where the text around them did not change. +/// +/// # Parameters +/// - `stamp`: the file's [`stamp`] when it was read. +pub fn take_disk_text( + state: &mut State, + id: BufferId, + file: lutra_vfs::TextFile, + stamp: Option, +) { let Some(open) = state.buffers.get(id) else { return; }; - if file.text == open.buffer.save_text() && file.bom == open.bom { - state.buffers.clear_conflict(id); - return; - } + let rel = open.path.clone(); + history::snapshot_buffer(state, id, Reason::BeforeReload); + history::snapshot_external(state, &rel, &file.text, file.bom); let Some(open) = state.buffers.get_mut(id) else { return; }; let hash = crate::journal::hex_hash(file.text.as_bytes()); + let was_dirty = open.dirty(); open.buffer = Buffer::new(&file.text); open.bom = file.bom; open.version = open.version.saturating_add(1); open.saved_version = open.version; let version = open.version; - state.buffers.clear_conflict(id); - state.buffers.note_written(id, hash, file.bom); + state.buffers.note_written(id, hash, file.bom, stamp); + state.buffers.mark_in_history(id); + settle_conflict(state, id); + crate::journal::forget(state, id); state.emit(Event::BufferChanged { id, version }); + if was_dirty { + state.emit(Event::DirtyChanged { id, dirty: false }); + } } /// A project-relative path as sent to clients: `/` separators, `""` for the diff --git a/crates/engine/tests/history.rs b/crates/engine/tests/history.rs new file mode 100644 index 0000000..5685ed2 --- /dev/null +++ b/crates/engine/tests/history.rs @@ -0,0 +1,260 @@ +//! Local History (C10 A2) through the engine: snapshots on save, reload, +//! deletion and revert, stored in the data directory, never in the project. + +#![allow( + clippy::unwrap_used, + clippy::expect_used, + clippy::panic, + clippy::indexing_slicing +)] + +use std::fs; +use std::time::{Duration, Instant}; + +use lutra_engine::{Engine, EngineConfig, Event, Outcome}; +use serde_json::{Value, json}; +use tempfile::TempDir; + +/// A project folder, a data directory and an engine with that project open. +struct Fixture { + project: TempDir, + data: TempDir, + _config: TempDir, + engine: Engine, +} + +fn fixture(files: &[(&str, &str)]) -> Fixture { + let project = tempfile::tempdir().unwrap(); + for (path, text) in files { + fs::write(project.path().join(path), text).unwrap(); + } + let data = tempfile::tempdir().unwrap(); + let config = tempfile::tempdir().unwrap(); + let mut engine = Engine::with_config(EngineConfig { + config_dir: config.path().to_path_buf(), + data_dir: data.path().to_path_buf(), + }) + .unwrap(); + engine + .execute("project.open", json!({"path": project.path()})) + .unwrap(); + Fixture { + project, + data, + _config: config, + engine, + } +} + +fn run(engine: &mut Engine, command: &str, params: Value) -> Outcome { + engine + .execute(command, params) + .unwrap_or_else(|e| panic!("{command} failed: {e}")) +} + +fn open(engine: &mut Engine, path: &str) -> u64 { + run(engine, "file.open", json!({"path": path})).value["id"] + .as_u64() + .unwrap() +} + +fn replace(engine: &mut Engine, id: u64, text: &str) { + let now = run(engine, "buffer.get", json!({"id": id})).value; + let len = now["text"].as_str().unwrap().encode_utf16().count(); + run( + engine, + "buffer.edit", + json!({"id": id, "baseVersion": now["version"], "edits": [{"from": 0, "to": len, "insert": text}]}), + ); +} + +/// `(reason, text)` of every entry of `path`, newest first. +fn entries(engine: &mut Engine, path: &str) -> Vec<(String, String)> { + let list = run(engine, "history.list", json!({"path": path})).value; + list["entries"] + .as_array() + .unwrap() + .iter() + .map(|e| { + let text = + run(engine, "history.read", json!({"path": path, "id": e["id"]})).value["text"] + .as_str() + .unwrap() + .to_string(); + (e["reason"].as_str().unwrap().to_string(), text) + }) + .collect() +} + +fn pair(reason: &str, text: &str) -> (String, String) { + (reason.to_string(), text.to_string()) +} + +fn wait_for(engine: &mut Engine, pred: impl Fn(&Event) -> bool) { + let deadline = Instant::now() + Duration::from_secs(5); + while Instant::now() < deadline { + if engine.pump_vfs().unwrap().iter().any(&pred) { + return; + } + std::thread::sleep(Duration::from_millis(25)); + } + panic!("event not seen within 5 s"); +} + +#[test] +fn saves_keep_the_original_and_every_saved_text() { + let mut f = fixture(&[("a.php", "original")]); + let id = open(&mut f.engine, "a.php"); + replace(&mut f.engine, id, "first"); + run(&mut f.engine, "file.save", json!({"id": id})); + run(&mut f.engine, "file.save", json!({"id": id})); + replace(&mut f.engine, id, "second"); + run(&mut f.engine, "file.saveAll", json!({})); + assert_eq!( + entries(&mut f.engine, "a.php"), + vec![ + pair("save", "second"), + pair("save", "first"), + pair("beforeSave", "original"), + ], + "a save of an unchanged text adds nothing" + ); + let list = run(&mut f.engine, "history.list", json!({"path": "a.php"})).value; + let entry = &list["entries"][0]; + assert!(entry["time"].as_u64().unwrap() > 0); + assert_eq!(entry["size"], 6); +} + +#[test] +fn the_history_lives_in_the_data_dir_and_survives_a_reopen() { + let mut f = fixture(&[("a.php", "one")]); + let id = open(&mut f.engine, "a.php"); + replace(&mut f.engine, id, "two"); + run(&mut f.engine, "file.save", json!({"id": id})); + let names: Vec = fs::read_dir(f.project.path()) + .unwrap() + .map(|e| e.unwrap().file_name().to_string_lossy().into_owned()) + .collect(); + assert_eq!( + names, + vec!["a.php".to_string()], + "nothing inside the project" + ); + assert!(f.data.path().join("history").is_dir()); + run( + &mut f.engine, + "project.open", + json!({"path": f.project.path()}), + ); + assert_eq!(entries(&mut f.engine, "a.php").len(), 2); +} + +#[test] +fn reloads_and_deletions_keep_the_buffers_text() { + let mut f = fixture(&[("a.php", "mine")]); + let id = open(&mut f.engine, "a.php"); + replace(&mut f.engine, id, "unsaved"); + fs::write(f.project.path().join("a.php"), "theirs").unwrap(); + wait_for(&mut f.engine, |e| { + matches!(e, Event::FileChangedOnDisk { removed: false, .. }) + }); + run(&mut f.engine, "file.reload", json!({"id": id})); + assert_eq!( + entries(&mut f.engine, "a.php"), + vec![ + pair("beforeReload", "unsaved"), + pair("externalChange", "theirs") + ] + ); + fs::remove_file(f.project.path().join("a.php")).unwrap(); + wait_for(&mut f.engine, |e| { + matches!(e, Event::FileChangedOnDisk { removed: true, .. }) + }); + assert_eq!( + entries(&mut f.engine, "a.php")[0], + pair("deleted", "theirs") + ); +} + +#[test] +fn revert_edits_the_buffer_and_keeps_what_it_replaced() { + let mut f = fixture(&[("a.php", "one")]); + let id = open(&mut f.engine, "a.php"); + replace(&mut f.engine, id, "two"); + run(&mut f.engine, "file.save", json!({"id": id})); + replace(&mut f.engine, id, "three, unsaved"); + let list = run(&mut f.engine, "history.list", json!({"path": "a.php"})).value; + let original = list["entries"][1]["id"].clone(); + let out = run( + &mut f.engine, + "history.revert", + json!({"path": "a.php", "id": original}), + ); + assert_eq!(out.value["id"], id); + assert_eq!(out.value["dirty"], true, "an ordinary, unsaved edit"); + assert!( + out.events + .iter() + .any(|e| matches!(e, Event::BufferChanged { id: got, .. } if *got == id)) + ); + let now = run(&mut f.engine, "buffer.get", json!({"id": id})).value; + assert_eq!(now["text"], "one"); + assert_eq!( + fs::read_to_string(f.project.path().join("a.php")).unwrap(), + "two" + ); + assert_eq!( + entries(&mut f.engine, "a.php")[0], + pair("beforeRevert", "three, unsaved") + ); +} + +#[test] +fn revert_opens_a_closed_file() { + let mut f = fixture(&[("a.php", "one")]); + let id = open(&mut f.engine, "a.php"); + replace(&mut f.engine, id, "two"); + run(&mut f.engine, "file.save", json!({"id": id})); + run(&mut f.engine, "buffer.close", json!({"id": id})); + let list = run(&mut f.engine, "history.list", json!({"path": "a.php"})).value; + let out = run( + &mut f.engine, + "history.revert", + json!({"path": "a.php", "id": list["entries"][1]["id"]}), + ); + let new_id = out.value["id"].as_u64().unwrap(); + assert!( + out.events + .iter() + .any(|e| matches!(e, Event::BufferOpened { id, .. } if *id == new_id)) + ); + let now = run(&mut f.engine, "buffer.get", json!({"id": new_id})).value; + assert_eq!(now["text"], "one"); +} + +#[test] +fn unknown_entries_and_projects_without_history_fail_clearly() { + let mut f = fixture(&[("a.php", "one")]); + let err = f + .engine + .execute("history.read", json!({"path": "a.php", "id": 99})) + .unwrap_err(); + assert_eq!(err.code(), "failed"); + assert!( + err.to_string().contains("no Local History entry 99"), + "{err}" + ); + assert_eq!( + run(&mut f.engine, "history.list", json!({"path": "a.php"})).value["entries"], + json!([]) + ); + + let dir = tempfile::tempdir().unwrap(); + let mut bare = Engine::new().unwrap(); + bare.execute("project.open", json!({"path": dir.path()})) + .unwrap(); + let err = bare + .execute("history.list", json!({"path": "a.php"})) + .unwrap_err(); + assert!(err.to_string().contains("not available"), "{err}"); +} diff --git a/crates/engine/tests/workspace.rs b/crates/engine/tests/workspace.rs index d56210b..effec26 100644 --- a/crates/engine/tests/workspace.rs +++ b/crates/engine/tests/workspace.rs @@ -267,7 +267,7 @@ fn save_all_saves_every_dirty_buffer() { let out = run(&mut engine, "file.saveAll", Value::Null); assert_eq!( out.value, - json!({"saved": [a, b], "failed": [], "skipped": []}) + json!({"saved": [a, b], "failed": [], "skipped": [], "diskChanged": []}) ); assert_eq!(fs::read_to_string(dir.path().join("b.php")).unwrap(), ">b"); } @@ -603,7 +603,7 @@ fn save_all_can_skip_buffers_in_conflict_with_the_disk() { let out = run(&mut engine, "file.saveAll", json!({"skipConflicts": true})); assert_eq!( out.value, - json!({"saved": [mine], "failed": [], "skipped": [other]}) + json!({"saved": [mine], "failed": [], "skipped": [other], "diskChanged": []}) ); assert_eq!( fs::read_to_string(dir.path().join("other.php")).unwrap(), @@ -614,7 +614,19 @@ fn save_all_can_skip_buffers_in_conflict_with_the_disk() { "xone" ); - run(&mut engine, "file.save", json!({"id": other})); + let err = engine + .execute("file.save", json!({"id": other})) + .unwrap_err(); + assert_eq!( + err.code(), + "disk_changed", + "a newer file is never overwritten silently" + ); + run( + &mut engine, + "file.save", + json!({"id": other, "overwrite": true}), + ); assert_eq!( fs::read_to_string(dir.path().join("other.php")).unwrap(), "xone" @@ -682,3 +694,198 @@ fn typing_right_after_a_save_is_not_a_conflict() { let out = run(&mut engine, "file.saveAll", json!({"skipConflicts": true})); assert_eq!(out.value["saved"], json!([id])); } + +fn edit(engine: &mut Engine, id: u64, base: u64, at: u64, insert: &str) { + run( + engine, + "buffer.edit", + json!({"id": id, "baseVersion": base, "edits": [{"from": at, "to": at, "insert": insert}]}), + ); +} + +#[test] +fn reload_discards_mine_and_settles_the_conflict() { + let (dir, mut engine, _) = open_project(&[("a.php", b"one")]); + let (id, _) = open_file(&mut engine, "a.php"); + edit(&mut engine, id, 1, 0, "mine "); + fs::write(dir.path().join("a.php"), "theirs").unwrap(); + wait_for( + &mut engine, + |e| matches!(e, Event::FileChangedOnDisk { id: got, removed: false, .. } if *got == id), + ); + assert_eq!( + run(&mut engine, "buffer.get", json!({"id": id})).value["diskConflict"], + "changed" + ); + let out = run(&mut engine, "file.reload", json!({"id": id})); + assert_eq!(out.value["dirty"], false); + assert!( + out.events + .iter() + .any(|e| matches!(e, Event::DiskConflictResolved { id: got } if *got == id)) + ); + assert!( + out.events + .iter() + .any(|e| matches!(e, Event::DirtyChanged { id: got, dirty: false } if *got == id)) + ); + let now = run(&mut engine, "buffer.get", json!({"id": id})).value; + assert_eq!(now["text"], "theirs"); + assert_eq!(now["diskConflict"], Value::Null); +} + +#[test] +fn a_save_over_a_newer_file_asks_even_before_the_watcher_saw_it() { + let (dir, mut engine, _) = open_project(&[("a.php", b"one"), ("b.php", b"one")]); + let (a, _) = open_file(&mut engine, "a.php"); + let (b, _) = open_file(&mut engine, "b.php"); + edit(&mut engine, a, 1, 0, "x"); + edit(&mut engine, b, 1, 0, "x"); + std::thread::sleep(Duration::from_millis(30)); + fs::write(dir.path().join("a.php"), "theirs, longer").unwrap(); + let err = engine.execute("file.save", json!({"id": a})).unwrap_err(); + assert_eq!(err.code(), "disk_changed"); + assert_eq!( + fs::read_to_string(dir.path().join("a.php")).unwrap(), + "theirs, longer" + ); + let out = run(&mut engine, "file.saveAll", json!({})); + assert_eq!(out.value["saved"], json!([b])); + assert_eq!(out.value["diskChanged"], json!([a])); + run( + &mut engine, + "file.save", + json!({"id": a, "overwrite": true}), + ); + assert_eq!( + fs::read_to_string(dir.path().join("a.php")).unwrap(), + "xone" + ); +} + +#[test] +fn a_touched_file_with_the_same_text_saves_without_asking() { + let (dir, mut engine, _) = open_project(&[("a.php", b"one")]); + let (id, _) = open_file(&mut engine, "a.php"); + edit(&mut engine, id, 1, 0, "x"); + std::thread::sleep(Duration::from_millis(30)); + fs::write(dir.path().join("a.php"), "one").unwrap(); + run(&mut engine, "file.save", json!({"id": id})); + assert_eq!( + fs::read_to_string(dir.path().join("a.php")).unwrap(), + "xone" + ); +} + +#[test] +fn a_deleted_file_keeps_its_buffer_as_unsaved_until_it_comes_back() { + let (dir, mut engine, _) = open_project(&[("a.php", b"one")]); + let (id, _) = open_file(&mut engine, "a.php"); + fs::remove_file(dir.path().join("a.php")).unwrap(); + let events = wait_for( + &mut engine, + |e| matches!(e, Event::FileChangedOnDisk { id: got, removed: true, .. } if *got == id), + ); + assert!( + events + .iter() + .any(|e| matches!(e, Event::DirtyChanged { id: got, dirty: true } if *got == id)) + ); + let now = run(&mut engine, "buffer.get", json!({"id": id})).value; + assert_eq!(now["text"], "one"); + assert_eq!(now["dirty"], true); + assert_eq!(now["diskConflict"], "removed"); + assert_eq!( + engine + .execute("buffer.close", json!({"id": id})) + .unwrap_err() + .code(), + "unsaved_changes", + "closing asks" + ); + + fs::write(dir.path().join("a.php"), "back, but different").unwrap(); + wait_for( + &mut engine, + |e| matches!(e, Event::DiskConflictResolved { id: got } if *got == id), + ); + let now = run(&mut engine, "buffer.get", json!({"id": id})).value; + assert_eq!( + now["text"], "back, but different", + "a buffer dirty only because its file was gone reloads like a clean one" + ); + assert_eq!(now["dirty"], false); +} + +#[test] +fn keep_as_unsaved_then_save_recreates_the_file() { + let (dir, mut engine, _) = open_project(&[("a.php", b"one")]); + let (id, _) = open_file(&mut engine, "a.php"); + edit(&mut engine, id, 1, 0, "x"); + fs::remove_file(dir.path().join("a.php")).unwrap(); + wait_for( + &mut engine, + |e| matches!(e, Event::FileChangedOnDisk { id: got, removed: true, .. } if *got == id), + ); + let out = run(&mut engine, "file.save", json!({"id": id})); + assert!( + out.events + .iter() + .any(|e| matches!(e, Event::DiskConflictResolved { id: got } if *got == id)) + ); + assert_eq!( + fs::read_to_string(dir.path().join("a.php")).unwrap(), + "xone" + ); +} + +#[test] +fn a_file_replaced_by_delete_and_create_reloads_instead_of_being_lost() { + let (dir, mut engine, _) = open_project(&[("a.php", b"one"), ("b.php", b"one")]); + let (clean, _) = open_file(&mut engine, "a.php"); + let (dirty, _) = open_file(&mut engine, "b.php"); + edit(&mut engine, dirty, 1, 0, "mine "); + for name in ["a.php", "b.php"] { + fs::remove_file(dir.path().join(name)).unwrap(); + fs::write(dir.path().join(name), "checked out").unwrap(); + } + let mut seen = wait_for( + &mut engine, + |e| matches!(e, Event::BufferChanged { id, .. } if *id == clean), + ); + if !seen + .iter() + .any(|e| matches!(e, Event::FileChangedOnDisk { id, removed: false, .. } if *id == dirty)) + { + seen.extend(wait_for( + &mut engine, + |e| matches!(e, Event::FileChangedOnDisk { id, removed: false, .. } if *id == dirty), + )); + } + let a = run(&mut engine, "buffer.get", json!({"id": clean})).value; + assert_eq!(a["text"], "checked out"); + assert_eq!(a["dirty"], false); + let b = run(&mut engine, "buffer.get", json!({"id": dirty})).value; + assert_eq!( + b["text"], "mine one", + "the dirty buffer keeps the user's text" + ); + assert_eq!(b["diskConflict"], "changed"); +} + +#[test] +fn read_disk_answers_like_git_show() { + let (_dir, mut engine, _) = + open_project(&[("a.php", b"on disk"), ("bin.dat", b"\xff\xfe\x00x")]); + let (id, _) = open_file(&mut engine, "a.php"); + edit(&mut engine, id, 1, 0, "mine "); + let out = run(&mut engine, "file.readDisk", json!({"path": "a.php"})); + assert_eq!( + out.value, + json!({"text": "on disk", "binary": false, "tooLarge": false}) + ); + let out = run(&mut engine, "file.readDisk", json!({"path": "gone.php"})); + assert_eq!(out.value["text"], Value::Null); + let out = run(&mut engine, "file.readDisk", json!({"path": "bin.dat"})); + assert_eq!(out.value["binary"], true); +}