Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git pre-commit hook format code - Intellij/Android Studio

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

like image 817
BenjaminButton Avatar asked Aug 29 '15 15:08

BenjaminButton


People also ask

How do I enable pre-commit in IntelliJ?

Create a file named "pre-commit-hook.sh" in project root, The plugin will run the hook before any commit with the file changed as arguments.

How do you set up a pre-commit hook?

Open a terminal window by using option + T in GitKraken Client. Once the terminal windows is open, change directory to . git/hooks . Then use the command chmod +x pre-commit to make the pre-commit file executable.

How can I format Android studio?

Automatically formatting code in Android Studio and IntelliJ To automatically format your code in the current source code window, use Cmd+Alt+L (on Mac) or Ctrl+Alt+L (on Windows and Linux).

What is pre-commit hook in git?

The pre-commit hook is run first, before you even type in a commit message. It's used to inspect the snapshot that's about to be committed, to see if you've forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.


2 Answers

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.

like image 65
mrts Avatar answered Sep 20 '22 08:09

mrts


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.

like image 20
Andreas Wederbrand Avatar answered Sep 20 '22 08:09

Andreas Wederbrand