【问题标题】:How to disable git push when there are TODOs in code?当代码中有 TODO 时如何禁用 git push?
【发布时间】:2012-12-02 09:11:40
【问题描述】:

我们的团队遇到了问题,我们决定检查是否有方法或 git 命令来拒绝代码中存在 TODO 的 git push。 有任何想法吗? 提前致谢。

【问题讨论】:

    标签: git push todo


    【解决方案1】:

    不可能在 github 中使用 pre-receive 钩子,所以我们在客户端使用 pre-commit 钩子: http://git-scm.com/book/en/Customizing-Git-Git-Hooks#Client-Side-Hooks

    我们的预提交脚本(基于http://mark-story.com/posts/view/using-git-commit-hooks-to-prevent-stupid-mistakes)如下所示:

    #!/bin/sh
    
    for FILE in `git diff-index -p -M --name-status HEAD -- | cut -c3-` ; do
        if [ "grep 'TODO' $FILE" ]
        then
            echo $FILE ' contains TODO'
            exit 1
        fi
    done
    exit
    

    我们有这个脚本在我们的控制版本系统下,并在 .git/hooks 中创建一个指向它的符号链接

    感谢您的帮助:)

    编辑:由于 if 语句中的 grep 行为,我们需要编辑脚本:

    #!/bin/sh
    
    for FILE in `git diff --name-only --cached`; do
        grep 'TODO' $FILE 2>&1 >/dev/null
        if [ $? -eq 0 ]; then
            echo $FILE ' contains TODO'
            exit 1
        fi
    done
    exit
    

    【讨论】:

    • 创建文件后别忘了chmod +x .git/hooks/pre-commit
    【解决方案2】:

    服务器上的预接收钩子,grep文件并中止推送:)

    有关预接收挂钩的更多信息可以在这里找到:http://git-scm.com/book/en/Customizing-Git-Git-Hooks#Server-Side-Hooks

    【讨论】:

    • 有什么想法或链接可以在哪里阅读有关在 github 中工作的信息?我们的项目托管在 github 上,我只看到 post-receive 钩子:?当然我们可以使用客户端的钩子(比如 pre-commit 钩子),但是我们需要在我们所有的机器上创建它。感谢您的帮助:)
    猜你喜欢
    • 2017-06-01
    • 2013-06-28
    • 2022-12-05
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 2018-06-29
    • 2021-05-15
    相关资源
    最近更新 更多