refactor/photorenamer-sidecar #8

Merged
ludovic.terrier merged 9 commits from refactor/photorenamer-sidecar into master 2026-11-11 20:26:28 +01:00
Owner
No description provided.
The burst test expected IMG_2 to get the -1 counter, which only holds
when find lists IMG_1 first. On a case-sensitive disk image it does not,
and the test failed with correct pairing. Each picture now carries its
own Artist tag, and each renamed picture must have a sidecar whose
content matches that tag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Some sidecar cases only exist on a case-sensitive file system, so the
suite must be able to run on a case-sensitive disk image. macOS
mktemp -d ignored TMPDIR, so PHOTORENAMER_TEST_DIR now picks the parent
of the work directory.

The suite removes and writes everything under that work directory. If
PHOTORENAMER_TEST_DIR is not an absolute directory, or mktemp fails,
the suite stops before it calls mktemp or sets its cleanup trap, so an
empty or relative work directory can never lead to rm or writes
elsewhere. tests/photorenamer_guard_test.sh runs the suite with a
missing dir, a relative dir and a failing mktemp, with an rm shim that
logs, and checks that each stops with status 1, calls no rm and creates
no file. It is a separate file so the suite does not call itself.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The sidecar pre-check, the Live Photo pairing and the dry-run report
each asked exiftool for the target their own way: sidecar_target ran
an extra -testname and parsed it, and the dry run ran its own. One
-testname call now gives the target for all three, and sidecar_target
is gone. Write mode still renames with -filename<date -d format, so
the behaviour is unchanged.

Runs per file: dry run 2 (3 for a Live Photo image before); write 3
(2 for a file without sidecar or video before). The next commit puts
the extra write-mode run to use.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Write mode derived the name again from the date format. If a file took
the target between -testname and the write, exiftool picked the next
counter for the picture, while the sidecar still went to the computed
name, next to the file that took it. The pair was split. Write mode now
renames to the name -testname gave, and exiftool refuses it when it is
taken.

exiftool -filename is kept over mv: it refuses an existing name with
its own error text, which the logs and the read-only test rely on,
while mv -n exits 0 on macOS when it skips.

Behaviour changes:
- A target that cannot be computed fails every file, in both modes,
  with "rename failed: cannot compute the target name". The message
  was "... for the sidecar" and only applied to a file with a sidecar
  in write mode.
- A target name that contains "%" is refused with that same message.
  exiftool expands %f, %e, %c and %d in a written FileName and has no
  escape for "%", so the file would get a wrong name. Only an odd
  extension can put "%" in the target.
- When a target without a counter is taken, for example the paired
  name of a Live Photo MOV, write mode now fails at -testname and logs
  "rename failed: Warning: File '...' already exists - ...", the text
  a dry run already gave. It was "rename failed: Error: '...' already
  exists - ...".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A dry run said "sidecar follows" for a picture that write mode then
refused, because the clash was only checked in write mode. The target
is known in both modes now, so the dry run refuses the same file with
the same message, count and exit status.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The scan skips *.[xX][mM][pP] but the lookup only tried "<file>.xmp".
On a case-sensitive file system IMG.JPG.XMP was skipped and left
behind; on APFS it was found but renamed to a lower-case .xmp.

The lookup now globs for the extension in any case and keeps the name
found on disk, so the sidecar keeps its own extension case. The glob
compares bytes, while APFS stat ignores case and Unicode form. So when
the glob finds nothing but "<file>.xmp" exists, a case-insensitive glob
gives the real name (IMG_1.JPG with IMG_1.jpg.xmp). If that also finds
nothing, as with an NFC sidecar next to an NFD picture under bash 3.2,
"<file>.xmp" itself is used: APFS resolves it, as on master, and the
target gets a lower-case .xmp. The glob options are restored after the
lookup.

A case-sensitive file system can hold IMG.JPG.xmp and IMG.JPG.XMP at
once. There is no safe way to know which one Immich reads, and moving
one would orphan the other, so such a file is refused in both modes
with "rename skipped, more than one sidecar". Only regular files and
symlinks count: a directory with a sidecar name is ignored, a dangling
link counts, and a lone link moves like a file.

Tests that need a file system which folds case or Unicode form skip
themselves elsewhere. The two-sidecar, directory and link tests only
run on a case-sensitive one. A note at those tests gives the commands
to run the suite on a case-sensitive disk image and under /bin/bash 3.2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The clash check tested only the exact target sidecar name. On a
case-sensitive file system, moving IMG.jpg.XMP next to an existing
T.jpg.xmp left two sidecars for one picture. On APFS the check found a
variant but named it in the case it guessed, not the one on disk.

The target check now uses the sidecar lookup, so any case of the
extension, and on APFS any case or Unicode form of the stem, is a clash.
The message names the file on disk. A dry run and write mode refuse
the same files before anything moves.

Behaviour change: a dangling symlink at the target sidecar is now a
clash in both modes. On master, write mode let mv -n replace the link.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When the sidecar could not follow, the file was counted as failed and
as processed, and the picture stayed on its new name while the sidecar
kept the old one, so Immich lost the pairing.

The clash check before the rename catches the usual case. What is left
is a target that appears between the check and the move, or an mv
error. Then the picture is moved back to its old name and the file
counts once, as failed. The old "renamed ... but the sidecar was not
moved" message stays for the case where the move back fails, for
example because a file or a directory took the old name. The picture
then keeps its new name, so its Live Photo pair is still recorded and
the MOV follows.

The move back uses exiftool -filename, not mv: mv -n moves the picture
into a directory found at the old name, while exiftool refuses any
existing name. exiftool expands "%" in a written FileName with no
escape, so a picture whose old name holds "%" is not moved back and is
reported as split. The result is checked on disk before the log says
"moved back".

When the picture of a Live Photo is moved back, its MOV is held on its
name and logged as "rename skipped, its Live Photo image was moved
back". Renaming it on its own date would split the stems again; keeping
both names lets a later run pair them. The hold is an empty stem in the
pair file, which a real target never has.

The existing [ -e "$sidecar" ] check now has a comment: mv -n exits 0
on macOS when the target exists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The sidecar-orphan-target test had the same setup and checks as the
sidecar-clash test, so it only cost run time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
ludovic.terrier/scripts!8
No description provided.