diff options
author | Fiachra Corcoran <fiachra.corcoran@est.tech> | 2019-05-15 11:06:05 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-05-15 11:06:05 +0000 |
commit | 3ebd2534167e73426d2b19efb05eaf9892f6f9d6 (patch) | |
tree | 456706569e4e4709baed36403df613739fffdea5 /ci_scripts/pre-commit.sh | |
parent | 8c6eda290e70036035b89d924d203a8c20e2a52c (diff) | |
parent | a723c89c6268739d134e1dd9b90b76bd1179c69c (diff) |
Merge "Add syntax check before pushing code"
Diffstat (limited to 'ci_scripts/pre-commit.sh')
-rwxr-xr-x | ci_scripts/pre-commit.sh | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/ci_scripts/pre-commit.sh b/ci_scripts/pre-commit.sh new file mode 100755 index 00000000..636f8ff3 --- /dev/null +++ b/ci_scripts/pre-commit.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# +# ============LICENSE_START======================================================= +# Copyright (C) 2019 Nordix Foundation. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END========================================================= +# +# Pre-commit hook for running checkstyle on changed Java sources +# +# To use this you need: +# 1. Checkstyle's jar file downloaded *version is important checkstyle-8.13-all.jar +# 2. To configure git: +# * git config --add checkstyle.jar <location_of_jar> +# 3. Copy this file to your .git/hooks directory as pre-commit +# +# Now, when you commit, you will be disallowed from doing so +# until you pass your checkstyle checks. + +changed_files=" " +for file in $(git diff --cached --name-status | grep -E '\.(java)$' | grep -vE '^D' | awk '{print $2}') +do + changed_files+="$file " +done + +printf "Using checkstyle sheet " +checkstlye_jar_command='git config --get checkstyle.jar' + +if ! ($checkstlye_jar_command) +then + printf "You must configure checkstyle in your git config" + exit 1 +fi + +checkstyle_warnings=$(java -jar $($checkstlye_jar_command) -c ci_scripts/onap-style-java.xml $changed_files | grep WARN) +if [ $? == 0 ] +then + printf "\nWarnings found\n\n" + echo "$checkstyle_warnings" + printf "\n###############################################################\n\nFix warnings before committing\n\n" + exit 1 +else + printf "\nCode checkstyle passed.\n" +fi |