forked from kollerma/git-submodule-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-mv-submodule
executable file
·80 lines (70 loc) · 1.86 KB
/
git-mv-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
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
#!/bin/bash -e
## Move the submodule
## Usage: git mv-submodule [ --no-commit ] from to
## does not commit if --no-commit is given
## read input, display help if necessary
if [[ "$@" == "" || "$@" == *--help* ]]; then
cat<<EOF
Move a submodule
This command moves the submodule <from> to <to> and creates a new
commit with a simple commit message including the url of the
removed submodule. It just removes <from> and then adds <to>.
This means, that you will require an internet connection to download
the submodule again.
Usage:
git mv-submodule [--no-commit] <from> <to>
--no-commit: do not commit the result
EOF
exit 0;
fi
if [ "$1" == "--no-commit" ]; then
nocommit=1
from=$2
to=$3
else
from=$1
to=$2
fi
## from the git mailinglist:
function git
{
LC_MESSAGES=C command git "$@"
}
export git
## check if this is a valid submodule
if [[ ! "$from" || $(git ls-files --error-unmatch --stage -- "$from" | grep -E '^160000') == "" ]]; then
echo >&2 "Error: \"$from\" is not a submodule."
exit 1
fi
## check destination
if [[ ! "$to" ]]; then
cat >&2 <<EOF
Error: argument <to> missing.
Use git mv-submodule [--no-commit] <from> <to>
EOF
exit 1
fi
if [ -e "$to" ]; then
echo >&2 "Error: \"$to\" exists already. Remove it first."
exit 1
fi
tdir=$PWD
cd "$from"
## check if submodule is clean
git check-clean || exit 1
## check for unpushed changes
git check-unpushed || exit 1
## check for local non-tracking-branches
git check-non-tracking || exit 1
cd "$tdir"
## seems we're safe, so start moving
## get submodule url
url=`git config -f .gitmodules --get submodule."$from".url`
## remove it
git rm-submodule --no-commit "$from"
## add it again at the new location
git submodule add "$url" "$to"
## commit changes
if [[ ! "$nocommit" ]]; then
git commit -m "moved submodule \"$from\" to \"$to\" (url: $url)"
fi