From 2429265ab178f5b3e17f1bd75de2ff5e68bbdb37 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Fri, 28 Sep 2018 14:04:34 +0200 Subject: [PATCH] Better gclone --- zsh/functions.zsh | 69 +--------------------------------------- zsh/gclone.zsh | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 68 deletions(-) create mode 100644 zsh/gclone.zsh diff --git a/zsh/functions.zsh b/zsh/functions.zsh index a6a646d..2ff3ad3 100644 --- a/zsh/functions.zsh +++ b/zsh/functions.zsh @@ -90,74 +90,7 @@ makelatex() { } source $HOME/.config/dotfiles/zsh/update.zsh - -# cdg : cd to a git repo -if [ -d "$GCLONE_PATH" ]; then - - gclone() { - # Check if cloning via HTTP(S) or SSH - if [[ $1 == http* ]]; then - server=`echo $1 | rev | cut -d '/' -f 3- | rev | cut -d '/' -f 3` - name=`echo $1 | rev | cut -d '/' -f -2 | rev` - else - server=`echo $1 | cut -d ':' -f 1 | cut -d '@' -f 2-` - name=`echo $1 | cut -d ':' -f 2-` - fi - - user=`echo $name | cut -d '/' -f 1` - repo=`echo $name | cut -d '/' -f 2-` - - if [[ $repo == *.git ]]; then - repo=${repo%.*} - fi - - # Final path to the repo - repo_path=$GCLONE_PATH/$server/$user/$repo - mkdir -p $repo_path - git clone $1 $repo_path - } - - cdg() { - if [ $# -ne 1 ]; then - echo "This function expects a single parameter" - return 1 - else - found_dirs=`find $GCLONE_PATH -maxdepth 3 -name $1 | sort` - total=`echo $found_dirs | wc -l` - - if [[ "" == "$found_dirs" ]]; then - echo "$1 not found" - return 2 - elif [ $total -gt 1 ]; then - counter=0 - echo "Multiple entries for $1 found:" - echo $found_dirs | while read found_dir; do - counter=$(($counter+1)) - echo "["$counter"]" $found_dir - done - read choice - if [[ "$choice" == "" ]]; then - cd `echo $found_dirs | head -1 | tail -1` - elif [[ "$choice" =~ '^[0-9]+$' ]] && [ $choice -le $total ] && [ $choice -gt 0 ]; then - cd `echo $found_dirs | head -$choice | tail -1` - else - echo "I expected a number that was less than the max..." - return 3 - fi - return 3 - else - cd $found_dirs - fi - fi - } - - _cdg() { - _arguments "1: :($(find $GCLONE_PATH -maxdepth 3 -exec basename {} \;))" - } - - compdef _cdg cdg - -fi +source $HOME/.config/dotfiles/zsh/gclone.zsh # Music things command -v music-server > /dev/null 2>&1 diff --git a/zsh/gclone.zsh b/zsh/gclone.zsh new file mode 100644 index 0000000..affd4b5 --- /dev/null +++ b/zsh/gclone.zsh @@ -0,0 +1,80 @@ +# cdg : cd to a git repo +if [ -d "$GCLONE_PATH" ]; then + + gclone() { + # Check if cloning via HTTP(S) or SSH + if [[ $1 == http* ]]; then + server=`echo $1 | rev | cut -d '/' -f 3- | rev | cut -d '/' -f 3-` + name=`echo $1 | rev | cut -d '/' -f -2 | rev` + else + server=`echo $1 | cut -d ':' -f 1 | cut -d '@' -f 2-` + name=`echo $1 | cut -d ':' -f 2-` + fi + + user=`echo $name | cut -d '/' -f 1` + repo=`echo $name | cut -d '/' -f 2-` + + if [[ $repo == *.git ]]; then + repo=${repo%.*} + fi + + tempdir=`mktemp -d` + git clone $1 $tempdir/repo + + # If the clone worked, move the repo in the good place + if [ $? -eq 0 ]; then + + # Final path to the repo + repo_path=$GCLONE_PATH/$server/$user/$repo + mkdir -p $repo_path + + mv $tempdir/repo $repo_path + + fi + + # Remove the temp dir + rm -rf $tempdir + } + + cdg() { + if [ $# -ne 1 ]; then + echo "This function expects a single parameter" + return 1 + else + found_dirs=`find $GCLONE_PATH -maxdepth 3 -name $1 | sort` + total=`echo $found_dirs | wc -l` + + if [[ "" == "$found_dirs" ]]; then + echo "$1 not found" + return 2 + elif [ $total -gt 1 ]; then + counter=0 + echo "Multiple entries for $1 found:" + echo $found_dirs | while read found_dir; do + counter=$(($counter+1)) + echo "["$counter"]" $found_dir + done + read choice + if [[ "$choice" == "" ]]; then + cd `echo $found_dirs | head -1 | tail -1` + elif [[ "$choice" =~ '^[0-9]+$' ]] && [ $choice -le $total ] && [ $choice -gt 0 ]; then + cd `echo $found_dirs | head -$choice | tail -1` + else + echo "I expected a number that was less than the max..." + return 3 + fi + return 3 + else + cd $found_dirs + fi + fi + } + + _cdg() { + _arguments "1: :($(find $GCLONE_PATH -maxdepth 3 -exec basename {} \;))" + } + + compdef _cdg cdg + +fi +