My Scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bin/goto

68 lines
1.1 KiB

#!/usr/bin/env bash
# cd to git repos, and start a subshell in that directory.
# Written by skiqqy with love.
# Usage: goto PATTERN
usage()
{
cat << EOF
$(basename "$0") ~ Git Repository Finder.
Usage: goto [OPTIONS] PATTERN
OPTIONS
-h Shows This Message.
-f Always goto the first matched expr.
-s SHELL Specify the shell to use (defaults to bash).
EOF
exit "$1"
}
main()
{
all=true
shell=bash
while getopts hfs: opt
do
case "$opt" in
h)
usage 0
;;
f)
all=false
;;
s)
shell="$OPTARG"
! command -v "$shell" > /dev/null && exit 1
;;
*)
usage 1
;;
esac
done
shift "$((OPTIND-1))" # Get rid of parsed options
[ -z "$1" ] && usage 1
mapfile -t dirs < <(find . -type d 2> /dev/null | grep -E "^.*/$1/\.git$" | sed -E 's|(.*)/\.git|\1|g')
if "$all" && [ "${#dirs[@]}" -gt 1 ]
then
select dir in ${dirs[*]}
do
[ -n "$dir" ] && break
done
elif [ "${#dirs[@]}" -ge 1 ]
then
dir="${dirs[0]}"
else
printf -- 'Pattern did not match a repo.\n'
exit 1
fi
cd "$dir" || exit 1
$shell # Start the subshell
}
main "$@"