Code Monkey home page Code Monkey logo

gjfpc-hook's Introduction

Google Java Format Pre-Commit Hook

A git pre-commit script that formats java code according to the Google Java Style Guide, using the Google Java Format library.

Installing

To use this pre-commit script, clone this repo locally, and then link the pre-commit.sh script from this repo as the .git/hooks/pre-commit script in your java project:

cd ~/my-libraries
git clone https://github.com/justinludwig/gjfpc-hook.git
cd ~/my-projects/some-java-project
ln -s ~/my-libraries/gjfpc-hook/pre-commit.sh .git/hooks/pre-commit

Details

The script automatically downloads the google-java-format jar from the Google Java Format project to your ~/.local/share/java directory on first run. It then calls this jar to format the entire contents of each staged file with a .java extension when git calls it for pre-commit processing.

If formatting fails, the commit will be aborted. If formatting succeeds, the file will be re-staged (to include any formatting changes in the commit). To avoid committing un-staged hunks, you must skip the script for the commit.

Formatting

To run the google-java-format jar independently of the pre-commit hook, run the format.sh script:

~/my-libraries/gjfpc-hook/format.sh --help

You can also link the formatting script to a local bin directory, either in your home folder:

ln -s ~/my-libraries/gjfpc-hook/format.sh ~/.local/bin/google-java-format

Or another bin directory on your PATH:

sudo ln -s ~/my-libraries/gjfpc-hook/format.sh /local/usr/bin/google-java-format

And then run the script simply as google-java-format:

google-java-format --help

Skipping

To skip formatting a single commit, use the --no-verify flag (-n for short):

git commit --no-verify

To skip a series of commits -- like when rebasing or merging -- set the NO_VERIFY environment variable in your shell to be not empty, make the commits, and then set NO_VERIFY back to empty:

export NO_VERIFY=1
git rebase -i master
export NO_VERIFY=

gjfpc-hook's People

Contributors

justinludwig avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.