Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a clean git working repository in a makefile?

I'm trying to make a deploy command in my Makefile, which simply overwrites to the branch deployment and then pushes this branch to origin.

However, the command must stop/fail with an error message when the working tree is not empty.

Something like the following:

deploy:

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

Unfortunately, this test on and status variable do not seem to function as wanted.

How would one achieve this? Am I indeed supposed to use test?

like image 335
Tom Avatar asked May 11 '12 19:05

Tom


People also ask

How does Git clean work?

Cleans the working tree by recursively removing files that are not under version control, starting from the current directory. Normally, only files unknown to Git are removed, but if the -x option is specified, ignored files are also removed. This can, for example, be useful to remove all build products.

What does Git clean mean?

Summary. To recap, git clean is a convenience method for deleting untracked files in a repo's working directory. Untracked files are those that are in the repo's directory but have not yet been added to the repo's index with git add .

What is Git dirty?

According to the official Git documentation, in the section on Stashing, a dirty state is defined as ... the dirty state of your working directory — that is, your modified tracked files and staged changes . From this definition, files staged for commit are dirty as well.


1 Answers

Use git diff-index to check if the repo is dirty:

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

If you want to check shell variables in a makefile, you need to ensure that you check the value of the variable in the same shell as the one in which it is set. Make will invoke each command in a separate shell, so you would need to do something like:

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

Note the double '$', the semi-colon, and the line continuations.

like image 73
William Pursell Avatar answered Sep 28 '22 15:09

William Pursell