Skip to content

Commit

Permalink
Add gjf code style pre commit hook support.
Browse files Browse the repository at this point in the history
  • Loading branch information
raviagarwal7 committed May 16, 2018
1 parent 9cc34d2 commit 7e861b9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -73,6 +73,17 @@ See the [Usage](https://github.com/uber/okbuck/blob/master/Usage.md) page for co

To speed up your builds even more, you can use an implementation of [Buck's HTTP Cache API](https://github.com/uber/buck-http-cache) to take advantage of building once and using the same build artifacts on all machines.

## Code Style
We use [google-java-format](https://github.com/google/google-java-format) to format Java files.
You can either install the IntelliJ plugin or use a pre commit hook to automatically re-format files.

To add a pre commit hook:

- Build `google-java-format` jar following instructions from [github page](https://github.com/google/google-java-format).
- Copy `config/git-hooks/pre-commit.template` to `.git/hooks/pre-commit`
- Make the script executable: `chmod +x .git/hooks/pre-commit`
- Replace `<<PATH_TO_JAR>>` in `.git/hooks/pre-commit` with the location of google-java-format jar.

## Contributors

We'd love for you to contribute to our open source projects. Before we can accept your contributions, we kindly ask you to sign our [Uber Contributor License Agreement](https://docs.google.com/a/uber.com/forms/d/1pAwS_-dA1KhPlfxzYLBqK6rsSWwRwH95OCCZrcsY5rk/viewform).
Expand Down
12 changes: 12 additions & 0 deletions pre-commit.template
@@ -0,0 +1,12 @@
#!/bin/bash

REPO_ROOT_DIR="$(git rev-parse --show-toplevel)"

FORMAT_JAR_LOCATION="<<PATH_TO_JAR>>/google-java-format-1.5-all-deps.jar"

for staged_file in $(git diff --cached --name-only); do
[ -f "${staged_file}" ] || continue

"${REPO_ROOT_DIR}/scripts/check-java-file-format" "${FORMAT_JAR_LOCATION}" "${staged_file}"
git add "${staged_file}"
done
12 changes: 12 additions & 0 deletions tooling/check-java-file-format
@@ -0,0 +1,12 @@
#!/bin/sh

JAR_PATH=$1
FILE_PATH=$2

# Test if the file is Java file
echo "${FILE_PATH}" | grep -Eqi "\.java$" || exit 1

# Try to format the file
java -jar "${JAR_PATH}" -i "${FILE_PATH}"

exit 0

0 comments on commit 7e861b9

Please sign in to comment.