-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-submodule
executable file
·48 lines (38 loc) · 1.15 KB
/
add-submodule
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
#!/bin/bash
# check whether we got the expected number of arguments
if [ "$#" == 0 ] || [ "$#" -gt 2 ]; then
cat << EOM
Add a git submodule from a GitHub HTTPS URL to the current repo.
The URL is converted to use SSH instead of HTTPS.
The submodule is checked out in its own subdirectory.
Tabs are replaced with spaces in the relevant .gitmodules.
Usage: $(basename "$0") GITHUB_URL [SUBMODULE_PATH]
EOM
exit 1
fi
# check whether we're currently in a git directory
if ! GIT_DIR=$(git rev-parse --show-toplevel 2> /dev/null); then
echo "Must be in git dir to add submodule!"
exit 1
fi
# check whether we got a https github URL
if ! [[ $1 =~ ^https://github.com/ ]]; then
echo "Must use a HTTPS URL from GitHub"
exit 1
fi
URL="[email protected]:${1:19}"
REPO_NAME=${URL##*/}
if [ -z "$2" ]; then
PATH_ARG="."
else
PATH_ARG="${2%/}"
fi
MODULE_PATH="$PATH_ARG/$REPO_NAME"
COMMAND="git submodule add $URL $MODULE_PATH"
GIT_MODULES="$GIT_DIR/.gitmodules"
echo "Executing \`$COMMAND\`..."
eval "$COMMAND"
echo "Replacing tabs with spaces in $GIT_MODULES..."
sed -i 's/\t/ /g' "$GIT_MODULES"
echo "Adding $GIT_MODULES to index..."
git add "$GIT_MODULES"