Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Why replace not work as expected in qmake?



  • #output: 2021-06-13 17:32:17
    GIT_TIME = $$system(git log -1 --date="format:"%Y-%m-%d %T"" --format="%cd")
    #why this connot get 2021? always output: 2021 17:32:17
    APP_COPYRIGHT_YEAR = $$replace(GIT_TIME, -[\s\S]*, )


  • Lifetime Qt Champion

    Hi,

    Which version of Qt are you using ?
    Which version of git are you using ?
    Silly question: why no just get the year directly from your git command ?



  • @shun said in Why replace not work as expected in qmake?:

    GIT_TIME = $$system(git log -1 --date="format:"%Y-%m-%d %T"" --format="%cd")

    This has a double-quoted string inside a double-quoted string. I am surprised it works at all. I had to adapt it to work here:

    GIT_TIME = $$system(git log -1 --date=format:%Y-%m-%d\ %T --format=%cd)
    APP_COPYRIGHT_YEAR = $$replace(GIT_TIME, -[\s\S]*, )
    

    In this version I get a result like yours. The reason is that GIT_TIME is being established as a list of two values '2021-06-14' and '21:47:47'. replace() is acting only on the first value and leaving the second intact. You could do this:

    GIT_TIME = $$quote( $$system(git log -1 --date=format:%Y-%m-%d\ %T --format=%cd) )
    

    to make GIT_TIME a single string.

    But, as SGaist says, just ask git for the value you want to start with:

    APP_COPYRIGHT_YEAR = $$system(git log -1 --date=format:%Y --format=%cd)
    


  • @SGaist @ChrisW67 thanks for reply.
    The reason that use git time instead of git year, I have to use it something like that build time, so I want to get the year from qmake built-in function.

    @ChrisW67 when use your version, it will get the error:
    fatal: ambiguous argument '%T': unknown revision or path not in the working tree.
    I'm using the lastest git in windows 10, with qt 5.9.

    The actual version is: GIT_TIME = $$system(git log -1 --date="format:\"%Y-%m-%d %T\"" --format="%cd")

    My first version, it seems that the forum transferred the \".

    May i ask another question here? How to make qmake print message only once, even in sub project? The !build_pass condition, it seems that work only in one project.



  • @shun said in Why replace not work as expected in qmake?:

    The reason that use git time instead of git year, I have to use it something like that build time, so I want to get the year from qmake built-in function.

    The time and date you are asking from from git is that on the latest log entry, which has nothing to do with the build time. You throw away the month, day, and time components anyway.

    If you want the year at build (actually qmake) time:

    win32 {
        year = $$system("echo %date:~10,4%")
    } else {
        year = $$system("date +%Y")
    }
    

    I'm using the lastest git in windows 10, with qt 5.9.

    I am using Linux. Quoting rules are different. You can probably do with fewer quotes even on Windows.

    May i ask another question here? How to make qmake print message only once, even in sub project? The !build_pass condition, it seems that work only in one project.

    message() outputs when processed by qmake. In a subdir project, qmake is not run in the sub-directories until the first time make is run. Qmake will not run again on subsequent make invocations.
    Perhaps I do not understand what problem you asking about.



  • @ChrisW67 thanks a lot.

    I means that rules are written in version.pri, and included by each sub project, but i want it calculated only once in a solution. Is it possible?


Log in to reply