Introduce gitattributes and version.dist to autogenerate version info for archives
[quassel.git] / src / common / genversion.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 /** This is called at compile time and generates a suitable version.gen.
22  *  usage: genversion git_root target_file
23  * 
24  */
25
26 #include <QDebug>
27 #include <QProcess>
28 #include <QString>
29 #include <QStringList>
30 #include <QRegExp>
31 #include <QFile>
32
33 int main(int argc, char **argv) {
34   if(argc < 3) {
35     qFatal("Usage: ./genversion <git_root> <target_file>");
36     return 255;
37   }
38   QString gitroot = argv[1];
39   QString target = argv[2];
40   QString version;
41
42   if(QFile::exists(gitroot + "/.git")) {
43     // try to execute git-describe to get a version string
44     QProcess git;
45     git.setWorkingDirectory(gitroot);
46     git.start("git-describe", QStringList("--long"));
47     if(git.waitForFinished(10000)) {
48       QString gitversion = git.readAllStandardOutput();
49       if(!gitversion.isEmpty() && !gitversion.contains("fatal")) {
50         // seems we have a valid version string, now prettify it...
51         // check if the workdir is dirty first
52         QString dirty;
53         QStringList params = QStringList() << "--name-only" << "HEAD";
54         git.start("git-diff-index", params);
55         if(git.waitForFinished(10000)) {
56           if(!git.readAllStandardOutput().isEmpty()) dirty = "*";
57         }
58         // Now we do some replacement magic...
59         QRegExp rxCheckTag("(.*)-0-g[0-9a-f]+\n$");
60         QRegExp rxGittify("(.*)-(\\d+)-g([0-9a-f]+)\n$");
61         gitversion.replace(rxCheckTag, QString("\\1%1").arg(dirty));
62         gitversion.replace(rxGittify, QString("\\1:git-\\3+\\2%1").arg(dirty));
63         if(!gitversion.isEmpty()) version = gitversion;
64       }
65     }
66   }
67   if(version.isEmpty()) {
68     // hmm, Git failed... let's check for version.dist instead
69     QFile dist(gitroot + "/version.dist");
70     if(dist.open(QIODevice::ReadOnly | QIODevice::Text)) {
71       version = dist.readAll();
72       dist.close();
73     }
74   }
75   // ok, create our version.gen now
76   QFile gen(target);
77   if(!gen.open(QIODevice::WriteOnly | QIODevice::Text)) {
78     qFatal("%s", qPrintable(QString("Could not write %1!").arg(target)));
79     return 255;
80   }
81   gen.write(QString("quasselGeneratedVersion = \"%1\";\n"
82                     "quasselBuildDate = \"%2\";\n"
83                     "quasselBuildTime = \"%3\";\n").arg(version).arg(__DATE__).arg(__TIME__).toAscii());
84   gen.close();
85   return EXIT_SUCCESS;
86 }