From 2ebc0aa6c1f1d7d66ff4fe88a0fcd82611330c0b Mon Sep 17 00:00:00 2001 From: SoftFever Date: Wed, 11 Mar 2026 13:32:29 +0800 Subject: [PATCH] Fix GIT_COMMIT_HASH not set in Flatpak builds (#12725) The env var check was gated inside the .git directory check, so Flatpak builds (which exclude .git from the sandbox) always fell back to "0000000". Lift the env var check to top level and inject the commit hash into the Flatpak manifest via build-options.env. --- .github/workflows/build_all.yml | 7 ++++++- CMakeLists.txt | 28 +++++++++++++++------------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/.github/workflows/build_all.yml b/.github/workflows/build_all.yml index 1ee1cbd012..c39501680c 100644 --- a/.github/workflows/build_all.yml +++ b/.github/workflows/build_all.yml @@ -170,7 +170,7 @@ jobs: git_commit_hash="${{ github.event.pull_request.head.sha }}" else ver=V$ver_pure - git_commit_hash="" + git_commit_hash="${{ github.sha }}" fi echo "ver=$ver" >> $GITHUB_ENV echo "ver_pure=$ver_pure" >> $GITHUB_ENV @@ -197,6 +197,11 @@ jobs: sed -i '0,/^finish-args:/s//build-options:\n no-debuginfo: true\n strip: true\nfinish-args:/' \ scripts/flatpak/io.github.orcaslicer.OrcaSlicer.yml shell: bash + - name: Inject git commit hash into Flatpak manifest + run: | + sed -i "/name: OrcaSlicer/{n;s|buildsystem: simple|buildsystem: simple\n build-options:\n env:\n git_commit_hash: \"$git_commit_hash\"|}" \ + scripts/flatpak/io.github.orcaslicer.OrcaSlicer.yml + shell: bash - uses: flatpak/flatpak-github-actions/flatpak-builder@master with: bundle: OrcaSlicer-Linux-flatpak_${{ env.ver }}_${{ matrix.variant.arch }}.flatpak diff --git a/CMakeLists.txt b/CMakeLists.txt index 9356c308ee..8f47f3a944 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,10 +89,9 @@ else () endif () find_package(Git) -if(GIT_FOUND AND EXISTS "${CMAKE_SOURCE_DIR}/.git") - if(DEFINED ENV{git_commit_hash} AND NOT "$ENV{git_commit_hash}" STREQUAL "") - message(STATUS "Specified git commit hash: $ENV{git_commit_hash}") - +if(DEFINED ENV{git_commit_hash} AND NOT "$ENV{git_commit_hash}" STREQUAL "") + message(STATUS "Specified git commit hash: $ENV{git_commit_hash}") + if(GIT_FOUND AND EXISTS "${CMAKE_SOURCE_DIR}/.git") # Convert the given hash to short hash execute_process( COMMAND ${GIT_EXECUTABLE} rev-parse --short "$ENV{git_commit_hash}" @@ -100,17 +99,20 @@ if(GIT_FOUND AND EXISTS "${CMAKE_SOURCE_DIR}/.git") OUTPUT_VARIABLE GIT_COMMIT_HASH OUTPUT_STRIP_TRAILING_WHITESPACE ) - add_definitions("-DGIT_COMMIT_HASH=\"${GIT_COMMIT_HASH}\"") else() - # Check current Git commit hash - execute_process( - COMMAND ${GIT_EXECUTABLE} log -1 --format=%h - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} - OUTPUT_VARIABLE GIT_COMMIT_HASH - OUTPUT_STRIP_TRAILING_WHITESPACE - ) - add_definitions("-DGIT_COMMIT_HASH=\"${GIT_COMMIT_HASH}\"") + # No .git directory (e.g., Flatpak sandbox) — truncate directly + string(SUBSTRING "$ENV{git_commit_hash}" 0 7 GIT_COMMIT_HASH) endif() + add_definitions("-DGIT_COMMIT_HASH=\"${GIT_COMMIT_HASH}\"") +elseif(GIT_FOUND AND EXISTS "${CMAKE_SOURCE_DIR}/.git") + # Check current Git commit hash + execute_process( + COMMAND ${GIT_EXECUTABLE} log -1 --format=%h + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + OUTPUT_VARIABLE GIT_COMMIT_HASH + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + add_definitions("-DGIT_COMMIT_HASH=\"${GIT_COMMIT_HASH}\"") endif() if(DEFINED ENV{SLIC3R_STATIC})