From 602de0c370467f75d07a7841672e8dacd09faec5 Mon Sep 17 00:00:00 2001 From: kalaposfos13 <153381648+kalaposfos13@users.noreply.github.com> Date: Thu, 27 Mar 2025 21:40:15 +0100 Subject: [PATCH] Fork detection: Fix PR actions only showing HEAD as the branch name (#2697) * I'd be very surprised if this works 1st try * More logging and cleanup * Minor fixes --- CMakeLists.txt | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c0932b5c..b3d214ec9 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -113,28 +113,39 @@ git_describe(GIT_DESC --always --long --dirty) git_branch_name(GIT_BRANCH) string(TIMESTAMP BUILD_DATE "%Y-%m-%d %H:%M:%S") +message("start git things") # Try to get the upstream remote and branch +message("check for remote and branch") execute_process( COMMAND git rev-parse --abbrev-ref --symbolic-full-name @{u} OUTPUT_VARIABLE GIT_REMOTE_NAME - RESULT_VARIABLE GIT_BRANCH_RESULT + RESULT_VARIABLE GIT_REMOTE_RESULT ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE ) - # If there's no upstream set or the command failed, check remote.pushDefault -if (GIT_BRANCH_RESULT OR GIT_REMOTE_NAME STREQUAL "") +if (GIT_REMOTE_RESULT OR GIT_REMOTE_NAME STREQUAL "") + message("check default push") execute_process( COMMAND git config --get remote.pushDefault OUTPUT_VARIABLE GIT_REMOTE_NAME - RESULT_VARIABLE GIT_PUSH_DEFAULT_RESULT + RESULT_VARIABLE GIT_REMOTE_RESULT ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE ) - - # If remote.pushDefault is not set or fails, default to origin - if (GIT_PUSH_DEFAULT_RESULT OR GIT_REMOTE_NAME STREQUAL "") - set(GIT_REMOTE_NAME "origin") +endif() +# If running in GitHub Actions and the above fails +if (GIT_REMOTE_RESULT OR GIT_REMOTE_NAME STREQUAL "") + message("check github") + set(GIT_REMOTE_NAME "origin") + + if (DEFINED ENV{GITHUB_HEAD_REF}) # PR branch name + set(GIT_BRANCH "pr-$ENV{GITHUB_HEAD_REF}") + elseif (DEFINED ENV{GITHUB_REF}) # Normal branch name + string(REGEX REPLACE "^refs/[^/]*/" "" GIT_BRANCH "$ENV{GITHUB_REF}") + else() + message("couldn't find branch") + set(GIT_BRANCH "detached-head") endif() else() # Extract remote name if the output contains a remote/branch format @@ -148,6 +159,7 @@ else() endif() # Get remote link +message("getting remote link") execute_process( COMMAND git config --get remote.${GIT_REMOTE_NAME}.url OUTPUT_VARIABLE GIT_REMOTE_URL @@ -156,6 +168,8 @@ execute_process( configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/common/scm_rev.cpp.in" "${CMAKE_CURRENT_BINARY_DIR}/src/common/scm_rev.cpp" @ONLY) +message("end git things, remote: ${GIT_REMOTE_NAME}, branch: ${GIT_BRANCH}") + find_package(Boost 1.84.0 CONFIG) find_package(FFmpeg 5.1.2 MODULE) find_package(fmt 10.2.0 CONFIG)