Skip to content

Commit

Permalink
Fix git import for packages
Browse files Browse the repository at this point in the history
This fixes importing for packages coming from git.  It adds support
for importing over ssh and gets rid of dependencies on sed ( because
OSX's sed is trash ) and awk.

The only thing not supported is packages that want to use the
Git daemon protocol, but it's recommended that you wrap that in
ssh/https anyways, due to it's concerning lack of auth.
  • Loading branch information
sjmh committed Nov 8, 2019
1 parent c1872aa commit ab6bd15
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 41 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ bake make-release
* Kyle Burton <[email protected]>
* Isaac Schaaf <[email protected]>
* Steve Hajducko <[email protected]>
# License
Expand Down
101 changes: 60 additions & 41 deletions bake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -Eeu -o pipefail


# TODO: detect if we're being sourced or if we're being executed
BAKE_VERSION="2014-11-05"
BAKE_VERSION="1.0.15"
BAKE_STDOUT_IS_TERMINAL=""
BAKE_COLOR_NORMAL=""
BAKE_COLOR_RED=""
Expand Down Expand Up @@ -77,6 +77,15 @@ function bake_bakefile_dir () {

BAKE_ROOT_DIR="$(bake_root_dir)"

function bake_looks_like_git () {
local thing="$1"

if [[ $thing == git@* ]]; then
return 0
fi
return 1
}

function bake_looks_like_url () {
local thing="$1"

Expand All @@ -92,6 +101,10 @@ function bake_looks_like_url () {
return 0
fi

if [[ $thing == ssh://* ]]; then
return 0
fi

return 1
}

Expand Down Expand Up @@ -140,69 +153,71 @@ function bake_require_from_fs () {

function bake_url_to_package_path () {
local url="$1"
# strip the http:// or https:// if present
# strip the *:// if present
# then see if it exists as $BAKE_PACKAGES_PATH/$url
local fpath
fpath="$(echo "$url" | sed 's/^https\?:\/\///')"
fpath=${url##*://}
fpath=${fpath##git@}
fpath=${fpath%%.git}
echo "$BAKE_PACKAGES_PATH/$fpath"
}

function bake_require_git () {
function bake_git_to_url () {
local url="$1"
local libpath="$2"
local tag="${3:-master}"

bake_ensure_bake_packages_path
local host
local path

pushd "$BAKE_PACKAGES_PATH" >/dev/null

# [email protected]:kyleburton/bake-recipes.git
# https://github.com/kyleburton/bake-recipes.git
# strip the leading git@, http:// or https://
local full_package_path
full_package_path="$(echo "$url" | sed 's/^git@//' | sed 's/https:\/\///' | sed 's/http:\/\///' | sed 's/.git$//' | sed 's/:/\//')"
local package_path
package_path="$(dirname "$full_package_path")"

test -d "$package_path" || mkdir -p "$package_path"
cd "$package_path"
local dname
dname="$(basename "$full_package_path")"
if [ ! -d "$dname" ]; then
git clone "$url"
cd "$dname"
git fetch
git co "$tag"
fi

popd > /dev/null

# translate the require path to local fs path
# source it!
source "$BAKE_PACKAGES_PATH/$full_package_path/$libpath"
[[ $url =~ git@(.*):(.*) ]] && host=${BASH_REMATCH[1]} && path=${BASH_REMATCH[2]}
echo ssh://git@${host}/${path}
}


function bake_ensure_bake_packages_path () {
test -d "$BAKE_PACKAGES_PATH" || mkdir -p "$BAKE_PACKAGES_PATH"
}

function bake_package_install () {
local url="$1"
local tag="${2:-master}"
local schema=

trap "bake_echo_red 'Error[bake_package_install] git command not found in PATH'" EXIT
which git > /dev/null
trap EXIT # clear the trap

# If no schema, assume it's https
if [[ $url != *://* ]]; then
url=https://${url}
fi
schema=${url%%://*}

bake_ensure_bake_packages_path
pushd "$BAKE_PACKAGES_PATH"
local git_project_name
git_project_name="$(echo "$url" | awk -F/ '{print $3 }')"
local git_host_and_user
git_host_and_user="$(echo "$url" | awk -F/ '{print $1 "/" $2 }')"
pushd "$BAKE_PACKAGES_PATH" > /dev/null

local git_project_name=
local git_host_and_user=
echo URL is $url
[[ $url =~ [a-zA-Z]+://([a-zA-Z0-9@-_.]+/[a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/(.*) ]] && \
git_host_and_user=${BASH_REMATCH[1]} && \
git_project_name=${BASH_REMATCH[2]}

trap "bake_echo_red 'Error[bake_package_install] could not parse url $url'" EXIT
test -n "$git_host_and_user"
test -n "$git_project_name"
trap EXIT

local git_url
git_url="$(echo "$url" | awk -F/ '{print $1 "/" $2 "/" $3}')"
git_url=${git_host_and_user}/${git_project_name}
git_host_and_user=${git_host_and_user##git@}
git_project_name=${git_project_name%%.git}
test -d "$git_host_and_user" || mkdir -p "$git_host_and_user"
cd "$git_host_and_user"

if [ ! -e "$git_project_name" ]; then
git clone "https://$git_url"
git clone "$schema://$git_url"
fi

# Checkout the project and branch/tag
cd "$git_project_name"
git checkout .
git pull
Expand All @@ -217,6 +232,7 @@ function bake_require_from_url () {

if [ -e "$fspath" ]; then
# shellcheck disable=SC2064
echo $fspath exists
trap "bake_echo_red 'Error[bake_require_from_url] loading require: $fspath'" EXIT
source "$fspath"
trap EXIT # clear the trap
Expand All @@ -242,6 +258,9 @@ function bake_require () {
local module="$1"
if bake_looks_like_url "$module"; then
bake_require_from_url "$module"
elif bake_looks_like_git "$module"; then
module="$(bake_git_to_url "$module")"
bake_require_from_url "$module"
else
bake_require_from_fs "$module"
fi
Expand Down

0 comments on commit ab6bd15

Please sign in to comment.