macos - 使用 exit 在预提交 Hook 中停止 git commit

标签 macos git bash githooks pre-commit-hook

我试图阻止 git 提交继续使用预提交 Hook 。通过阅读手册,当您返回 0 以外的退出代码时它应该停止。我正在测试 csscomb 命令是否返回错误,如果返回错误则中断循环并退出,但 git commit 仍在继续通过 git commit (-a) 输入消息。

我从 https://github.com/filtercake/git-pre-commit-csscomb fork 并修改了下面的脚本

#!/bin/bash

# pre-commit hook to comb staged .sass and .scss files
# save as .git/hooks/pre-commit
# make executable: chmod +x .git/hooks/pre-commit

# sources:

# http://www.hagenburger.net/BLOG/Using-Git-Commit-Hooks-to-Autocompile-Sass-to-CSS.html
# http://stackoverflow.com/questions/8470755/git-pre-commit-hook-add-file-into-index/8471009#8471009
# http://stackoverflow.com/questions/592620/how-to-check-if-a-program-exists-from-a-bash-script/677212#677212
# https://gist.github.com/openam/8406343

# check if sass/scss files are staged for commit

# diff-lines from http://stackoverflow.com/a/12179492/3019532
# takes the line
function diff-lines() {
    local path=
    local line=
    while read; do
        esc=$'\033'
        if [[ $REPLY =~ ---\ (a/)?.* ]]; then
            continue
        elif [[ $REPLY =~ \+\+\+\ (b/)?([^[:blank:]$esc]+).* ]]; then
            path=${BASH_REMATCH[2]}
        elif [[ $REPLY =~ @@\ -[0-9]+(,[0-9]+)?\ \+([0-9]+)(,[0-9]+)?\ @@.* ]]; then
            line=${BASH_REMATCH[2]}
        elif [[ $REPLY =~ ^($esc\[[0-9;]+m)*([\ +-]) ]]; then
            echo "Line $line:$REPLY"
            if [[ ${BASH_REMATCH[2]} != - ]]; then
                ((line++))
            fi
        fi
    done
}

if ! git diff --quiet --cached -- **/*.{sass,scss}; then

            echo ""
            echo "--> you checked in sass/scss files. lets comb them..."
            echo ""

            # check if csscomb is installed

            if hash csscomb 2>/dev/null; then

                echo -e "\033[1;32m--> found csscomb\033[0m"

                # TODO: check for csscomb update once a week

                # check if configfile exists

                if [ ! -f ./.csscomb.json ]; then
                    echo "--> no config file, using defaults"
                else
                    echo -e "\033[1;32m--> found .csscomb.json\033[0m"
                fi

                echo ""

                # Necessary check for initial commit
                against="4b825dc642cb6eb9a060e54bf8d69288fbee4904"
                git rev-parse --verify HEAD >/dev/null 2>&1 && against="HEAD"
                EXITCODE=0

                # (A)dded (C)opied or (M)odified
                # encapsulate the loop in {} http://stackoverflow.com/a/13665483/3019532
                git diff-index --cached --full-index --diff-filter=ACM $against | \
                {
                    while read -r line; do
                      FILE_PATH="$(echo ${line} |cut -d' ' -f6-)"
                      EXTENSION="${FILE_PATH##*.}"
                      # EXTENSION=${EXTENSION,,} # Convert to lowercase
                      REGEX=""

                      # Select discouraged words based on extension

                      if [ "${EXTENSION}" = "sass" ] || [ "${EXTENSION}" = "scss" ]; then
                        echo "--> staged sass/scss file: " $FILE_PATH
                        echo "--> combing..."
                        if csscomb $FILE_PATH; then
                            echo "--> adding combed file"
                            git add $FILE_PATH
                            echo "--> done"
                        else
                            echo "Check your CSS file for combing errors or alternatively add '-n' to your git commit to bypass this hook"
                            break
                            exit 1 # Should stop the git commit from continuing
                        fi
                      fi
                    done
                }
            else
                echo -e "\033[0;31m--> Oh noes, CSS Comb is not installed. Do:"
                echo "--> npm install csscomb -g"
                echo -e "\033[0m"
                echo "--> (you need to change and stage a sass/scss file again to see it work on the next commit...)"
                echo ""
            fi
fi

# Necessary check for initial commit
against="4b825dc642cb6eb9a060e54bf8d69288fbee4904"
git rev-parse --verify HEAD >/dev/null 2>&1 && against="HEAD"
EXITCODE=0

exit 0

最佳答案

            git diff-index --cached --full-index --diff-filter=ACM $against | \
            {
                while read -r line; do

麻烦来了。多命令管道阶段在子 shell 中运行,并且该循环的导出退出其子 shell。子外壳也有自己的环境,因此您也不能从它们传递变量设置。

while read -r; do
        if ! good $REPLY; then exit 1; fi
done <<EOD
$(git diff-index --cached --full-index --diff-filter=ACM $against)
EOD

关于macos - 使用 exit 在预提交 Hook 中停止 git commit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30609480/

相关文章:

ios - 应用程序加载器iOS问题

objective-c - 将 NSURL 转换为本地文件路径

linux - 我怎样才能恢复我的 git-daemon 存储库

javascript - Selenium:服务器终止,状态为 127

php - 在优胜美地更改 php ini 文件后,mysql 上传大小不会改变

c++ - 使用 Xcode 调试器查看动态分配的数组?

git - 从 git 存储库中删除工作文件以节省空间

html - 如何在 Github 上将 HTML 页面视为正常呈现的 HTML 页面以在浏览器中查看预览,而无需下载?

bash - 使用 --getopts 获取整个单词标志

linux - Bash脚本被杀死时如何杀死当前命令