dotfiles/zsh/gclone.zsh

124 lines
3.6 KiB
Bash
Raw Normal View History

2018-09-28 13:04:34 +01: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
2018-10-08 10:18:15 +01:00
# Final path to the repo
repo_path=$GCLONE_PATH/$server/$user
2018-10-08 12:32:39 +01:00
if [ -d $repo_path/$repo ]; then
2018-10-08 10:18:15 +01:00
echo "Repository already exists"
return 1
fi
2018-09-28 13:04:34 +01:00
tempdir=`mktemp -d`
2018-09-29 20:46:42 +01:00
git clone $1 $tempdir/$repo
2018-09-28 13:04:34 +01:00
# If the clone worked, move the repo in the good place
if [ $? -eq 0 ]; then
2018-12-10 10:05:49 +00:00
mkdir -p $repo_path
mv $tempdir/$repo $repo_path/$repo
2018-09-28 13:04:34 +01:00
2018-10-07 10:07:05 +01:00
if [ ! -f $GCLONE_PATH/.cdgcache ]; then
# If there is no cache, generate the cache manually
_cdg_refresh_cache
else
# If there is a cache, just append the current dir at the end
# and sort it back
echo $repo_path >> $GCLONE_PATH/.cdgcache
echo $repo_path/$repo >> $GCLONE_PATH/.cdgcache
cat $GCLONE_PATH/.cdgcache | sort | uniq > $GCLONE_PATH/.cdgcache.tmp
mv $GCLONE_PATH/.cdgcache.tmp $GCLONE_PATH/.cdgcache
fi
2018-09-28 13:04:34 +01:00
fi
# Remove the temp dir
rm -rf $tempdir
}
cdg() {
if [ $# -ne 1 ]; then
echo "This function expects a single parameter"
return 1
else
2018-10-07 10:07:05 +01:00
found_dirs=`_cdg_existing_dirs | grep /$1$`
2018-09-28 13:04:34 +01:00
total=`echo $found_dirs | wc -l`
if [[ "" == "$found_dirs" ]]; then
2018-11-16 12:38:09 +00:00
echo "$1 not found, refreshing cache"
_cdg_refresh_cache
2018-09-28 13:04:34 +01:00
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
2018-10-07 10:07:05 +01:00
cd $found_dirs > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "cd didn't work, refreshing cache..."
_cdg_refresh_cache
2018-11-16 12:38:09 +00:00
return 4
2018-10-07 10:07:05 +01:00
fi
2018-09-28 13:04:34 +01:00
fi
fi
}
2018-12-28 14:32:51 +00:00
cdgclone() {
gclone $1 && cdg $1
}
2018-09-28 13:04:34 +01:00
_cdg() {
2018-10-07 10:07:05 +01:00
_arguments "1: :($(_cdg_existing_dirs | rev | cut -d '/' -f 1 | rev))"
2018-09-28 13:04:34 +01:00
}
compdef _cdg cdg
2018-10-07 10:07:05 +01:00
_cdg_refresh_cache() {
tmpfile=`mktemp`
2018-10-07 10:07:05 +01:00
find $GCLONE_PATH -maxdepth 3 \
-not -name ".cdgcache" \
-not -name "git" | sort > $tmpfile
mv $tmpfile $GCLONE_PATH/.cdgcache
2018-10-07 10:07:05 +01:00
}
_cdg_existing_dirs() {
if [ ! -f $GCLONE_PATH/.cdgcache ]; then
_cdg_refresh_cache
fi
cat $GCLONE_PATH/.cdgcache
}
2018-09-28 13:04:34 +01:00
fi