Skip to content

Let jsonpatch --backup replace an existing backup - #270

Open
stefankoegl wants to merge 1 commit into
masterfrom
claude/cli-backup-overwrite
Open

stefankoegl wants to merge 1 commit into
masterfrom
claude/cli-backup-overwrite

Conversation

@stefankoegl

Copy link
Copy Markdown
Owner

jsonpatch -i -b FILE PATCH failed with a FileExistsError if FILE.orig already existed, so running it a second time on the same file failed. The fallback that is used when no temporary file can be created behaved differently: it replaced the backup on POSIX, and failed on Windows, where os.rename does not overwrite.

Both paths now replace an older backup.

How

  • Normal (atomic) path: os.link cannot overwrite, so the backup is linked under a temporary name and then moved over the old backup with os.replace. If that fails, the old backup and the original are both kept and the temporary files are removed.
  • Fallback path: os.replace instead of os.rename.

One corner case is handled explicitly: if an earlier run failed after creating the backup, FILE.orig is a hard link to FILE, and replacing one link to a file by another does nothing on POSIX. The temporary link is removed in that case too.

Tests

test_jsonpatch_in_place_existing_backup asserted the old behaviour (fail and keep the old backup); it now asserts that the backup is replaced, twice in a row. New tests cover the fallback path, a backup that links to the original, and that a failing backup loses neither file.

python tests.py passes with coverage at 100%; flake8 is clean (Python 3.13, Linux). The Windows behaviour of the fallback is covered only by the CI job.


Generated by Claude Code

'jsonpatch -i -b' failed with a FileExistsError if ORIGINAL.orig existed,
so the second run on a file failed. The fallback that is used if no
temporary file can be created replaced the backup on POSIX, but failed on
Windows, where rename() does not overwrite.

Both now replace an older backup. An older backup is kept if creating the
new one fails.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PewzpjVeke4LTQ7Q88nyu6
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 100.0%. remained the same — claude/cli-backup-overwrite into master

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants