Порівняти коміти

...
Автор SHA1 Повідомлення Дата
l0ng-ai c4d7901a30 fix(local-history): keep a file's content from before its first edit
A file with no Local History entries had nothing to revert to at a label put before its first save. The first edit of an open document now records the file's content on disk as an "Original" snapshot when the file has no history yet, dated by its modification time (but not before the oldest entry kept, so retention and working days are unaffected). A label put before the edit finds it; a file written after the label is dated after it.
2026-11-18 20:29:44 +08:00
2 змінених файлів з 112 додано та 7 видалено

No files matched your search

+33 -5
Переглянути файл
@@ -324,7 +324,7 @@ impl Host {
let syntax = Arc::new(SyntaxCache::new(self.syntax()));
documents.add_observer(Arc::new(SyntaxObserver(syntax.clone())));
documents.add_observer(Arc::new(LspObserver(lsp.clone())));
documents.add_observer(Arc::new(HistoryObserver(history.clone())));
documents.add_observer(Arc::new(HistoryObserver::new(history.clone())));
documents.add_observer(Arc::new(DeployObserver {
remote: self.remote.clone(),
settings: settings.clone(),
@@ -785,7 +785,7 @@ impl Host {
lsp.set_servers_hook(Box::new(|_, _| Vec::new()));
let syntax = Arc::new(SyntaxCache::new(self.syntax()));
documents.add_observer(Arc::new(SyntaxObserver(syntax.clone())));
documents.add_observer(Arc::new(HistoryObserver(history.clone())));
documents.add_observer(Arc::new(HistoryObserver::new(history.clone())));
let watcher = match watch_dir {
Some(dir) => light_watcher(dir, &sink, &documents, &history)?,
// A quiet private folder stands in until the first save brings a
@@ -1890,14 +1890,42 @@ impl DocumentObserver for SyntaxObserver {
}
}
struct HistoryObserver(Arc<LocalHistory>);
struct HistoryObserver {
history: Arc<LocalHistory>,
/// Documents edited since they were opened: only the first edit looks for a missing original.
edited: Mutex<HashSet<ide_core::DocId>>,
}
impl HistoryObserver {
fn new(history: Arc<LocalHistory>) -> Self {
Self { history, edited: Mutex::default() }
}
}
impl DocumentObserver for HistoryObserver {
fn saved(&self, _doc: ide_core::DocId, path: &Path, text: &str) {
if let Err(e) = self.0.record(path, text, "Saved") {
fn changed(
&self,
doc: ide_core::DocId,
path: &Path,
_version: u32,
_changes: &[AppliedChange],
_full_text: &dyn Fn() -> String,
) {
// The file on disk still has its content from before the edit.
if self.edited.lock().insert(doc)
&& let Err(e) = self.history.record_original(path)
{
tracing::warn!("local history: {e}");
}
}
fn saved(&self, _doc: ide_core::DocId, path: &Path, text: &str) {
if let Err(e) = self.history.record(path, text, "Saved") {
tracing::warn!("local history: {e}");
}
}
fn closed(&self, doc: ide_core::DocId, _path: &Path) {
self.edited.lock().remove(&doc);
}
}
/// Uploads saved files: remote files' local copies, and automatic upload to the default server.
+79 -2
Переглянути файл
@@ -92,6 +92,8 @@ pub const DEFAULT_MAX_AGE_DAYS: u32 = 5;
pub const DEFAULT_MAX_BYTES: u64 = 512 * 1024 * 1024;
/// Label of the snapshot taken before a file is deleted.
pub const DELETED: &str = "Deleted";
/// The label of a file's content from before its first edit ([`LocalHistory::record_original`]).
pub const ORIGINAL: &str = "Original";
/// Files larger than this are not snapshotted automatically (watcher, deletes, VCS).
const AUTO_SNAPSHOT_LIMIT: u64 = 2 * 1024 * 1024;
const DAY_MS: f64 = 86_400_000.0;
@@ -195,6 +197,18 @@ impl LocalHistory {
if !force && state.last.get(path) == Some(&id) {
return Ok(());
}
self.store_locked(&mut state, path, id, content, label, now_ms())
}
fn store_locked(
&self,
state: &mut HistoryState,
path: &Path,
id: String,
content: &str,
label: &str,
timestamp_ms: f64,
) -> IdeResult<()> {
let blob = self.blob_path(&id);
if !blob.exists() {
fs::create_dir_all(blob.parent().expect("blob has a parent"))?;
@@ -203,12 +217,12 @@ impl LocalHistory {
let entry = HistoryEntry {
id,
path: path.into(),
timestamp_ms: now_ms(),
timestamp_ms,
label: label.to_owned(),
size: content.len() as u64,
kind: HistoryKind::Snapshot,
};
self.append(&mut state, entry)
self.append(state, entry)
}
/// Records a snapshot unless it equals the latest one recorded for `path`.
@@ -230,6 +244,31 @@ impl LocalHistory {
}
}
/// Before the first edit of a file with no history: its content on disk, so a label put
/// before the edit can be reverted to. Dated by the file's modification time, since it
/// has had that content from then on, but not before the oldest entry kept, so it neither
/// adds a working day of its own nor falls to retention first. Returns whether one was recorded.
pub fn record_original(&self, path: &Path) -> IdeResult<bool> {
let Some(text) = read_snapshot_text(path) else { return Ok(false) };
let modified = fs::metadata(path)
.and_then(|m| m.modified())
.ok()
.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
.map(|d| d.as_secs_f64() * 1000.0);
let mut state = self.state.lock();
if state.entries.iter().any(|e| e.kind == HistoryKind::Snapshot && e.path == path) {
return Ok(false);
}
let now = now_ms();
let oldest = state.entries.iter().map(|e| e.timestamp_ms).reduce(f64::min);
let timestamp = match (modified, oldest) {
(Some(modified), Some(oldest)) => modified.max(oldest - 1.0).min(now),
_ => now,
};
let id = blake3::hash(text.as_bytes()).to_hex().to_string();
self.store_locked(&mut state, path, id, &text, ORIGINAL, timestamp).map(|()| true)
}
/// Snapshots `path`, or every file below it when it is a folder (at most `limit`
/// files), before it is deleted. Returns how many files were recorded.
pub fn record_deletion(&self, path: &Path, limit: usize) -> usize {
@@ -659,4 +698,42 @@ mod tests {
assert_eq!(h.list(Path::new("/p/a")).unwrap().len(), 1);
assert_eq!(h.content(&old).unwrap_err().kind, ide_core::error::ErrorKind::NotFound);
}
#[test]
fn a_files_original_content_is_there_at_a_label_put_before_its_first_edit() {
use std::time::{Duration, SystemTime};
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("project");
std::fs::create_dir_all(&root).unwrap();
let h = LocalHistory::open(&tmp.path().join("history"), &root).unwrap();
let set_modified = |path: &Path, at: SystemTime| {
std::fs::File::options().write(true).open(path).unwrap().set_modified(at).unwrap();
};
let old = root.join("old.txt");
std::fs::write(&old, "as it was").unwrap();
set_modified(&old, SystemTime::now() - Duration::from_secs(3600));
h.record(&root.join("other.txt"), "x", "Saved").unwrap();
std::thread::sleep(Duration::from_millis(2));
h.put_label(&root, "checkpoint").unwrap();
// Written after the label: it didn't have this content at the label.
let new = root.join("new.txt");
std::fs::write(&new, "fresh").unwrap();
set_modified(&new, SystemTime::now() + Duration::from_secs(1));
assert!(h.record_original(&old).unwrap());
assert!(h.record_original(&new).unwrap());
// Only a file without history gets one.
assert!(!h.record_original(&old).unwrap());
h.record(&old, "edited", "Saved").unwrap();
let entries = h.list(&old).unwrap();
let label = entries.iter().find(|e| e.kind == HistoryKind::Label).unwrap();
let original = entries.iter().find(|e| e.label == ORIGINAL).unwrap();
assert!(original.timestamp_ms < label.timestamp_ms);
assert_eq!(h.content(&original.id).unwrap(), "as it was");
// Not dated before the oldest entry kept: no working day of its own.
let oldest = h.list(&root.join("other.txt")).unwrap().last().unwrap().timestamp_ms;
assert!(original.timestamp_ms >= oldest - 1.0);
let fresh = h.list(&new).unwrap().into_iter().find(|e| e.label == ORIGINAL).unwrap();
assert!(fresh.timestamp_ms > label.timestamp_ms);
}
}