0

I am currently trying to create a short and portable way to check if my working git directory is dirty and return T/F accordingly. My ultimate goal is to incorporate this into a Makefile and I have implemented it as such:

GIT_DIRTY       := $(shell [[ -n $(git status -s) ]] && echo '\ dev')

My issue is that the [[ operator is bash only, and as a requirement this Makefile needs to be able to run with sh only, no more fully featured shells. My hunch is that there is a way to accomplish this using [ or test, but I am not sure how.

Thanks!

Decade
  • 3

1 Answers1

0
GIT_DIRTY := $(shell [ -n "$(git status -s)" ] && echo '\ dev')

should work - but it doesn't, even though the command works fine in sh. It seems something in the shell function of make does not like the command.

Here's a workaround using a simpler command to give to shell and using a conditional function in make itself to do the rest of the job:

GIT_STATUS := $(shell git status -s)

GIT_DIRTY := $(if ${GIT_STATUS},DIRTY,CLEAN)

all: @echo ${GIT_DIRTY}

NickD
  • 2,926
  • 1
    This worked for me. However, just as a heads up, the leading spaces in front of "DIRTY" and "CLEAN" also become part of the value of "GIT_DIRTY". This caused some issues for me since I was using "GIT_DIRTY" as a "-D" preprocessor argument for gcc and the space made GCC look for files called "DIRTY". To match my original question "DIRTY" and "CLEAN" should be "\ DIRTY" and "\ CLEAN". – Decade Nov 30 '21 at 13:59
  • Yes, that's right - I'll get rid of the spaces. – NickD Nov 30 '21 at 15:23