-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit-get
More file actions
executable file
·115 lines (97 loc) · 2.62 KB
/
git-get
File metadata and controls
executable file
·115 lines (97 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash
set -eu -o pipefail
function print_help {
echo "Usage: git get [--print-path] [--location <dir>] <repository> [<args>]"
echo
echo " git-get clones <repository> into a folder derived from the repository URL"
echo
echo " For example, git get git@github.com:stilvoid/git-get.git"
echo " will be cloned into ~/code/github.com/stilvoid/git-get"
echo
echo " You can override the default base path (~/code) with"
echo " git config --global get.location \"/path/to/your/code\""
echo
echo " --print-path will print out the full path that the repository would be cloned into"
echo " and then exits immediately without cloning"
echo
echo " --location <dir> override the base directory for this clone only"
echo
echo " -h, --help show this help message and exit"
echo
echo " Any other arguments are passed to the \"git clone\" command"
}
# Parse CLI opts
COMMAND="checkout"
REPO=""
EXTRA_ARGS=()
OVERRIDE_LOCATION=""
while [[ $# -gt 0 ]]; do
case $1 in
--print-path)
COMMAND="print"
shift
;;
--location)
if [ $# -lt 2 ] || [ -z "${2:-}" ]; then
echo "Error: --location requires a directory path"
exit 1
fi
OVERRIDE_LOCATION="$2"
shift 2
;;
-h|--help)
print_help
exit
;;
-*)
EXTRA_ARGS+=("$1")
shift
;;
*)
if [ -z "$REPO" ]; then
REPO="$1"
else
EXTRA_ARGS+=("$1")
fi
shift
;;
esac
done
GIT_ARGS="${EXTRA_ARGS[*]+"${EXTRA_ARGS[*]}"}"
if [ -z "$REPO" ] ; then
print_help
exit 1
fi
# Set base directory for git checkout
if [ -n "$OVERRIDE_LOCATION" ]; then
# Use override location, expanding tilde if present
base_dir="${OVERRIDE_LOCATION/#~/$HOME}"
else
# Use configured or default location
base_dir="$(git config get.location || true)"
if [ -z "$base_dir" ]; then
base_dir="$HOME/code"
else
# Replace a tilde with $HOME
base_dir="${base_dir/#~/$HOME}"
fi
fi
# Strip scheme
dir="${REPO#*://}"
# Strip username
dir="${dir#*@}"
# Replace : with /
dir="${dir/://}"
# Remove .git
dir="${dir%.git}"
# Remove trailing slash from base_dir if present
base_dir="${base_dir%/}"
dir="${base_dir}/${dir}"
if [ "$COMMAND" == "print" ]; then
echo "$dir"
exit
fi
echo "Cloning into '$dir'..."
CMD="git clone $GIT_ARGS -- \"$REPO\" \"$dir\""
eval "$CMD"
echo "Successfully cloned to: $dir"