-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgit-submodule-url-rewrite
More file actions
executable file
·151 lines (118 loc) · 3.36 KB
/
git-submodule-url-rewrite
File metadata and controls
executable file
·151 lines (118 loc) · 3.36 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env bash
set -euo pipefail
export SHELL="${SHELL?}"
export GIT_SUBMODULE_CMD="git submodule --quiet"
export UPDATE_CMD="${GIT_SUBMODULE_CMD} update --init"
usage() {
cat <<EOF
usage: git submodule-url-rewrite [-h|--help] [-v|--verbose] [-q|--quiet] [-r|--recursive] [-s|--no-stage] [-u|--no-update] sed-command
Rewrites all submodule urls using the given sed-script
options:
-h|--help Print this help
-v|--verbose Make this script verbose
-q|--quiet Don't print anything
-r|--recursive Also rewrite submodules of submodules
-s|--no-stage Don't stage changed .gitmodule files for commit
-u|--no-update Don't run '${UPDATE_CMD}' in each submodule
sed-command: A sed command used to transform urls.
EOF
}
export VERBOSE=false
export QUIET=false
export RECURSIVE=false
export NO_STAGE=false
export NO_UPDATE=false
export SED_COMMAND=""
while true; do
case "${1:-""}" in
-h|--help)
usage && exit 0
;;
"")
usage
exit 1
;;
-v|--verbose)
VERBOSE=true && shift
GIT_SUBMODULE_CMD="git submodule"
UPDATE_CMD="${GIT_SUBMODULE_CMD} update --init"
;;
-q|--quiet)
QUIET=true && shift
;;
-r|--recursive)
RECURSIVE=true && shift
;;
-s|--no-stage)
NO_STAGE=true && shift
;;
-u|--no-update)
NO_UPDATE=true && shift
;;
*)
SED_COMMAND="${1}" && shift
break
;;
esac
done
sm_url_rewrite(){
local name="${1}" && shift
local toplevel="$(git rev-parse --show-toplevel)"
local old_url="$(git config --file="${gitmodules}" "submodule.${name}.url")"
local new_url="$(echo "${old_url}" | sed -r -e "${SED_COMMAND}")"
if [ "${old_url}" == "${new_url}" ]; then
return
fi
if [ "${QUIET}" = false ]; then
echo "rewrite url for submodule '${name}' in '${toplevel}' from '${old_url}' to '${new_url}'"
fi
git config --file="${gitmodules}" "submodule.${name}.url" "${new_url}"
}
export -f sm_url_rewrite
url_rewrite(){
set -euo pipefail
if [ "${VERBOSE}" = true ]; then set -x; fi
local toplevel="$(git rev-parse --show-toplevel)"
local gitmodules="${toplevel}/.gitmodules"
(
cd "${toplevel}"
if [ ! -r "${gitmodules}" ]; then
return
fi
# loop for each submodule
#
# we can not use submodule foreach here
# because that requires a checkout of the
# submodule, which we don't what to do at
# this point in time since we are just about
# to rewrite the checkout url...
# all this sed / tr / xargs / printf magic
# is only here to make this script support
# submodules with newlines and spaces in
# the name... (ugly init?)
# On top of that, we have to read -rd ''
# in order to support \0 entry seperators.
# Which only really works in bash...
sed -nre \
's/^\[submodule \"(.*)\"]$/\1\x0/p' \
"${gitmodules}" \
| tr -d '\n' \
| xargs -0 -n1 printf "%b\0" \
| while IFS= read -rd '' submodule; do
sm_url_rewrite "${submodule}"
done
${GIT_SUBMODULE_CMD} sync
if [ "${NO_STAGE}" = false ]; then
git -C "${toplevel}" add "${gitmodules}"
fi
# after rewrite we can update the submodules
if [ "${RECURSIVE}" = true ]; then
if [ "${NO_UPDATE}" = false ]; then
${UPDATE_CMD}
fi
${GIT_SUBMODULE_CMD} foreach ${SHELL} -c url_rewrite
fi
)
}
export -f url_rewrite
url_rewrite