Repository navigation
Let jsonpatch --backup replace an existing backup - #270
Merged
Merged
Conversation
'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
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
A critical fallback bug can overwrite the backup and lose pre-patch contents when FILE.orig hard-links to FILE; the replacement handling needs a fix and a regression test.
1 open issue
What changed in this PR
This PR updates the JSON Patch CLI so repeated in-place patch operations can replace existing .orig backups.
Changes:
- Stages atomic backups under a temporary hard-link name before replacing the previous backup.
- Uses
os.replacein fallback mode to support overwriting backups across platforms. - Adds regression tests for repeated backups, fallback behavior, hard-linked backups, and backup-replacement failures.
| File | Description |
|---|---|
| tests.py | Updates overwrite expectations and adds backup preservation and cleanup tests. |
| jsonpatch_cli.py | Adds atomic backup staging and changes fallback backup replacement. |
Review effort: Balanced
Handle file replacement and unlinking for backups. Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

jsonpatch -i -b FILE PATCHfailed with aFileExistsErrorifFILE.origalready 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, whereos.renamedoes not overwrite.Both paths now replace an older backup.
How
os.linkcannot overwrite, so the backup is linked under a temporary name and then moved over the old backup withos.replace. If that fails, the old backup and the original are both kept and the temporary files are removed.os.replaceinstead ofos.rename.One corner case is handled explicitly: if an earlier run failed after creating the backup,
FILE.origis a hard link toFILE, 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_backupasserted 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.pypasses 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