Skip to content

Commit f5c9286

Browse files
authored
Factor out the commit hash from the library version. (#1310)
This PR captures the current commit hash in a different variable from the library version. There is no user-facing impact. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent d96b282 commit f5c9286

File tree

7 files changed

+92
-38
lines changed

7 files changed

+92
-38
lines changed

Package.swift

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -468,16 +468,23 @@ extension Array where Element == PackageDescription.CXXSetting {
468468
.define("SWT_NO_LIBDISPATCH", .whenEmbedded()),
469469
]
470470

471-
// Capture the testing library's version as a C++ string constant.
471+
// Capture the testing library's commit info as C++ constants.
472472
if let git {
473-
let testingLibraryVersion = if let tag = git.currentTag {
474-
tag
475-
} else if git.hasUncommittedChanges {
476-
"\(git.currentCommit) (modified)"
473+
if let tag = git.currentTag {
474+
result.append(.define("SWT_TESTING_LIBRARY_VERSION", to: #""\#(tag)""#))
477475
} else {
478-
git.currentCommit
476+
result.append(.define("SWT_TESTING_LIBRARY_VERSION", to: "0"))
479477
}
480-
result.append(.define("SWT_TESTING_LIBRARY_VERSION", to: #""\#(testingLibraryVersion)""#))
478+
479+
result.append(.define("SWT_TESTING_LIBRARY_COMMIT_HASH", to: #""\#(git.currentCommit)""#))
480+
if git.hasUncommittedChanges {
481+
result.append(.define("SWT_TESTING_LIBRARY_COMMIT_MODIFIED", to: "1"))
482+
}
483+
} else if let gitHubSHA = Context.environment["GITHUB_SHA"] {
484+
// When building in GitHub Actions, the git command may fail to get us the
485+
// commit hash, so check if GitHub shared it with us instead.
486+
result.append(.define("SWT_TESTING_LIBRARY_VERSION", to: "0"))
487+
result.append(.define("SWT_TESTING_LIBRARY_COMMIT_HASH", to: #""\#(gitHubSHA)""#))
481488
}
482489

483490
return result

Sources/Testing/Support/Versions.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,26 @@ let simulatorVersion: String = {
129129
/// an event writer.
130130
///
131131
/// This value is not part of the public interface of the testing library.
132-
var testingLibraryVersion: String {
133-
swt_getTestingLibraryVersion().flatMap(String.init(validatingCString:)) ?? "unknown"
134-
}
132+
let testingLibraryVersion: String = {
133+
var result = swt_getTestingLibraryVersion().flatMap(String.init(validatingCString:)) ?? "unknown"
134+
135+
// Get details of the git commit used when compiling the testing library.
136+
var commitHash: UnsafePointer<CChar>?
137+
var commitModified = CBool(false)
138+
swt_getTestingLibraryCommit(&commitHash, &commitModified)
139+
140+
if let commitHash = commitHash.flatMap(String.init(validatingCString:)) {
141+
// Truncate to 15 characters of the hash to match `swift --version`.
142+
let commitHash = commitHash.prefix(15)
143+
if commitModified {
144+
result = "\(result) (\(commitHash) - modified)"
145+
} else {
146+
result = "\(result) (\(commitHash))"
147+
}
148+
}
149+
150+
return result
151+
}()
135152

136153
/// Get the LLVM target triple used to build the testing library, if available.
137154
///

Sources/_TestingInternals/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
set(CMAKE_CXX_SCAN_FOR_MODULES 0)
1010

11+
include(GitCommit)
1112
include(LibraryVersion)
1213
include(TargetTriple)
1314
add_library(_TestingInternals STATIC

Sources/_TestingInternals/Versions.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ const char *swt_getTestingLibraryVersion(void) {
1919
#endif
2020
}
2121

22+
void swt_getTestingLibraryCommit(const char *_Nullable *_Nonnull outHash, bool *outModified) {
23+
#if defined(SWT_TESTING_LIBRARY_COMMIT_HASH)
24+
*outHash = SWT_TESTING_LIBRARY_COMMIT_HASH;
25+
#else
26+
*outHash = nullptr;
27+
#endif
28+
#if defined(SWT_TESTING_LIBRARY_COMMIT_MODIFIED)
29+
*outModified = (SWT_TESTING_LIBRARY_COMMIT_MODIFIED != 0);
30+
#else
31+
*outModified = false;
32+
#endif
33+
}
34+
2235
const char *swt_getTargetTriple(void) {
2336
#if defined(SWT_TARGET_TRIPLE)
2437
return SWT_TARGET_TRIPLE;

Sources/_TestingInternals/include/Versions.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ static inline uint64_t swt_getSwiftCompilerVersion(void) {
3838
/// other conditions. Do not attempt to parse it.
3939
SWT_EXTERN const char *_Nullable swt_getTestingLibraryVersion(void);
4040

41+
/// Get details of the source control (git) commit from which the testing
42+
/// library was built.
43+
///
44+
/// - Parameters:
45+
/// - outHash: On return, set to a pointer to a string containing the commit
46+
/// hash from which the testing library was built.
47+
/// - outModified: On return, whether or not there were uncommitted changes.
48+
SWT_EXTERN void swt_getTestingLibraryCommit(const char *_Nullable *_Nonnull outHash, bool *outModified);
49+
4150
/// Get the LLVM target triple used to build the testing library.
4251
///
4352
/// - Returns: A string containing the LLVM target triple used to build the

cmake/modules/GitCommit.cmake

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2024–2025 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See http://swift.org/LICENSE.txt for license information
7+
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
find_package(Git QUIET)
10+
if(Git_FOUND)
11+
# Get the commit hash corresponding to the current build.
12+
execute_process(
13+
COMMAND ${GIT_EXECUTABLE} rev-parse --verify HEAD
14+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
15+
OUTPUT_VARIABLE SWT_TESTING_LIBRARY_COMMIT_HASH
16+
OUTPUT_STRIP_TRAILING_WHITESPACE
17+
ERROR_QUIET)
18+
19+
# Check if there are local changes.
20+
execute_process(
21+
COMMAND ${GIT_EXECUTABLE} status -s
22+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
23+
OUTPUT_VARIABLE SWT_TESTING_LIBRARY_COMMIT_MODIFIED
24+
OUTPUT_STRIP_TRAILING_WHITESPACE)
25+
endif()
26+
27+
if(SWT_TESTING_LIBRARY_COMMIT_HASH)
28+
message(STATUS "Swift Testing commit hash: ${SWT_TESTING_LIBRARY_COMMIT_HASH}")
29+
add_compile_definitions(
30+
"$<$<COMPILE_LANGUAGE:CXX>:SWT_TESTING_LIBRARY_COMMIT_HASH=\"${SWT_TESTING_LIBRARY_COMMIT_HASH}\">")
31+
if(SWT_TESTING_LIBRARY_COMMIT_MODIFIED)
32+
add_compile_definitions(
33+
"$<$<COMPILE_LANGUAGE:CXX>:SWT_TESTING_LIBRARY_COMMIT_MODIFIED=1>")
34+
endif()
35+
endif()

cmake/modules/LibraryVersion.cmake

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,6 @@
1010
# remember to remove -dev.
1111
set(SWT_TESTING_LIBRARY_VERSION "6.3-dev")
1212

13-
find_package(Git QUIET)
14-
if(Git_FOUND)
15-
# Get the commit hash corresponding to the current build. Limit length to 15
16-
# to match `swift --version` output format.
17-
execute_process(
18-
COMMAND ${GIT_EXECUTABLE} rev-parse --short=15 --verify HEAD
19-
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
20-
OUTPUT_VARIABLE GIT_VERSION
21-
OUTPUT_STRIP_TRAILING_WHITESPACE
22-
ERROR_QUIET)
23-
24-
# Check if there are local changes.
25-
execute_process(
26-
COMMAND ${GIT_EXECUTABLE} status -s
27-
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
28-
OUTPUT_VARIABLE GIT_STATUS
29-
OUTPUT_STRIP_TRAILING_WHITESPACE)
30-
if(GIT_STATUS)
31-
set(GIT_VERSION "${GIT_VERSION} - modified")
32-
endif()
33-
endif()
34-
35-
# Combine the hard-coded Swift version with available Git information.
36-
if(GIT_VERSION)
37-
set(SWT_TESTING_LIBRARY_VERSION "${SWT_TESTING_LIBRARY_VERSION} (${GIT_VERSION})")
38-
endif()
39-
40-
# All done!
4113
message(STATUS "Swift Testing version: ${SWT_TESTING_LIBRARY_VERSION}")
4214
add_compile_definitions(
4315
"$<$<COMPILE_LANGUAGE:CXX>:SWT_TESTING_LIBRARY_VERSION=\"${SWT_TESTING_LIBRARY_VERSION}\">")

0 commit comments

Comments
 (0)