Skip to content

Instantly share code, notes, and snippets.

@costdev
Created July 10, 2022 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save costdev/b9d01f4b8f1987497b75547805c326ac to your computer and use it in GitHub Desktop.
Save costdev/b9d01f4b8f1987497b75547805c326ac to your computer and use it in GitHub Desktop.
Autofix a patch with a single sniff.
#!/bin/bash
# Change to the root directory.
cd $(git rev-parse --show-toplevel)
# A single argument should be provided: The path to the local patch file.
PATCH_RESULTS=$(grunt patch:$1)
# Detect a patch failure and exit.
if [ "$PATCH_RESULTS" == '*HUNK failed*' ] || [ "$PATCH_RESULTS" == '*unexpected*' ]; then
echo "Patch needs manual refresh. Add the 'needs-refresh' keyword to the ticket."
# Delete .orig files.
find . -name "*.orig" -type f -delete
# Delete .rej files.
find . -name "*.rej" -type f -delete
# Restore the working tree.
git restore .
exit 0
fi
# Add the standard. dealerdirect/phpcodesniffer-composer-installer automatically adds this to "installed_paths".
yes | composer require slevomat/coding-standard --dev
# Run the relevant autofix against the changed patch files.
vendor/bin/phpcbf $(git diff --name-only) --standard=SlevomatCodingStandard --sniffs=SlevomatCodingStandard.ControlStructures.DisallowYodaComparison
# Remove the standard before updating the patch file (clears composer.json changes from the working tree).
yes | composer remove slevomat/coding-standard --dev
# Update the patch file.
git diff > $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment