From 3ed5038ee2df566fe66eaeea3eee499dc901f02d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rich=C3=A1rd=20V=C3=A9gh?= Date: Fri, 10 Jun 2022 09:23:47 +0200 Subject: [PATCH 1/4] Git tag info is supported --- better-example/git.cc.in | 3 +++ better-example/git.h | 2 ++ better-example/main.cc | 1 + git_watcher.cmake | 10 ++++++++++ 4 files changed, 16 insertions(+) diff --git a/better-example/git.cc.in b/better-example/git.cc.in index ca1d265..b281ba8 100644 --- a/better-example/git.cc.in +++ b/better-example/git.cc.in @@ -30,3 +30,6 @@ std::string GitMetadata::Describe() { std::string GitMetadata::Branch() { return "@GIT_BRANCH@"; } +std::string GitMetadata::Tag() { + return "@GIT_TAG@"; +} diff --git a/better-example/git.h b/better-example/git.h index bf4b449..e4a900e 100644 --- a/better-example/git.h +++ b/better-example/git.h @@ -28,4 +28,6 @@ class GitMetadata { static std::string Describe(); // The symbolic reference tied to HEAD. static std::string Branch(); + // The most recent tag + static std::string Tag(); }; diff --git a/better-example/main.cc b/better-example/main.cc index 9296a60..f9c66b5 100644 --- a/better-example/main.cc +++ b/better-example/main.cc @@ -10,6 +10,7 @@ int main() { } std::cout << "commit " << GitMetadata::CommitSHA1() << " (" << GitMetadata::Branch() << ")\n" << "describe " << GitMetadata::Describe() << "\n" + << "tag " << GitMetadata::Tag() << "\n" << "Author: " << GitMetadata::AuthorName() << " <" << GitMetadata::AuthorEmail() << ">\n" << "Date: " << GitMetadata::CommitDate() << "\n\n" << GitMetadata::CommitSubject() << "\n" << GitMetadata::CommitBody() << std::endl; diff --git a/git_watcher.cmake b/git_watcher.cmake index 4d67e68..1ba006a 100644 --- a/git_watcher.cmake +++ b/git_watcher.cmake @@ -105,6 +105,7 @@ set(_state_variable_names GIT_COMMIT_BODY GIT_DESCRIBE GIT_BRANCH + GIT_TAG # >>> # 1. Add the name of the additional git variable you're interested in monitoring # to this list. @@ -242,6 +243,15 @@ function(GetGitState _working_dir) set(ENV{GIT_BRANCH} "${output}") endif() + set(_permit_git_failure ON) + RunGitCommand(describe --tags ${object}) + unset(_permit_git_failure) + if(exit_code EQUAL 0) + set(ENV{GIT_TAG} "${output}") + else() + set(ENV{GIT_TAG} "") # empty string. + endif() + # >>> # 2. Additional git properties can be added here via the # "execute_process()" command. Be sure to set them in From 7a55013f71301a05afb002dea5cf7fc14d72b6c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rich=C3=A1rd=20V=C3=A9gh?= Date: Mon, 13 Jun 2022 17:06:17 +0200 Subject: [PATCH 2/4] Git tag info shows also the dirty status --- git_watcher.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git_watcher.cmake b/git_watcher.cmake index 1ba006a..e151ca7 100644 --- a/git_watcher.cmake +++ b/git_watcher.cmake @@ -244,7 +244,7 @@ function(GetGitState _working_dir) endif() set(_permit_git_failure ON) - RunGitCommand(describe --tags ${object}) + RunGitCommand(describe --tags --dirty ${object}) unset(_permit_git_failure) if(exit_code EQUAL 0) set(ENV{GIT_TAG} "${output}") From 0aa9d999df57d07046b87f38bfb2df50ac31a733 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rich=C3=A1rd=20V=C3=A9gh?= Date: Mon, 13 Jun 2022 17:09:34 +0200 Subject: [PATCH 3/4] Hostname, username, kernel info are added --- better-example/git.cc.in | 10 ++++++++++ better-example/git.h | 6 ++++++ git_watcher.cmake | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/better-example/git.cc.in b/better-example/git.cc.in index b281ba8..ed77df8 100644 --- a/better-example/git.cc.in +++ b/better-example/git.cc.in @@ -33,3 +33,13 @@ std::string GitMetadata::Branch() { std::string GitMetadata::Tag() { return "@GIT_TAG@"; } +std::string GitMetadata::Whoami() { + return "@WHOAMI@"; +} +std::string GitMetadata::Hostname() { + return "@HOSTNAME@"; +} +std::string GitMetadata::Uname() { + return "@UNAME@"; +} + diff --git a/better-example/git.h b/better-example/git.h index e4a900e..1c8a091 100644 --- a/better-example/git.h +++ b/better-example/git.h @@ -30,4 +30,10 @@ class GitMetadata { static std::string Branch(); // The most recent tag static std::string Tag(); + // The user + static std::string Whoami(); + // The host machine name + static std::string Hostname(); + // The kernel name + static std::string Uname(); }; diff --git a/git_watcher.cmake b/git_watcher.cmake index e151ca7..aef7e49 100644 --- a/git_watcher.cmake +++ b/git_watcher.cmake @@ -106,6 +106,9 @@ set(_state_variable_names GIT_DESCRIBE GIT_BRANCH GIT_TAG + WHOAMI + HOSTNAME + UNAME # >>> # 1. Add the name of the additional git variable you're interested in monitoring # to this list. @@ -142,7 +145,18 @@ macro(RunGitCommand) endif() endmacro() - +# Macro: RunNormalCommand +# Description: short-hand macro for calling any command function. Outputs are the +# "exit_code" and "output" variables. +macro(RunNormalCommand) + execute_process(COMMAND + ${ARGV} + WORKING_DIRECTORY "${_working_dir}" + RESULT_VARIABLE exit_code + OUTPUT_VARIABLE output + ERROR_VARIABLE stderr + OUTPUT_STRIP_TRAILING_WHITESPACE) +endmacro() # Function: GetGitState # Description: gets the current state of the git repo. @@ -252,6 +266,27 @@ function(GetGitState _working_dir) set(ENV{GIT_TAG} "") # empty string. endif() + RunNormalCommand(whoami) + if(exit_code EQUAL 0) + set(ENV{WHOAMI} "${output}") + else() + set(ENV{WHOAMI} "") # empty string. + endif() + + RunNormalCommand(hostname -f) + if(exit_code EQUAL 0) + set(ENV{HOSTNAME} "${output}") + else() + set(ENV{HOSTNAME} "") # empty string. + endif() + + RunNormalCommand(uname -ar) + if(exit_code EQUAL 0) + set(ENV{UNAME} "${output}") + else() + set(ENV{UNAME} "") # empty string. + endif() + # >>> # 2. Additional git properties can be added here via the # "execute_process()" command. Be sure to set them in From bbbea1c43e1daa9b88b1d7e003b73add8104fcef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rich=C3=A1rd=20V=C3=A9gh?= Date: Mon, 13 Jun 2022 19:05:03 +0200 Subject: [PATCH 4/4] Eliminating 'Got this: fatal: --dirty is incompatible with commit-ishes' which is resulted an empty string for GIT_TAG. The 'HEAD' is not needed for 'git describe --tags --dirty' at the end. --- git_watcher.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git_watcher.cmake b/git_watcher.cmake index aef7e49..0199f6f 100644 --- a/git_watcher.cmake +++ b/git_watcher.cmake @@ -258,7 +258,7 @@ function(GetGitState _working_dir) endif() set(_permit_git_failure ON) - RunGitCommand(describe --tags --dirty ${object}) + RunGitCommand(describe --tags --dirty) unset(_permit_git_failure) if(exit_code EQUAL 0) set(ENV{GIT_TAG} "${output}")