From 22b1d14b67a7ce7831a90aac7832e23bdc519160 Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Tue, 25 Jul 2023 20:58:15 +0200 Subject: [PATCH] Add helper function to create version string And remove all the duplicate code. Depending on the situation prof_get_version() will return: * 0.13.1 * 0.13.1dev * 0.13.1dev.master.69d8c1f9 --- src/common.c | 21 +++++++++++++++++++++ src/common.h | 2 ++ src/main.c | 15 ++------------- src/profanity.c | 15 ++------------- src/ui/console.c | 27 ++++----------------------- src/ui/core.c | 4 ---- src/xmpp/capabilities.c | 4 ---- src/xmpp/iq.c | 19 ++----------------- src/xmpp/stanza.c | 17 ++--------------- 9 files changed, 35 insertions(+), 89 deletions(-) diff --git a/src/common.c b/src/common.c index 3d7d48b7..3924a260 100644 --- a/src/common.c +++ b/src/common.c @@ -62,6 +62,10 @@ #include "log.h" #include "common.h" +#ifdef HAVE_GIT_VERSION +#include "gitversion.h" +#endif + struct curl_data_t { char* buffer; @@ -688,3 +692,20 @@ glib_hash_table_free(GHashTable* hash_table) g_hash_table_remove_all(hash_table); g_hash_table_unref(hash_table); } + +/* build profanity version string. + * example: 0.13.1dev.master.69d8c1f9 + */ +gchar* +prof_get_version(void) +{ + if (strcmp(PACKAGE_STATUS, "development") == 0) { +#ifdef HAVE_GIT_VERSION + return g_strdup_printf("%sdev.%s.%s", PACKAGE_VERSION, PROF_GIT_BRANCH, PROF_GIT_REVISION); +#else + return g_strdup_printf("%sdev", PACKAGE_VERSION); +#endif + } else { + return g_strdup_printf("%s", PACKAGE_VERSION); + } +} diff --git a/src/common.h b/src/common.h index f3a04005..d71db28a 100644 --- a/src/common.h +++ b/src/common.h @@ -180,4 +180,6 @@ gchar* get_expanded_path(const char* path); void glib_hash_table_free(GHashTable* hash_table); char* basename_from_url(const char* url); +gchar* prof_get_version(void); + #endif diff --git a/src/main.c b/src/main.c index 6edfd3c8..8e2604c1 100644 --- a/src/main.c +++ b/src/main.c @@ -39,10 +39,6 @@ #include #include -#ifdef HAVE_GIT_VERSION -#include "gitversion.h" -#endif - #ifdef HAVE_LIBOTR #include "otr/otr.h" #endif @@ -104,15 +100,8 @@ main(int argc, char** argv) g_option_context_free(context); if (version == TRUE) { - if (strcmp(PACKAGE_STATUS, "development") == 0) { -#ifdef HAVE_GIT_VERSION - g_print("Profanity, version %sdev.%s.%s\n", PACKAGE_VERSION, PROF_GIT_BRANCH, PROF_GIT_REVISION); -#else - g_print("Profanity, version %sdev\n", PACKAGE_VERSION); -#endif - } else { - g_print("Profanity, version %s\n", PACKAGE_VERSION); - } + auto_gchar gchar* prof_version = prof_get_version(); + g_print("Profanity, version %s\n", prof_version); // lets use fixed email instead of PACKAGE_BUGREPORT g_print("Copyright (C) 2012 - 2019 James Booth .\n"); diff --git a/src/profanity.c b/src/profanity.c index 42983993..0731d711 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -35,10 +35,6 @@ */ #include "config.h" -#ifdef HAVE_GIT_VERSION -#include "gitversion.h" -#endif - #ifdef HAVE_GTK #include "ui/tray.h" #endif @@ -180,15 +176,8 @@ _init(char* log_level, char* config_file, char* log_file, char* theme_name) log_init(prof_log_level, log_file); log_stderr_init(PROF_LEVEL_ERROR); - if (strcmp(PACKAGE_STATUS, "development") == 0) { -#ifdef HAVE_GIT_VERSION - log_info("Starting Profanity (%sdev.%s.%s)…", PACKAGE_VERSION, PROF_GIT_BRANCH, PROF_GIT_REVISION); -#else - log_info("Starting Profanity (%sdev)…", PACKAGE_VERSION); -#endif - } else { - log_info("Starting Profanity (%s)…", PACKAGE_VERSION); - } + auto_gchar gchar* prof_version = prof_get_version(); + log_info("Starting Profanity (%s)…", prof_version); chat_log_init(); groupchat_log_init(); diff --git a/src/ui/console.c b/src/ui/console.c index 079ae8ca..b0020077 100644 --- a/src/ui/console.c +++ b/src/ui/console.c @@ -66,10 +66,6 @@ #include "xmpp/muc.h" #include "xmpp/roster_list.h" -#ifdef HAVE_GIT_VERSION -#include "gitversion.h" -#endif - static void _cons_splash_logo(void); static void _show_roster_contacts(GSList* list, gboolean show_groups); static GList* alert_list; @@ -413,16 +409,8 @@ cons_about(void) if (prefs_get_boolean(PREF_SPLASH)) { _cons_splash_logo(); } else { - - if (strcmp(PACKAGE_STATUS, "development") == 0) { -#ifdef HAVE_GIT_VERSION - win_println(console, THEME_DEFAULT, "-", "Welcome to Profanity, version %sdev.%s.%s", PACKAGE_VERSION, PROF_GIT_BRANCH, PROF_GIT_REVISION); -#else - win_println(console, THEME_DEFAULT, "Welcome to Profanity, version %sdev", PACKAGE_VERSION); -#endif - } else { - win_println(console, THEME_DEFAULT, "-", "Welcome to Profanity, version %s", PACKAGE_VERSION); - } + auto_gchar gchar* prof_version = prof_get_version(); + win_println(console, THEME_DEFAULT, "-", "Welcome to Profanity, version %s", prof_version); } win_println(console, THEME_DEFAULT, "-", "Copyright (C) 2012 - 2019 James Booth ."); @@ -2778,15 +2766,8 @@ _cons_splash_logo(void) win_println(console, THEME_SPLASH, "-", "|_| (____/ "); win_println(console, THEME_SPLASH, "-", ""); - if (strcmp(PACKAGE_STATUS, "development") == 0) { -#ifdef HAVE_GIT_VERSION - win_println(console, THEME_DEFAULT, "-", "Version %sdev.%s.%s", PACKAGE_VERSION, PROF_GIT_BRANCH, PROF_GIT_REVISION); -#else - win_println(console, THEME_DEFAULT, "Version %sdev", PACKAGE_VERSION); -#endif - } else { - win_println(console, THEME_DEFAULT, "-", "Version %s", PACKAGE_VERSION); - } + auto_gchar gchar* prof_version = prof_get_version(); + win_println(console, THEME_DEFAULT, "-", "Version %s", prof_version); } static void diff --git a/src/ui/core.c b/src/ui/core.c index 4b33f0bd..feb428fc 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -35,10 +35,6 @@ #include "config.h" -#ifdef HAVE_GIT_VERSION -#include "gitversion.h" -#endif - #include #include #include diff --git a/src/xmpp/capabilities.c b/src/xmpp/capabilities.c index e1821db6..dd83cf4f 100644 --- a/src/xmpp/capabilities.c +++ b/src/xmpp/capabilities.c @@ -35,10 +35,6 @@ #include "config.h" -#ifdef HAVE_GIT_VERSION -#include "gitversion.h" -#endif - #include #include diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index 067e7b2e..1e4c272a 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -35,10 +35,6 @@ #include "config.h" -#ifdef HAVE_GIT_VERSION -#include "gitversion.h" -#endif - #include #include #include @@ -1627,20 +1623,10 @@ _version_get_handler(xmpp_stanza_t* const stanza) xmpp_stanza_t* version = xmpp_stanza_new(ctx); xmpp_stanza_set_name(version, "version"); xmpp_stanza_t* version_txt = xmpp_stanza_new(ctx); - GString* version_str = g_string_new(PACKAGE_VERSION); + auto_gchar gchar* prof_version = prof_get_version(); if (!is_custom_client) { - if (strcmp(PACKAGE_STATUS, "development") == 0) { -#ifdef HAVE_GIT_VERSION - g_string_append(version_str, "dev."); - g_string_append(version_str, PROF_GIT_BRANCH); - g_string_append(version_str, "."); - g_string_append(version_str, PROF_GIT_REVISION); -#else - g_string_append(version_str, "dev"); -#endif - } - xmpp_stanza_set_text(version_txt, version_str->str); + xmpp_stanza_set_text(version_txt, prof_version); xmpp_stanza_add_child(version, version_txt); xmpp_stanza_add_child(query, version); @@ -1678,7 +1664,6 @@ _version_get_handler(xmpp_stanza_t* const stanza) iq_send_stanza(response); // Cleanup - g_string_free(version_str, TRUE); xmpp_stanza_release(version_txt); xmpp_stanza_release(name_txt); if (!is_custom_client && include_os) { diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c index d737e2f2..9ea3d362 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -35,10 +35,6 @@ #include "config.h" -#ifdef HAVE_GIT_VERSION -#include "gitversion.h" -#endif - #include #include #include @@ -963,17 +959,8 @@ stanza_create_caps_query_element(xmpp_ctx_t* ctx) GString* name_str = g_string_new(is_custom_client ? client : "Profanity "); if (!is_custom_client) { xmpp_stanza_set_type(identity, "console"); - g_string_append(name_str, PACKAGE_VERSION); - if (g_strcmp0(PACKAGE_STATUS, "development") == 0) { -#ifdef HAVE_GIT_VERSION - g_string_append(name_str, "dev."); - g_string_append(name_str, PROF_GIT_BRANCH); - g_string_append(name_str, "."); - g_string_append(name_str, PROF_GIT_REVISION); -#else - g_string_append(name_str, "dev"); -#endif - } + auto_gchar gchar* prof_version = prof_get_version(); + g_string_append(name_str, prof_version); } account_free(account);