简体   繁体   中英

Git commit message validation

My commit message will be in the following format:

ANDROID-123 #commit Your commit message

ANDROID- is fixed keyword.

Any suggestion please?

Git hooks are scripts that run automatically at certain stages of your git workflow. There is a commit-msg git hook for the purpose of checking or changing commit messages during the commit process. See the git scm documentation for more details.

You can use a gitlabhook, for example a pre-receive hook where you will defined a regex for your commit message , and then check if the new commit message is like your predefined regex. its gonna be something like this:

REGEX="Your regex"

while read oldrev newrev refname ; do
#Validate commit message format
  for COMMIT in `git rev-list $oldrev..$newrev`;
  do
    MESSAGE=`git cat-file commit $COMMIT | sed '1,/^$/d'`       
    if [[ $MESSAGE =~ $REGEX ]]; then
      echo ""
    else 
      echo -e "Error Message" >&2
      exit 1
    fi
  done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM