[Qbs] Get Git commit hash

Christian Kandeler christian.kandeler at qt.io
Fri Aug 11 10:00:22 CEST 2017

On Thu, 10 Aug 2017 23:36:43 +0200
Rémi Thébault <remi.thebault at gmail.com> wrote:
> I'd like Qbs to generate a development archive file with the current git
> commit hash in the name.
> I came up with this solution:

[ ... ]

> I havn't given much tests, but it appears to work fine.
> There's a downside however: I get tons of warning in QtCreator about using
> TextFile during property evaluation.
> Warnings suggest using a Probe instead, which I've tried.
> It doesn't fit quite nicely however, as I can't get the probe to run again
> (from QtCreator at least) after a commit or checkout.

There are two circumstances under which a Probe's configure script is re-run for an existing build:
    a) You force it using the --force-probe-execution option or the equivalent
      checkbox in the Qt Creator build settings. This is meant to be used when
      probing for external resources such as tools that may or may not be
      present on your system. If you install or de-install such a tool after
      the Probe has run, you need to force re-probing to get your project state
      up to date. 
    b) One of the Probe's properties changes its value.

You can make use of b) by writing your Probe like this:

Probe {
    id: gitProbe
    property var gitTimestamp1: File.lastModified(project.sourceDirectory + "/.git")
    property var gitTimestamp2: File.lastModified(project.sourceDirectory + "/.git/refs/heads")
    // ...
    configure: { /* ... */ }

This Probe is re-run if (and only if) a relevant timestamp in the git metadata changes. (Not sure about the set of git files to look at, but you get the idea.) 
By the way, for retrieving the actual commit hash, wouldn't it make more sense to simply run git, rather than parsing the metadata yourself?

Hope this helps,

More information about the Qbs mailing list