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
?
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.
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With