Skip to content

Commit

Permalink
VersionInfo: Mimic github's hash styling
Browse files Browse the repository at this point in the history
  • Loading branch information
tresf committed Feb 11, 2025
1 parent c81d497 commit d162f03
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions cmake/modules/VersionInfo.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ IF(GIT_FOUND AND NOT FORCE_VERSION)
# number from the environment and add it to the build metadata
if("$ENV{GITHUB_REF}" MATCHES "refs/pull/([0-9]+)/merge")
list(APPEND BUILD_METADATA "pr${CMAKE_MATCH_1}")
# Parse hash from merge description
# e.g. "Merge abc1234 into def4567"
execute_process(
COMMAND "${GIT_EXECUTABLE}" log -n 1 --format=%s
OUTPUT_VARIABLE COMMIT_HASH
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
TIMEOUT 10
OUTPUT_STRIP_TRAILING_WHITESPACE)
# If successful, use the first 7 characters to mimic github's hash style
if(COMMIT_HASH)
string(SUBSTRING "${COMMIT_HASH}" 6 7 COMMIT_HASH)
endif()
endif()

# Look for git tag information (e.g. Tagged: "v1.0.0", Untagged: "v1.0.0-123-a1b2c3d")
Expand Down Expand Up @@ -44,7 +56,12 @@ IF(GIT_FOUND AND NOT FORCE_VERSION)
ELSEIF(TAG_LIST_LENGTH EQUAL 3)
# Get the number of commits and latest commit hash
LIST(GET TAG_LIST 1 EXTRA_COMMITS)
LIST(GET TAG_LIST 2 COMMIT_HASH)
# Prefer PR hash from above if present
if(NOT COMMIT_HASH)
list(GET TAG_LIST 2 COMMIT_HASH)
# Mimic github's hash style
string(SUBSTRING "${COMMIT_HASH}" 1 7 COMMIT_HASH)
endif()
list(APPEND PRERELEASE_DATA "${EXTRA_COMMITS}")
list(APPEND BUILD_METADATA "${COMMIT_HASH}")
# Bump the patch version, since a pre-release (as specified by the extra
Expand All @@ -57,7 +74,12 @@ IF(GIT_FOUND AND NOT FORCE_VERSION)
# Get the pre-release stage, number of commits, and latest commit hash
LIST(GET TAG_LIST 1 VERSION_STAGE)
LIST(GET TAG_LIST 2 EXTRA_COMMITS)
LIST(GET TAG_LIST 3 COMMIT_HASH)
# Prefer PR hash from above if present
if(NOT COMMIT_HASH)
list(GET TAG_LIST 3 COMMIT_HASH)
# Mimic github's hash style
string(SUBSTRING "${COMMIT_HASH}" 1 7 COMMIT_HASH)
endif()
list(APPEND PRERELEASE_DATA "${VERSION_STAGE}")
list(APPEND PRERELEASE_DATA "${EXTRA_COMMITS}")
list(APPEND BUILD_METADATA "${COMMIT_HASH}")
Expand Down

0 comments on commit d162f03

Please sign in to comment.