diff --git a/.gitignore b/.gitignore index 35a8c558..d324ff7e 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ libTgBot*.so # Generated resources/scripts/git-askpass.sh resources/about.html +src/include/GitBuildInfo.hpp __pycache__ # Visual studio code diff --git a/CMakeLists.txt b/CMakeLists.txt index b8ac3d86..509bdc21 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -151,8 +151,59 @@ if (NOT MSVC) endif() ##################### Fill in readme with cmake ##################### -string(TIMESTAMP TODAY "%Y-%m-%d") +string(TIMESTAMP TODAY "%Y-%m-%d %H:%M:%S UTC" UTC) + +find_package(Git REQUIRED) # It will obviously have +function(git_execute_proc) + cmake_parse_arguments(GIT_PROC + "" # Option + "NAME;VAR" # Single + "COMMAND" # Multiple + ${ARGN} + ) + if (NOT (GIT_PROC_NAME AND GIT_PROC_VAR AND GIT_PROC_COMMAND)) + message(SEND_ERROR "Missing arguments") + endif() + + # Run the git command to get the commit ID or other info + execute_process( + COMMAND ${GIT_EXECUTABLE} ${GIT_PROC_COMMAND} + OUTPUT_VARIABLE GIT_PROC_OUT + ERROR_VARIABLE GIT_PROC_ERR + RESULT_VARIABLE GIT_PROC_RESULT + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + + # Check if the git command was successful + if (GIT_PROC_RESULT EQUAL 0) + set(${GIT_PROC_VAR} ${GIT_PROC_OUT} PARENT_SCOPE) + message(STATUS "Git ${GIT_PROC_NAME}: ${GIT_PROC_OUT}") + else() + set(${GIT_PROC_VAR} "-" PARENT_SCOPE) + message(WARNING "Error retrieving Git ${GIT_PROC_NAME}: ${GIT_PROC_ERR}") + endif() +endfunction() + +git_execute_proc( + COMMAND rev-parse HEAD + NAME "commit-id" + VAR GIT_COMMIT_ID +) + +git_execute_proc( + COMMAND log -1 --pretty=%B + NAME "commit-message" + VAR GIT_COMMIT_MESSAGE +) + +git_execute_proc( + COMMAND remote get-url origin + NAME "origin-url" + VAR GIT_ORIGIN_URL +) + configure_file(resources/about.html.in ${CMAKE_SOURCE_DIR}/resources/about.html) +configure_file(src/include/GitBuildInfo.hpp.inc ${CMAKE_SOURCE_DIR}/src/include/GitBuildInfo.hpp) ##################################################################### ################## Declare common macros #################### diff --git a/resources/about.html.in b/resources/about.html.in index c0908270..40f693cb 100644 --- a/resources/about.html.in +++ b/resources/about.html.in @@ -11,7 +11,7 @@ Hi! I'm _botname_ - Host OS: @CMAKE_SYSTEM_NAME@ - C Compiler: @CMAKE_C_COMPILER@ v@CMAKE_C_COMPILER_VERSION@ - CXX Compiler: @CMAKE_CXX_COMPILER@ v@CMAKE_CXX_COMPILER_VERSION@ -- Date: @TODAY@ +- Build Date: @TODAY@
GIT INFO: -- Commit-Id: _commitid_ -- Commit-Msg: _commitmsg_
+- Commit-Id: @GIT_COMMIT_ID@ +- Commit-Msg: @GIT_COMMIT_MESSAGE@ diff --git a/src/api/TgBotApiImpl.cpp b/src/api/TgBotApiImpl.cpp index e826bd6b..cee788d4 100644 --- a/src/api/TgBotApiImpl.cpp +++ b/src/api/TgBotApiImpl.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include #include @@ -164,7 +164,8 @@ void TgBotApiImpl::commandHandler(const std::string& command, } if (!_rateLimiter.check()) { - LOG(INFO) << fmt::format("Ratelimiting user {}", ext->get()); + LOG(INFO) << fmt::format("Ratelimiting user {}", + ext->get()); return; } @@ -232,14 +233,8 @@ void TgBotApiImpl::startPoll() { // Deleting webhook getApi().deleteWebhook(); - std::string sourceString; - GitData data; - if (GitData::Fill(&data)) { - sourceString = fmt::format(", sources: {}", data.originurl); - } - - getApi().setMyDescription( - fmt::format("A C++ written Telegram bot{}", sourceString)); + getApi().setMyDescription(fmt::format( + "A C++ written Telegram bot, sources: {}", git::buildinfo::ORIGIN_URL)); std::string ownerString; if (auto owner = _provider->database->getOwnerUserId(); owner) { @@ -560,7 +555,8 @@ bool TgBotApiImpl::answerCallbackQuery_impl( TgBotApiImpl::TgBotApiImpl(const std::string_view token, AuthContext* auth, StringResLoaderBase* loader, Providers* providers) - : _bot(std::string(token), std::make_unique(std::chrono::seconds(30))), + : _bot(std::string(token), + std::make_unique(std::chrono::seconds(30))), _auth(auth), _loader(loader), _provider(providers), diff --git a/src/command_modules/alive.cpp b/src/command_modules/alive.cpp index e30609e8..bbed0abf 100644 --- a/src/command_modules/alive.cpp +++ b/src/command_modules/alive.cpp @@ -5,7 +5,7 @@ #include #include -#include +#include #include #include #include @@ -20,19 +20,11 @@ DECLARE_COMMAND_HANDLER(alive) { std::call_once(once, [provider, api] { std::string _version; - GitData data; - - GitData::Fill(&data); _version = provider->resource->get("about.html"); - std::vector splitMsg = - absl::StrSplit(data.commitmsg, '\n'); - // Replace placeholders in the version string with actual values. version = absl::StrReplaceAll( - _version, {{"_commitid_", data.commitid}, - {"_commitmsg_", splitMsg.front()}, - {"_botname_", api->getBotUser()->firstName}, + _version, {{"_botname_", api->getBotUser()->firstName}, {"_botusername_", api->getBotUser()->username.value_or("unknown")}}); }); diff --git a/src/include/GitBuildInfo.hpp.inc b/src/include/GitBuildInfo.hpp.inc new file mode 100644 index 00000000..d26f9bad --- /dev/null +++ b/src/include/GitBuildInfo.hpp.inc @@ -0,0 +1,9 @@ +#pragma once + +#include + +namespace git::buildinfo { +constexpr static std::string_view COMMIT_ID = "@GIT_COMMIT_ID@"; +constexpr static std::string_view COMMIT_MESSAGE = "@GIT_COMMIT_MESSAGE@"; +constexpr static std::string_view ORIGIN_URL = "@GIT_ORIGIN_URL@"; +} // namespace git::buildinfo \ No newline at end of file diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index de57cd90..73858867 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -1,5 +1,4 @@ find_package(Boost COMPONENTS program_options REQUIRED) -find_package(libgit2 REQUIRED) ################# TgBot Utilities (generic) Library ################# add_my_library( @@ -7,10 +6,9 @@ add_my_library( SRCS ConfigManager.cpp Env_${TARGET_VARIANT}.cpp - GitData.cpp CommandLine.cpp ResourceManager.cpp libfs_${TARGET_VARIANT}.cpp - LIBS Boost::program_options ${LIBGIT2_LIBRARIES} + LIBS Boost::program_options LIBS_WIN32 shlwapi ) \ No newline at end of file diff --git a/src/utils/GitData.cpp b/src/utils/GitData.cpp deleted file mode 100644 index 6212130e..00000000 --- a/src/utils/GitData.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include -#include -#include - -#include -#include - -constexpr int SHA1_HASH_LEN = 40; - -bool GitData::Fill(GitData *data) { - std::filesystem::path path = std::filesystem::current_path(); - git_repository *repo = nullptr; - git_commit *head_commit = nullptr; - git_reference *head_ref = nullptr; - git_remote *origin = nullptr; - std::array head_sha = {0}; - bool rc = true; - - // Initial invalid ret - int error = -1; - - git_libgit2_init(); - - // Open the repository, try going up the directory tree until we find a .git - // folder - auto gitdir = walk_up_tree([&repo](const std::filesystem::path& path) { - return git_repository_open(&repo, path.string().c_str()) == 0; - }); - if (!gitdir) { - LOG(ERROR) << "Couldn't find git repository"; - return false; - } - data->gitSrcRoot = gitdir.value(); - - error = git_repository_head(&head_ref, repo); - if (error == 0) { - const git_oid *head_oid = git_reference_target(head_ref); - error = git_commit_lookup(&head_commit, repo, head_oid); - git_reference_free(head_ref); - if (error != 0) { - LOG(ERROR) << "Error looking up head commit"; - git_repository_free(repo); - return false; - } - } else { - LOG(ERROR) << "Error getting HEAD commit: " << giterr_last()->message; - git_repository_free(repo); - return false; - } - - // Get the SHA1 (OID) of the HEAD commit - git_oid_fmt(head_sha.data(), git_commit_id(head_commit)); - - // Get the origin URL - error = git_remote_lookup(&origin, repo, "origin"); - if (error == 0) { - data->originurl = git_remote_url(origin); - data->commitmsg = git_commit_message(head_commit); - data->commitid = head_sha.data(); - git_remote_free(origin); - } else { - LOG(ERROR) << "Error getting origin URL: " << giterr_last()->message; - rc = false; - } - - // Clean up - git_commit_free(head_commit); - git_repository_free(repo); - git_libgit2_shutdown(); - - if (rc) { - static std::once_flag once; - std::call_once(once, [&data]() { - const std::string oneline_msg = - data->commitmsg.substr(0, data->commitmsg.find_first_of('\n')); - DLOG(INFO) << "GitData::Fill returning:"; - DLOG(INFO) << "- originurl: " << std::quoted(data->originurl); - DLOG(INFO) << "- commitmsg: " << std::quoted(oneline_msg); - DLOG(INFO) << "- commitid: " << std::quoted(data->commitid); - DLOG(INFO) << "- gitSrcRoot: " - << std::quoted(data->gitSrcRoot.string()); - }); - } - - return rc; -} - -bool GitData::Fill() { return Fill(this); } \ No newline at end of file diff --git a/src/utils/GitData.hpp b/src/utils/GitData.hpp deleted file mode 100644 index 9d7e6928..00000000 --- a/src/utils/GitData.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include - -#include -#include - -struct Utils_API GitData { - std::string commitid, commitmsg, originurl; - std::filesystem::path gitSrcRoot; - static bool Fill(GitData *gitData); - bool Fill(); -}; \ No newline at end of file