-
Notifications
You must be signed in to change notification settings - Fork 1
Add NORMILIZE_GIT_URI functionality #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
## Main | ||
# | ||
# Normalize Git URI to HTTP or SSH format compatible with | ||
# all major Git hosting services like Github, Gitlab, Bitbucket, etc. | ||
# | ||
# [Definition] | ||
# Git URIs can be in two main formats: | ||
# SSH format: git@hostname:path/to/repo.git | ||
# HTTP format: https://hostname/path/to/repo.git | ||
# | ||
# This module provides functionality to convert between these formats | ||
# while preserving all path information and handling edge cases like | ||
# port numbers and repositories without .git extension. | ||
# | ||
|
||
IF(DEFINED CMUTIL_NORMALIZE_GIT_URI_MODULE) | ||
RETURN() | ||
ENDIF() | ||
SET(CMUTIL_NORMALIZE_GIT_URI_MODULE 1) | ||
|
||
FIND_PACKAGE(CMLIB) | ||
|
||
|
||
|
||
## | ||
# Normalize Git URI to specified target format (SSH or HTTP). | ||
# | ||
# Converts Git URIs between SSH and HTTP formats: | ||
# SSH format: git@hostname:path/to/repo.git | ||
# HTTP format: https://hostname/path/to/repo.git | ||
# | ||
# If the input URI is already in the target format, it is returned unchanged. | ||
# Supports complex paths, custom domains, port numbers, and repositories | ||
# without .git extension. | ||
# | ||
# <function>( | ||
# URI <git_uri> | ||
# TARGET_TYPE <SSH|HTTP> | ||
# OUTPUT_VAR <output_variable> | ||
# ) | ||
FUNCTION(CMUTIL_NORMALIZE_GIT_URI) | ||
CMLIB_PARSE_ARGUMENTS( | ||
ONE_VALUE | ||
URI | ||
TARGET_TYPE | ||
OUTPUT_VAR | ||
REQUIRED | ||
URI | ||
TARGET_TYPE | ||
OUTPUT_VAR | ||
P_ARGN ${ARGN} | ||
) | ||
|
||
SET(output_value) | ||
IF(__TARGET_TYPE STREQUAL "SSH") | ||
_CMUTIL_NORMALIZE_GIT_URI_SSH( | ||
URI "${__URI}" | ||
OUTPUT_VAR _var | ||
) | ||
SET(output_value "${_var}") | ||
ELSEIF(__TARGET_TYPE STREQUAL "HTTP") | ||
_CMUTIL_NORMALIZE_GIT_URI_HTTP( | ||
URI "${__URI}" | ||
OUTPUT_VAR _var | ||
) | ||
SET(output_value "${_var}") | ||
ELSE() | ||
MESSAGE(FATAL_ERROR "Invalid TARGET_TYPE '${__TARGET_TYPE}'") | ||
ENDIF() | ||
|
||
SET(${__OUTPUT_VAR} "${output_value}" PARENT_SCOPE) | ||
ENDFUNCTION() | ||
|
||
|
||
|
||
## Helper | ||
# | ||
# Converts HTTP(S) Git URIs to SSH format. | ||
# If URI is already in SSH format, returns it unchanged. | ||
# | ||
# Transformation examples: | ||
# https://github.com/user/repo.git --> git@github.com:user/repo.git | ||
# https://gitlab.com/user/repo.git --> git@gitlab.com:user/repo.git | ||
# git@gitlab.com:user/repo.git --> git@gitlab.com:user/repo.git (unchanged) | ||
# | ||
# <function>( | ||
# URI <uri> | ||
# OUTPUT_VAR <output_variable> | ||
# ) | ||
# | ||
FUNCTION(_CMUTIL_NORMALIZE_GIT_URI_SSH) | ||
CMLIB_PARSE_ARGUMENTS( | ||
ONE_VALUE | ||
URI | ||
OUTPUT_VAR | ||
REQUIRED | ||
URI | ||
OUTPUT_VAR | ||
P_ARGN ${ARGN} | ||
) | ||
|
||
SET(uri "${__URI}") | ||
|
||
STRING(REGEX MATCH "^git@.*" git_ssh_uri "${uri}") | ||
IF(git_ssh_uri) | ||
SET(${__OUTPUT_VAR} "${uri}" PARENT_SCOPE) | ||
RETURN() | ||
ENDIF() | ||
|
||
STRING(REGEX MATCH "^https?://" git_http_uri "${uri}") | ||
IF(NOT git_http_uri) | ||
MESSAGE(FATAL_ERROR "URI '${uri}' is not a valid HTTP(S) or git@ Git URI") | ||
ENDIF() | ||
|
||
STRING(REGEX MATCH "^https?://([^/]+)/(.+)$" http_match "${uri}") | ||
IF(NOT http_match) | ||
MESSAGE(FATAL_ERROR "URI '${uri}' is not a valid HTTP(S) or git@ Git URI") | ||
ENDIF() | ||
koudis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
STRING(REGEX REPLACE "^https?://([^/]+)/(.+)$" "\\1" hostname "${uri}") | ||
STRING(REGEX REPLACE "^https?://([^/]+)/(.+)$" "\\2" path "${uri}") | ||
|
||
SET(ssh_uri "git@${hostname}:${path}") | ||
SET(${__OUTPUT_VAR} "${ssh_uri}" PARENT_SCOPE) | ||
ENDFUNCTION() | ||
|
||
|
||
|
||
## Helper | ||
# | ||
# Converts SSH Git URIs to HTTPS format. | ||
# If URI is already in HTTP(S) format, returns it unchanged. | ||
# | ||
# Transformation examples: | ||
# git@github.com:user/repo.git --> https://github.com/user/repo.git | ||
# git@gitlab.com:user/repo.git --> https://gitlab.com/user/repo.git | ||
# https://github.com/user/repo.git --> https://github.com/user/repo.git (unchanged) | ||
# | ||
# <function>( | ||
# URI <uri> | ||
# OUTPUT_VAR <output_variable> | ||
# ) | ||
# | ||
FUNCTION(_CMUTIL_NORMALIZE_GIT_URI_HTTP) | ||
CMLIB_PARSE_ARGUMENTS( | ||
ONE_VALUE | ||
URI | ||
OUTPUT_VAR | ||
REQUIRED | ||
URI | ||
OUTPUT_VAR | ||
P_ARGN ${ARGN} | ||
) | ||
|
||
SET(uri "${__URI}") | ||
|
||
STRING(REGEX MATCH "^https?://" git_http_uri "${uri}") | ||
IF(git_http_uri) | ||
SET(${__OUTPUT_VAR} "${uri}" PARENT_SCOPE) | ||
RETURN() | ||
ENDIF() | ||
|
||
STRING(REGEX MATCH "^git@.*" git_ssh_uri "${uri}") | ||
IF(NOT git_ssh_uri) | ||
MESSAGE(FATAL_ERROR "URI '${uri}' is not a valid git@ or HTTP(S) Git URI") | ||
ENDIF() | ||
|
||
STRING(REGEX MATCH "^git@([^:]+):(.+)$" ssh_match "${uri}") | ||
IF(NOT ssh_match) | ||
MESSAGE(FATAL_ERROR "URI '${uri}' is not a valid git@ or HTTP(S) Git URI") | ||
ENDIF() | ||
koudis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
STRING(REGEX REPLACE "^git@([^:]+):(.+)$" "\\1" hostname "${uri}") | ||
STRING(REGEX REPLACE "^git@([^:]+):(.+)$" "\\2" path "${uri}") | ||
|
||
SET(https_uri "https://${hostname}/${path}") | ||
SET(${__OUTPUT_VAR} "${https_uri}" PARENT_SCOPE) | ||
koudis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ENDFUNCTION() | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
IF(NOT DEFINED CMAKE_SCRIPT_MODE_FILE) | ||
CMAKE_MINIMUM_REQUIRED(VERSION 3.18) | ||
PROJECT(CMUTIL_NORMALIZE_GIT_URI_TEST) | ||
ENDIF() | ||
|
||
LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../") | ||
|
||
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/../TEST.cmake") | ||
|
||
FIND_PACKAGE(CMUTIL) | ||
|
||
|
||
|
||
FUNCTION(TEST_SSH_TARGET_TYPE) | ||
CMUTIL_NORMALIZE_GIT_URI( | ||
URI "https://github.com/username/repository.git" | ||
TARGET_TYPE "SSH" | ||
OUTPUT_VAR result | ||
) | ||
TEST_VAR_EQUALS_LITERAL(result "git@github.com:username/repository.git") | ||
koudis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
CMUTIL_NORMALIZE_GIT_URI( | ||
URI "https://gitlab.com/user/repo.git" | ||
TARGET_TYPE "SSH" | ||
OUTPUT_VAR result | ||
) | ||
TEST_VAR_EQUALS_LITERAL(result "git@gitlab.com:user/repo.git") | ||
|
||
CMUTIL_NORMALIZE_GIT_URI( | ||
URI "http://example.com/path/to/repo.git" | ||
TARGET_TYPE "SSH" | ||
OUTPUT_VAR result | ||
) | ||
TEST_VAR_EQUALS_LITERAL(result "git@example.com:path/to/repo.git") | ||
|
||
CMUTIL_NORMALIZE_GIT_URI( | ||
URI "git@github.com:user/repo.git" | ||
TARGET_TYPE "SSH" | ||
OUTPUT_VAR result | ||
) | ||
TEST_VAR_EQUALS_LITERAL(result "git@github.com:user/repo.git") | ||
|
||
CMUTIL_NORMALIZE_GIT_URI( | ||
URI "https://gitlab.example.com/group/subgroup/project.git" | ||
TARGET_TYPE "SSH" | ||
OUTPUT_VAR result | ||
) | ||
TEST_VAR_EQUALS_LITERAL(result "git@gitlab.example.com:group/subgroup/project.git") | ||
|
||
CMUTIL_NORMALIZE_GIT_URI( | ||
URI "https://github.com/user/repo" | ||
TARGET_TYPE "SSH" | ||
OUTPUT_VAR result | ||
) | ||
TEST_VAR_EQUALS_LITERAL(result "git@github.com:user/repo") | ||
|
||
CMUTIL_NORMALIZE_GIT_URI( | ||
URI "https://git.example.com:8080/user/repo.git" | ||
TARGET_TYPE "SSH" | ||
OUTPUT_VAR result | ||
) | ||
TEST_VAR_EQUALS_LITERAL(result "git@git.example.com:8080:user/repo.git") | ||
koudis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ENDFUNCTION() | ||
|
||
|
||
|
||
FUNCTION(TEST_HTTP_TARGET_TYPE) | ||
CMUTIL_NORMALIZE_GIT_URI( | ||
URI "git@github.com:username/repository.git" | ||
TARGET_TYPE "HTTP" | ||
OUTPUT_VAR result | ||
) | ||
TEST_VAR_EQUALS_LITERAL(result "https://github.com/username/repository.git") | ||
|
||
CMUTIL_NORMALIZE_GIT_URI( | ||
URI "git@gitlab.com:user/repo.git" | ||
TARGET_TYPE "HTTP" | ||
OUTPUT_VAR result | ||
) | ||
TEST_VAR_EQUALS_LITERAL(result "https://gitlab.com/user/repo.git") | ||
|
||
CMUTIL_NORMALIZE_GIT_URI( | ||
URI "git@example.com:path/to/repo.git" | ||
TARGET_TYPE "HTTP" | ||
OUTPUT_VAR result | ||
) | ||
TEST_VAR_EQUALS_LITERAL(result "https://example.com/path/to/repo.git") | ||
|
||
CMUTIL_NORMALIZE_GIT_URI( | ||
URI "https://github.com/user/repo.git" | ||
TARGET_TYPE "HTTP" | ||
OUTPUT_VAR result | ||
) | ||
TEST_VAR_EQUALS_LITERAL(result "https://github.com/user/repo.git") | ||
|
||
CMUTIL_NORMALIZE_GIT_URI( | ||
URI "http://example.com/user/repo.git" | ||
TARGET_TYPE "HTTP" | ||
OUTPUT_VAR result | ||
) | ||
TEST_VAR_EQUALS_LITERAL(result "http://example.com/user/repo.git") | ||
|
||
CMUTIL_NORMALIZE_GIT_URI( | ||
URI "git@gitlab.example.com:group/subgroup/project.git" | ||
TARGET_TYPE "HTTP" | ||
OUTPUT_VAR result | ||
) | ||
TEST_VAR_EQUALS_LITERAL(result "https://gitlab.example.com/group/subgroup/project.git") | ||
|
||
CMUTIL_NORMALIZE_GIT_URI( | ||
URI "git@github.com:user/repo" | ||
TARGET_TYPE "HTTP" | ||
OUTPUT_VAR result | ||
) | ||
TEST_VAR_EQUALS_LITERAL(result "https://github.com/user/repo") | ||
ENDFUNCTION() | ||
|
||
TEST_SSH_TARGET_TYPE() | ||
TEST_HTTP_TARGET_TYPE() | ||
|
||
TEST_RUN("fail/") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
IF(NOT DEFINED CMAKE_SCRIPT_MODE_FILE) | ||
CMAKE_MINIMUM_REQUIRED(VERSION 3.18) | ||
PROJECT(CMUTIL_NORMALIZE_GIT_URI_FAIL_TEST) | ||
ENDIF() | ||
|
||
LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../../") | ||
|
||
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/../../TEST.cmake") | ||
|
||
TEST_INVALID_CMAKE_RUN("invalid_target_type/" "Invalid TARGET_TYPE") | ||
TEST_INVALID_CMAKE_RUN("invalid_http_uri/" "is not a valid HTTP\\(S\\) or git@ Git URI") | ||
TEST_INVALID_CMAKE_RUN("invalid_ssh_uri/" "is not a valid git@ or HTTP\\(S\\) Git URI") |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.