Better gclone

This commit is contained in:
Thomas Forgione 2018-09-28 14:04:34 +02:00
parent 0975168f35
commit 2429265ab1
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
2 changed files with 81 additions and 68 deletions

View File

@ -90,74 +90,7 @@ makelatex() {
} }
source $HOME/.config/dotfiles/zsh/update.zsh source $HOME/.config/dotfiles/zsh/update.zsh
source $HOME/.config/dotfiles/zsh/gclone.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
# Music things # Music things
command -v music-server > /dev/null 2>&1 command -v music-server > /dev/null 2>&1

80
zsh/gclone.zsh Normal file
View File

@ -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