如何在makefile中强制使用干净的git工作存储库?

Tom*_*Tom 4 git makefile working-directory

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

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

类似于以下内容:

deploy:

    status=$(git status --porcelain)
    test "x$(status)" = "x"
    git branch -f deployment
    git push origin deployment
Run Code Online (Sandbox Code Playgroud)

不幸的是,这个测试和状态变量似乎没有按照需要运行.

如何实现这一目标?我确实应该使用test吗?

Wil*_*ell 12

使用git diff-index检查,如果回购是脏的:

deploy:
        git diff-index --quiet HEAD 
        git branch -f deployment
        git push origin deployment
Run Code Online (Sandbox Code Playgroud)

如果要在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
Run Code Online (Sandbox Code Playgroud)

注意双'$',分号和行连续.