X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Fgenversion.cpp;h=405d39160304ca72a2b97e5139482f09e53b3346;hp=02a7a5eedf1175fdd6148b8c3253d99d361b20fa;hb=2dca302472ee44923e164784191891711eff626a;hpb=21bae45824145c256bdca18b84b34e31aa2f668d diff --git a/src/common/genversion.cpp b/src/common/genversion.cpp index 02a7a5ee..405d3916 100644 --- a/src/common/genversion.cpp +++ b/src/common/genversion.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-08 by the Quassel Project * + * Copyright (C) 2005-2014 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -15,12 +15,11 @@ * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ /** This is called at compile time and generates a suitable version.gen. * usage: genversion git_root target_file - * */ #include @@ -29,64 +28,113 @@ #include #include #include +#include -int main(int argc, char **argv) { - if(argc < 3) { - qFatal("Usage: ./genversion "); - return 255; - } - QString gitroot = argv[1]; - QString target = argv[2]; - QString version, commit, archivetime; - - if(QFile::exists(gitroot + "/.git")) { - // try to execute git-describe to get a version string - QProcess git; - git.setWorkingDirectory(gitroot); - git.start("git-describe", QStringList("--long")); - if(git.waitForFinished(10000)) { - QString gitversion = git.readAllStandardOutput(); - if(!gitversion.isEmpty() && !gitversion.contains("fatal")) { - // seems we have a valid version string, now prettify it... - // check if the workdir is dirty first - QString dirty; - QStringList params = QStringList() << "--name-only" << "HEAD"; - git.start("git-diff-index", params); - if(git.waitForFinished(10000)) { - if(!git.readAllStandardOutput().isEmpty()) dirty = "*"; +int main(int argc, char **argv) +{ + if (argc < 3) { + qFatal("Usage: ./genversion "); + return 255; + } + + QCoreApplication app(argc, argv); + + QString gitroot = app.arguments()[1]; + QString target = app.arguments()[2]; + QString basever, protover, clientneeds, coreneeds, descrver, dirty; + QString committish, commitdate; + + // check Git for information if present + if (QFile::exists(gitroot + "/.git")) { + // try to execute git-describe to get a version string + QProcess git; + git.setWorkingDirectory(gitroot); + #ifdef Q_OS_WIN + git.start("cmd.exe", QStringList() << "/C" << "git" << "describe" << "--long"); + #else + git.start("git", QStringList() << "describe" << "--long"); + #endif + if (git.waitForFinished(10000)) { + QString descr = git.readAllStandardOutput().trimmed(); + if (!descr.isEmpty() && !descr.contains("fatal")) { + // seems we have a valid git describe string + descrver = descr; + // check if the workdir is dirty + #ifdef Q_OS_WIN + git.start("cmd.exe", QStringList() << "/C" << "git" << "diff-index" << "--name-only" << "HEAD"); + #else + git.start("git", QStringList() << "diff-index" << "--name-only" << "HEAD"); + #endif + if (git.waitForFinished(10000)) { + if (!git.readAllStandardOutput().isEmpty()) dirty = "*"; + } + // get a full committish + #ifdef Q_OS_WIN + git.start("cmd.exe", QStringList() << "/C" << "git" << "rev-parse" << "HEAD"); + #else + git.start("git", QStringList() << "rev-parse" << "HEAD"); + #endif + if (git.waitForFinished(10000)) { + committish = git.readAllStandardOutput().trimmed(); + } + // Now we do some replacement magic... + //QRegExp rxCheckTag("(.*)-0-g[0-9a-f]+\n$"); + //QRegExp rxGittify("(.*)-(\\d+)-g([0-9a-f]+)\n$"); + //gitversion.replace(rxCheckTag, QString("\\1%1").arg(dirty)); + //gitversion.replace(rxGittify, QString("\\1:git-\\3+\\2%1").arg(dirty)); + } } - // Now we do some replacement magic... - QRegExp rxCheckTag("(.*)-0-g[0-9a-f]+\n$"); - QRegExp rxGittify("(.*)-(\\d+)-g([0-9a-f]+)\n$"); - gitversion.replace(rxCheckTag, QString("\\1%1").arg(dirty)); - gitversion.replace(rxGittify, QString("\\1:git-\\3+\\2%1").arg(dirty)); - if(!gitversion.isEmpty()) version = gitversion; - } } - } - if(version.isEmpty()) { - // hmm, Git failed... let's check for version.dist instead - QFile dist(gitroot + "/version.dist"); - if(dist.open(QIODevice::ReadOnly | QIODevice::Text)) { - QRegExp rxCommit("(^[0-9a-f]+)"); - QRegExp rxTimestamp("(^[0-9]+)"); - if(rxCommit.indexIn(dist.readLine()) > -1) commit = rxCommit.cap(1); - if(rxTimestamp.indexIn(dist.readLine()) > -1) archivetime = rxTimestamp.cap(1); - dist.close(); + + // parse version.inc + QFile verfile(gitroot + "/version.inc"); + if (verfile.open(QIODevice::ReadOnly | QIODevice::Text)) { + QString ver = verfile.readAll(); + + QRegExp rxBasever("baseVersion\\s*=\\s*\"(.*)\";"); + if (rxBasever.indexIn(ver) >= 0) + basever = rxBasever.cap(1); + + QRegExp rxProtover("protocolVersion\\s*=\\s*(\\d+)"); + if (rxProtover.indexIn(ver) >= 0) + protover = rxProtover.cap(1); + + QRegExp rxClientneeds("clientNeedsProtocol\\s*=\\s*(\\d+)"); + if (rxClientneeds.indexIn(ver) >= 0) + clientneeds = rxClientneeds.cap(1); + + QRegExp rxCoreneeds("coreNeedsProtocol\\s*=\\s*(\\d+)"); + if (rxCoreneeds.indexIn(ver) >= 0) + coreneeds = rxCoreneeds.cap(1); + + if (committish.isEmpty()) { + QRegExp rxCommit("distCommittish\\s*=\\s*([0-9a-f]+)"); + if (rxCommit.indexIn(ver) >= 0) committish = rxCommit.cap(1); + } + + QRegExp rxTimestamp("distCommitDate\\s*=\\s*([0-9]+)"); + if (rxTimestamp.indexIn(ver) >= 0) commitdate = rxTimestamp.cap(1); + verfile.close(); + } + + // generate the contents for version.gen + QByteArray contents = QString("QString buildinfo = \"%1,%2,%3,%4,%5,%6,%7,%8\";\n") + .arg(basever, descrver, dirty, committish, commitdate, protover, clientneeds, coreneeds) + .toAscii(); + + QFile gen(target); + if (!gen.open(QIODevice::ReadWrite | QIODevice::Text)) { + qFatal("%s", qPrintable(QString("Could not write %1!").arg(target))); + return EXIT_FAILURE; } - } - // ok, create our version.gen now - QFile gen(target); - if(!gen.open(QIODevice::WriteOnly | QIODevice::Text)) { - qFatal("%s", qPrintable(QString("Could not write %1!").arg(target))); - return 255; - } - gen.write(QString("quasselGeneratedVersion = \"%1\";\n" - "quasselBuildDate = \"%2\";\n" - "quasselBuildTime = \"%3\";\n" - "quasselCommit = \"%4\";\n" - "quasselArchiveDate = %5;\n") - .arg(version).arg(__DATE__).arg(__TIME__).arg(commit).arg(archivetime.toUInt()).toAscii()); - gen.close(); - return EXIT_SUCCESS; + QByteArray oldContents = gen.readAll(); + if (oldContents != contents) { // only touch the file if something changed + gen.seek(0); + gen.resize(0); + gen.write(contents); + gen.waitForBytesWritten(10000); + } + gen.close(); + + return EXIT_SUCCESS; }