Pre-commit-hooks for dev
Hi my IoTDB friends!๐
๐After spending days on the git hooks mechanism, the pre-commit hooks for us developers is ready now! ๐
Description
The pre-commit hooks enable auto checks before commits. So next time a single git commit should do all the style checks automatically.
Currently, the support functionalities are:
- Turn on/off the hook in local config files.
- Auto
mvn spotless:applybefore commits - Auto
mvn validatebefore commits - Smart selection of only the changed modules for validation
Installation
All the files are under tools/git-hooks.
Windows
Double click the install.ps1, and allow to execute in admin mode. Make sure that the Git for Windows is the default git on Windows.
Note: Windows support is based on the Git for Windows, which provide a minimal shell environment called Git Bash. Actually, all these hooks on Windows are executed under the Git Bash environment, so the hook script can be somewhat platform-independent.
Unix
./install.sh
Configuration
After installation, a config.sh shall be created. Modification to the config.sh file would take effect the next time you commit.
All the configurable options is in config.sample.sh.
# enable the pre-commit hooks, 0 - off, 1 - on
export IOTDB_GIT_HOOKS=1
# maven path
# If in Git Bash, replace all '\' to '/', and change the driver path from 'C:\' to '/c/
# No escape character is needed.
# Example:
# export IOTDB_MAVEN_PATH="/c/Program Files/apache-maven-3.6/bin/mvn"
export IOTDB_MAVEN_PATH="mvn"
# git path
# If in Git Bash, see 'IOTDB_MAVEN_PATH'
export IOTDB_GIT_PATH="git"
# smart select the changed java module, 0 - off, 1 - on
export IOTDB_SMART_JAVA_MODULES=1
# auto spotless:apply, 0 - off, 1 - on
export IOTDB_SPOTLESS_APPLY=0
# maven validate, 0 - off, 1 - on
export IOTDB_VALIDATE=1
# 0 - discard all, 1 - logs on errors, 2 - stdout, 3 - stdout & logs on errors
export IOTDB_VERBOSE=2
Note 1 for Windows user: If you are on Windows, pay attention to the IOTDB_MAVEN_PATH and IOTDB_GIT_PATH. Since the hook script is executed under Git Bash, you must make sure the Git Bash could get to the maven & git binary file correctly. The path expression in Git Bash is slightly different from Windows. Check the examples in config file carefully.
Note 2 for Windows user: The easiest way to handle these binary path is to put them into the %PATH% environment variable in Windows. Then the default configuration should work well.
Uninstall
See uninstall.ps1 and uninstall.sh.
Pipeline
The overall pipeline of the hook script:
-
IOTDB_GIT_HOOKSchecks if the hook is on -
IOTDB_SMART_JAVA_MODULESto select only the changed modules -
IOTDB_SPOTLESS_APPLYrunsmvn spotless:apply -
IOTDB_VALIDATErunsmvn validateormvn -pl <changed_modules> validate
The hook is tested on both Linux and Windows.
Doc
Not sure where to place the entire documentation. Any suggestion?
BTW, in my test case:
- I modify a java file (which violates spotless rules)
- use
git addthe file - use
git commit -m 'message'
The spotless:apply is triggered, and the java file style is corrected.
however, the modification is not committed.
(So, we get a git commit log which only commits the java modification we manually made)
Better to find a solution.
BTW, in my test case:
- I modify a java file (which violates spotless rules)
- use
git addthe file- use
git commit -m 'message'The
spotless:applyis triggered, and the java file style is corrected. however, the modification is not committed. (So, we get a git commit log which only commits the java modification we manually made)Better to find a solution.
I figure out that the indices of files modified in the pre-commit hooks should be updated by end of the script.
And it's solved now by the update_changed_file_indices function called after the spotless:apply.