Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git tag info is supported #42

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions better-example/git.cc.in
Original file line number Diff line number Diff line change
Expand Up @@ -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@";
}

8 changes: 8 additions & 0 deletions better-example/git.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
1 change: 1 addition & 0 deletions better-example/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
47 changes: 46 additions & 1 deletion git_watcher.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down