fix(build): support Git commit hash injection for tarball-based package builds (#2266)

This commit is contained in:
Chandragupt Singh
2026-04-18 23:29:54 +05:30
committed by GitHub
parent 600cc5f96d
commit a3ac701c1a
5 changed files with 45 additions and 14 deletions

View File

@@ -33,6 +33,13 @@ while [[ $# -gt 0 ]]; do
USE_SYSTEM_LIBS=true
shift
;;
-commit-hash)
# Inject build provenance for tarball builds (Homebrew, released source tarball, etc.)
# where no .git directory is present. Passed through to pre-build.sh
# via the exported env var GIT_COMMIT_HASH_OVERRIDE.
export GIT_COMMIT_HASH_OVERRIDE="$2"
shift 2
;;
-*)
echo "Unknown option $1"
exit 1

View File

@@ -1,6 +1,11 @@
#!/usr/bin/env bash
echo "Obtaining Git commit"
commit=(`git rev-parse HEAD 2>/dev/null`)
if [ -n "$GIT_COMMIT_HASH_OVERRIDE" ]; then
commit="$GIT_COMMIT_HASH_OVERRIDE"
echo "Using injected commit hash: $commit"
else
commit=(`git rev-parse HEAD 2>/dev/null`)
fi
if [ -z "$commit" ]; then
echo "Git command not present, trying folder approach"
if [ -d "../.git" ]; then

View File

@@ -35,6 +35,13 @@ while [[ $# -gt 0 ]]; do
USE_SYSTEM_LIBS=true
shift
;;
-commit-hash)
# Inject build provenance for tarball builds (Homebrew, released source tarball, etc.)
# where no .git directory is present. Passed through to pre-build.sh
# via the exported env var GIT_COMMIT_HASH_OVERRIDE.
export GIT_COMMIT_HASH_OVERRIDE="$2"
shift 2
;;
-*)
echo "Unknown option $1"
exit 1

View File

@@ -1,6 +1,11 @@
#!/bin/bash
echo "Obtaining Git commit"
commit=(`git rev-parse HEAD 2>/dev/null`)
if [ -n "$GIT_COMMIT_HASH_OVERRIDE" ]; then
commit="$GIT_COMMIT_HASH_OVERRIDE"
echo "Using injected commit hash: $commit"
else
commit=(`git rev-parse HEAD 2>/dev/null`)
fi
if [ -z "$commit" ]; then
echo "Git command not present, trying folder approach"
if [ -d "../.git" ]; then

View File

@@ -21,18 +21,25 @@ set (CCEXTRACTOR_VERSION_MINOR 96)
# Get project directory
get_filename_component(BASE_PROJ_DIR ../ ABSOLUTE)
# Get the latest commit hash of the working branch
IF(EXISTS "${BASE_PROJ_DIR}/.git")
execute_process(
COMMAND git rev-parse HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
ELSE(EXISTS "${BASE_PROJ_DIR}/.git")
set(GIT_BRANCH "Unknown")
set(GIT_COMMIT_HASH "Unknown")
ENDIF(EXISTS "${BASE_PROJ_DIR}/.git")
# GIT_COMMIT_HASH can be injected via -DGIT_COMMIT_HASH=<value> for Homebrew
# or any tarball-based build where no .git directory is present.
# Source builds fall through to git rev-parse automatically.
set(GIT_COMMIT_HASH "" CACHE STRING
"Build provenance override for tarball-based builds (Homebrew, released source tarball)")
if(GIT_COMMIT_HASH STREQUAL "")
IF(EXISTS "${BASE_PROJ_DIR}/.git")
execute_process(
COMMAND git rev-parse HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
ELSE(EXISTS "${BASE_PROJ_DIR}/.git")
set(GIT_BRANCH "Unknown")
set(GIT_COMMIT_HASH "Unknown")
ENDIF(EXISTS "${BASE_PROJ_DIR}/.git")
endif()
#Get the date
string(TIMESTAMP COMPILATION_DATE %Y-%m-%d)