diff --git a/CMakeLists.txt b/CMakeLists.txt index ef99ab4..6dc88ee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,12 +8,17 @@ set(CMAKE_AUTORCC ON) set(CMAKE_AUTOUIC ON) # The repo-root `version` file is the single source of truth for release -# versioning. It is compiled into the binaries as well as installed next to -# them, so a running app always reports the release it was built from instead -# of trusting a `version` file that an update may have left stale. +# versioning. It is read at configure time and compiled into the binaries, so a +# running app knows which release it was built from. file(READ ${CMAKE_CURRENT_SOURCE_DIR}/version PACMANAGER_VERSION) string(STRIP "${PACMANAGER_VERSION}" PACMANAGER_VERSION) +# Reading it above only happens during configuration, and bumping the file is +# not otherwise a reason to configure again: without this, `cmake --build` after +# a version bump kept building the previous version into the binary. +set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS + ${CMAKE_CURRENT_SOURCE_DIR}/version) + # Find Qt6 find_package(Qt6 REQUIRED COMPONENTS Widgets Network) diff --git a/README.en.md b/README.en.md index e99239d..f1cd04c 100644 --- a/README.en.md +++ b/README.en.md @@ -139,9 +139,12 @@ binary (and plugins) needs `sudo`, and only when the target directory is owned by root. PacManager then restarts automatically on the new version. All four methods install to the same place — the running binary's directory — -and rewrite the `version` file there. On top of that, the app reports the -version **compiled into the binary** (from the repo's `version` file) first, so -the same update is never offered again once it has been applied. +and rewrite the `version` file there. To know which release is really installed, +PacManager keeps the **newest** of two sources: the version compiled into the +binary (read from the repo's `version` file at configure time) and the one in the +`version` file next to the executable. An applied update is therefore never +offered again, even when one of the two lags behind (a binary rebuilt without +configuring again, an install older than the binary). ## Graphical interface diff --git a/README.md b/README.md index af89cc4..4858e4d 100644 --- a/README.md +++ b/README.md @@ -140,9 +140,13 @@ appartient à root. PacManager redémarre ensuite automatiquement sur la nouvell version. Les quatre méthodes installent au même endroit — le dossier du binaire en cours -— et y réécrivent le fichier `version`. L'application lit de plus en priorité la -version **compilée dans le binaire** (à partir du fichier `version` du dépôt) : -une mise à jour déjà appliquée n'est donc jamais reproposée au lancement. +— et y réécrivent le fichier `version`. Pour savoir quelle version est réellement +installée, PacManager retient la **plus récente** des deux sources : la version +compilée dans le binaire (lue dans le fichier `version` du dépôt au moment de la +configuration) et celle du fichier `version` posé à côté de l'exécutable. Une mise +à jour déjà appliquée n'est donc jamais reproposée, même quand l'une des deux +retarde (binaire recompilé sans reconfiguration, installation plus ancienne que +le binaire). ## Interface graphique diff --git a/include/update-manager.h b/include/update-manager.h index e859c1b..c8146e9 100644 --- a/include/update-manager.h +++ b/include/update-manager.h @@ -26,11 +26,17 @@ class UpdateManager : public QObject { public: explicit UpdateManager(QObject* parent = nullptr); - // Current app version, compiled in from the repo-root `version` file (see - // CMakeLists.txt), falling back to the `version` file installed next to the - // executable when the build bypassed CMake. + // Current app version: the newest of the version compiled in from the + // repo-root `version` file (see CMakeLists.txt) and the one recorded in the + // `version` file installed next to the executable. static QString currentVersion(); + // Highest of two dotted versions. Used to reconcile the two places that know + // the version: either can lag behind (a build made without configuring + // again, or a marker left by an older install), and reporting the lower one + // would offer an already installed release for ever. + static QString newestVersion(const QString& a, const QString& b); + // Path of the state file storing the last check date and the latest // known remote version (e.g. ~/.local/share/PacManager/update-state.json). static QString stateFilePath(); diff --git a/src/update-manager.cpp b/src/update-manager.cpp index a698e78..9353bbc 100644 --- a/src/update-manager.cpp +++ b/src/update-manager.cpp @@ -51,21 +51,28 @@ UpdateManager::UpdateManager(QObject* parent) : QObject(parent) { }); } -QString UpdateManager::currentVersion() { - // The compiled-in version wins: it describes the code that is actually - // running, so a `version` file left behind (or never refreshed) by an - // update cannot make the app re-offer a release it already installed. - QString version = QStringLiteral(FWSPACMANAGE_VERSION).trimmed(); +QString UpdateManager::newestVersion(const QString& a, const QString& b) { + // isNewerVersion() also normalises a leading "v", so "v1.4.1" wins over + // "1.3.1" and an empty side always loses. + return isNewerVersion(b, a) ? b : a; +} - // Fallback for builds that bypass CMake: the `version` file installed next - // to the executable. - if (version.isEmpty()) { - QFile f(QCoreApplication::applicationDirPath() + "/version"); - if (f.open(QIODevice::ReadOnly | QIODevice::Text)) { - version = QString::fromUtf8(f.readAll()).trimmed(); - } +QString UpdateManager::currentVersion() { + // Two places know the version, and either can lag behind: the version + // compiled into the binary (a build made without configuring again keeps the + // previous one) and the marker installed next to the executable (an install + // made before an update left the old one behind). Reporting the lower of the + // two would offer an already installed release again at every launch, so the + // newest one known wins. + const QString compiled = QStringLiteral(FWSPACMANAGE_VERSION).trimmed(); + + QString installed; + QFile file(QCoreApplication::applicationDirPath() + "/version"); + if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { + installed = QString::fromUtf8(file.readAll()).trimmed(); } + const QString version = newestVersion(compiled, installed); return version.isEmpty() ? QStringLiteral("1.0.0") : version; } diff --git a/tests/update-manager.test.cpp b/tests/update-manager.test.cpp index f61182c..ccc468d 100644 --- a/tests/update-manager.test.cpp +++ b/tests/update-manager.test.cpp @@ -119,12 +119,30 @@ static void testChooseMethod_noneWhenNothingAvailable() { "method_none_without_anything"); } -// The version the app reports is the one compiled into it, so a stale `version` -// file next to the executable can never make it re-offer an installed update. -static void testCurrentVersion_isCompiledIn() { - check(UpdateManager::currentVersion() == QStringLiteral(FWSPACMANAGE_VERSION), - "current_version_compiled_in"); - check(!UpdateManager::currentVersion().isEmpty(), "current_version_not_empty"); +// Reconciliation of the two sources of truth: whichever is ahead wins, an empty +// side never does. This is what stops a release from being offered again when the +// binary and the installed marker disagree. +static void testNewestVersion() { + check(UpdateManager::newestVersion("1.3.1", "1.4.1") == "1.4.1", + "newest_installed_ahead"); + check(UpdateManager::newestVersion("1.4.1", "1.3.1") == "1.4.1", + "newest_compiled_ahead"); + check(UpdateManager::newestVersion("1.4.1", "1.4.1") == "1.4.1", "newest_equal"); + check(UpdateManager::newestVersion("v1.4.1", "1.3.1") == "v1.4.1", + "newest_tag_prefix"); + check(UpdateManager::newestVersion("", "1.3.1") == "1.3.1", "newest_empty_compiled"); + check(UpdateManager::newestVersion("1.3.1", "") == "1.3.1", "newest_empty_installed"); + check(UpdateManager::newestVersion("1.10.0", "1.9.9") == "1.10.0", + "newest_minor_beats_patch"); +} + +// Whatever sits next to the test binary, the reported version never goes +// backwards relative to the one compiled into it. +static void testCurrentVersion_neverOlderThanCompiled() { + const QString reported = UpdateManager::currentVersion(); + check(!reported.isEmpty(), "current_version_not_empty"); + check(!UpdateManager::isNewerVersion(QStringLiteral(FWSPACMANAGE_VERSION), reported), + "current_version_not_older_than_compiled"); } // The staged install must replace an executable that is currently running, @@ -296,7 +314,8 @@ int main() { check(UpdateManager::isNewerVersion("", "1.3.1") == false, "empty_remote"); // Reported version - testCurrentVersion_isCompiledIn(); + testNewestVersion(); + testCurrentVersion_neverOlderThanCompiled(); // Update-strategy choice testChooseMethod_checkoutWinsOverEverything();