From 98c620cfac2683a9a3dde3c133a73165be48c12c Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Wed, 22 Dec 2021 11:19:21 +0800 Subject: [PATCH] inject git info for clickhouse-client clickhouse-server --- programs/client/Client.cpp | 16 ++++++++++++++++ programs/server/Server.cpp | 13 +++++++++++++ src/CMakeLists.txt | 28 ++++++++++++++++++++++++++++ src/Common/git_info.h.in | 19 +++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 src/Common/git_info.h.in diff --git a/programs/client/Client.cpp b/programs/client/Client.cpp index e01677aaac6..013870907db 100644 --- a/programs/client/Client.cpp +++ b/programs/client/Client.cpp @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -76,6 +77,13 @@ namespace ErrorCodes extern const int AUTHENTICATION_FAILED; } +namespace GitInfo +{ + extern const std::string GIT_SHA1; + extern const std::string GIT_BRANCH; + extern const std::string GIT_DATE; + extern const std::string GIT_COMMIT_SUBJECT; +} void Client::processError(const String & query) const { @@ -1143,6 +1151,14 @@ void Client::processOptions(const OptionsDescription & options_description, global_context->getClientInfo().client_trace_context.tracestate = options["opentelemetry-tracestate"].as(); } +static void showClientVersion() +{ + std::cout << DBMS_NAME << " client version " << VERSION_STRING << VERSION_OFFICIAL << "." << std::endl; + std::cout << "GIT SHA1: " << GitInfo::GIT_SHA1 << std::endl; + std::cout << "GIT BRANCH: " << GitInfo::GIT_BRANCH << std::endl; + std::cout << "GIT DATE: " << GitInfo::GIT_DATE << std::endl; + std::cout << "GIT COMMIT SUBJECT: " << GitInfo::GIT_COMMIT_SUBJECT << std::endl; +} void Client::processConfig() { diff --git a/programs/server/Server.cpp b/programs/server/Server.cpp index 14075f9fbf2..560ecb5d249 100644 --- a/programs/server/Server.cpp +++ b/programs/server/Server.cpp @@ -84,6 +84,7 @@ #include "config_core.h" #include "Common/config_version.h" +#include "Common/git_info.h" #if defined(OS_LINUX) # include @@ -127,6 +128,14 @@ namespace CurrentMetrics extern const Metric MaxPushedDDLEntryID; } +namespace GitInfo +{ + extern const std::string GIT_SHA1; + extern const std::string GIT_BRANCH; + extern const std::string GIT_DATE; + extern const std::string GIT_COMMIT_SUBJECT; +} + namespace fs = std::filesystem; #if USE_JEMALLOC @@ -396,6 +405,10 @@ int Server::run() if (config().hasOption("version")) { std::cout << DBMS_NAME << " server version " << VERSION_STRING << VERSION_OFFICIAL << "." << std::endl; + std::cout << "GIT SHA1: " << GitInfo::GIT_SHA1 << std::endl; + std::cout << "GIT BRANCH: " << GitInfo::GIT_BRANCH << std::endl; + std::cout << "GIT DATE: " << GitInfo::GIT_DATE << std::endl; + std::cout << "GIT COMMIT SUBJECT: " << GitInfo::GIT_COMMIT_SUBJECT << std::endl; return 0; } return Application::run(); // NOLINT diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7124961821e..2d097e47ef9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -20,6 +20,7 @@ include(../cmake/limit_jobs.cmake) set (CONFIG_VERSION "${CMAKE_CURRENT_BINARY_DIR}/Common/config_version.h") set (CONFIG_COMMON "${CMAKE_CURRENT_BINARY_DIR}/Common/config.h") +set (GIT_INFO "${CMAKE_CURRENT_BINARY_DIR}/Common/git_info.h") include (../cmake/version.cmake) message (STATUS "Will build ${VERSION_FULL} revision ${VERSION_REVISION} ${VERSION_OFFICIAL}") @@ -27,6 +28,33 @@ configure_file (Common/config.h.in ${CONFIG_COMMON}) configure_file (Common/config_version.h.in ${CONFIG_VERSION}) configure_file (Core/config_core.h.in "${CMAKE_CURRENT_BINARY_DIR}/Core/include/config_core.h") +set(GIT_EXECUTABLE /usr/bin/git) +# the commit's SHA1, and whether the building workspace was dirty or not +execute_process(COMMAND + "${GIT_EXECUTABLE}" rev-parse HEAD + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + OUTPUT_VARIABLE GIT_SHA1 + ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) +# git branch name +execute_process(COMMAND + "${GIT_EXECUTABLE}" rev-parse --abbrev-ref HEAD + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + OUTPUT_VARIABLE GIT_BRANCH + ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) +# the date of the commit +execute_process(COMMAND + "${GIT_EXECUTABLE}" log -1 --format=%ad --date=local + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + OUTPUT_VARIABLE GIT_DATE + ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) +# the subject of the commit +execute_process(COMMAND + "${GIT_EXECUTABLE}" log -1 --format=%s + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + OUTPUT_VARIABLE GIT_COMMIT_SUBJECT + ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) +configure_file (Common/git_info.h.in ${GIT_INFO}) + if (USE_DEBUG_HELPERS) get_target_property(MAGIC_ENUM_INCLUDE_DIR magic_enum INTERFACE_INCLUDE_DIRECTORIES) set (INCLUDE_DEBUG_HELPERS "-I\"${ClickHouse_SOURCE_DIR}/base\" -I\"${MAGIC_ENUM_INCLUDE_DIR}\" -include \"${ClickHouse_SOURCE_DIR}/src/Core/iostream_debug_helpers.h\"") diff --git a/src/Common/git_info.h.in b/src/Common/git_info.h.in new file mode 100644 index 00000000000..d338ae2e1e5 --- /dev/null +++ b/src/Common/git_info.h.in @@ -0,0 +1,19 @@ +#pragma once + +// .h autogenerated by cmake! + +#include + + +namespace DB +{ + +namespace GitInfo +{ + const std::string GIT_SHA1 = "@GIT_SHA1@"; + const std::string GIT_BRANCH = "@GIT_BRANCH@"; + const std::string GIT_DATE = "@GIT_DATE@"; + const std::string GIT_COMMIT_SUBJECT = "@GIT_COMMIT_SUBJECT@"; +} + +}