From f11410eaebd0492b531740f09b3670e278dd0819 Mon Sep 17 00:00:00 2001
From: Lukas Holecek <hluk@email.cz>
Date: Sat, 23 May 2015 14:25:50 +0200
Subject: [PATCH] Retrieve application version using git

---
 CMakeLists.txt                 | 16 ++++++++++++++++
 src/common/common.h            |  3 ---
 src/common/version.h           |  8 ++++++++
 src/gui/aboutdialog.cpp        |  3 ++-
 src/scriptable/scriptable.cpp  |  5 +++--
 src/tests/tests.cpp            |  3 ++-
 utils/create_source_package.sh |  4 ++++
 7 files changed, 35 insertions(+), 7 deletions(-)
 create mode 100644 src/common/version.h

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8302bb078..f2d82bb35 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -71,6 +71,22 @@ if(WITH_TESTS)
     endif()
 endif()
 
+find_package(Git)
+if(GIT_FOUND)
+    execute_process(COMMAND
+        "${GIT_EXECUTABLE}" describe
+        WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
+        RESULT_VARIABLE copyq_git_describe_result
+        OUTPUT_VARIABLE copyq_git_describe_output
+        ERROR_QUIET
+        OUTPUT_STRIP_TRAILING_WHITESPACE
+        )
+    if(copyq_git_describe_result EQUAL 0)
+        message(STATUS "Building CopyQ version ${copyq_git_describe_output}.")
+        add_definitions( -DCOPYQ_VERSION="${copyq_git_describe_output}" )
+    endif()
+endif()
+
 if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
     install(FILES ${copyq_ICON_NORMAL} DESTINATION ${ICON_INSTALL_PREFIX} RENAME copyq-normal.svg)
     install(FILES ${copyq_ICON_BUSY}   DESTINATION ${ICON_INSTALL_PREFIX} RENAME copyq-busy.svg)
diff --git a/src/common/common.h b/src/common/common.h
index 161c5b471..e8ff06f5e 100644
--- a/src/common/common.h
+++ b/src/common/common.h
@@ -26,9 +26,6 @@
 #include <QtGlobal> // Q_WS_*
 #include <QVariantMap>
 
-// Application version
-#define COPYQ_VERSION "2.4.6"
-
 class QAction;
 class QByteArray;
 class QIODevice;
diff --git a/src/common/version.h b/src/common/version.h
new file mode 100644
index 000000000..96d90361d
--- /dev/null
+++ b/src/common/version.h
@@ -0,0 +1,8 @@
+#ifndef VERSION_H
+#define VERSION_H
+
+#ifndef COPYQ_VERSION
+#   define COPYQ_VERSION "v2.4.6"
+#endif
+
+#endif
diff --git a/src/gui/aboutdialog.cpp b/src/gui/aboutdialog.cpp
index 28af611fd..1935d0b36 100644
--- a/src/gui/aboutdialog.cpp
+++ b/src/gui/aboutdialog.cpp
@@ -21,6 +21,7 @@
 #include "ui_aboutdialog.h"
 
 #include "common/common.h"
+#include "common/version.h"
 #include "configurationmanager.h"
 
 namespace {
@@ -131,7 +132,7 @@ QString AboutDialog::aboutPage()
         "<div class='h1'>CopyQ</div>"
         // subtitle
         "<div class=\"h1x\">" + escapeHtml(tr("Clipboard Manager"))
-            + " v" COPYQ_VERSION "</div>"
+            + " " COPYQ_VERSION "</div>"
 
         "<p>"
         "<table class='links'>"
diff --git a/src/scriptable/scriptable.cpp b/src/scriptable/scriptable.cpp
index 30e2414fd..c34c59ae4 100644
--- a/src/scriptable/scriptable.cpp
+++ b/src/scriptable/scriptable.cpp
@@ -24,6 +24,7 @@
 #include "common/commandstatus.h"
 #include "common/common.h"
 #include "common/mimetypes.h"
+#include "common/version.h"
 #include "item/serialize.h"
 #include "scriptable/commandhelp.h"
 #include "scriptable/dirclass.h"
@@ -368,7 +369,7 @@ void Scriptable::sendWindowActivationCommandToClient(const QByteArray &message)
 
 QScriptValue Scriptable::version()
 {
-    return tr(programName) + " v" COPYQ_VERSION " (hluk@email.cz)\n"
+    return tr(programName) + " " COPYQ_VERSION " (hluk@email.cz)\n"
             + tr("Built with: ")
             + "Qt " + QT_VERSION_STR +
             + ", LibQxt " + QXT_VERSION_STR
@@ -386,7 +387,7 @@ QScriptValue Scriptable::help()
             helpString.append(hlp.toString());
 
         helpString.append("\n" + helpTail() + "\n\n" + tr(programName)
-            + " v" + COPYQ_VERSION + " (hluk@email.cz)\n");
+            + " " + COPYQ_VERSION + " (hluk@email.cz)\n");
     } else {
         for (int i = 0; i < argumentCount(); ++i) {
             const QString &cmd = toString(argument(i));
diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp
index eace6bb0b..89593cd6c 100644
--- a/src/tests/tests.cpp
+++ b/src/tests/tests.cpp
@@ -24,6 +24,7 @@
 #include "common/common.h"
 #include "common/mimetypes.h"
 #include "common/monitormessagecode.h"
+#include "common/version.h"
 #include "item/itemfactory.h"
 #include "item/itemwidget.h"
 #include "item/serialize.h"
@@ -721,7 +722,7 @@ void Tests::versionCommand()
 
     const QString version = QString::fromUtf8(stdoutActual);
     // Version contains application name and version.
-    QVERIFY( version.contains(QRegExp("\\bCopyQ\\b.*v" + QRegExp::escape(COPYQ_VERSION))) );
+    QVERIFY( version.contains(QRegExp("\\bCopyQ\\b.*" + QRegExp::escape(COPYQ_VERSION))) );
     // Version contains Qt version.
     QVERIFY( version.contains(QRegExp("\\bQt\\s+\\d")) );
     // Version contains Qxt version.
diff --git a/utils/create_source_package.sh b/utils/create_source_package.sh
index 557082802..ed7501d7c 100755
--- a/utils/create_source_package.sh
+++ b/utils/create_source_package.sh
@@ -1,6 +1,7 @@
 #!/bin/bash
 version=$1
 out=${2:-"copyq-${version}.tar.gz"}
+version_header="src/common/version.h"
 
 set -e
 
@@ -12,6 +13,9 @@ die () {
 grep -q '^v'"$version"'$' CHANGES ||
     die "CHANGES file doesn't contain changes for given version!"
 
+grep -q '"v'"$version"'"' "$version_header" ||
+    die "String for given version is missing in \"$version_header\" file!"
+
 git archive --format=tar.gz --prefix="copyq-$version/" --output="$out" "v$version" ||
     die "First arguments must be existing version (tag v<VERSION> must exist in repository)!"
 
-- 
GitLab