Skip to content

Commit a3ac701

Browse files
authored
fix(build): support Git commit hash injection for tarball-based package builds (#2266)
1 parent 600cc5f commit a3ac701

5 files changed

Lines changed: 45 additions & 14 deletions

File tree

linux/build

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ while [[ $# -gt 0 ]]; do
3333
USE_SYSTEM_LIBS=true
3434
shift
3535
;;
36+
-commit-hash)
37+
# Inject build provenance for tarball builds (Homebrew, released source tarball, etc.)
38+
# where no .git directory is present. Passed through to pre-build.sh
39+
# via the exported env var GIT_COMMIT_HASH_OVERRIDE.
40+
export GIT_COMMIT_HASH_OVERRIDE="$2"
41+
shift 2
42+
;;
3643
-*)
3744
echo "Unknown option $1"
3845
exit 1

linux/pre-build.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#!/usr/bin/env bash
22
echo "Obtaining Git commit"
3-
commit=(`git rev-parse HEAD 2>/dev/null`)
3+
if [ -n "$GIT_COMMIT_HASH_OVERRIDE" ]; then
4+
commit="$GIT_COMMIT_HASH_OVERRIDE"
5+
echo "Using injected commit hash: $commit"
6+
else
7+
commit=(`git rev-parse HEAD 2>/dev/null`)
8+
fi
49
if [ -z "$commit" ]; then
510
echo "Git command not present, trying folder approach"
611
if [ -d "../.git" ]; then

mac/build.command

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ while [[ $# -gt 0 ]]; do
3535
USE_SYSTEM_LIBS=true
3636
shift
3737
;;
38+
-commit-hash)
39+
# Inject build provenance for tarball builds (Homebrew, released source tarball, etc.)
40+
# where no .git directory is present. Passed through to pre-build.sh
41+
# via the exported env var GIT_COMMIT_HASH_OVERRIDE.
42+
export GIT_COMMIT_HASH_OVERRIDE="$2"
43+
shift 2
44+
;;
3845
-*)
3946
echo "Unknown option $1"
4047
exit 1

mac/pre-build.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#!/bin/bash
22
echo "Obtaining Git commit"
3-
commit=(`git rev-parse HEAD 2>/dev/null`)
3+
if [ -n "$GIT_COMMIT_HASH_OVERRIDE" ]; then
4+
commit="$GIT_COMMIT_HASH_OVERRIDE"
5+
echo "Using injected commit hash: $commit"
6+
else
7+
commit=(`git rev-parse HEAD 2>/dev/null`)
8+
fi
49
if [ -z "$commit" ]; then
510
echo "Git command not present, trying folder approach"
611
if [ -d "../.git" ]; then

src/CMakeLists.txt

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,25 @@ set (CCEXTRACTOR_VERSION_MINOR 96)
2121
# Get project directory
2222
get_filename_component(BASE_PROJ_DIR ../ ABSOLUTE)
2323

24-
# Get the latest commit hash of the working branch
25-
IF(EXISTS "${BASE_PROJ_DIR}/.git")
26-
execute_process(
27-
COMMAND git rev-parse HEAD
28-
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
29-
OUTPUT_VARIABLE GIT_COMMIT_HASH
30-
OUTPUT_STRIP_TRAILING_WHITESPACE
31-
)
32-
ELSE(EXISTS "${BASE_PROJ_DIR}/.git")
33-
set(GIT_BRANCH "Unknown")
34-
set(GIT_COMMIT_HASH "Unknown")
35-
ENDIF(EXISTS "${BASE_PROJ_DIR}/.git")
24+
# GIT_COMMIT_HASH can be injected via -DGIT_COMMIT_HASH=<value> for Homebrew
25+
# or any tarball-based build where no .git directory is present.
26+
# Source builds fall through to git rev-parse automatically.
27+
set(GIT_COMMIT_HASH "" CACHE STRING
28+
"Build provenance override for tarball-based builds (Homebrew, released source tarball)")
29+
30+
if(GIT_COMMIT_HASH STREQUAL "")
31+
IF(EXISTS "${BASE_PROJ_DIR}/.git")
32+
execute_process(
33+
COMMAND git rev-parse HEAD
34+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
35+
OUTPUT_VARIABLE GIT_COMMIT_HASH
36+
OUTPUT_STRIP_TRAILING_WHITESPACE
37+
)
38+
ELSE(EXISTS "${BASE_PROJ_DIR}/.git")
39+
set(GIT_BRANCH "Unknown")
40+
set(GIT_COMMIT_HASH "Unknown")
41+
ENDIF(EXISTS "${BASE_PROJ_DIR}/.git")
42+
endif()
3643

3744
#Get the date
3845
string(TIMESTAMP COMPILATION_DATE %Y-%m-%d)

0 commit comments

Comments
 (0)