5

我试图deploy在我的 Makefile 中创建一个命令,它只是覆盖到分支deployment,然后将此分支推送到origin.

但是,当工作树不为空时,该命令必须停止/失败并显示错误消息。

类似于以下内容:

deploy:

    status=$(git status --porcelain)
    test "x$(status)" = "x"
    git branch -f deployment
    git push origin deployment

不幸的是,这个测试和状态变量似乎没有按预期运行。

一个人将如何实现这一目标?我真的应该使用test吗?

4

1 回答 1

14

用于git diff-index检查 repo 是否脏:

deploy:
        git diff-index --quiet HEAD 
        git branch -f deployment
        git push origin deployment

如果要检查 makefile 中的 shell 变量,则需要确保在与设置变量的 shell 相同的 shell 中检查变量的值。Make 将在单独的 shell 中调用每个命令,因此您需要执行以下操作:

deploy:
        @status=$$(git status --porcelain); \
        if test "x$${status}" = x; then \
            git branch -f deployment; \
            git push origin deployment; \
        else \
            echo Working directory is dirty >&2; \
        fi

请注意双 '$'、分号和续行符。

于 2012-05-11T20:01:46.977 回答