git pre-commit hook format code - Intellij/Android

2019-03-25 20:18发布

问题:

This gist shows how to auto-format Java code using the Eclipse formatter at pre-commit.

Source: https://gist.github.com/ktoso/708972

Code:

#!/bin/sh
#
# This hook will run the eclipse code formatter before any commit
# to make the source look as it's supposed to look like in the repo.

ECLIPSE_HOME=$HOME/eclipse
STYLE_FILE=$HOME/org.eclipse.jdt.core.prefs
echo "Running pre-commit hook: run-eclipse-formatter---------------------"
echo "Will run eclipse formatter, using: $STYLE_FILE"
echo "Listing folders to run formatter on… "
code_dirs=`find . -maxdepth 3 | grep 'src/'`
for dir in $code_dirs; do
   echo $dir;
done;

echo "Launching eclipse code formatter…    "
exec $ECLIPSE_HOME/eclipse \
                    -nosplash \
                    -application org.eclipse.jdt.core.JavaCodeFormatter \
                    -verbose \
                    -config $STYLE_FILE \
                    $code_dirs

echo "done---------------------------------------------------------------"

I'd like to achieve this with IntelliJ and Android Studio. How would the script look like then?

Also I guess it would be best to only run the formatter on changed files. Maybe this is useful:

changedJavaFiles=$(git diff --cached --name-only --diff-filter=ACM | grep '.java$')

ACM stands for Added, Copied, Modified. Source: http://git-scm.com/docs/git-diff

Please comment if anything is unclear.

Update

My setup is Windows 10 and I'd like to use the command line tool MINGW32(Git Bash). Git version is 1.9.5 msysgit.1

回答1:

You can use the IntelliJ command-line source code formatter available in IntelliJ since version 2016.3. For example (for Git Bash):

CHANGED_JAVA_SRC_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '.java$')
$INTELLIJ_DIR/bin/format $CHANGED_JAVA_SRC_FILES

Although Android Studio is based on IntelliJ, it seems not to support this when the IDE is open, see comment below.

A more advanced pre-commit hook example is available in the git-hooks-code-autoformat project in GitHub.

Note that a snap package is available for Ubuntu, so it is easy to add this to a Ubuntu Git or CI server as well.



回答2:

IDEA has this built into the normal commit dialog. Just check "reformat code" and it will all happen automatically.

If you want to run the reformat part of IDEA from command line I don't think it's possible. The only think I've found that can run outside is code inspections.



回答3:

If someone else will need solution on Windows - it took me half of the day to work, so maybe it will be usefull for someone:

ive create auto_reformat.bat, content:

for /F "usebackq delims=" %%P in (`git diff --name-only --diff-filter=ACM ^| findstr -I "java kt"`) do %INTELLIJ_DIR%\bin\format %%P

and problem with multiple instances reported here:

https://youtrack.jetbrains.com/issue/IDEA-160462

solution:

https://confluence.jetbrains.com/display/IDEADEV/Command-Line+Source+Code+Formatter

but it doesnt work for me :( - we dont have spare licenses on license server, anyway, hopefully will help someone