From 30f2e83aca9fc2584939b2c090f4abe3a87c5004 Mon Sep 17 00:00:00 2001 From: Clearfall <237151555+Clearfall@users.noreply.github.com> Date: Tue, 13 Oct 2026 13:34:32 +0800 Subject: [PATCH] LR-IMP-DUPES: exclude Local browse records from library duplicate matching Prefer real library entries when importing browsed copies and track promoted hashes across scan and background-import batches. Add synthetic regressions for permanent deletion, restart, trash restoration, and duplicate promotion. Draft validation on the previous base: 59 import tests passed; all 8 cargo xtask ci steps passed with PR #1052 in a temporary validation worktree. Rebased onto main 68c9240, which includes #1052; validation of this base is recorded in the PR. Co-authored-by: Codex --- crates/engine/src/import.rs | 61 ++++++++++---- crates/engine/src/tests_import.rs | 128 ++++++++++++++++++++++++++++++ docs/parity-checklist.md | 5 +- 3 files changed, 178 insertions(+), 16 deletions(-) diff --git a/crates/engine/src/import.rs b/crates/engine/src/import.rs index 1f008e7666f..a8eae10c35c 100644 --- a/crates/engine/src/import.rs +++ b/crates/engine/src/import.rs @@ -549,7 +549,7 @@ pub struct ScanInput { probe: Option, skip: Option, /// path → (photo, its summary) for files already in the library. - by_path: HashMap, + by_path: HashMap)>, by_hash: HashMap, cache: HashMap, } @@ -585,13 +585,16 @@ impl ScanInput { height: p.height, file_size: p.file_size, captured: p.captured.clone(), - duplicate: Some("path".into()), + duplicate: (!p.local).then(|| "path".into()), existing: Some(p.id.0), ..Default::default() }; - by_path.insert(PathBuf::from(path), (p.id, c)); + by_path.insert(PathBuf::from(path), (p.id, c, p.content_hash.clone())); } - if let Some(h) = &p.content_hash { + // Browsing another copy does not add its content to My Photos. + if !p.local + && let Some(h) = &p.content_hash + { by_hash.insert(h.clone(), p.id); } } @@ -637,9 +640,22 @@ pub fn scan_with(mut input: ScanInput, paths: &[String], progress: &ScanProgress let mut out = Vec::with_capacity(files.len()); let mut kept = HashMap::new(); for f in files { - if let Some((_, c)) = input.by_path.get(Path::new(&f)) { + if let Some((id, c, hash)) = input.by_path.get(Path::new(&f)) { let mut c = c.clone(); c.name = Path::new(&f).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| f.clone()); + if c.duplicate.is_none() + && let Some(h) = hash + { + match seen_hash.get(h) { + Some(existing) => { + c.duplicate = Some("content".into()); + c.existing = *existing; + } + None => { + seen_hash.insert(h.clone(), Some(id.0)); + } + } + } out.push(c); continue; } @@ -701,8 +717,8 @@ pub struct ImportJob { mode: ImportMode, lib_dir: Option, copy_root: Option, - /// path → (photo, a Local browse record?) - by_path: HashMap, + /// path → (photo, a Local browse record?, content hash) + by_path: HashMap)>, /// content hash → the photo that has it (`None`: a file earlier in this import) by_hash: HashMap>, probe: Option, @@ -850,9 +866,11 @@ impl ImportJob { continue; } if let Source::File { path } = &p.source { - by_path.insert(path.clone(), (p.id, p.local)); + by_path.insert(path.clone(), (p.id, p.local, p.content_hash.clone())); } - if let Some(h) = &p.content_hash { + if !p.local + && let Some(h) = &p.content_hash + { by_hash.insert(h.clone(), Some(p.id)); } } @@ -906,14 +924,26 @@ impl ImportJob { let mut out = Prepared { scanned: files.len(), items: Vec::new() }; let mut todo = Vec::new(); for f in files { - match self.by_path.get(f.as_str()).copied() { - Some((id, true)) if !opts.local => { + match self.by_path.get(f.as_str()).cloned() { + Some((id, true, hash)) if !opts.local => { + // A library match takes precedence over promoting a browsed copy. Newly + // promoted content counts immediately, including across worker batches. + if let Some(h) = &hash + && let Some(existing) = self.by_hash.get(h) + { + out.items.push(PreparedItem::Duplicate { path: f, existing: *existing, reason: "content", hash }); + self.done.fetch_add(1, Relaxed); + continue; + } + if let Some(h) = &hash { + self.by_hash.insert(h.clone(), Some(id)); + } // a file that was only browsed (Local) joins the library when it is imported for real - self.by_path.insert(f, (id, false)); + self.by_path.insert(f, (id, false, hash)); out.items.push(PreparedItem::Promote(id)); self.done.fetch_add(1, Relaxed); } - Some((id, _)) => { + Some((id, _, _)) => { out.items.push(PreparedItem::Duplicate { path: f, existing: Some(id), reason: "path", hash: None }); self.done.fetch_add(1, Relaxed); } @@ -1289,7 +1319,10 @@ pub fn commit_prepared(s: &mut Session, opts: &ImportOptions, now: &str, prepare PreparedItem::Duplicate { path, existing, reason, hash } => { let existing = existing.or_else(|| { let h = hash?; - new_hash.get(&h).copied().or_else(|| s.catalog.photos().find(|p| p.content_hash.as_deref() == Some(h.as_str())).map(|p| p.id)) + new_hash + .get(&h) + .copied() + .or_else(|| s.catalog.photos().find(|p| !p.local && p.content_hash.as_deref() == Some(h.as_str())).map(|p| p.id)) }); let trashed = existing.filter(|id| s.catalog.photo(*id).is_some_and(|p| p.deleted)); match trashed { diff --git a/crates/engine/src/tests_import.rs b/crates/engine/src/tests_import.rs index d387d2fe1dd..2252f034ae3 100644 --- a/crates/engine/src/tests_import.rs +++ b/crates/engine/src/tests_import.rs @@ -30,6 +30,134 @@ fn ids(v: &Value, key: &str) -> usize { v[key].as_array().map(Vec::len).unwrap_or(0) } +#[test] +fn local_twin_does_not_block_reimport_after_permanent_delete_and_restart() { + use lightcraft_catalog::PhotoId; + for mode in ["add", "copy"] { + let src = temp_dir(&format!("local-twin-src-{mode}")); + let lib = temp_dir(&format!("local-twin-lib-{mode}")); + let original = src.join("original.png"); + let twin = src.join("browsed.png"); + write_png(&original, 71); + std::fs::copy(&original, &twin).unwrap(); + let bytes = std::fs::read(&original).unwrap(); + let mut s = Session::new().with_fs(); + s.open_library(&lib, false).unwrap(); + let r = s.execute("library.import", &json!({"paths": [original], "mode": mode})).unwrap(); + let old = r["imported"][0].as_u64().unwrap(); + let r = s.execute("library.import", &json!({"paths": [twin], "local": true})).unwrap(); + let local = PhotoId(r["imported"][0].as_u64().unwrap()); + let before = s.catalog.photo(local).unwrap().clone(); + s.execute("photo.delete", &json!({"ids": [old]})).unwrap(); + s.execute("library.source", &json!({"kind": "recentlyDeleted"})).unwrap(); + s.execute("library.select", &json!({"ids": [old]})).unwrap(); + s.execute("photo.deletePermanently", &json!({"ids": [old]})).unwrap(); + s.close_library().unwrap(); + s.open_library(&lib, false).unwrap(); + let scan = s.execute("library.importPreview", &json!({"paths": [original]})).unwrap(); + assert_eq!(scan["duplicates"], 0, "a browsed copy is not a library duplicate: {scan}"); + let r = s.execute("library.import", &json!({"paths": [original], "mode": mode})).unwrap(); + assert_eq!(ids(&r, "imported"), 1, "{r}"); + assert_eq!(ids(&r, "restored"), 0); + assert_eq!(ids(&r, "duplicates"), 0); + assert_ne!(r["imported"][0], old); + assert_eq!(s.catalog.photo(local).unwrap(), &before, "leave the other path's browse record alone"); + assert_eq!(s.catalog.photos().filter(|p| p.in_library()).count(), 1); + let scan = s.execute("library.importPreview", &json!({"paths": [original]})).unwrap(); + assert_eq!(scan["duplicates"], 1, "the actual library photo still counts"); + assert_eq!(std::fs::read(&original).unwrap(), bytes); + assert_eq!(std::fs::read(&twin).unwrap(), bytes); + s.close_library().unwrap(); + let _ = std::fs::remove_dir_all(src); + let _ = std::fs::remove_dir_all(lib); + } +} + +#[test] +fn local_twin_import_matches_the_library_photo_before_promoting() { + use lightcraft_catalog::PhotoId; + let src = temp_dir("local-twin-priority"); + let original = src.join("original.png"); + let twin = src.join("browsed.png"); + write_png(&original, 72); + std::fs::copy(&original, &twin).unwrap(); + let mut s = Session::new().with_fs(); + let r = s.execute("library.import", &json!({"paths": [original]})).unwrap(); + let id = r["imported"][0].as_u64().unwrap(); + s.execute("photo.rate", &json!({"ids": [id], "rating": 5})).unwrap(); + let r = s.execute("library.import", &json!({"paths": [twin], "local": true})).unwrap(); + let local = PhotoId(r["imported"][0].as_u64().unwrap()); + let scan = s.execute("library.importPreview", &json!({"paths": [twin]})).unwrap(); + assert_eq!(scan["duplicates"], 1, "{scan}"); + assert_eq!(scan["candidates"][0]["existing"], id); + let r = s.execute("library.import", &json!({"paths": [twin]})).unwrap(); + assert_eq!(ids(&r, "imported"), 0); + assert_eq!(r["duplicates"][0]["existing"], id); + s.execute("photo.delete", &json!({"ids": [id]})).unwrap(); + let r = s.execute("library.import", &json!({"paths": [twin], "onDeleted": "skip"})).unwrap(); + assert_eq!(ids(&r, "imported"), 0, "skip must not promote the local twin: {r}"); + assert_eq!(r["duplicates"][0]["existingDeleted"], true); + let r = s.execute("library.import", &json!({"paths": [twin]})).unwrap(); + assert_eq!(r["restored"], json!([id])); + assert!(s.catalog.photo(local).unwrap().local); + assert_eq!(s.catalog.photo(PhotoId(id)).unwrap().rating, 5); + let _ = std::fs::remove_dir_all(src); +} + +#[test] +fn local_twins_are_deduplicated_when_promoted_in_one_import() { + let src = temp_dir("local-twin-batch"); + let a = src.join("a.png"); + let b = src.join("b.png"); + let c = src.join("c.png"); + write_png(&a, 73); + std::fs::copy(&a, &b).unwrap(); + std::fs::copy(&a, &c).unwrap(); + let mut s = Session::new().with_fs(); + let r = s.execute("library.import", &json!({"paths": [a, b], "local": true})).unwrap(); + assert_eq!(ids(&r, "imported"), 2, "browsing shows both paths: {r}"); + let scan = s.execute("library.importPreview", &json!({"paths": [a, b, c]})).unwrap(); + assert_eq!(scan["duplicates"], 2, "only one candidate per content: {scan}"); + let r = s.execute("library.import", &json!({"paths": [a, b, c]})).unwrap(); + assert_eq!(ids(&r, "imported"), 1, "{r}"); + assert_eq!(ids(&r, "duplicates"), 2); + assert_eq!(s.catalog.photos().filter(|p| p.in_library()).count(), 1); + assert_eq!(s.catalog.photos().filter(|p| p.local).count(), 1); + s.execute("edit.undo", &json!({})).unwrap(); + assert_eq!(s.catalog.photos().filter(|p| p.in_library()).count(), 0); + assert_eq!(s.catalog.photos().filter(|p| p.local).count(), 2); + let _ = std::fs::remove_dir_all(src); +} + +#[test] +fn local_twin_promotion_counts_across_background_import_batches() { + use crate::import::{ImportJob, ImportOptions, commit_prepared}; + let src = temp_dir("local-twin-worker"); + let a = src.join("a.png"); + let b = src.join("b.png"); + let c = src.join("c.png"); + write_png(&a, 74); + std::fs::copy(&a, &b).unwrap(); + std::fs::copy(&a, &c).unwrap(); + let mut s = Session::new().with_fs(); + let r = s.execute("library.import", &json!({"paths": [a, b], "local": true})).unwrap(); + let id = r["imported"][0].as_u64().unwrap(); + s.execute("photo.rate", &json!({"ids": [id], "rating": 4})).unwrap(); + let mut job = ImportJob::new(&mut s, ImportOptions::default()).unwrap(); + let cancel = std::sync::atomic::AtomicBool::new(false); + let first = job.prepare(&[a.to_string_lossy().to_string()], &cancel); + let report = commit_prepared(&mut s, &job.opts, job.now(), first).unwrap(); + assert_eq!(report.imported, [id], "promote the existing record with its edits"); + assert_eq!(s.catalog.photo(lightcraft_catalog::PhotoId(id)).unwrap().rating, 4); + let next = job.prepare(&[b.to_string_lossy().to_string(), c.to_string_lossy().to_string()], &cancel); + let report = commit_prepared(&mut s, &job.opts, job.now(), next).unwrap(); + assert!(report.imported.is_empty()); + assert_eq!(report.duplicates.len(), 2); + assert!(report.duplicates.iter().all(|d| d.existing == Some(id))); + assert_eq!(s.catalog.photos().filter(|p| p.in_library()).count(), 1); + let _ = std::fs::remove_dir_all(src); +} + #[test] fn recursive_import_with_duplicates() { let src = temp_dir("src"); diff --git a/docs/parity-checklist.md b/docs/parity-checklist.md index 0ab638f35d5..28aecd431d7 100644 --- a/docs/parity-checklist.md +++ b/docs/parity-checklist.md @@ -1,6 +1,6 @@ # Lightroom parity checklist -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-11 · **Change:** minor (LR-MERGE-HDR: merges of raws without colour matrices keep their colour (#965); rapid slider entry; LR-PROF-CAMERACOLOR: tone curve from black-and-white camera JPEGs; LR-VIEW-ZOOM, LR-VIEW-COMPARE: Compare and Reference zoom windows; LR-IMP-FORMATS, LR-IMP-CAMERA-COVERAGE: EOS R5 Mark II CR3 decodes; renamed from `parity.md`; Top gaps moved to gaps.md, shortcut notes to ui-parity.md; filesystem history retry/Undo/Redo evidence; renamed from `parity.md`; Top gaps moved to gaps.md, shortcut notes to ui-parity.md) · **Target:** Adobe Lightroom Classic 15.5 / Lightroom 9.6 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-13 · **Change:** minor (Local browse records no longer block import; LR-MERGE-HDR: merges of raws without colour matrices keep their colour (#965); rapid slider entry; LR-PROF-CAMERACOLOR: tone curve from black-and-white camera JPEGs; LR-VIEW-ZOOM, LR-VIEW-COMPARE: Compare and Reference zoom windows; LR-IMP-FORMATS, LR-IMP-CAMERA-COVERAGE: EOS R5 Mark II CR3 decodes; renamed from `parity.md`; Top gaps moved to gaps.md, shortcut notes to ui-parity.md; filesystem history retry/Undo/Redo evidence; renamed from `parity.md`; Top gaps moved to gaps.md, shortcut notes to ui-parity.md) · **Target:** Adobe Lightroom Classic 15.5 / Lightroom 9.6 The measured, row-by-row checklist: one row per Lightroom feature, menu item and shortcut, what we have and what is missing. It answers *does the feature exist?* (feature breadth). Whether it is good @@ -71,7 +71,7 @@ differences and missing bindings are in [ui-parity.md](ui-parity.md#shortcuts). |---|---|---|---|---|---| | LR-IMP-ADD-DIALOG | Add photos/folders | P0 | ✅ | `cmd:file.addPhotos`, `cmd:library.importPreview`, `cmd:library.import` (`mode`, `album`, `albumName`, `preset`, `keywords`), `crates/ui-egui/src/import.rs` | review dialog (Import Photos): the scanned source (a scanned folder is not added to Local), candidate grid with thumbnails and checkboxes (duplicates by path/content marked and unchecked; with several file types, a toggle per type checks or unchecks all of its files, e.g. the raws without the JPEGs beside them, issue #344) filling a resizable window (issue #337), transfer mode (add in place / copy / move, each explained), album (existing/new), preset, keywords; the folder scan runs on a background thread, shown in the activity stack with ✕ to cancel (a NAS folder no longer freezes the window); the import itself (probing, sidecars, copying / moving files) runs on a worker thread and its batches join the library between frames, shown in the activity stack with ✕ to cancel; one undo step. Drag-and-drop goes through the same background import. File dialogs list the importable extensions (`lightcraft_engine::import::EXTENSIONS`) in any letter case on Linux, where GTK and the desktop portal match globs case-sensitively (`apps/lightcraft/src/dialog_filter.rs`, issue #342; 0.4.0 hid `.JPG` / `.NEF`, issue #705). The folder scan, Browse Folder, drag-and-drop and the sidecar lookup match extensions in any case (`crates/engine/src/tests_import.rs`) | | LR-IMP-DRAGDROP | Drop files/folders to import | P0 | ✅ | `crates/ui-egui/src/lib.rs` (dropped files → `cmd:library.import`) | dropping onto a specific album not supported | -| LR-IMP-DUPES | Skip duplicates by content | P1 | ✅ | `crates/engine/src/import.rs`, `crates/engine/src/tests_import.rs` | | +| LR-IMP-DUPES | Skip duplicates by content | P1 | ✅ | `crates/engine/src/import.rs`, `crates/engine/src/tests_import.rs` | Only library records (including Recently Deleted) count as existing content; a copy merely browsed in Local does not block importing another path. A browsed file at the same path is offered for import and promoted with its edits, unless a library match or an earlier promotion already has its content. The review and background import batches deduplicate promoted files too. | | LR-IMP-DEVICE | Import from camera/card | P1 | ✅ | `cmd:library.devices`, `cmd:file.addFromDevice`, `crates/engine/src/devices.rs` | mounted volumes with a DCIM folder (macOS /Volumes, Linux /media and /run/media, Windows drive letters); File → Import from Device → the import review, copying into the library by default; menus serve the last scan and rescan in the background (never blocking a frame; hot-plugs repaint); copies are verified (a new file, synced, checked against the content hash the scan computed — byte compare with the source when there is none, issue #134; a bad copy is removed and reported as failed, never counted as imported; taken names get -1, -2…, never replaced — issue #96, `crates/engine/src/import_move.rs`); no PTP/MTP (cameras that don't mount as a disk) | | LR-IMP-AUTO | Watched-folder auto import | P2 | ✅ | `cmd:library.autoImport`, `cmd:library.autoImportScan`, `crates/ui-egui/src/panels/settings.rs` | Settings ▸ Import ▸ Auto Import: a watched folder whose new photos are added (in place or copied) once complete, into an optional album; scanned every 3 s | | LR-IMP-PRESET | Preset on import | P2 | ✅ | `cmd:library.import` (`preset`) | chosen in the import review; one History entry | @@ -788,6 +788,7 @@ Latin stays upright. Advanced Japanese composition remains open. Regression cove | Date | Change | Summary | |---|---|---| +| 2026-10-12 | patch | LR-IMP-DUPES: distinguish Local browsing from library membership in import review and content matching; regression tests cover permanent deletion/restart, restoration, promotion, worker batches and undo. Status and summary counts unchanged. | | 2026-10-11 | minor | LR-MERGE-HDR: HDR and panorama merges of raws without colour matrices (CR2, NEF, ARW…) write the colour they open with as DNG tags (issue #965); status unchanged | | 2026-10-12 | minor | LR-META-XMP: Read Metadata from File no longer auto-writes the sidecar it has just read, which added `lc:settings` and hid another app's later `crs:` edits (#934); status unchanged (✅) | | 2026-10-12 | patch | LR-IMP-CAMERA-COVERAGE: CR2 white level from the active area, not the saturated masked rows above it (issue #1272); status unchanged (🟡) |