Add git hooks that can run multiple scripts (#12300)

* Add git hooks that can run multiple scripts

* scripts to install/uninstall hooks

* better message for uninstall; support pre-push params
This commit is contained in:
Jihoon Son 2022-03-09 07:16:47 +09:00 committed by GitHub
parent 875e0696e0
commit 0e097ead36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 173 additions and 2 deletions

View File

@ -47,7 +47,7 @@ will generate a report to show the current code coverage on the code (not just y
## Git Checkstyle Verification Hook (Optional)
Git Checkstyle pre-commit hook can be installed to automatically run checkstyle verification before committing,
saving cycle from avoiding the checkstyle failing later in Travis/CI environment.
The hook can be setup easily by running the <DRUID_HOME>/setup-hooks.sh script.
The hook can be setup easily by running the <DRUID_HOME>/hooks/install-hooks.sh script.
## Metadata
The installation of a MySQL metadata store is outside the scope of this document, but instructions on setting up MySQL can be found at [docs/development/extensions-core/mysql.md](/docs/development/extensions-core/mysql.md). This assumes you followed the example there and have a database named `druid` with proper permissions for a user named `druid` and a password of `diurd`.

40
hooks/install-hooks.sh Executable file
View File

@ -0,0 +1,40 @@
#!/bin/bash -eu
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
function cp_if_not_exist(){
if [ -e "$2" ]
then
echo "$2 already exists!"
exit 1
else
cp -r "$1" "$2"
fi
}
if [ $# != 1 ]
then
echo 'usage: program {$DRUID_ROOT}'
exit 1
fi
DRUID_ROOT=$1
cp_if_not_exist ${DRUID_ROOT}/hooks/run-all-in-dir.py ${DRUID_ROOT}/.git/hooks/run-all-in-dir.py
cp_if_not_exist ${DRUID_ROOT}/hooks/pre-commit ${DRUID_ROOT}/.git/hooks/pre-commit
cp_if_not_exist ${DRUID_ROOT}/hooks/pre-push ${DRUID_ROOT}/.git/hooks/pre-push
cp_if_not_exist ${DRUID_ROOT}/hooks/pre-commits ${DRUID_ROOT}/.git/hooks/pre-commits
cp_if_not_exist ${DRUID_ROOT}/hooks/pre-pushes ${DRUID_ROOT}/.git/hooks/pre-pushes

18
hooks/pre-commit Executable file
View File

@ -0,0 +1,18 @@
#!/bin/bash -eu
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
.git/hooks/run-all-in-dir.py .git/hooks/pre-commits

View File

@ -1,4 +1,5 @@
#!/bin/bash -eu
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
@ -14,4 +15,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
ln -s ../../hooks/pre-push.sh .git/hooks/pre-push
echo 'pre-commit sample'

22
hooks/pre-push Executable file
View File

@ -0,0 +1,22 @@
#!/bin/bash -eu
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
#
.git/hooks/run-all-in-dir.py .git/hooks/pre-pushes $1 $2

View File

@ -0,0 +1,18 @@
#!/bin/bash -eu
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
echo 'pre-push sample'

View File

@ -1,4 +1,5 @@
#!/bin/bash -eu
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.

34
hooks/run-all-in-dir.py Executable file
View File

@ -0,0 +1,34 @@
#!/usr/bin/env python3
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
import os
import sys
import subprocess
if len(sys.argv) < 2:
sys.stderr.write('usage: program <hooks directory>\n')
sys.exit(1)
hooks_dir = sys.argv[1]
args = sys.argv[2:]
for hook in os.listdir(hooks_dir):
if not hook.startswith("_"):
command = [os.path.join(hooks_dir, hook)] + args
print("Running {}".format(command))
subprocess.run(command, shell=True)

37
hooks/uninstall-hooks.sh Executable file
View File

@ -0,0 +1,37 @@
#!/bin/bash -eu
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
#
if [ $# != 1 ]
then
echo 'usage: program {$DRUID_ROOT}'
exit 1
fi
DRUID_ROOT=$1
# This script does not remove .git/hooks/pre-commit, .git/hooks/pre-push, or any other git hook scripts
# because those files may have user-custom hooks.
# Instead, we remove only the files and directories that we are sure it is safe to remove.
rm -f ${DRUID_ROOT}/.git/hooks/run-all-in-dir.py
rm -rf ${DRUID_ROOT}/.git/hooks/pre-commits
rm -rf ${DRUID_ROOT}/.git/hooks/pre-pushes
echo "This script does not remove or modify the git hook script files in .git/hooks, such as 'pre-commit' or 'pre-push'. Those scripts should be removed or modified manually."