Skip to content

Instantly share code, notes, and snippets.

@appleseedexm
Created January 28, 2022 15:42
Show Gist options
  • Save appleseedexm/6681a2276817653d9b17f1355a81cc47 to your computer and use it in GitHub Desktop.
Save appleseedexm/6681a2276817653d9b17f1355a81cc47 to your computer and use it in GitHub Desktop.
Small shell script that uses the IntelliJ command line formatter to format all staged java files. Workaround when you don't want to work with IntelliJ but need it's formatter.
#!/bin/sh
# script is inspired by https://github.com/michalrus/git-hooks-code-autoformat
patterns="\.java$"
intellijFormatter="/Applications/IntelliJ IDEA.app/Contents/bin/format.sh"
configpath="/Users/path/to/your/config.xml"
root="$(git rev-parse --show-toplevel)"
[ -d "$root" ] || exit 1
# formatter doesnt exist, ignore
[ -f "$intellijFormatter" ] || (echo "formatter path is invalid" && exit 1)
git diff --name-only --cached | {
labort=0
echo $orig
while IFS= read -r orig ; do
orig="${root}/${orig}"
# file is getting deleted, ignore
[ -f "$orig" ] || continue
# file matches one of the patterns
match_pattern=''
echo "$orig" | grep -Eqi "$patterns" && match_pattern='1'
# if none, ignore
[ "$match_pattern" ] || continue
"$intellijFormatter" -s "$configpath" "$orig" || labort=1
git add "$orig"
done
exit $labort
} || exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment