Git pre-commit hook check message

走远了吗. 提交于 2020-01-05 01:40:48

问题


i have git commit hook script , whom checking commit input message, and if message not contain the word "updated" the script has reject commit.

#!/bin/bash
read -p "Enter a commit message: " message

if [[ ${message} != *"updated"* ]];then
  echo "Your commit message must contain the word 'updated'"
  else
  git commit -m "$message"
  fi

how to make this hook automatically executable if i try to push some files in my local repo with message: git commit -m "updated:something" , my idea is make it not like "run this script to do commit" , but then you open console and trying to make a commit by typing commant , script will check your commit message automatically and pass it or reject.


回答1:


Taking commit-msg for example.

#!/bin/bash

MSG="$1"

if ! grep -qE "updated" "$MSG";then
    cat "$MSG"
    echo "Your commit message must contain the word 'updated'"
    exit 1
fi

chmod 755 commit-msg and copy it as .git/hooks/commit-msg.



来源:https://stackoverflow.com/questions/45416049/git-pre-commit-hook-check-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!