From a3ac701c1a6c1ed702a579c8c10bc9927041e3b9 Mon Sep 17 00:00:00 2001 From: Chandragupt Singh Date: Sat, 18 Apr 2026 23:29:54 +0530 Subject: [PATCH] fix(build): support Git commit hash injection for tarball-based package builds (#2266) --- linux/build | 7 +++++++ linux/pre-build.sh | 7 ++++++- mac/build.command | 7 +++++++ mac/pre-build.sh | 7 ++++++- src/CMakeLists.txt | 31 +++++++++++++++++++------------ 5 files changed, 45 insertions(+), 14 deletions(-) diff --git a/linux/build b/linux/build index aa481f7d..0088a850 100755 --- a/linux/build +++ b/linux/build @@ -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 diff --git a/linux/pre-build.sh b/linux/pre-build.sh index 19a3dd36..570c6641 100755 --- a/linux/pre-build.sh +++ b/linux/pre-build.sh @@ -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 diff --git a/mac/build.command b/mac/build.command index f1ec6721..2fb70eab 100755 --- a/mac/build.command +++ b/mac/build.command @@ -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 diff --git a/mac/pre-build.sh b/mac/pre-build.sh index 1503d933..c4852ca3 100755 --- a/mac/pre-build.sh +++ b/mac/pre-build.sh @@ -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 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6cd98b3e..088d9235 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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= 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)