Skip to content

Commit

Permalink
Issue #SB-2730: Add hook for auto format of Java code on commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinaya Kumar B committed Apr 17, 2018
1 parent 79ef660 commit 472a77a
Show file tree
Hide file tree
Showing 73 changed files with 4,740 additions and 4,656 deletions.
12 changes: 12 additions & 0 deletions git-hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

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

FORMAT_JAR_LOCATION="${REPO_ROOT_DIR}/tools/java-format/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
17 changes: 17 additions & 0 deletions scripts/check-java-file-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

FILES_TO_SKIP="^(src-gen|third-party|(test/.*\/testdata\/)).*\.java$"

JAR_PATH=$1
FILE_PATH=$2

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

# Test if the file should be skipped
echo "${FILE_PATH}" | grep -Eqi "${FILES_TO_SKIP}" && exit 0

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

exit 0
5 changes: 5 additions & 0 deletions scripts/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

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

cp "${REPO_ROOT_DIR}/git-hooks/pre-commit" "${REPO_ROOT_DIR}/.git/hooks/"
Loading

0 comments on commit 472a77a

Please sign in to comment.