From b6b4662dc854b9c15d885e0412b1a46f921fd97b Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Fri, 30 Oct 2026 23:10:29 -0500 Subject: [PATCH 1/4] fix(lightroom): a content duplicate keeps what its XMP sidecar says (#2589) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A content duplicate's new record took its file's metadata and capture time but not its XMP sidecar: a file import merges the sidecar (caption, copyright, rating, develop…), so a field only the sidecar states was lost whenever the catalog's XMP didn't restate it. The import never read a duplicate's sidecar: the batch's sidecar lookup (#2954) passed over every file whose content the library has, the planning loop skipped the read when the file has a capture time, and revalidate_add dropped the parsed sidecar. A duplicate's sidecar is now looked up and read as any file's (in the same batched lookup), FileFacts carries it, and one function (`start_from_file`: the file's metadata and capture time, the import defaults, then the sidecar) starts both a file import's record and the Lightroom duplicate's; the catalog's metadata and settings still apply on top. FileFacts also carries the file's own orientation, so #2674's rule (the file's orientation, when it states one, turns the sidecar's stored-frame regions upright) holds for both, after the empty-sidecar check: an orientation alone is not a sidecar that said something. Test: a content duplicate whose file has a capture time and whose sidecar states a caption and copyright keeps both through the Lightroom job. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/import.rs | 1 + crates/engine/src/import/commit.rs | 31 ++++++++++----- crates/engine/src/import/job.rs | 27 ++++--------- crates/engine/src/import/types.rs | 5 +++ crates/engine/src/lightroom_catalog.rs | 36 +++++++++-------- crates/engine/src/lightroom_catalog/tests.rs | 42 ++++++++++++++++++++ 6 files changed, 96 insertions(+), 46 deletions(-) diff --git a/crates/engine/src/import.rs b/crates/engine/src/import.rs index 96ce93d0c17..fda577f0c06 100644 --- a/crates/engine/src/import.rs +++ b/crates/engine/src/import.rs @@ -42,6 +42,7 @@ mod job; mod scan; mod types; +pub(crate) use commit::start_from_file; #[cfg(not(target_arch = "wasm32"))] pub(crate) use commit::workers; pub use commit::{batch_size, civil, commit_prepared, system_clock}; diff --git a/crates/engine/src/import/commit.rs b/crates/engine/src/import/commit.rs index 016ce013932..1a142972497 100644 --- a/crates/engine/src/import/commit.rs +++ b/crates/engine/src/import/commit.rs @@ -1,7 +1,7 @@ use super::defaults::apply_import_defaults; use super::files::copy_into; use super::job::{Prepared, PreparedItem, ReadyFile}; -use super::types::{Duplicate, ImportOptions, ImportReport, Kept, Moved, OnDeleted}; +use super::types::{Duplicate, FileFacts, ImportOptions, ImportReport, Kept, Moved, OnDeleted}; use super::{HashMap, Op, Path, PathBuf, Photo, PhotoId, ProbeInfo, Session, Source}; /// A batch's items in file order while its transfers are pending ([`ImportJob::run_plan`]). @@ -181,6 +181,22 @@ fn trashed_file_is_gone(cat: &lightcraft_catalog::Catalog, id: PhotoId) -> bool } } +/// What a new record takes from its file, as a file import gives it: the file's metadata and +/// capture time, the import defaults, then its XMP sidecar. Returns the sidecar when it said +/// anything (labels read through the library's label names). +pub(crate) fn start_from_file(s: &Session, p: &mut Photo, file: FileFacts, now: &str) -> Option { + let FileFacts { meta, captured, orientation, sidecar } = file; + p.meta = meta; + p.captured = captured; + apply_import_defaults(s, p); + let sc = sidecar.map(|sc| sc.resolve_label(&s.catalog)).filter(|sc| *sc != crate::sidecar::SidecarData::default())?; + // the photo is upright as the file's own orientation turns it, when the file states one + // (after the check above: an orientation alone isn't a sidecar that said something) + let sc = crate::sidecar::SidecarData { orientation: orientation.or(sc.orientation), ..sc }; + crate::sidecar::merge_into(p, &sc, now); + Some(sc) +} + /// The catalog half of an import: add the photos [`ImportJob::prepare`] readied (one undoable op; /// a Move's sources are removed once it is saved). On a failed commit a Move's placed files are /// taken back. @@ -252,25 +268,20 @@ pub fn commit_prepared(s: &mut Session, opts: &ImportOptions, now: &str, prepare purge.push(*old); } } - let sidecar = sidecar.map(|sc| sc.resolve_label(&s.catalog)).filter(|sc| *sc != crate::sidecar::SidecarData::default()); - // the photo is upright as the file's own orientation turns it, when the file states one - let sidecar = sidecar.map(|sc| crate::sidecar::SidecarData { orientation: info.orientation.or(sc.orientation), ..sc }); // a copy is catalogued under its new name let name = Path::new(&stored).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| path.clone()); let mut p = Photo::new(id, Source::File { path: stored }, &name, &info.format, info.width, info.height, &now); p.kind = info.kind; p.file_size = info.file_size; - p.captured = info.captured; - p.meta = info.meta; p.as_shot_wb = info.as_shot_wb; p.content_hash = info.content_hash; p.embedded_lens = info.embedded_lens; p.set_preview_only(info.preview_only.clone()); - apply_import_defaults(s, &mut p); - if let Some(sc) = &sidecar { - crate::sidecar::merge_into(&mut p, sc, &now); + if let Some(sc) = + start_from_file(s, &mut p, FileFacts { meta: info.meta, captured: info.captured, orientation: info.orientation, sidecar }, &now) + { report.sidecars += 1; - report.warnings.extend(sc.warnings.iter().cloned().map(|mut warning| { + report.warnings.extend(sc.warnings.into_iter().map(|mut warning| { warning.path = path.clone(); warning })); diff --git a/crates/engine/src/import/job.rs b/crates/engine/src/import/job.rs index ca906c61922..4c120acc19a 100644 --- a/crates/engine/src/import/job.rs +++ b/crates/engine/src/import/job.rs @@ -144,7 +144,7 @@ impl Prepared { existing: Some(existing), reason: if existing_path.is_some() { "path" } else { "content" }, hash: info.content_hash, - file: Some(Box::new(FileFacts { meta: info.meta, captured: info.captured })), + file: Some(Box::new(FileFacts { meta: info.meta, captured: info.captured, orientation: info.orientation, sidecar })), }); } else { items.push(PreparedItem::Ready(Box::new(ReadyFile { path, stored, info, sidecar, placed }))); @@ -298,17 +298,10 @@ impl ImportJob { // The sidecar of every file that was probed, looked up for the batch at once: each is a few // names asked of the disk, which on a network share made a folder of thousands of photos wait // a round trip per name, one name after another. + // (a file whose content the library has is read too: a Lightroom import makes it a new record + // from its file and sidecar, #2589) let asked: Vec<&str> = todo.iter().zip(&probed).filter(|(_, p)| p.is_ok()).map(|(f, _)| f.as_str()).collect(); - // (a file whose content the library has is skipped below as a duplicate: it asks the disk nothing) - let known: std::collections::HashSet<&str> = todo - .iter() - .zip(&probed) - .filter(|(_, p)| !opts.local && p.as_ref().is_ok_and(|i| i.content_hash.as_ref().is_some_and(|h| self.by_hash.contains_key(h)))) - .map(|(f, _)| f.as_str()) - .collect(); - let mut found = - sidecars_of(&asked, cancel, |f| if known.contains(f) { None } else { crate::sidecar::find_sidecar_for_import(&stems, self.naming, f) }) - .into_iter(); + let mut found = sidecars_of(&asked, cancel, |f| crate::sidecar::find_sidecar_for_import(&stems, self.naming, f)).into_iter(); // Files are planned one by one, in order (duplicates, sidecars, names, folders); their // copies or moves — the slow part, reading and writing every byte and verifying it — are // collected and run on several threads (issue #367), then their outcomes take their places. @@ -338,17 +331,13 @@ impl ImportJob { break; } } - // same bytes as a library photo: skipped below, once its capture time is known + // same bytes as a library photo: skipped below, once it is read as a file import reads it let duplicate_of = info.content_hash.as_ref().filter(|_| !opts.local).and_then(|h| self.by_hash.get(h).map(|existing| (h.clone(), *existing))); // the XMP sidecar (or a raw's embedded XMP), read before copying: its capture time files - // and names the copy when the file itself has none (a duplicate needs only that time) + // and names the copy when the file itself has none let raw = info.kind == MediaKind::Raw; - let packet = if duplicate_of.is_some() && info.captured.is_some() { - None - } else { - found_sidecar.and_then(|f| std::fs::read_to_string(f).ok()).or_else(|| info.xmp.clone().filter(|_| raw)) - }; + let packet = found_sidecar.and_then(|f| std::fs::read_to_string(f).ok()).or_else(|| info.xmp.clone().filter(|_| raw)); let target = crate::crs::Target::for_file(info.kind, &info.format, info.preview_only.is_some(), &info.meta.camera); let sidecar = packet.and_then(|x| match crate::sidecar::parse_sidecar(&x, target) { Ok(sc) => Some(sc), @@ -371,7 +360,7 @@ impl ImportJob { existing, reason: "content", hash: Some(hash), - file: Some(Box::new(FileFacts { meta: info.meta, captured: info.captured })), + file: Some(Box::new(FileFacts { meta: info.meta, captured: info.captured, orientation: info.orientation, sidecar })), })); continue; } diff --git a/crates/engine/src/import/types.rs b/crates/engine/src/import/types.rs index aab839931bc..29641105840 100644 --- a/crates/engine/src/import/types.rs +++ b/crates/engine/src/import/types.rs @@ -171,6 +171,11 @@ pub struct FileFacts { /// The capture time a file import gives it: the file's, else its XMP sidecar's, else the /// file's modification time. pub captured: Option, + /// The file's own orientation, when it states one ([`crate::media::ProbeInfo::orientation`]): it turns what its + /// sidecar's regions were drawn on, the photo as stored, upright (#2674). + pub orientation: Option, + /// Its XMP sidecar (or a raw's embedded XMP), which a file import merges into the record. + pub sidecar: Option, } #[derive(Clone, Debug, Default, Serialize)] diff --git a/crates/engine/src/lightroom_catalog.rs b/crates/engine/src/lightroom_catalog.rs index 9a84b37ddbe..bd8057cf3f7 100644 --- a/crates/engine/src/lightroom_catalog.rs +++ b/crates/engine/src/lightroom_catalog.rs @@ -1302,8 +1302,8 @@ pub(crate) fn apply_prepared(s: &mut crate::Session, mut data: CatalogImport, co // The new record takes only what the bytes determine from the photo with the same // bytes, never that photo's own state: not its Recently Deleted (#2589), virtual copy, // edits, history, versions, ratings, capture time or the metadata a person writes. Its - // metadata and capture time are what its own file gives it, as a file import reads them - // (copyright, creator…); the catalog's capture time, when it has one, applies on top. + // metadata and capture time are what its own file and XMP sidecar give it, as a file + // import reads them (copyright, caption, creator…); the catalog's apply on top. let name = Path::new(&src.path).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| src.path.clone()); let mut p = Photo::new( s.catalog.alloc_photo_id(), @@ -1322,25 +1322,27 @@ pub(crate) fn apply_prepared(s: &mut crate::Session, mut data: CatalogImport, co p.embedded_lens = duplicate.embedded_lens; p.preview_only = duplicate.preview_only.clone(); p.preview_checked = duplicate.preview_checked.clone(); - if let Some(file) = file { - p.meta = file.meta.clone(); - p.captured = file.captured.clone(); - } else { + let file = file.cloned().unwrap_or_else(|| { // (the file wasn't read: only the exposure description, which no command edits; // not GPS, which placing a photo on the map does, nor the capture time, which // Edit Capture Time does) let m = &duplicate.meta; - p.meta.camera = m.camera.clone(); - p.meta.lens = m.lens.clone(); - p.meta.focal_mm = m.focal_mm; - p.meta.aperture = m.aperture; - p.meta.shutter = m.shutter.clone(); - p.meta.iso = m.iso; - p.meta.focus = m.focus.clone(); - } - // the settings a file import gives it (as-shot white balance, embedded lens - // corrections, the default preset); the catalog's settings apply on top - crate::import::apply_import_defaults(s, &mut p); + let meta = lightcraft_catalog::Meta { + camera: m.camera.clone(), + lens: m.lens.clone(), + focal_mm: m.focal_mm, + aperture: m.aperture, + shutter: m.shutter.clone(), + iso: m.iso, + focus: m.focus.clone(), + ..Default::default() + }; + crate::import::FileFacts { meta, ..Default::default() } + }); + // as a file import of it: its file's metadata and capture time, the import defaults + // (as-shot white balance, embedded lens corrections, the default preset), its XMP + // sidecar; the catalog's settings and metadata apply on top + crate::import::start_from_file(s, &mut p, file, &now); p } else { let id = s.catalog.alloc_photo_id(); diff --git a/crates/engine/src/lightroom_catalog/tests.rs b/crates/engine/src/lightroom_catalog/tests.rs index 837db27c906..cf0b2ab6798 100644 --- a/crates/engine/src/lightroom_catalog/tests.rs +++ b/crates/engine/src/lightroom_catalog/tests.rs @@ -214,6 +214,8 @@ fn a_content_duplicate_of_a_trashed_photo_or_virtual_copy_is_a_new_live_photo() file: Some(Box::new(crate::import::FileFacts { meta: lightcraft_catalog::Meta { camera: "Same Camera".into(), ..Default::default() }, captured: None, + orientation: None, + sidecar: None, })), }], ..Default::default() @@ -356,6 +358,46 @@ fn a_content_duplicate_keeps_the_rights_metadata_and_capture_time_in_its_file() assert_eq!(new.captured.as_deref(), Some("2024-05-06T07:08:09"), "{r}"); std::fs::remove_dir_all(dir).unwrap(); } +/// A content duplicate's XMP sidecar goes into its new record as it does for a file import of it +/// (caption, copyright…) when the catalog doesn't restate them, also when its file has a capture +/// time of its own (#2589). +#[test] +fn a_content_duplicate_keeps_what_its_sidecar_says() { + let dir = std::env::temp_dir().join(format!("lightcraft-lrcat-duplicate-sidecar-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(dir.join("Photos")).unwrap(); + let source = dir.join("source.lrcat"); + std::fs::write(dir.join("Photos/test.ARW"), b"same bytes").unwrap(); + std::fs::write( + dir.join("Photos/test.xmp"), + r#" + +Harbour at dawn +© Sidecar Author +"#, + ) + .unwrap(); + std::fs::write(&source, native_catalog(&photo_tables((dir.to_str().unwrap(), "")))).unwrap(); + let mut s = crate::Session::new(); + let other = library_photo(&mut s, "/elsewhere/a.ARW", |p| p.meta.caption = "Other photo's caption".into()); + s.media.file_probe = Some(Arc::new(|_| { + Ok(crate::media::ProbeInfo { + format: "ARW".into(), + width: 20, + height: 10, + content_hash: Some("same-bytes".into()), + captured: Some("2024-05-06T07:08:09".into()), + ..Default::default() + }) + })); + let mut job = crate::lightroom_job::LightroomJob::new(&mut s, source, false).unwrap(); + let prepared = job.prepare(&AtomicBool::new(false)).unwrap(); + let r = crate::lightroom_job::commit_prepared(&mut s, prepared).unwrap().report; + let new = s.catalog.photos().find(|p| p.id != other).unwrap_or_else(|| panic!("no new record: {r}")); + assert_eq!((new.meta.caption.as_str(), new.meta.copyright.as_str()), ("Harbour at dawn", "© Sidecar Author"), "{r}"); + assert_eq!(new.captured.as_deref(), Some("2024-05-06T07:08:09"), "the file's own capture time: {r}"); + std::fs::remove_dir_all(dir).unwrap(); +} /// A catalog photo matched (by path) to a library photo in Recently Deleted leaves it there, as /// the import was set up to do (a Lightroom import doesn't restore, see `OnDeleted`), and the report /// counts it so the photos don't go missing unexplained (#2589). A new virtual copy of it is From 752065dd92e5f70addaead3576b60ad0db88ea6b Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Mon, 2 Nov 2026 09:32:57 -0600 Subject: [PATCH 2/4] fix(lightroom): only the Lightroom import reads a content duplicate's sidecar (#2589) The previous commit gave every import's content duplicates a sidecar lookup, so that the Lightroom import could start the duplicate's new record from its sidecar. Only the Lightroom import makes such a record; any other import skips the duplicate, and #2954 had it ask the disk nothing for it (each sidecar lookup is a few names, a round trip apiece on a network share). ImportOptions::read_duplicates says the import makes a record of its duplicates; only LightroomJob sets it. Without it, a duplicate's sidecar is not looked up and, when the file has its own capture time, its embedded XMP is not parsed, as before the previous commit. Test: the same duplicate beside the same sidecar reaches the report without its sidecar from a plain import and with it when read_duplicates is set. With the skip removed, the plain import's duplicate carries the sidecar's caption and the test fails. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/library/import.rs | 2 ++ crates/engine/src/import/job.rs | 26 ++++++++++---- crates/engine/src/import/types.rs | 4 +++ crates/engine/src/lightroom_catalog/tests.rs | 38 ++++++++++++++++++++ crates/engine/src/lightroom_job.rs | 6 ++-- 5 files changed, 68 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/cmd/library/import.rs b/crates/engine/src/cmd/library/import.rs index 188dc6ae995..61a7ccefdaa 100644 --- a/crates/engine/src/cmd/library/import.rs +++ b/crates/engine/src/cmd/library/import.rs @@ -82,6 +82,8 @@ pub fn import_params(s: &Session, p: &Value) -> Result { metadata_preset, convert_dng: bool_or(p, "dng", false), local: bool_or(p, "local", false), + // (only the Lightroom import makes records of duplicates) + read_duplicates: false, recipe: crate::cmd::recipe_param(p, "library.import")?, }; let album_name = str_param(p, "albumName").map(str::trim).filter(|n| !n.is_empty()).map(str::to_string); diff --git a/crates/engine/src/import/job.rs b/crates/engine/src/import/job.rs index 4c120acc19a..847942b3cc5 100644 --- a/crates/engine/src/import/job.rs +++ b/crates/engine/src/import/job.rs @@ -298,10 +298,19 @@ impl ImportJob { // The sidecar of every file that was probed, looked up for the batch at once: each is a few // names asked of the disk, which on a network share made a folder of thousands of photos wait // a round trip per name, one name after another. - // (a file whose content the library has is read too: a Lightroom import makes it a new record - // from its file and sidecar, #2589) let asked: Vec<&str> = todo.iter().zip(&probed).filter(|(_, p)| p.is_ok()).map(|(f, _)| f.as_str()).collect(); - let mut found = sidecars_of(&asked, cancel, |f| crate::sidecar::find_sidecar_for_import(&stems, self.naming, f)).into_iter(); + // (a file whose content the library has is skipped below as a duplicate: it asks the disk + // nothing, unless the import makes a record of it from its file and sidecar, #2589) + let skipped = !opts.local && !opts.read_duplicates; + let known: std::collections::HashSet<&str> = todo + .iter() + .zip(&probed) + .filter(|(_, p)| skipped && p.as_ref().is_ok_and(|i| i.content_hash.as_ref().is_some_and(|h| self.by_hash.contains_key(h)))) + .map(|(f, _)| f.as_str()) + .collect(); + let mut found = + sidecars_of(&asked, cancel, |f| if known.contains(f) { None } else { crate::sidecar::find_sidecar_for_import(&stems, self.naming, f) }) + .into_iter(); // Files are planned one by one, in order (duplicates, sidecars, names, folders); their // copies or moves — the slow part, reading and writing every byte and verifying it — are // collected and run on several threads (issue #367), then their outcomes take their places. @@ -331,13 +340,18 @@ impl ImportJob { break; } } - // same bytes as a library photo: skipped below, once it is read as a file import reads it + // same bytes as a library photo: skipped below, once its capture time is known (and, + // when the import makes a record of it, once it is read as a file import reads it) let duplicate_of = info.content_hash.as_ref().filter(|_| !opts.local).and_then(|h| self.by_hash.get(h).map(|existing| (h.clone(), *existing))); // the XMP sidecar (or a raw's embedded XMP), read before copying: its capture time files - // and names the copy when the file itself has none + // and names the copy when the file itself has none (a skipped duplicate needs only that time) let raw = info.kind == MediaKind::Raw; - let packet = found_sidecar.and_then(|f| std::fs::read_to_string(f).ok()).or_else(|| info.xmp.clone().filter(|_| raw)); + let packet = if duplicate_of.is_some() && info.captured.is_some() && !opts.read_duplicates { + None + } else { + found_sidecar.and_then(|f| std::fs::read_to_string(f).ok()).or_else(|| info.xmp.clone().filter(|_| raw)) + }; let target = crate::crs::Target::for_file(info.kind, &info.format, info.preview_only.is_some(), &info.meta.camera); let sidecar = packet.and_then(|x| match crate::sidecar::parse_sidecar(&x, target) { Ok(sc) => Some(sc), diff --git a/crates/engine/src/import/types.rs b/crates/engine/src/import/types.rs index 29641105840..298ff987464 100644 --- a/crates/engine/src/import/types.rs +++ b/crates/engine/src/import/types.rs @@ -67,6 +67,10 @@ pub struct ImportOptions { pub metadata_preset: Option, /// Copy: raws are copied as DNG (Copy as DNG). pub convert_dng: bool, + /// A duplicate by content is read as a new file is, its XMP sidecar included: the Lightroom + /// import makes a record of its own from it ([`Duplicate::file`], #2589). Any other import + /// skips it without looking up its sidecar (a round trip per name on a network share, #2954). + pub read_duplicates: bool, /// A batch-edit recipe: develop control id / value pairs (e.g. `light.exposure` → 0.5), /// applied to every imported photo *after* its XMP sidecar, camera default and import preset, /// as one more History step (`Import adjustments`). Only the named controls change; the recipe diff --git a/crates/engine/src/lightroom_catalog/tests.rs b/crates/engine/src/lightroom_catalog/tests.rs index cf0b2ab6798..c5621821beb 100644 --- a/crates/engine/src/lightroom_catalog/tests.rs +++ b/crates/engine/src/lightroom_catalog/tests.rs @@ -398,6 +398,44 @@ fn a_content_duplicate_keeps_what_its_sidecar_says() { assert_eq!(new.captured.as_deref(), Some("2024-05-06T07:08:09"), "the file's own capture time: {r}"); std::fs::remove_dir_all(dir).unwrap(); } +/// Only the Lightroom import reads a content duplicate's XMP sidecar: any other import skips the +/// duplicate without looking its sidecar up (#2954), so the same file beside the same sidecar +/// reaches the report with no sidecar unless the import makes a record of it (#2589). +#[test] +fn only_an_import_that_makes_a_record_of_a_content_duplicate_reads_its_sidecar() { + let dir = std::env::temp_dir().join(format!("lightcraft-lrcat-duplicate-skip-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let file = dir.join("test.ARW"); + std::fs::write(&file, b"same bytes").unwrap(); + std::fs::write( + dir.join("test.xmp"), + r#" + +Harbour at dawn +"#, + ) + .unwrap(); + let paths = [file.to_string_lossy().into_owned()]; + for read_duplicates in [false, true] { + let mut s = crate::Session::new(); + let other = library_photo(&mut s, "/elsewhere/a.ARW", |p| p.meta.caption = "Other photo's caption".into()); + s.media.file_probe = + Some(Arc::new(|_| Ok(crate::media::ProbeInfo { format: "ARW".into(), content_hash: Some("same-bytes".into()), ..Default::default() }))); + let opts = crate::import::ImportOptions { read_duplicates, ..Default::default() }; + let r = crate::import::import_with(&mut s, &paths, &opts).unwrap(); + assert!(r.imported.is_empty() && s.catalog.photos().count() == 1, "{read_duplicates}: {r:?}"); + let [d] = r.duplicates.as_slice() else { panic!("{read_duplicates}: {r:?}") }; + assert_eq!((d.reason, d.existing), ("content", Some(other.0)), "{read_duplicates}"); + let caption = d.file.as_ref().and_then(|f| f.sidecar.as_ref()).and_then(|sc| sc.caption.clone()); + if read_duplicates { + assert_eq!(caption.as_deref(), Some("Harbour at dawn"), "the Lightroom import reads it: {r:?}"); + } else { + assert_eq!(caption, None, "a skipped duplicate's sidecar isn't looked up: {r:?}"); + } + } + std::fs::remove_dir_all(dir).unwrap(); +} /// A catalog photo matched (by path) to a library photo in Recently Deleted leaves it there, as /// the import was set up to do (a Lightroom import doesn't restore, see `OnDeleted`), and the report /// counts it so the photos don't go missing unexplained (#2589). A new virtual copy of it is diff --git a/crates/engine/src/lightroom_job.rs b/crates/engine/src/lightroom_job.rs index defdb850f0e..7a039fe0f1e 100644 --- a/crates/engine/src/lightroom_job.rs +++ b/crates/engine/src/lightroom_job.rs @@ -207,8 +207,10 @@ pub struct LightroomJob { impl LightroomJob { /// Snapshot session state only. This constructor performs no filesystem work. pub fn new(s: &mut crate::Session, path: PathBuf, update_existing: bool) -> crate::Result { - // a photo deleted here stays deleted: the catalog's record is matched, not restored - let opts = ImportOptions { mode: ImportMode::Add, on_deleted: crate::import::OnDeleted::Skip, ..ImportOptions::default() }; + // a photo deleted here stays deleted: the catalog's record is matched, not restored; a + // duplicate by content becomes a record of its own, so its file and sidecar are read (#2589) + let opts = + ImportOptions { mode: ImportMode::Add, on_deleted: crate::import::OnDeleted::Skip, read_duplicates: true, ..ImportOptions::default() }; let import = ImportJob::new(s, opts)?; let archive_dir = s.library.as_ref().filter(|library| library.on_disk).map(|library| library.dir.join("Interop")); let progress = LightroomProgress { From f840d2d7e2328aed6a4b8861ca0ea5ee7abe5408 Mon Sep 17 00:00:00 2001 From: THEtheChad <880190+THEtheChad@users.noreply.github.com> Date: Thu, 5 Nov 2026 04:52:07 -0600 Subject: [PATCH 3/4] test(lightroom): a content duplicate's sidecar regions turn by its file's orientation (#2589) Pins how #2674's rule (#2857) reads through start_from_file, which the file import and a Lightroom content duplicate share: the file's own orientation turns sidecar regions drawn on the photo as stored onto the upright record, also for the duplicate (its size is its file's upright one; the catalog's orientation is a develop rotation on top). And an empty sidecar beside a turned photo stays uncounted: the orientation is given to a sidecar that said something, never makes one. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/lightroom_catalog/tests.rs | 53 ++++++++++++++++++++ crates/engine/src/tests/xmp.rs | 19 +++++++ 2 files changed, 72 insertions(+) diff --git a/crates/engine/src/lightroom_catalog/tests.rs b/crates/engine/src/lightroom_catalog/tests.rs index c5621821beb..ecb6b11481c 100644 --- a/crates/engine/src/lightroom_catalog/tests.rs +++ b/crates/engine/src/lightroom_catalog/tests.rs @@ -398,6 +398,59 @@ fn a_content_duplicate_keeps_what_its_sidecar_says() { assert_eq!(new.captured.as_deref(), Some("2024-05-06T07:08:09"), "the file's own capture time: {r}"); std::fs::remove_dir_all(dir).unwrap(); } +/// A content duplicate's sidecar regions drawn on its photo as stored (its stored size as +/// `AppliedToDimensions`, no `mwg-rs:Rotation`) land on the upright photo, turned by its file's own +/// orientation as a file import of it turns them (#2674): the record's size is its file's upright one, +/// and the catalog's orientation is a develop rotation on top of it (#2589). +#[test] +fn a_content_duplicates_sidecar_regions_are_turned_by_its_files_orientation() { + let dir = std::env::temp_dir().join(format!("lightcraft-lrcat-duplicate-regions-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(dir.join("Photos")).unwrap(); + let source = dir.join("source.lrcat"); + std::fs::write(dir.join("Photos/test.ARW"), b"same bytes").unwrap(); + std::fs::write( + dir.join("Photos/test.xmp"), + r#" + + + + + + + +"#, + ) + .unwrap(); + std::fs::write(&source, native_catalog(&photo_tables((dir.to_str().unwrap(), "")))).unwrap(); + let mut s = crate::Session::new(); + // stored 32 × 24, a quarter turn clockwise upright: 24 × 32 (the size the new record takes) + let other = library_photo(&mut s, "/elsewhere/a.ARW", |p| (p.width, p.height) = (24, 32)); + s.media.file_probe = Some(Arc::new(|_| { + Ok(crate::media::ProbeInfo { + format: "ARW".into(), + width: 24, + height: 32, + content_hash: Some("same-bytes".into()), + captured: Some("2024-05-06T07:08:09".into()), + orientation: Some(lightcraft_geom::Orientation::Rotate90), + ..Default::default() + }) + })); + let mut job = crate::lightroom_job::LightroomJob::new(&mut s, source, false).unwrap(); + let prepared = job.prepare(&AtomicBool::new(false)).unwrap(); + let r = crate::lightroom_job::commit_prepared(&mut s, prepared).unwrap().report; + let new = s.catalog.photos().find(|p| p.id != other).unwrap_or_else(|| panic!("no new record: {r}")); + assert_eq!(new.meta.regions.len(), 1, "{r}"); + let b = new.meta.regions[0].rect; + let got = [b.x0, b.y0, b.x1, b.y1]; + assert!( + got.iter().zip([0.525, 0.2, 0.675, 0.3]).all(|(g, w)| (g - w).abs() < 1e-9), + "the box is where the photo as stored had the face: {got:?}" + ); + std::fs::remove_dir_all(dir).unwrap(); +} /// Only the Lightroom import reads a content duplicate's XMP sidecar: any other import skips the /// duplicate without looking its sidecar up (#2954), so the same file beside the same sidecar /// reaches the report with no sidecar unless the import makes a record of it (#2589). diff --git a/crates/engine/src/tests/xmp.rs b/crates/engine/src/tests/xmp.rs index 006a0a488a1..44aefb16992 100644 --- a/crates/engine/src/tests/xmp.rs +++ b/crates/engine/src/tests/xmp.rs @@ -627,6 +627,25 @@ fn regions_drawn_on_the_photo_as_stored_land_on_the_upright_photo() { let _ = std::fs::remove_dir_all(&src); } +/// A sidecar that says nothing beside a photo whose file states an orientation isn't one the import counts: the +/// file's orientation goes to a sidecar that said something, not into an empty one (#2674, #2589). +#[test] +fn an_empty_sidecar_beside_a_turned_photo_is_not_counted() { + let src = temp_dir("regions-empty-sidecar"); + let file = src.join("p.jpg"); + write_turned_jpeg(&file, None); + std::fs::write( + src.join("p.xmp"), + r#""#, + ) + .unwrap(); + let mut s = Session::new().with_fs(); + let r = s.execute("library.import", &json!({"paths": [file.to_string_lossy()]})).unwrap(); + assert_eq!(r["imported"].as_array().map(Vec::len), Some(1), "{r}"); + assert_eq!(r["sidecars"], 0, "{r}"); + let _ = std::fs::remove_dir_all(&src); +} + /// Review of #2857: a photo whose Exif states no orientation, but whose XMP says `tiff:Orientation=6`, is shown as /// stored (LightCraft turns by the Exif alone), so boxes drawn on its stored frame are right as written: at import /// and when the file's metadata is read again, which must agree. From b5127e5c0f758a2bba96473f2bfc37e6b502054a Mon Sep 17 00:00:00 2001 From: THEtheChad <880190+THEtheChad@users.noreply.github.com> Date: Mon, 9 Nov 2026 10:50:33 -0600 Subject: [PATCH 4/4] test(lightroom): a file that turns out to be a content duplicate at commit keeps its sidecar (#2589) revalidate_add turns a file prepared as ready into a content duplicate when a photo with its bytes arrives before the commit; the duplicate keeps the sidecar it was prepared with, so its new record has the sidecar's caption and copyright. --- crates/engine/src/lightroom_catalog/tests.rs | 44 ++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/crates/engine/src/lightroom_catalog/tests.rs b/crates/engine/src/lightroom_catalog/tests.rs index ecb6b11481c..1f558ecf5bb 100644 --- a/crates/engine/src/lightroom_catalog/tests.rs +++ b/crates/engine/src/lightroom_catalog/tests.rs @@ -398,6 +398,50 @@ fn a_content_duplicate_keeps_what_its_sidecar_says() { assert_eq!(new.captured.as_deref(), Some("2024-05-06T07:08:09"), "the file's own capture time: {r}"); std::fs::remove_dir_all(dir).unwrap(); } +/// A file that becomes a content duplicate only after it was prepared (another import added a photo +/// with its bytes while the catalog was read) is turned into one just before the commit +/// (`revalidate_add`); its new record keeps what its sidecar says all the same (#2589). +#[test] +fn a_file_that_turns_out_to_be_a_content_duplicate_at_commit_keeps_what_its_sidecar_says() { + let dir = std::env::temp_dir().join(format!("lightcraft-lrcat-late-duplicate-sidecar-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(dir.join("Photos")).unwrap(); + let source = dir.join("source.lrcat"); + std::fs::write(dir.join("Photos/test.ARW"), b"same bytes").unwrap(); + std::fs::write( + dir.join("Photos/test.xmp"), + r#" + +Harbour at dawn +© Sidecar Author +"#, + ) + .unwrap(); + std::fs::write(&source, native_catalog(&photo_tables((dir.to_str().unwrap(), "")))).unwrap(); + let mut s = crate::Session::new(); + s.media.file_probe = Some(Arc::new(|_| { + Ok(crate::media::ProbeInfo { + format: "ARW".into(), + width: 20, + height: 10, + content_hash: Some("same-bytes".into()), + captured: Some("2024-05-06T07:08:09".into()), + ..Default::default() + }) + })); + // prepared while the library has no photo with the file's bytes: the file is ready to add + let mut job = crate::lightroom_job::LightroomJob::new(&mut s, source, false).unwrap(); + let prepared = job.prepare(&AtomicBool::new(false)).unwrap(); + // then a photo with the same bytes arrives before the commit (the library is still the one + // the job was prepared for: the commit checks its identity, not its photos) + let other = library_photo(&mut s, "/elsewhere/a.ARW", |p| p.meta.caption = "Other photo's caption".into()); + let r = crate::lightroom_job::commit_prepared(&mut s, prepared).unwrap().report; + assert_eq!(s.catalog.photos().count(), 2, "{r}"); + let new = s.catalog.photos().find(|p| p.id != other).unwrap_or_else(|| panic!("no new record: {r}")); + assert_eq!((new.meta.caption.as_str(), new.meta.copyright.as_str()), ("Harbour at dawn", "© Sidecar Author"), "{r}"); + assert_eq!(s.catalog.photo(other).unwrap().meta.caption, "Other photo's caption", "{r}"); + std::fs::remove_dir_all(dir).unwrap(); +} /// A content duplicate's sidecar regions drawn on its photo as stored (its stored size as /// `AppliedToDimensions`, no `mwg-rs:Rotation`) land on the upright photo, turned by its file's own /// orientation as a file import of it turns them (#2674): the record's size is its file's upright one,