1# Adds version control information to the variable VERS. For 2# determining the Version Control System used (if any) it inspects the 3# existence of certain subdirectories under SOURCE_DIR (if provided as an 4# extra argument, otherwise uses CMAKE_CURRENT_SOURCE_DIR). 5 6function(get_source_info path revision repository) 7 find_package(Git QUIET) 8 if(GIT_FOUND) 9 execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --git-dir 10 WORKING_DIRECTORY ${path} 11 RESULT_VARIABLE git_result 12 OUTPUT_VARIABLE git_output 13 ERROR_QUIET) 14 if(git_result EQUAL 0) 15 string(STRIP "${git_output}" git_output) 16 get_filename_component(git_dir ${git_output} ABSOLUTE BASE_DIR ${path}) 17 execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse HEAD 18 WORKING_DIRECTORY ${path} 19 RESULT_VARIABLE git_result 20 OUTPUT_VARIABLE git_output) 21 if(git_result EQUAL 0) 22 string(STRIP "${git_output}" git_output) 23 set(${revision} ${git_output} PARENT_SCOPE) 24 endif() 25 execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref --symbolic-full-name @{upstream} 26 WORKING_DIRECTORY ${path} 27 RESULT_VARIABLE git_result 28 OUTPUT_VARIABLE git_output 29 ERROR_QUIET) 30 if(git_result EQUAL 0) 31 string(REPLACE "/" ";" branch ${git_output}) 32 list(GET branch 0 remote) 33 else() 34 set(remote "origin") 35 endif() 36 execute_process(COMMAND ${GIT_EXECUTABLE} remote get-url ${remote} 37 WORKING_DIRECTORY ${path} 38 RESULT_VARIABLE git_result 39 OUTPUT_VARIABLE git_output 40 ERROR_QUIET) 41 if(git_result EQUAL 0) 42 # Passwords or tokens should not be stored in the remote URL at the 43 # risk of being leaked. In case we find one, error out and teach the 44 # user the best practices. 45 string(REGEX MATCH "https?://[^/]*:[^/]*@.*" 46 http_password "${git_output}") 47 if(http_password) 48 message(SEND_ERROR "The git remote repository URL has an embedded \ 49password. Remove the password from the URL or use \ 50`-DLLVM_FORCE_VC_REPOSITORY=<URL without password>` in order to avoid \ 51leaking your password (see https://git-scm.com/docs/gitcredentials for \ 52alternatives).") 53 endif() 54 # GitHub token formats are described at: 55 # https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/about-authentication-to-github#githubs-token-formats 56 string(REGEX MATCH 57 "https?://(gh[pousr]|github_pat)_[^/]+@github.com.*" 58 github_token "${git_output}") 59 if(github_token) 60 message(SEND_ERROR "The git remote repository URL has an embedded \ 61GitHub Token. Remove the token from the URL or use \ 62`-DLLVM_FORCE_VC_REPOSITORY=<URL without token>` in order to avoid leaking \ 63your token (see https://git-scm.com/docs/gitcredentials for alternatives).") 64 endif() 65 66 string(STRIP "${git_output}" git_output) 67 set(${repository} ${git_output} PARENT_SCOPE) 68 else() 69 set(${repository} ${path} PARENT_SCOPE) 70 endif() 71 endif() 72 else() 73 message(WARNING "Git not found. Version cannot be determined.") 74 endif() 75endfunction() 76