11#! /bin/bash
22
3- base_dir=" $( git config get.location) "
3+ set -eu -o pipefail
4+
5+ function print_help {
6+ echo " Usage: git get [--print-path] <repository> [<args>]"
7+ echo
8+ echo " git-get clones <repository> into a folder derived from the repository URL"
9+ echo
10+ echo " For example, git get [email protected] :stilvoid/git-get.git" 11+ echo " will be checkout into ~/code/github.com/stilvoid/git-get.git"
12+ echo
13+ echo " You can override the default base path (~/code) with"
14+ echo " git config --global get.loation \" /path/to/your/code\" "
15+ echo
16+ echo " --print-path will print out the full path that the repository would be clone into"
17+ echo " and then exits immediately without cloning"
18+ echo
19+ echo " Any other arguments are passed to the \" git clone\" command"
20+ }
21+
22+ # Set base directory for git checkout
23+ base_dir=" $( git config get.location || true) "
424
525if [ -z " $base_dir " ]; then
626 base_dir=" $HOME /code"
929 base_dir=" ${base_dir/# ~/ $HOME } "
1030fi
1131
32+ # Parse CLI opts
33+ COMMAND=" checkout"
34+ REPO=" "
35+ EXTRA_ARGS=()
1236
13- repo=" $1 "
37+ while [[ $# -gt 0 ]]; do
38+ case $1 in
39+ --print-path)
40+ COMMAND=" print"
41+ shift
42+ ;;
43+ -h|--help)
44+ print_help
45+ exit
46+ ;;
47+ -* )
48+ EXTRA_ARGS+=(" $1 " )
49+ shift
50+ ;;
51+ * )
52+ if [ -z " $REPO " ]; then
53+ REPO=" $1 "
54+ else
55+ EXTRA_ARGS+=(" $1 " )
56+ fi
57+ shift
58+ ;;
59+ esac
60+ done
1461
15- if [ -z " $repo " ] || [ " $repo " == " -h" ]; then
16- echo " Usage: git get <repository>"
17- echo
18- echo " git-get clones <repository> into a folder derived from the repository URL"
19- echo
20- echo " For example, git get [email protected] :stilvoid/git-get.git" 21- echo " will be checkout into ~/code/github.com/stilvoid/git-get.git"
22- echo
23- echo " You can override the default base path (~/code) with"
24- echo " git config --global get.loation \" /path/to/your/code\" "
62+ GIT_ARGS=" ${EXTRA_ARGS[*]+" ${EXTRA_ARGS[*]} " } "
2563
26- if [ " $repo " == " -h" ]; then
27- exit
28- else
29- exit 1
30- fi
64+ if [ -z " $REPO " ] ; then
65+ print_help
66+ exit 1
3167fi
3268
3369# Strip scheme
34- dir=" ${repo #*:// } "
70+ dir=" ${REPO #*:// } "
3571
3672# Strip username
3773dir=" ${dir#*@ } "
@@ -44,4 +80,11 @@ dir="${dir%.git}"
4480
4581dir=" ${base_dir} /${dir} "
4682
47- git clone " $repo " " $dir "
83+ if [ " $COMMAND " == " print" ]; then
84+ echo " $dir "
85+ exit
86+ fi
87+
88+ CMD=" git clone $GIT_ARGS -- \" $REPO \" \" $dir \" "
89+
90+ eval " $CMD "
0 commit comments