Skip to content

Instantly share code, notes, and snippets.

@Ragnoroct
Last active March 2, 2022 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ragnoroct/943bda9ed238c2919e751393ffcb2a72 to your computer and use it in GitHub Desktop.
Save Ragnoroct/943bda9ed238c2919e751393ffcb2a72 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Diffs changelist to correct prettier format
#
# You can apply the prettier patch from this output
# xclip -out | git apply -
# pbpaste | git apply -
# cat /dev/clipboard | git apply -
git_root=$(git rev-parse --show-toplevel)
files_in_commit=$(git diff-index --name-only --diff-filter=ACMR HEAD -- )
prettier="$git_root/node_modules/.bin/prettier"
prettier_success=0
result=$(( result + $? ))
cd "$git_root" || exit
for file in $files_in_commit; do
# psuedo: prettier_format | git_diff | fix_filenames_for_patch_output
# shellcheck disable=SC2002
cat "$file" | "$prettier" --stdin-filepath "$file"\
| git --no-pager diff --color --no-index -- "$file" - \
| sed "s/b\/-/b\/${file//\//\\/}/g"
if [ "${PIPESTATUS[2]}" -ne "0" ]; then
prettier_success=1
fi
# gnu diff also works
# diff --color -u "$file" <(cat "$file" | "$prettier" --stdin-filepath "$file")
done
exit $prettier_success
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment