[Qbs] Get Git commit hash

Rémi Thébault remi.thebault at gmail.com
Thu Aug 10 23:36:43 CEST 2017


Hi,

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:

// gitutils.js:
var TextFile = require("qbs.TextFile");
var FileInfo = require("qbs.FileInfo");

function getGitFileLine(projectPath, fileName)
{
    var filePath = FileInfo.joinPaths(projectPath, ".git", fileName);
    var file = new TextFile(filePath, TextFile.ReadOnly);
    var str = file.readLine();
    file.close();
    return str;
}

function getCommitHash(projectPath)
{
    var headStr = getGitFileLine(projectPath, "HEAD");

    var hash = "unknown_hash";
    if (!headStr.startsWith("ref: ")) {
        // most likely detach-head, this is the commit hash
        hash = headStr.substr(0, 7);
    }
    else {
        // most likely on a branch, we must follow the ref
        var branchRef = headStr.substr(5);
        var branchCommit = getGitFileLine(projectPath, branchRef);
        hash = branchCommit.substr(0, 7);
    }

    return hash;
}

// project.qbs:
import 'gitutils.js' as Git

InstallPackage {

    archiver.type: "7zip"

    name: "MyAwesomeProject-"+Git.getCommitHash(project.sourceDirectory)


    Depends { name: "..." }
    ...

}


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.

Is there a better way to proceed? Or a way to shut the warnings down?

Thanks and regards,
Rémi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/qbs/attachments/20170810/9d922e42/attachment.html>


More information about the Qbs mailing list