Skip to content

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

Merged
stefankoegl merged 2 commits into
masterfrom
claude/cli-backup-overwrite
Nov 19, 2026
Merged

stefankoegl merged 2 commits 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

coveralls commented Nov 11, 2026 •

Copy link
Copy Markdown

Coverage Status

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.replace in 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

Comment thread jsonpatch_cli.py Resolved
Handle file replacement and unlinking for backups.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@stefankoegl
stefankoegl merged commit b88cd76 into master Nov 19, 2026
22 checks passed
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.

4 participants