Skip to content

Commit c7ef1b7

Browse files
author
Alessandro Bertulli
committed
Implement the git short commit SHA1, using git rev-parse --short
1 parent 6c0cb87 commit c7ef1b7

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

git.c.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ const char* git_Describe() {
3030
const char* git_Branch() {
3131
return "@GIT_BRANCH@";
3232
}
33+
const char* git_CommitShort() {
34+
return "@GIT_SHORT@";
35+
}

git.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ const char* git_Describe();
5252
/// The symbolic reference tied to HEAD.
5353
const char* git_Branch();
5454

55+
/// The commit short SHA1.
56+
const char* git_CommitShort();
57+
5558
GIT_VERSION_TRACKING_EXTERN_C_END
5659
#undef GIT_VERSION_TRACKING_EXTERN_C_BEGIN
5760
#undef GIT_VERSION_TRACKING_EXTERN_C_END
@@ -143,6 +146,10 @@ inline const StringOrView Branch() {
143146
static const StringOrView kValue = internal::InitString(git_Branch());
144147
return kValue;
145148
}
149+
inline const StringOrView CommitShort() {
150+
static const StringOrView kValue = internal::InitString(git_CommitShort());
151+
return kValue;
152+
}
146153

147154
} // namespace git
148155

git_watcher.cmake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ set(_state_variable_names
105105
GIT_COMMIT_BODY
106106
GIT_DESCRIBE
107107
GIT_BRANCH
108+
GIT_SHORT
108109
# >>>
109110
# 1. Add the name of the additional git variable you're interested in monitoring
110111
# to this list.
@@ -242,6 +243,14 @@ function(GetGitState _working_dir)
242243
set(ENV{GIT_BRANCH} "${output}")
243244
endif()
244245

246+
# Get output of git describe
247+
RunGitCommand(rev-parse --short ${object})
248+
if(NOT exit_code EQUAL 0)
249+
set(ENV{GIT_SHORT} "unknown")
250+
else()
251+
set(ENV{GIT_SHORT} "${output}")
252+
endif()
253+
245254
# >>>
246255
# 2. Additional git properties can be added here via the
247256
# "execute_process()" command. Be sure to set them in

0 commit comments

Comments
 (0)