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 <noreply@anthropic.com>
This commit is contained in:
Chad ElliottandClaude Opus 5.5 authored and THEtheChad committed 2026-11-13 00:35:38 -06:00
1 parent b6b4662dc8
commit 752065dd92
5 files changed
+68 -8

No files matched your search

+2
View File
@@ -82,6 +82,8 @@ pub fn import_params(s: &Session, p: &Value) -> Result<ImportRequest> {
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);
+20 -6
View File
@@ -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),
+4
View File
@@ -67,6 +67,10 @@ pub struct ImportOptions {
pub metadata_preset: Option<String>,
/// 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
@@ -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#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:description><rdf:Alt><rdf:li xml:lang="x-default">Harbour at dawn</rdf:li></rdf:Alt></dc:description>
</rdf:Description></rdf:RDF></x:xmpmeta>"#,
)
.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
+4 -2
View File
@@ -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<Self> {
// 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 {