-
Notifications
You must be signed in to change notification settings - Fork 0
/
clone-into-dir
executable file
·41 lines (36 loc) · 1.49 KB
/
clone-into-dir
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
#!/bin/bash
set -euo pipefail
# if you don't like ~/develpment simply:
# export CLONEINTO_ROOT=/path/to/your/preference
rootDir=${CLONEINTO_ROOT:-~/development}
cloneURL="${1:?missing git clone url}"
# make sure the rootDir exists, and go there
mkdir -p "${rootDir}"
cd "${rootDir}"
# anything that looks like a remote git repo
pattern='^.+:(.*)/(.*).git$'
if [[ "$cloneURL" =~ $pattern ]]; then
if [ -d "${rootDir}/${BASH_REMATCH[1]}/${BASH_REMATCH[2]}" ]; then
echo "# project already exists: ${rootDir}/${BASH_REMATCH[1]}/${BASH_REMATCH[2]}"
else
echo "# cloning ${cloneURL} into ${rootDir}/${BASH_REMATCH[1]}…"
mkdir -p "${BASH_REMATCH[1]}"
git -C "${BASH_REMATCH[1]}" clone "${cloneURL}"
echo "# repository cloned to: ${rootDir}/${BASH_REMATCH[1]}/${BASH_REMATCH[2]}"
fi
else
echo "# unsupported git uri: $cloneURL"
fi
# if we find VSCode INsiders, open the new repo in is
if type code-insiders 2>/dev/null; then
if [ -d "${rootDir}/${BASH_REMATCH[1]}/${BASH_REMATCH[2]}" ]; then
echo "# opening ${rootDir}/${BASH_REMATCH[1]}/${BASH_REMATCH[2]} in VSCode…"
code-insiders "${rootDir}/${BASH_REMATCH[1]}/${BASH_REMATCH[2]}"
fi
# if we find VSCode, open the new repo
elif type code 2>/dev/null; then
if [ -d "${rootDir}/${BASH_REMATCH[1]}/${BASH_REMATCH[2]}" ]; then
echo "# opening ${rootDir}/${BASH_REMATCH[1]}/${BASH_REMATCH[2]} in VSCode…"
code "${rootDir}/${BASH_REMATCH[1]}/${BASH_REMATCH[2]}"
fi
fi