Skip to content

Commit bfacdd2

Browse files
authored
VersionInfo: Mimic github's hash styling (LMMS#7694)
1 parent d145f78 commit bfacdd2

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

cmake/modules/VersionInfo.cmake

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ IF(GIT_FOUND AND NOT FORCE_VERSION)
88
# number from the environment and add it to the build metadata
99
if("$ENV{GITHUB_REF}" MATCHES "refs/pull/([0-9]+)/merge")
1010
list(APPEND BUILD_METADATA "pr${CMAKE_MATCH_1}")
11+
# Parse hash from merge description
12+
# e.g. "Merge abc1234 into def4567"
13+
execute_process(
14+
COMMAND "${GIT_EXECUTABLE}" log -n 1 --format=%s
15+
OUTPUT_VARIABLE COMMIT_HASH
16+
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
17+
TIMEOUT 10
18+
OUTPUT_STRIP_TRAILING_WHITESPACE)
19+
# If successful, use the first 7 characters to mimic github's hash style
20+
if(COMMIT_HASH)
21+
string(SUBSTRING "${COMMIT_HASH}" 6 7 COMMIT_HASH)
22+
endif()
1123
endif()
1224

1325
# Look for git tag information (e.g. Tagged: "v1.0.0", Untagged: "v1.0.0-123-a1b2c3d")
@@ -44,7 +56,12 @@ IF(GIT_FOUND AND NOT FORCE_VERSION)
4456
ELSEIF(TAG_LIST_LENGTH EQUAL 3)
4557
# Get the number of commits and latest commit hash
4658
LIST(GET TAG_LIST 1 EXTRA_COMMITS)
47-
LIST(GET TAG_LIST 2 COMMIT_HASH)
59+
# Prefer PR hash from above if present
60+
if(NOT COMMIT_HASH)
61+
list(GET TAG_LIST 2 COMMIT_HASH)
62+
# Mimic github's hash style
63+
string(SUBSTRING "${COMMIT_HASH}" 1 7 COMMIT_HASH)
64+
endif()
4865
list(APPEND PRERELEASE_DATA "${EXTRA_COMMITS}")
4966
list(APPEND BUILD_METADATA "${COMMIT_HASH}")
5067
# Bump the patch version, since a pre-release (as specified by the extra
@@ -57,7 +74,12 @@ IF(GIT_FOUND AND NOT FORCE_VERSION)
5774
# Get the pre-release stage, number of commits, and latest commit hash
5875
LIST(GET TAG_LIST 1 VERSION_STAGE)
5976
LIST(GET TAG_LIST 2 EXTRA_COMMITS)
60-
LIST(GET TAG_LIST 3 COMMIT_HASH)
77+
# Prefer PR hash from above if present
78+
if(NOT COMMIT_HASH)
79+
list(GET TAG_LIST 3 COMMIT_HASH)
80+
# Mimic github's hash style
81+
string(SUBSTRING "${COMMIT_HASH}" 1 7 COMMIT_HASH)
82+
endif()
6183
list(APPEND PRERELEASE_DATA "${VERSION_STAGE}")
6284
list(APPEND PRERELEASE_DATA "${EXTRA_COMMITS}")
6385
list(APPEND BUILD_METADATA "${COMMIT_HASH}")

0 commit comments

Comments
 (0)