|
| 1 | +## Main |
| 2 | +# |
| 3 | +# Normalize Git URI to HTTP or SSH format compatible with |
| 4 | +# all major Git hosting services like Github, Gitlab, Bitbucket, etc. |
| 5 | +# |
| 6 | +# [Definition] |
| 7 | +# Git URIs can be in two main formats: |
| 8 | +# SSH format: git@hostname:path/to/repo.git |
| 9 | +# HTTP format: https://hostname/path/to/repo.git |
| 10 | +# |
| 11 | +# This module provides functionality to convert between these formats |
| 12 | +# while preserving all path information and handling edge cases like |
| 13 | +# port numbers and repositories without .git extension. |
| 14 | +# |
| 15 | + |
| 16 | +IF(DEFINED CMUTIL_NORMALIZE_GIT_URI_MODULE) |
| 17 | + RETURN() |
| 18 | +ENDIF() |
| 19 | +SET(CMUTIL_NORMALIZE_GIT_URI_MODULE 1) |
| 20 | + |
| 21 | +FIND_PACKAGE(CMLIB) |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +## |
| 26 | +# Normalize Git URI to specified target format (SSH or HTTP). |
| 27 | +# |
| 28 | +# Converts Git URIs between SSH and HTTP formats: |
| 29 | +# SSH format: git@hostname:path/to/repo.git |
| 30 | +# HTTP format: https://hostname/path/to/repo.git |
| 31 | +# |
| 32 | +# If the input URI is already in the target format, it is returned unchanged. |
| 33 | +# Supports complex paths, custom domains, port numbers, and repositories |
| 34 | +# without .git extension. |
| 35 | +# |
| 36 | +# <function>( |
| 37 | +# URI <git_uri> |
| 38 | +# TARGET_TYPE <SSH|HTTP> |
| 39 | +# OUTPUT_VAR <output_variable> |
| 40 | +# ) |
| 41 | +FUNCTION(CMUTIL_NORMALIZE_GIT_URI) |
| 42 | + CMLIB_PARSE_ARGUMENTS( |
| 43 | + ONE_VALUE |
| 44 | + URI |
| 45 | + TARGET_TYPE |
| 46 | + OUTPUT_VAR |
| 47 | + REQUIRED |
| 48 | + URI |
| 49 | + TARGET_TYPE |
| 50 | + OUTPUT_VAR |
| 51 | + P_ARGN ${ARGN} |
| 52 | + ) |
| 53 | + |
| 54 | + SET(output_value) |
| 55 | + IF(__TARGET_TYPE STREQUAL "SSH") |
| 56 | + _CMUTIL_NORMALIZE_GIT_URI_SSH( |
| 57 | + URI "${__URI}" |
| 58 | + OUTPUT_VAR _var |
| 59 | + ) |
| 60 | + SET(output_value "${_var}") |
| 61 | + ELSEIF(__TARGET_TYPE STREQUAL "HTTP") |
| 62 | + _CMUTIL_NORMALIZE_GIT_URI_HTTP( |
| 63 | + URI "${__URI}" |
| 64 | + OUTPUT_VAR _var |
| 65 | + ) |
| 66 | + SET(output_value "${_var}") |
| 67 | + ELSE() |
| 68 | + MESSAGE(FATAL_ERROR "Invalid TARGET_TYPE '${__TARGET_TYPE}'") |
| 69 | + ENDIF() |
| 70 | + |
| 71 | + SET(${__OUTPUT_VAR} "${output_value}" PARENT_SCOPE) |
| 72 | +ENDFUNCTION() |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +## Helper |
| 77 | +# |
| 78 | +# Converts HTTP(S) Git URIs to SSH format. |
| 79 | +# If URI is already in SSH format, returns it unchanged. |
| 80 | +# |
| 81 | +# Transformation examples: |
| 82 | +# https://github.com/user/repo.git --> git@github.com:user/repo.git |
| 83 | +# https://gitlab.com/user/repo.git --> git@gitlab.com:user/repo.git |
| 84 | +# git@gitlab.com:user/repo.git --> git@gitlab.com:user/repo.git (unchanged) |
| 85 | +# |
| 86 | +# <function>( |
| 87 | +# URI <uri> |
| 88 | +# OUTPUT_VAR <output_variable> |
| 89 | +# ) |
| 90 | +# |
| 91 | +FUNCTION(_CMUTIL_NORMALIZE_GIT_URI_SSH) |
| 92 | + CMLIB_PARSE_ARGUMENTS( |
| 93 | + ONE_VALUE |
| 94 | + URI |
| 95 | + OUTPUT_VAR |
| 96 | + REQUIRED |
| 97 | + URI |
| 98 | + OUTPUT_VAR |
| 99 | + P_ARGN ${ARGN} |
| 100 | + ) |
| 101 | + |
| 102 | + SET(uri "${__URI}") |
| 103 | + |
| 104 | + STRING(REGEX MATCH "^git@.*" git_ssh_uri "${uri}") |
| 105 | + IF(git_ssh_uri) |
| 106 | + SET(${__OUTPUT_VAR} "${uri}" PARENT_SCOPE) |
| 107 | + RETURN() |
| 108 | + ENDIF() |
| 109 | + |
| 110 | + STRING(REGEX MATCH "^https?://" git_http_uri "${uri}") |
| 111 | + IF(NOT git_http_uri) |
| 112 | + MESSAGE(FATAL_ERROR "URI '${uri}' is not a valid HTTP(S) or git@ Git URI") |
| 113 | + ENDIF() |
| 114 | + |
| 115 | + STRING(REGEX MATCH "^https?://([^/]+)/(.+)$" http_match "${uri}") |
| 116 | + IF(NOT http_match) |
| 117 | + MESSAGE(FATAL_ERROR "URI '${uri}' is not a valid HTTP(S) or git@ Git URI") |
| 118 | + ENDIF() |
| 119 | + |
| 120 | + STRING(REGEX REPLACE "^https?://([^/]+)/(.+)$" "\\1" hostname "${uri}") |
| 121 | + STRING(REGEX REPLACE "^https?://([^/]+)/(.+)$" "\\2" path "${uri}") |
| 122 | + |
| 123 | + SET(ssh_uri "git@${hostname}:${path}") |
| 124 | + SET(${__OUTPUT_VAR} "${ssh_uri}" PARENT_SCOPE) |
| 125 | +ENDFUNCTION() |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | +## Helper |
| 130 | +# |
| 131 | +# Converts SSH Git URIs to HTTPS format. |
| 132 | +# If URI is already in HTTP(S) format, returns it unchanged. |
| 133 | +# |
| 134 | +# Transformation examples: |
| 135 | +# git@github.com:user/repo.git --> https://github.com/user/repo.git |
| 136 | +# git@gitlab.com:user/repo.git --> https://gitlab.com/user/repo.git |
| 137 | +# https://github.com/user/repo.git --> https://github.com/user/repo.git (unchanged) |
| 138 | +# |
| 139 | +# <function>( |
| 140 | +# URI <uri> |
| 141 | +# OUTPUT_VAR <output_variable> |
| 142 | +# ) |
| 143 | +# |
| 144 | +FUNCTION(_CMUTIL_NORMALIZE_GIT_URI_HTTP) |
| 145 | + CMLIB_PARSE_ARGUMENTS( |
| 146 | + ONE_VALUE |
| 147 | + URI |
| 148 | + OUTPUT_VAR |
| 149 | + REQUIRED |
| 150 | + URI |
| 151 | + OUTPUT_VAR |
| 152 | + P_ARGN ${ARGN} |
| 153 | + ) |
| 154 | + |
| 155 | + SET(uri "${__URI}") |
| 156 | + |
| 157 | + STRING(REGEX MATCH "^https?://" git_http_uri "${uri}") |
| 158 | + IF(git_http_uri) |
| 159 | + SET(${__OUTPUT_VAR} "${uri}" PARENT_SCOPE) |
| 160 | + RETURN() |
| 161 | + ENDIF() |
| 162 | + |
| 163 | + STRING(REGEX MATCH "^git@.*" git_ssh_uri "${uri}") |
| 164 | + IF(NOT git_ssh_uri) |
| 165 | + MESSAGE(FATAL_ERROR "URI '${uri}' is not a valid git@ or HTTP(S) Git URI") |
| 166 | + ENDIF() |
| 167 | + |
| 168 | + STRING(REGEX MATCH "^git@([^:]+):(.+)$" ssh_match "${uri}") |
| 169 | + IF(NOT ssh_match) |
| 170 | + MESSAGE(FATAL_ERROR "URI '${uri}' is not a valid git@ or HTTP(S) Git URI") |
| 171 | + ENDIF() |
| 172 | + |
| 173 | + STRING(REGEX REPLACE "^git@([^:]+):(.+)$" "\\1" hostname "${uri}") |
| 174 | + STRING(REGEX REPLACE "^git@([^:]+):(.+)$" "\\2" path "${uri}") |
| 175 | + |
| 176 | + SET(https_uri "https://${hostname}/${path}") |
| 177 | + SET(${__OUTPUT_VAR} "${https_uri}" PARENT_SCOPE) |
| 178 | +ENDFUNCTION() |
| 179 | + |
0 commit comments