Last active
August 29, 2015 14:23
-
-
Save nweldev/b0223ebaf02217cff4e9 to your computer and use it in GitHub Desktop.
cd faster to a local git repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# cd faster to a local git repository | |
# author : Noël Macé (noelmace.com) | |
# license : LGPL v3.0 | |
# syntaxe : gogit { folder_name | --option } | |
# options : | |
# --show : print all the absolute pathes to detected git repositories | |
# --basenames : print all repositories names | |
# --reload : redetect the folders | |
# this tool is based on locate | |
# updatedb after every change on local repositories location | |
# install : add this function to your bashrc file | |
# dependencies : locate, sed | |
# tip : add this line to your bashrc file | |
# source ~/.bashrc.d/* | |
# and put this file in ~/.bashrc.d/ | |
GOGIT_COMP_FOLDERS=() | |
GOGIT_COMP_BASENAMES=() | |
_gogit_reload_folders_ () { | |
GOGIT_COMP_FOLDERS=( $( locate -b .git | grep "/.git$" | sed -be s/"\.git$"//g ) ) | |
GOGIT_COMP_BASENAMES=() | |
for folder in ${GOGIT_COMP_FOLDERS[@]}; do | |
GOGIT_COMP_BASENAMES+=("$(basename $folder)") | |
done | |
} | |
_gogit_reload_folders_ | |
gogit () { | |
rslts_pos=() | |
rslts=() | |
if [[ "$1" == "-"* ]]; then | |
case $1 in | |
--show) | |
printf '%s\n' "${GOGIT_COMP_FOLDERS[@]}" | |
#echo ${GOGIT_COMP_FOLDERS[@]} | |
;; | |
--basenames) | |
printf '%s\n' "${GOGIT_COMP_BASENAMES[@]}" | |
#echo ${GOGIT_COMP_BASENAMES[@]} | |
;; | |
--reload) | |
_gogit_reload_folders_ | |
gogit --show | |
echo -e "if your repository is not detectd, launch updatedb and rerun gogit --reload" | |
;; | |
esac | |
else | |
local i=0 | |
for name in "${GOGIT_COMP_BASENAMES[@]}"; do | |
if [[ "$name" == *"$1"* ]]; then | |
rslts_pos+=($i) | |
fi | |
((i++)) | |
done | |
for id in "${rslts_pos[@]}"; do | |
rslts+=("${GOGIT_COMP_FOLDERS[$id]}") | |
done | |
if [ ${#rslts[@]} == 1 ]; then | |
cd "${rslts[0]}" | |
pwd | |
else | |
printf '%s\n' "${rslts[@]}" | |
fi | |
fi | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bash completion for the gogist function | |
# copy this file to your bash_completion folder | |
# after installing gogit function in bashrc | |
# see : https://gist.github.com/b0223ebaf02217cff4e9 | |
function _gogit_complete_() { | |
local cur=${COMP_WORDS[COMP_CWORD]} | |
COMPREPLY=( $(compgen -W "$( IFS=$' '; echo "${GOGIT_COMP_BASENAMES[*]}" )" -- "$cur") ) | |
return 0 | |
} | |
complete -F _gogit_complete_ gogit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment