git.fiddlerwoaroof.com
zsh/zsh_plugins/dependencies/git-get.zsh
13cc20cf
 GIT_3DP_DIR="${GIT_3DP_DIR:-"$HOME/git_repos/3dp"}"
0b3a2c90
 GITHUB_USE_SSH=${GIT_USE_SSH:-${GITHUB_USE_SSH:-yes}}
 GITLAB_USE_SSH=${GIT_USE_SSH:-${GITLAB_USE_SSH:-yes}}
13cc20cf
 typeset -g -A FORGE_ALIASES
 
0b3a2c90
 function initialize-git-get() {
   set -x
   mkdir -p "$GIT_3DP_DIR"
   set +x
 }
 
 local alias_usage="alias_forge <alias> <forge>"
13cc20cf
 function alias_forge() {
   local alias=${1:?the first parameter to alias_forge should be the alias you want to set}
0b3a2c90
   local forge=${2:?the second parameter to alias_forge should be the forge the alias points to}
13cc20cf
 
   if (( $# > 2 )); then
0b3a2c90
     echo "$alias_usage"
13cc20cf
   else
     FORGE_ALIASES[$alias]=$forge
   fi
 }
 
 function github_url() {
   local git_spec="$package"
 
   if [[ -n "$git_user" ]]; then
     git_spec="$git_user/$package"
   fi
 
   if [[ "$GITHUB_USE_SSH" == "yes" ]]; then
     printf "git@github.com:%s.git" "$git_spec"
   else
     printf "https://github.com/%s.git" "$git_spec"
   fi
 }
 
4b1e8cd3
 function bitbucket_url() {
   local git_spec="$package"
 
   if [[ -n "$git_user" ]]; then
     git_spec="$git_user/$package"
   fi
 
   if [[ "$bitbucket_USE_SSH" == "yes" ]]; then
     printf "git@bitbucket.com:%s.git" "$git_spec"
   else
     printf "https://bitbucket.com/%s.git" "$git_spec"
   fi
 }
 
13cc20cf
 function gitlab_url() {
   local git_spec="$package"
 
   if [[ -n "$git_user" ]]; then
     git_spec="$git_user/$package"
   fi
 
   if [[ "$GITLAB_USE_SSH" == "yes" ]]; then
     printf "git@gitlab.com:%s.git" "$git_spec"
   else
     printf "https://gitlab.com/%s.git" "$git_spec"
   fi
 }
 
 function git-forge-clone() {
   if [[ ! -e "$(basename "$package")" ]]; then
     git clone "$($1)"
   else
     echo "package already cloned"
   fi
 }
 
 function get_forge_function() {
   local forge="$1"
   local aliased_forge="${forge_aliases[$1]}"
   if [[ ! -z "$aliased_forge" ]]; then
     forge="$aliased_forge"
   fi
   echo "${forge}_url"
 }
 
fe429cbf
 function get_forge_root() {
   local forge="$1"
f147c85a
   # echo "$@" >&2
   shift
   if command -v "${forge}_root" &>>/dev/null; then
     "${forge}_root" "$@"
fe429cbf
   else
     echo "$GIT_3DP_DIR"/
   fi
 }
 
4b1e8cd3
 alias_forge bb github
0b3a2c90
 alias_forge gh github
 alias_forge gl gitlab
 
f147c85a
 github_root() {
   local proj_dir="$HOME"/git_repos/github/"$1"/
   if ! [[ -d "$proj_dir" ]]; then
     mkdir -p "$proj_dir"
   fi
 
   echo "$proj_dir"
 }
 
13cc20cf
 function git-get() {
   local git_user
   local package
f147c85a
 
13cc20cf
   forge=${1?Need a forge spec}
 
f147c85a
   shift
13cc20cf
 
   if [[ $# == 1 ]]; then
f147c85a
     if [[ "$1" == */* ]]; then
       git_user="${1%%/*}"
       package="${1#*/}"
     else
       git_user=
       package=$1
     fi
13cc20cf
   elif (( $# == 2 )); then
     git_user=$1
     package=$2
     shift
   else
fe429cbf
     echo 'usage: <forge> <user> <package>'
13cc20cf
     return 2
   fi
 
   package=${package%.git}
 
   shift
 
f147c85a
   # echo "git user? $git_user package? $package" >&2
   local target="$(get_forge_root "$forge" "$git_user")"
fe429cbf
   cd "$target"
13cc20cf
 
   local forge_url_function="${$(get_forge_function "$forge"):?forge not recognized}"
   git-forge-clone "$forge_url_function"
f147c85a
 
13cc20cf
   cd "$(basename "$package")"
 }