Solved Git commit date in .pro
Hello!! I am trying to use a commit date in my code. I am using this command:
GIT_DATE = $$system(git --git-dir $$PWD\..\.git --work-tree $$PWD log -1 --format=%cd) DEFINES += GIT_DATE=\\\"$$GIT_DATE\\\"
When I use this variable in a .cpp, it seems that some of the command doesn't like qt, because it behaves as if it did not recognize the variable.
If I don't include the "-1" in the command, the error dissappears but the program doesn't compile I think because the output of the command is too long.
How can I fix it??
Thank you very much!
GIT_DATE = $$system(git --git-dir $$PWD\..\.git --work-tree $$PWD log -n 1 --format=%cd)
@ivanicy What's the full error message that you get when you try to build? Your screenshot mentions a "unexpected expression" but the whole error message might include what the compiler thinks the expression is, which can be a useful hint about what exactly is going wrong. Also, if you are building verbosely with the compiler invocation, you'll also see the definition as it is passed to the compiler.
I wouldn't be surprised if the ''s in your git invocation are resulting in an odd path being passed in '--git-dir' Windows?
@SGaist Yes, I tried it, but it throws the same error I showed
By the way, why are you passing
--work-treeto that command ?
@aha_1980 The output of the command in the General Messages tab is:
Mon Dec 2 16:42:48 2019 +0100
This is exactly that I want but when I use the variable in a .cpp, it gives me this error
@SGaist I am not an expert in this commands. I found an example and it came with those modifiers
@wrosecrans When I build the project, The error that it throws is that there is a ")" missing. This is because the compiler can't recognise or can't use the variable. Let me show another command that works perfectly:
GIT_VERSION = $$system(git --git-dir $$PWD\..\.git --work-tree $$PWD describe --always --tags) DEFINES += GIT_VERSION=\\\"$$GIT_VERSION\\\"
This command gives the last tag and the number of commits after this tag. And, like I said, works perfectly.
Because GIT_VERSION is a string without any spaces.
You need to escape them all to use them in a define on the command line.
GIT_DATE = $$join($$list($$GIT_DATE), "\ ")
@SGaist Thank you very much!!! I changed the "\ " to "|" because it puts a blank and it works!
why did you delete this topic? You should have rather marked it as SOLVED so others know that a solution has been found.
@aha_1980 Yes, It was a mistake. It is solved now. Thank you