dotfiles/zsh/gclone.zsh

81 lines
2.3 KiB
Bash
Raw Normal View History

2018-09-28 14:04:34 +02:00
# 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