From d049c27189cc485d6e9b04863f2db1fa21200911 Mon Sep 17 00:00:00 2001 From: Mitch Tishmack Date: Wed, 25 Feb 2015 12:22:53 -0600 Subject: [PATCH] Update shell functions for git directory nonsense. --- git/.profile.d/20-functions-git.sh | 80 ++++++++++++------------------ 1 file changed, 31 insertions(+), 49 deletions(-) diff --git a/git/.profile.d/20-functions-git.sh b/git/.profile.d/20-functions-git.sh index 8c410ff..272486f 100644 --- a/git/.profile.d/20-functions-git.sh +++ b/git/.profile.d/20-functions-git.sh @@ -1,60 +1,42 @@ # git/.profile.d/20-functions-git.sh - -_git_repo_add() +maybe_git_repo() { - proto=${1} - where=${2} - location=${3} - repo=$(echo ${HOME}/Developer/${where}/${location} | sed -e 's/\.git$//g') - [[ ! -d ${repo} ]] && mkdir -p ${repo} - git clone ${proto}://${where}/${location} ${repo} -} - -_git_https_repo_add() -{ - _git_repo_add https $* -} - -_git_http_repo_add() -{ - _git_repo_add http $* -} - -_git_git_repo_add() -{ - _git_repo_add git $* + # assume https if input doesn't contain a protocol + proto=https + destination=${HOME}/src + echo "${1}" | grep '://' > /dev/null 2>&1 + [ $? = 0 ] && proto=$(echo "${1}" | sed -e 's|[:]\/\/.*||g') + git_dir=$(echo "${1}" | sed -e 's|.*[:]\/\/||g') + repo=$(echo "${destination}/${git_dir}" | sed -e 's/\.git$//g') + + if [ ! -d "${repo}" ]; then + mkdir -p "${repo}" + echo "git clone ${proto}://${git_dir} ${repo}" + git clone "${proto}://${git_dir}" "${repo}" + ${cmd} + if [ $? != 0 ]; then + # Try removing up to git_dir at worst empty directories. + # Cheap trick, but oldies are goodies. + ( + repo_dir="${repo}" + until [ "${repo_dir}" = "${destination}" ]; do + cd "${repo_dir}" > /dev/null 2>&1 + rmdir "${repo_dir}" > /dev/null 2>&1 + repo_dir=$(echo "${repo_dir}" | sed -e 's/\/[^\/]*$//g') + done + ) + echo "git clone of ${proto}://${repo} failed" + fi + fi + [ -d "${repo}" ] && cd "${repo}" } gh() { - _cd_git_repo https github.com ${1} + maybe_git_repo "https://github.com/${1}" } bb() { - _cd_git_repo https bitbucket.org ${1} -} - -_cd_git_repo() -{ - proto=${1} - where=${2} - what=${3} - repo=$(echo "Developer/${where}/${what}"| sed -e 's/\.git$//g') - full_repo=${HOME}/${repo} - [[ ! -d ${full_repo} ]] && _git_repo_add $* - - if [[ $? != 0 ]]; then - # Try removing up to ~/Developer at worst empty directories. - # Cheap trick, but oldies are goodies. - ( - until [[ ${full_repo} == "${HOME}/Developer" ]]; do - cd ${full_repo} > /dev/null 2>&1 - rmdir ${full_repo} > /dev/null 2>&1 - full_repo=$(echo ${full_repo} | sed -e 's/\/[^\/]*$//g') - done - ) - fi - - [[ -d ${full_repo} ]] && cd ${full_repo} + maybe_git_repo "https://bitbucket.org/${1}" }