Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get git-commit ID during build and display it as version information

How to get git-commit ID during build and display it as version information

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 2.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    ynatynat
    wrote on 11 Oct 2023, 09:06 last edited by
    #1

    I would like to get the Git commit ID when building and display that ID as the software version number.
    Is there a good way?

    K 1 Reply Last reply 11 Oct 2023, 20:35
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 11 Oct 2023, 18:39 last edited by
      #2

      Hi,

      Are you using qmake or cmake ?

      In either case, what you can do is generate a header file from a template that contains that information.

      You will have to execute git and retrieve the result that you will then use as input for said template.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • Y ynatynat
        11 Oct 2023, 09:06

        I would like to get the Git commit ID when building and display that ID as the software version number.
        Is there a good way?

        K Offline
        K Offline
        KH-219Design
        wrote on 11 Oct 2023, 20:35 last edited by
        #3

        @ynatynat As SGaist indicated, cmake has "magic" to do this for you.

        You can also "roll your own" fairly simply. I use this bash script:

        https://github.com/219-design/qt-qml-project-template-with-ci/blob/master/tools/update_version_strings.sh

        Which is invoked (for example, on github CI Actions) like so:

        tools/update_version_strings.sh \
          /home/runner/work/qt-qml-project-template-with-ci/qt-qml-project-template-with-ci/src/version.genfiles.input/version.h \
          /home/runner/work/qt-qml-project-template-with-ci/qt-qml-project-template-with-ci/build/generated_files/autogenerated/version.h
        

        Then you have to make sure that the path to "build/generated_files/autogenerated/version.h" is part of INCLUDEPATH for anything that needs to #include version.h

        www.219design.com
        Software | Electrical | Mechanical | Product Design

        Y 1 Reply Last reply 13 Oct 2023, 09:26
        0
        • K KH-219Design
          11 Oct 2023, 20:35

          @ynatynat As SGaist indicated, cmake has "magic" to do this for you.

          You can also "roll your own" fairly simply. I use this bash script:

          https://github.com/219-design/qt-qml-project-template-with-ci/blob/master/tools/update_version_strings.sh

          Which is invoked (for example, on github CI Actions) like so:

          tools/update_version_strings.sh \
            /home/runner/work/qt-qml-project-template-with-ci/qt-qml-project-template-with-ci/src/version.genfiles.input/version.h \
            /home/runner/work/qt-qml-project-template-with-ci/qt-qml-project-template-with-ci/build/generated_files/autogenerated/version.h
          

          Then you have to make sure that the path to "build/generated_files/autogenerated/version.h" is part of INCLUDEPATH for anything that needs to #include version.h

          Y Offline
          Y Offline
          ynatynat
          wrote on 13 Oct 2023, 09:26 last edited by ynatynat
          #4

          @SGaist
          @KH-219Design

          Thank you for your reply.
          The shell script is nice to get version.

          I am using qmake.
          How to contain the version in qmake?
          .pro file, build setting on qtcreator ,etc ...

          K J 2 Replies Last reply 13 Oct 2023, 16:18
          0
          • Y ynatynat
            13 Oct 2023, 09:26

            @SGaist
            @KH-219Design

            Thank you for your reply.
            The shell script is nice to get version.

            I am using qmake.
            How to contain the version in qmake?
            .pro file, build setting on qtcreator ,etc ...

            K Offline
            K Offline
            KH-219Design
            wrote on 13 Oct 2023, 16:18 last edited by
            #5

            @ynatynat I don't know that I can be of any further help here. I do not use Qt Creator. I do use qmake, but I have an outer bash script that drives the entire build, and the outermost bash script first invokes my versioning script and then invokes qmake. So in my solution, qmake has no idea about where the version comes from (it has already been produced by the time qmake gets involved). The only part that needs to be "known" to the pro files is what I mentioned earlier about INCLUDEPATH:

            src/app/app.pro:15:INCLUDEPATH += $${top_srcdir}/build/generated_files # for version.h
            

            You can probably look at 50 different projects and see 50 variations on how to make a version header. I absolutely do not claim that mine is the "best" solution. But this has been one area of my projects where I have applied the mindset of "put in place a working, good-enough, adequate, well-understood solution and then just move on to solving bigger issues."

            www.219design.com
            Software | Electrical | Mechanical | Product Design

            1 Reply Last reply
            0
            • Y ynatynat
              13 Oct 2023, 09:26

              @SGaist
              @KH-219Design

              Thank you for your reply.
              The shell script is nice to get version.

              I am using qmake.
              How to contain the version in qmake?
              .pro file, build setting on qtcreator ,etc ...

              J Offline
              J Offline
              JoeCFD
              wrote on 13 Oct 2023, 18:06 last edited by JoeCFD
              #6

              @ynatynat
              I guess you can get the current number with
              git rev-list --count HEAD
              this command can be added to pro file as

              GIT_PATH=$$system(which git)
              !isEmpty(GIT_PATH) {
                  BUILD_VERSION=$$system($$GIT_PATH rev-list --count HEAD)
              }
              isEmpty(BUILD_VERSION) {
                  BUILD_VERSION=0
              }
              
              DEFINES += "BUILD_SERIES_NUMBER=\"\\\"$$BUILD_VERSION\\\"\""
              

              BUILD_SERIES_NUMBER can be accessed in qt gui code.

              Y 1 Reply Last reply 16 Oct 2023, 01:58
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 13 Oct 2023, 18:34 last edited by
                #7

                QMAKE_SUBSTITUTE as described here will avoid the need to fiddle with all the backslash escaping changing from one OS to another.

                In the example linked they used it with define but you can create a QString with it or even a basic char array.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • J JoeCFD
                  13 Oct 2023, 18:06

                  @ynatynat
                  I guess you can get the current number with
                  git rev-list --count HEAD
                  this command can be added to pro file as

                  GIT_PATH=$$system(which git)
                  !isEmpty(GIT_PATH) {
                      BUILD_VERSION=$$system($$GIT_PATH rev-list --count HEAD)
                  }
                  isEmpty(BUILD_VERSION) {
                      BUILD_VERSION=0
                  }
                  
                  DEFINES += "BUILD_SERIES_NUMBER=\"\\\"$$BUILD_VERSION\\\"\""
                  

                  BUILD_SERIES_NUMBER can be accessed in qt gui code.

                  Y Offline
                  Y Offline
                  ynatynat
                  wrote on 16 Oct 2023, 01:58 last edited by
                  #8

                  @KH-219Design @JoeCFD @SGaist
                  Thank you for nice solutions!
                  I tried @JoeCFD 's solution. It worked and suitable for me!

                  1 Reply Last reply
                  0
                  • Y ynatynat has marked this topic as solved on 16 Oct 2023, 01:59

                  1/8

                  11 Oct 2023, 09:06

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved