Skip to content

Commit

Permalink
Fix commit hash for PRs
Browse files Browse the repository at this point in the history
  • Loading branch information
tresf committed Feb 11, 2025
1 parent deb9989 commit 151b370
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions cmake/modules/VersionInfo.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ 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}")
execute_process(
COMMAND "${GIT_EXECUTABLE}" git log --format="%h" HEAD~1...HEAD~2
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}" 0 6 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,9 +54,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)
# Mimic github's hash style
STRING(SUBSTRING "${COMMIT_HASH}" 1 7 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 @@ -59,9 +72,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)
# Mimic github's hash style
STRING(SUBSTRING "${COMMIT_HASH}" 1 7 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 151b370

Please sign in to comment.