diff --git a/better-example/git.cc.in b/better-example/git.cc.in index ca1d265..ed77df8 100644 --- a/better-example/git.cc.in +++ b/better-example/git.cc.in @@ -30,3 +30,16 @@ std::string GitMetadata::Describe() { std::string GitMetadata::Branch() { return "@GIT_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 bf4b449..1c8a091 100644 --- a/better-example/git.h +++ b/better-example/git.h @@ -28,4 +28,12 @@ class GitMetadata { static std::string Describe(); // The symbolic reference tied to HEAD. 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/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..0199f6f 100644 --- a/git_watcher.cmake +++ b/git_watcher.cmake @@ -105,6 +105,10 @@ set(_state_variable_names GIT_COMMIT_BODY 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. @@ -141,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. @@ -242,6 +257,36 @@ function(GetGitState _working_dir) set(ENV{GIT_BRANCH} "${output}") endif() + set(_permit_git_failure ON) + RunGitCommand(describe --tags --dirty) + unset(_permit_git_failure) + if(exit_code EQUAL 0) + set(ENV{GIT_TAG} "${output}") + else() + 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