Add --restore flag to apply command for clean package restoration#9
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fe813505a0
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
Pull request overview
This PR introduces a --restore option for the p12y apply workflow to restore (force-reinstall) a package to a clean state before applying patch files, which helps recover from previously-applied patches or manual edits in the environment.
Changes:
- Added
--restoreflag to theapplysubcommand and plumbed it through tocore.apply_patch(). - Extended
apply_patch()withrestore/env_pathparameters and a call torestore_clean_package()when enabled. - Updated documentation and added unit tests covering the new restore behavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
patch_package_py/core.py |
Adds restore-aware behavior to apply_patch() (including new parameters and error logging). |
patch_package_py/cli.py |
Adds --restore CLI flag and passes it into apply_patch(). |
skills/patch-package-py/SKILL.md |
Documents --restore and updates “reinstall” → “restore” terminology. |
tests/test_core.py |
Adds tests validating restore invocation and related error handling. |
tests/test_e2e.py |
Formatting-only changes (line wrapping). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…store mode - Move restore_clean_package call before resolve/version checks so --restore can recover a missing or version-drifted package - Raise RuntimeError (non-zero exit) when dry-run fails after restore instead of silently returning https://claude.ai/code/session_01UUAdSdSQWt6eAP11HMQU3J
| logger.warning( | ||
| f"Patch `{patch_name}` appears to be already applied, skipping...", | ||
| ) |
When restore=True and the real patch application fails (after a successful dry-run), raise RuntimeError instead of leaking a raw CalledProcessError. Adds test coverage for this scenario. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The restore happy path test now uses a real venv and real patch command instead of mocking subprocess. Error branch tests (missing env_path, dry-run failure, real apply failure) stay in unit tests since they need to simulate specific failure scenarios. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
patch_package_py/core.py:396
- Same as above:
from Nonedrops the originalCalledProcessErrorcontext on real apply failures whenrestore=True, making it harder to debug why patch application failed.
except subprocess.CalledProcessError:
if restore:
raise RuntimeError(
f"Failed to apply patch `{patch_name}` after restoring clean package."
) from None
…n change Previously --restore ran before version validation, which meant it would force-reinstall the version from the patch filename regardless of what the project actually had installed. Now version mismatch is caught first, and restore only reinstalls when the versions already match. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Remove `from None` so the underlying CalledProcessError is chained, making failures easier to diagnose. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
apply_patch now derives site_packages_dir internally via find_site_packages, eliminating the redundant parameter that callers always computed from env_path. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
| apply_patch( | ||
| patch_file, | ||
| env_path, | ||
| restore=args.restore, | ||
| ) |
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary
This PR adds a
--restoreflag to theapplycommand that allows users to restore a package to its clean state before applying patches. This is useful when the environment may already contain a previous version of the patch or manual edits.Key Changes
New
--restoreparameter inapply_patch()function: Added optionalenv_pathandrestoreparameters to theapply_patch()function. Whenrestore=True, the function callsrestore_clean_package()before attempting to apply the patch.CLI flag support: Added
--restoreargument to theapplysubcommand that passes the flag through toapply_patch().Validation: Added validation to ensure
env_pathis provided whenrestore=True, raising aValueErrorif missing.Improved error handling: Modified error logging to distinguish between:
restore=False(existing behavior)restore=Trueand the patch fails after restoration (new behavior)Documentation updates: Updated SKILL.md to document the new
--restoreflag and clarified terminology around "restore" vs "reinstall" throughout the documentation.Implementation Details
--restoreflag is optional and defaults toFalseto maintain backward compatibilityhttps://claude.ai/code/session_01UUAdSdSQWt6eAP11HMQU3J