33set -eu -o pipefail
44
55function print_help {
6- echo " Usage: git get [--print-path] <repository> [<args>]"
6+ echo " Usage: git get [--print-path] [--location <dir>] <repository> [<args>]"
77 echo
88 echo " git-get clones <repository> into a folder derived from the repository URL"
99 echo
@@ -16,32 +16,33 @@ function print_help {
1616 echo " --print-path will print out the full path that the repository would be cloned into"
1717 echo " and then exits immediately without cloning"
1818 echo
19+ echo " --location <dir> override the base directory for this clone only"
20+ echo
1921 echo " -h, --help show this help message and exit"
2022 echo
2123 echo " Any other arguments are passed to the \" git clone\" command"
2224}
2325
24- # Set base directory for git checkout
25- base_dir=" $( git config get.location || true) "
26-
27- if [ -z " $base_dir " ]; then
28- base_dir=" $HOME /code"
29- else
30- # Replace a tilde with $HOME
31- base_dir=" ${base_dir/# ~/ $HOME } "
32- fi
33-
3426# Parse CLI opts
3527COMMAND=" checkout"
3628REPO=" "
3729EXTRA_ARGS=()
30+ OVERRIDE_LOCATION=" "
3831
3932while [[ $# -gt 0 ]]; do
4033 case $1 in
4134 --print-path)
4235 COMMAND=" print"
4336 shift
4437 ;;
38+ --location)
39+ if [ $# -lt 2 ] || [ -z " ${2:- } " ]; then
40+ echo " Error: --location requires a directory path"
41+ exit 1
42+ fi
43+ OVERRIDE_LOCATION=" $2 "
44+ shift 2
45+ ;;
4546 -h|--help)
4647 print_help
4748 exit
@@ -68,6 +69,22 @@ if [ -z "$REPO" ] ; then
6869 exit 1
6970fi
7071
72+ # Set base directory for git checkout
73+ if [ -n " $OVERRIDE_LOCATION " ]; then
74+ # Use override location, expanding tilde if present
75+ base_dir=" ${OVERRIDE_LOCATION/# ~/ $HOME } "
76+ else
77+ # Use configured or default location
78+ base_dir=" $( git config get.location || true) "
79+
80+ if [ -z " $base_dir " ]; then
81+ base_dir=" $HOME /code"
82+ else
83+ # Replace a tilde with $HOME
84+ base_dir=" ${base_dir/# ~/ $HOME } "
85+ fi
86+ fi
87+
7188# Strip scheme
7289dir=" ${REPO#*:// } "
7390
@@ -80,6 +97,9 @@ dir="${dir/://}"
8097# Remove .git
8198dir=" ${dir% .git} "
8299
100+ # Remove trailing slash from base_dir if present
101+ base_dir=" ${base_dir%/ } "
102+
83103dir=" ${base_dir} /${dir} "
84104
85105if [ " $COMMAND " == " print" ]; then
0 commit comments