-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib/portability: Add @go.native_file_path_or_url
Closes #187. Part of #186. Replaces `git_for_windows_native_path` and updates the interface to print to a result variable rather than standard output, avoiding a subshell when no conversion takes place.
- Loading branch information
Showing
5 changed files
with
71 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#! /bin/bash | ||
#! /usr/bin/env bash | ||
# | ||
# Sets the `_GO_PLATFORM_*` family of environment variables | ||
# | ||
|
This file contains 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,35 @@ | ||
#! /usr/bin/env bash | ||
# | ||
# Functions to manage platform differences | ||
# | ||
# Exports: | ||
# @go.native_file_path_or_url | ||
# Converts a file system path or 'file://' URL to a platform-native path | ||
|
||
. "$_GO_USE_MODULES" 'platform' 'validation' | ||
|
||
# Converts a file system path or 'file://' URL to a platform-native path | ||
# | ||
# This is useful when passing file paths or URLs to native programs on Git for | ||
# Windows, or validating the output of such programs, to ensure portability. | ||
# The resulting path will contain forward slashes. | ||
# | ||
# Prints both converted and unconverted paths and URLs to the specified result | ||
# variable. | ||
# | ||
# Arguments: | ||
# result_var_name: Name of caller's variable in which to store the result | ||
# path: File system path or 'file://' URL to convert | ||
@go.native_file_path_or_url() { | ||
local _gnp_protocol="${2%%://*}" | ||
@go.validate_identifier_or_die 'Result variable name' "$1" | ||
|
||
if [[ "$_GO_PLATFORM_ID" != 'msys-git' ]] || | ||
[[ "$_gnp_protocol" != "$2" && "$_gnp_protocol" != 'file' ]]; then | ||
printf -v "$1" '%s' "$2" | ||
elif [[ "$protocol" == 'file' ]]; then | ||
printf -v "$1" 'file://%s' "$(cygpath -m "${2#file://}")" | ||
else | ||
printf -v "$1" '%s' "$(cygpath -m "$2")" | ||
fi | ||
} |
This file contains 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 |
---|---|---|
|
@@ -206,10 +206,8 @@ [email protected]_file() { | |
if [[ "${url:0:1}" != '/' ]]; then | ||
url="${PWD}/${url}" | ||
fi | ||
if [[ "$url" =~ ^/[^/]+ && -d "$BASH_REMATCH/Windows" ]]; then | ||
url="${BASH_REMATCH}:/${url#$BASH_REMATCH/}" | ||
fi | ||
url="file://${url}" | ||
. "$_GO_USE_MODULES" 'portability' | ||
@go.native_file_path_or_url 'url' "file://${url}" | ||
fi | ||
[email protected]_file_impl "$download_dir" "$filename" "$url" | ||
} | ||
|
This file contains 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,30 @@ | ||
#! /usr/bin/env bats | ||
|
||
load ../environment | ||
load "$_GO_CORE_DIR/lib/portability" | ||
|
||
@test "$SUITE: return unaltered path if _GO_PLATFORM_ID isn't msys-git" { | ||
local result | ||
_GO_PLATFORM_ID='foobar' @go.native_file_path_or_url 'result' '/foo/bar' | ||
assert_equal '/foo/bar' "$result" | ||
} | ||
|
||
@test "$SUITE: return unaltered path if protocol isn't file://" { | ||
local result | ||
_GO_PLATFORM_ID='msys-git' \ | ||
@go.native_file_path_or_url 'result' 'https://mike-bland.com/' | ||
assert_equal 'https://mike-bland.com/' "$result" | ||
} | ||
|
||
@test "$SUITE: return updated file system path" { | ||
skip_if_system_missing 'cygpath' | ||
_GO_PLATFORM_ID='msys-git' @go.native_file_path_or_url 'result' '/foo/bar' | ||
assert_equal "$(cygpath -m '/foo/bar')" "$result" | ||
} | ||
|
||
@test "$SUITE: return updated file:// URL" { | ||
skip_if_system_missing 'cygpath' | ||
_GO_PLATFORM_ID='msys-git' \ | ||
@go.native_file_path_or_url 'result' 'file:///foo/bar' | ||
assert_equal "file://$(cygpath -m '/foo/bar')" "$result" | ||
} |
This file contains 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