iotdb icon indicating copy to clipboard operation
iotdb copied to clipboard

Pre-commit-hooks for dev

Open leonardodalinky opened this issue 3 years ago โ€ข 2 comments

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:apply before commits
  • Auto mvn validate before 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:

  1. IOTDB_GIT_HOOKS checks if the hook is on
  2. IOTDB_SMART_JAVA_MODULES to select only the changed modules
  3. IOTDB_SPOTLESS_APPLY runs mvn spotless:apply
  4. IOTDB_VALIDATE runs mvn validate or mvn -pl <changed_modules> validate

The hook is tested on both Linux and Windows.

Doc

Not sure where to place the entire documentation. Any suggestion?

leonardodalinky avatar Jun 22 '22 07:06 leonardodalinky

BTW, in my test case:

  • I modify a java file (which violates spotless rules)
  • use git add the 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.

jixuan1989 avatar Jun 26 '22 03:06 jixuan1989

BTW, in my test case:

  • I modify a java file (which violates spotless rules)
  • use git add the 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.

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.

leonardodalinky avatar Jun 26 '22 10:06 leonardodalinky