diff --git a/zsh/gclone.zsh b/zsh/gclone.zsh index 58b98a9..d77b7b5 100644 --- a/zsh/gclone.zsh +++ b/zsh/gclone.zsh @@ -25,11 +25,24 @@ if [ -d "$GCLONE_PATH" ]; then if [ $? -eq 0 ]; then # Final path to the repo - repo_path=$GCLONE_PATH/$server/$user/ + repo_path=$GCLONE_PATH/$server/$user mkdir -p $repo_path mv $tempdir/$repo $repo_path + 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 + 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 + fi # Remove the temp dir @@ -41,7 +54,8 @@ if [ -d "$GCLONE_PATH" ]; then echo "This function expects a single parameter" return 1 else - found_dirs=`find $GCLONE_PATH -maxdepth 3 -name $1 | sort` + + found_dirs=`_cdg_existing_dirs | grep /$1$` total=`echo $found_dirs | wc -l` if [[ "" == "$found_dirs" ]]; then @@ -65,16 +79,33 @@ if [ -d "$GCLONE_PATH" ]; then fi return 3 else - cd $found_dirs + cd $found_dirs > /dev/null 2>&1 + if [ $? -ne 0 ]; then + echo "cd didn't work, refreshing cache..." + _cdg_refresh_cache + fi fi fi } _cdg() { - _arguments "1: :($(find $GCLONE_PATH -maxdepth 3 -exec basename {} \;))" + _arguments "1: :($(_cdg_existing_dirs | rev | cut -d '/' -f 1 | rev))" } compdef _cdg cdg + _cdg_refresh_cache() { + find $GCLONE_PATH -maxdepth 3 \ + -not -name ".cdgcache" \ + -not -name "git" | sort > $GCLONE_PATH/.cdgcache + } + + _cdg_existing_dirs() { + if [ ! -f $GCLONE_PATH/.cdgcache ]; then + _cdg_refresh_cache + fi + + cat $GCLONE_PATH/.cdgcache + } fi