]>
Commit | Line | Data |
---|---|---|
70c7ac22 LH |
1 | #!/bin/sh |
2 | # | |
4c8a9db6 | 3 | # git-submodule.sh: add, init, update or list git submodules |
70c7ac22 LH |
4 | # |
5 | # Copyright (c) 2007 Lars Hjemli | |
6 | ||
1d5bec8b | 7 | dashless=$(basename "$0" | sed -e 's/-/ /') |
681b036f | 8 | USAGE="[--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--] <repository> [<path>] |
64b19ffe | 9 | or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...] |
1d5bec8b | 10 | or: $dashless [--quiet] init [--] [<path>...] |
06b1abb5 | 11 | or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...] |
adc54235 | 12 | or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...] |
15fc56a8 | 13 | or: $dashless [--quiet] foreach [--recursive] <command> |
82f49f29 | 14 | or: $dashless [--quiet] sync [--recursive] [--] [<path>...]" |
8f321a39 | 15 | OPTIONS_SPEC= |
70c7ac22 | 16 | . git-sh-setup |
d0ad8251 | 17 | . git-sh-i18n |
98fcf840 | 18 | . git-parse-remote |
70c7ac22 LH |
19 | require_work_tree |
20 | ||
5c08dbbd | 21 | command= |
ecda0723 | 22 | branch= |
d27b876b | 23 | force= |
d92a3959 | 24 | reference= |
70c7ac22 | 25 | cached= |
48bb3033 GP |
26 | recursive= |
27 | init= | |
1c244f6e | 28 | files= |
06b1abb5 | 29 | remote= |
31ca3ac3 | 30 | nofetch= |
32948425 | 31 | update= |
15fc56a8 | 32 | prefix= |
73b0898d | 33 | custom_name= |
70c7ac22 | 34 | |
967b2c66 JS |
35 | # The function takes at most 2 arguments. The first argument is the |
36 | # URL that navigates to the submodule origin repo. When relative, this URL | |
37 | # is relative to the superproject origin URL repo. The second up_path | |
38 | # argument, if specified, is the relative path that navigates | |
39 | # from the submodule working tree to the superproject working tree. | |
40 | # | |
41 | # The output of the function is the origin URL of the submodule. | |
42 | # | |
43 | # The output will either be an absolute URL or filesystem path (if the | |
44 | # superproject origin URL is an absolute URL or filesystem path, | |
45 | # respectively) or a relative file system path (if the superproject | |
46 | # origin URL is a relative file system path). | |
47 | # | |
48 | # When the output is a relative file system path, the path is either | |
49 | # relative to the submodule working tree, if up_path is specified, or to | |
50 | # the superproject working tree otherwise. | |
f31a522a ML |
51 | resolve_relative_url () |
52 | { | |
98fcf840 | 53 | remote=$(get_default_remote) |
8e7e6f39 | 54 | remoteurl=$(git config "remote.$remote.url") || |
4d689320 | 55 | remoteurl=$(pwd) # the repository is its own authoritative upstream |
f31a522a | 56 | url="$1" |
99b120af | 57 | remoteurl=${remoteurl%/} |
ea640cc6 | 58 | sep=/ |
967b2c66 JS |
59 | up_path="$2" |
60 | ||
61 | case "$remoteurl" in | |
62 | *:*|/*) | |
63 | is_relative= | |
64 | ;; | |
758615e2 JS |
65 | ./*|../*) |
66 | is_relative=t | |
67 | ;; | |
967b2c66 JS |
68 | *) |
69 | is_relative=t | |
758615e2 | 70 | remoteurl="./$remoteurl" |
967b2c66 JS |
71 | ;; |
72 | esac | |
73 | ||
f31a522a ML |
74 | while test -n "$url" |
75 | do | |
76 | case "$url" in | |
77 | ../*) | |
78 | url="${url#../}" | |
ea640cc6 TR |
79 | case "$remoteurl" in |
80 | */*) | |
81 | remoteurl="${remoteurl%/*}" | |
82 | ;; | |
83 | *:*) | |
84 | remoteurl="${remoteurl%:*}" | |
85 | sep=: | |
86 | ;; | |
87 | *) | |
758615e2 JS |
88 | if test -z "$is_relative" || test "." = "$remoteurl" |
89 | then | |
90 | die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")" | |
91 | else | |
92 | remoteurl=. | |
93 | fi | |
ea640cc6 TR |
94 | ;; |
95 | esac | |
f31a522a ML |
96 | ;; |
97 | ./*) | |
98 | url="${url#./}" | |
99 | ;; | |
100 | *) | |
101 | break;; | |
102 | esac | |
103 | done | |
758615e2 JS |
104 | remoteurl="$remoteurl$sep${url%/}" |
105 | echo "${is_relative:+${up_path}}${remoteurl#./}" | |
f31a522a ML |
106 | } |
107 | ||
a7b3269c DA |
108 | # |
109 | # Get submodule info for registered submodules | |
110 | # $@ = path to limit submodule list | |
111 | # | |
112 | module_list() | |
113 | { | |
be9d0a3a HV |
114 | ( |
115 | git ls-files --error-unmatch --stage -- "$@" || | |
116 | echo "unmatched pathspec exists" | |
117 | ) | | |
313ee0d6 NMC |
118 | perl -e ' |
119 | my %unmerged = (); | |
120 | my ($null_sha1) = ("0" x 40); | |
be9d0a3a HV |
121 | my @out = (); |
122 | my $unmatched = 0; | |
313ee0d6 | 123 | while (<STDIN>) { |
be9d0a3a HV |
124 | if (/^unmatched pathspec/) { |
125 | $unmatched = 1; | |
126 | next; | |
127 | } | |
313ee0d6 NMC |
128 | chomp; |
129 | my ($mode, $sha1, $stage, $path) = | |
130 | /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/; | |
131 | next unless $mode eq "160000"; | |
132 | if ($stage ne "0") { | |
133 | if (!$unmerged{$path}++) { | |
be9d0a3a | 134 | push @out, "$mode $null_sha1 U\t$path\n"; |
313ee0d6 NMC |
135 | } |
136 | next; | |
137 | } | |
be9d0a3a HV |
138 | push @out, "$_\n"; |
139 | } | |
140 | if ($unmatched) { | |
141 | print "#unmatched\n"; | |
142 | } else { | |
143 | print for (@out); | |
313ee0d6 NMC |
144 | } |
145 | ' | |
a7b3269c DA |
146 | } |
147 | ||
be9d0a3a HV |
148 | die_if_unmatched () |
149 | { | |
150 | if test "$1" = "#unmatched" | |
151 | then | |
152 | exit 1 | |
153 | fi | |
154 | } | |
155 | ||
88ce00c3 TK |
156 | # |
157 | # Print a submodule configuration setting | |
158 | # | |
159 | # $1 = submodule name | |
160 | # $2 = option name | |
161 | # $3 = default value | |
162 | # | |
163 | # Checks in the usual git-config places first (for overrides), | |
164 | # otherwise it falls back on .gitmodules. This allows you to | |
165 | # distribute project-wide defaults in .gitmodules, while still | |
166 | # customizing individual repositories if necessary. If the option is | |
167 | # not in .gitmodules either, print a default value. | |
168 | # | |
169 | get_submodule_config () { | |
170 | name="$1" | |
171 | option="$2" | |
172 | default="$3" | |
173 | value=$(git config submodule."$name"."$option") | |
174 | if test -z "$value" | |
175 | then | |
176 | value=$(git config -f .gitmodules submodule."$name"."$option") | |
177 | fi | |
178 | printf '%s' "${value:-$default}" | |
179 | } | |
180 | ||
181 | ||
941987a5 LH |
182 | # |
183 | # Map submodule path to submodule name | |
184 | # | |
185 | # $1 = path | |
186 | # | |
187 | module_name() | |
188 | { | |
537601ac | 189 | # Do we have "submodule.<something>.path = $1" defined in .gitmodules file? |
64394e3a | 190 | sm_path="$1" |
fe22e542 | 191 | re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g') |
a5099bb4 | 192 | name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' | |
537601ac | 193 | sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' ) |
1e42258a | 194 | test -z "$name" && |
64394e3a | 195 | die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")" |
1e42258a | 196 | echo "$name" |
941987a5 | 197 | } |
33aa6fff LH |
198 | |
199 | # | |
200 | # Clone a submodule | |
201 | # | |
23a485e3 | 202 | # Prior to calling, cmd_update checks that a possibly existing |
ecda0723 | 203 | # path is not a git repository. |
23a485e3 | 204 | # Likewise, cmd_add checks that path does not exist at all, |
ecda0723 SV |
205 | # since it is the location of a new submodule. |
206 | # | |
33aa6fff LH |
207 | module_clone() |
208 | { | |
64394e3a | 209 | sm_path=$1 |
73b0898d JL |
210 | name=$2 |
211 | url=$3 | |
212 | reference="$4" | |
7e60407f JL |
213 | quiet= |
214 | if test -n "$GIT_QUIET" | |
215 | then | |
216 | quiet=-q | |
217 | fi | |
33aa6fff | 218 | |
501770e1 FG |
219 | gitdir= |
220 | gitdir_base= | |
69c30517 | 221 | base_name=$(dirname "$name") |
33aa6fff | 222 | |
501770e1 | 223 | gitdir=$(git rev-parse --git-dir) |
69c30517 JL |
224 | gitdir_base="$gitdir/modules/$base_name" |
225 | gitdir="$gitdir/modules/$name" | |
501770e1 FG |
226 | |
227 | if test -d "$gitdir" | |
d92a3959 | 228 | then |
64394e3a | 229 | mkdir -p "$sm_path" |
501770e1 | 230 | rm -f "$gitdir/index" |
d92a3959 | 231 | else |
501770e1 | 232 | mkdir -p "$gitdir_base" |
be8779f7 DG |
233 | ( |
234 | clear_local_git_env | |
235 | git clone $quiet -n ${reference:+"$reference"} \ | |
236 | --separate-git-dir "$gitdir" "$url" "$sm_path" | |
237 | ) || | |
64394e3a | 238 | die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")" |
501770e1 | 239 | fi |
ea115a0d | 240 | |
6eafa6d0 JL |
241 | # We already are at the root of the work tree but cd_to_toplevel will |
242 | # resolve any symlinks that might be present in $PWD | |
243 | a=$(cd_to_toplevel && cd "$gitdir" && pwd)/ | |
15b3c82c | 244 | b=$(cd_to_toplevel && cd "$sm_path" && pwd)/ |
4dce7d9b JS |
245 | # normalize Windows-style absolute paths to POSIX-style absolute paths |
246 | case $a in [a-zA-Z]:/*) a=/${a%%:*}${a#*:} ;; esac | |
247 | case $b in [a-zA-Z]:/*) b=/${b%%:*}${b#*:} ;; esac | |
d75219b4 JL |
248 | # Remove all common leading directories after a sanity check |
249 | if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then | |
250 | die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")" | |
251 | fi | |
252 | while test "${a%%/*}" = "${b%%/*}" | |
253 | do | |
254 | a=${a#*/} | |
255 | b=${b#*/} | |
256 | done | |
257 | # Now chop off the trailing '/'s that were added in the beginning | |
258 | a=${a%/} | |
259 | b=${b%/} | |
260 | ||
c5bc42b9 BW |
261 | # Turn each leading "*/" component into "../" |
262 | rel=$(echo $b | sed -e 's|[^/][^/]*|..|g') | |
64394e3a | 263 | echo "gitdir: $rel/$a" >"$sm_path/.git" |
69c30517 | 264 | |
c5bc42b9 | 265 | rel=$(echo $a | sed -e 's|[^/][^/]*|..|g') |
64394e3a | 266 | (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b") |
33aa6fff LH |
267 | } |
268 | ||
ecda0723 SV |
269 | # |
270 | # Add a new submodule to the working tree, .gitmodules and the index | |
271 | # | |
ec05df35 | 272 | # $@ = repo path |
ecda0723 SV |
273 | # |
274 | # optional branch is stored in global branch variable | |
275 | # | |
23a485e3 | 276 | cmd_add() |
ecda0723 | 277 | { |
5c08dbbd JH |
278 | # parse $args after "submodule ... add". |
279 | while test $# -ne 0 | |
280 | do | |
281 | case "$1" in | |
282 | -b | --branch) | |
283 | case "$2" in '') usage ;; esac | |
284 | branch=$2 | |
285 | shift | |
286 | ;; | |
d27b876b JL |
287 | -f | --force) |
288 | force=$1 | |
289 | ;; | |
5c08dbbd | 290 | -q|--quiet) |
2e6a30ef | 291 | GIT_QUIET=1 |
5c08dbbd | 292 | ;; |
d92a3959 MT |
293 | --reference) |
294 | case "$2" in '') usage ;; esac | |
295 | reference="--reference=$2" | |
296 | shift | |
297 | ;; | |
298 | --reference=*) | |
299 | reference="$1" | |
d92a3959 | 300 | ;; |
73b0898d JL |
301 | --name) |
302 | case "$2" in '') usage ;; esac | |
303 | custom_name=$2 | |
304 | shift | |
305 | ;; | |
5c08dbbd JH |
306 | --) |
307 | shift | |
308 | break | |
309 | ;; | |
310 | -*) | |
311 | usage | |
312 | ;; | |
313 | *) | |
314 | break | |
315 | ;; | |
316 | esac | |
317 | shift | |
318 | done | |
319 | ||
ecda0723 | 320 | repo=$1 |
64394e3a | 321 | sm_path=$2 |
ecda0723 | 322 | |
64394e3a RJ |
323 | if test -z "$sm_path"; then |
324 | sm_path=$(echo "$repo" | | |
1414e578 JL |
325 | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g') |
326 | fi | |
327 | ||
64394e3a | 328 | if test -z "$repo" -o -z "$sm_path"; then |
ecda0723 SV |
329 | usage |
330 | fi | |
331 | ||
ec05df35 ML |
332 | # assure repo is absolute or relative to parent |
333 | case "$repo" in | |
334 | ./*|../*) | |
335 | # dereference source url relative to parent's url | |
336 | realrepo=$(resolve_relative_url "$repo") || exit | |
337 | ;; | |
338 | *:*|/*) | |
339 | # absolute url | |
340 | realrepo=$repo | |
341 | ;; | |
342 | *) | |
497ee872 | 343 | die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")" |
ec05df35 ML |
344 | ;; |
345 | esac | |
346 | ||
db75ada5 MG |
347 | # normalize path: |
348 | # multiple //; leading ./; /./; /../; trailing / | |
64394e3a | 349 | sm_path=$(printf '%s/\n' "$sm_path" | |
db75ada5 MG |
350 | sed -e ' |
351 | s|//*|/|g | |
352 | s|^\(\./\)*|| | |
353 | s|/\./|/|g | |
354 | :start | |
355 | s|\([^/]*\)/\.\./|| | |
356 | tstart | |
357 | s|/*$|| | |
358 | ') | |
64394e3a RJ |
359 | git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 && |
360 | die "$(eval_gettext "'\$sm_path' already exists in the index")" | |
ecda0723 | 361 | |
64394e3a | 362 | if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1 |
d27b876b | 363 | then |
6ff875c5 | 364 | eval_gettextln "The following path is ignored by one of your .gitignore files: |
64394e3a | 365 | \$sm_path |
6ff875c5 | 366 | Use -f if you really want to add it." >&2 |
d27b876b JL |
367 | exit 1 |
368 | fi | |
369 | ||
73b0898d JL |
370 | if test -n "$custom_name" |
371 | then | |
372 | sm_name="$custom_name" | |
373 | else | |
374 | sm_name="$sm_path" | |
375 | fi | |
376 | ||
d4264ca3 | 377 | # perhaps the path exists and is already a git repo, else clone it |
64394e3a | 378 | if test -e "$sm_path" |
d4264ca3 | 379 | then |
64394e3a | 380 | if test -d "$sm_path"/.git -o -f "$sm_path"/.git |
d4264ca3 | 381 | then |
64394e3a | 382 | eval_gettextln "Adding existing repo at '\$sm_path' to the index" |
d4264ca3 | 383 | else |
64394e3a | 384 | die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")" |
d4264ca3 | 385 | fi |
c2f93917 | 386 | |
d4264ca3 | 387 | else |
4b7c286e JL |
388 | if test -d ".git/modules/$sm_name" |
389 | then | |
390 | if test -z "$force" | |
391 | then | |
392 | echo >&2 "$(eval_gettext "A git directory for '\$sm_name' is found locally with remote(s):")" | |
393 | GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2 | |
394 | echo >&2 "$(eval_gettext "If you want to reuse this local git directory instead of cloning again from")" | |
395 | echo >&2 " $realrepo" | |
396 | echo >&2 "$(eval_gettext "use the '--force' option. If the local git directory is not the correct repo")" | |
397 | die "$(eval_gettext "or you are unsure what this means choose another name with the '--name' option.")" | |
398 | else | |
399 | echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")" | |
400 | fi | |
401 | fi | |
73b0898d | 402 | module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" || exit |
ea10b60c | 403 | ( |
74ae1419 | 404 | clear_local_git_env |
64394e3a | 405 | cd "$sm_path" && |
ea10b60c BJ |
406 | # ash fails to wordsplit ${branch:+-b "$branch"...} |
407 | case "$branch" in | |
408 | '') git checkout -f -q ;; | |
502dc5b6 | 409 | ?*) git checkout -f -q -B "$branch" "origin/$branch" ;; |
ea10b60c | 410 | esac |
64394e3a | 411 | ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")" |
d4264ca3 | 412 | fi |
73b0898d | 413 | git config submodule."$sm_name".url "$realrepo" |
d4264ca3 | 414 | |
64394e3a RJ |
415 | git add $force "$sm_path" || |
416 | die "$(eval_gettext "Failed to add submodule '\$sm_path'")" | |
ecda0723 | 417 | |
73b0898d JL |
418 | git config -f .gitmodules submodule."$sm_name".path "$sm_path" && |
419 | git config -f .gitmodules submodule."$sm_name".url "$repo" && | |
b9289227 TK |
420 | if test -n "$branch" |
421 | then | |
422 | git config -f .gitmodules submodule."$sm_name".branch "$branch" | |
423 | fi && | |
31991b02 | 424 | git add --force .gitmodules || |
64394e3a | 425 | die "$(eval_gettext "Failed to register submodule '\$sm_path'")" |
ecda0723 SV |
426 | } |
427 | ||
19a31f9c ML |
428 | # |
429 | # Execute an arbitrary command sequence in each checked out | |
430 | # submodule | |
431 | # | |
432 | # $@ = command to execute | |
433 | # | |
434 | cmd_foreach() | |
435 | { | |
1d5bec8b JH |
436 | # parse $args after "submodule ... foreach". |
437 | while test $# -ne 0 | |
438 | do | |
439 | case "$1" in | |
440 | -q|--quiet) | |
441 | GIT_QUIET=1 | |
442 | ;; | |
15fc56a8 JH |
443 | --recursive) |
444 | recursive=1 | |
445 | ;; | |
1d5bec8b JH |
446 | -*) |
447 | usage | |
448 | ;; | |
449 | *) | |
450 | break | |
451 | ;; | |
452 | esac | |
453 | shift | |
454 | done | |
455 | ||
f030c96d ÆAB |
456 | toplevel=$(pwd) |
457 | ||
4dca1aa6 BC |
458 | # dup stdin so that it can be restored when running the external |
459 | # command in the subshell (and a recursive call to this function) | |
460 | exec 3<&0 | |
461 | ||
a7b3269c | 462 | module_list | |
64394e3a | 463 | while read mode sha1 stage sm_path |
19a31f9c | 464 | do |
be9d0a3a | 465 | die_if_unmatched "$mode" |
64394e3a | 466 | if test -e "$sm_path"/.git |
19a31f9c | 467 | then |
64394e3a RJ |
468 | say "$(eval_gettext "Entering '\$prefix\$sm_path'")" |
469 | name=$(module_name "$sm_path") | |
15fc56a8 | 470 | ( |
64394e3a | 471 | prefix="$prefix$sm_path/" |
74ae1419 | 472 | clear_local_git_env |
64394e3a RJ |
473 | # we make $path available to scripts ... |
474 | path=$sm_path | |
475 | cd "$sm_path" && | |
15fc56a8 JH |
476 | eval "$@" && |
477 | if test -n "$recursive" | |
478 | then | |
479 | cmd_foreach "--recursive" "$@" | |
480 | fi | |
4dca1aa6 | 481 | ) <&3 3<&- || |
64394e3a | 482 | die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")" |
19a31f9c ML |
483 | fi |
484 | done | |
485 | } | |
486 | ||
70c7ac22 | 487 | # |
211b7f19 | 488 | # Register submodules in .git/config |
70c7ac22 LH |
489 | # |
490 | # $@ = requested paths (default to all) | |
491 | # | |
23a485e3 | 492 | cmd_init() |
70c7ac22 | 493 | { |
5c08dbbd JH |
494 | # parse $args after "submodule ... init". |
495 | while test $# -ne 0 | |
496 | do | |
497 | case "$1" in | |
498 | -q|--quiet) | |
2e6a30ef | 499 | GIT_QUIET=1 |
5c08dbbd JH |
500 | ;; |
501 | --) | |
502 | shift | |
503 | break | |
504 | ;; | |
505 | -*) | |
506 | usage | |
507 | ;; | |
508 | *) | |
509 | break | |
510 | ;; | |
511 | esac | |
512 | shift | |
513 | done | |
514 | ||
a7b3269c | 515 | module_list "$@" | |
64394e3a | 516 | while read mode sha1 stage sm_path |
70c7ac22 | 517 | do |
be9d0a3a | 518 | die_if_unmatched "$mode" |
64394e3a | 519 | name=$(module_name "$sm_path") || exit |
c1c259e2 JL |
520 | |
521 | # Copy url setting when it is not set yet | |
2cd9de3e JL |
522 | if test -z "$(git config "submodule.$name.url")" |
523 | then | |
524 | url=$(git config -f .gitmodules submodule."$name".url) | |
525 | test -z "$url" && | |
64394e3a | 526 | die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")" |
2cd9de3e JL |
527 | |
528 | # Possibly a url relative to parent | |
529 | case "$url" in | |
530 | ./*|../*) | |
531 | url=$(resolve_relative_url "$url") || exit | |
532 | ;; | |
533 | esac | |
534 | git config submodule."$name".url "$url" || | |
64394e3a | 535 | die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")" |
c1c259e2 JL |
536 | |
537 | say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")" | |
2cd9de3e | 538 | fi |
70c7ac22 | 539 | |
2cd9de3e | 540 | # Copy "update" setting when it is not set yet |
32948425 JH |
541 | upd="$(git config -f .gitmodules submodule."$name".update)" |
542 | test -z "$upd" || | |
2cd9de3e | 543 | test -n "$(git config submodule."$name".update)" || |
32948425 | 544 | git config submodule."$name".update "$upd" || |
64394e3a | 545 | die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")" |
70c7ac22 LH |
546 | done |
547 | } | |
548 | ||
549 | # | |
211b7f19 | 550 | # Update each submodule path to correct revision, using clone and checkout as needed |
70c7ac22 LH |
551 | # |
552 | # $@ = requested paths (default to all) | |
553 | # | |
23a485e3 | 554 | cmd_update() |
70c7ac22 | 555 | { |
5c08dbbd | 556 | # parse $args after "submodule ... update". |
98dbe63d | 557 | orig_flags= |
5c08dbbd JH |
558 | while test $# -ne 0 |
559 | do | |
560 | case "$1" in | |
561 | -q|--quiet) | |
2e6a30ef | 562 | GIT_QUIET=1 |
5c08dbbd | 563 | ;; |
be4d2c83 | 564 | -i|--init) |
d92a3959 | 565 | init=1 |
be4d2c83 | 566 | ;; |
06b1abb5 TK |
567 | --remote) |
568 | remote=1 | |
569 | ;; | |
31ca3ac3 | 570 | -N|--no-fetch) |
31ca3ac3 FF |
571 | nofetch=1 |
572 | ;; | |
9db31bdf NMC |
573 | -f|--force) |
574 | force=$1 | |
575 | ;; | |
ca2cedba | 576 | -r|--rebase) |
32948425 | 577 | update="rebase" |
ca2cedba | 578 | ;; |
d92a3959 MT |
579 | --reference) |
580 | case "$2" in '') usage ;; esac | |
581 | reference="--reference=$2" | |
98dbe63d KB |
582 | orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")" |
583 | shift | |
d92a3959 MT |
584 | ;; |
585 | --reference=*) | |
586 | reference="$1" | |
d92a3959 | 587 | ;; |
42b49178 | 588 | -m|--merge) |
42b49178 JH |
589 | update="merge" |
590 | ;; | |
b13fd5c1 | 591 | --recursive) |
b13fd5c1 JH |
592 | recursive=1 |
593 | ;; | |
efc5fb6a JH |
594 | --checkout) |
595 | update="checkout" | |
596 | ;; | |
5c08dbbd JH |
597 | --) |
598 | shift | |
599 | break | |
600 | ;; | |
601 | -*) | |
602 | usage | |
603 | ;; | |
604 | *) | |
605 | break | |
606 | ;; | |
607 | esac | |
98dbe63d KB |
608 | orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")" |
609 | shift | |
5c08dbbd JH |
610 | done |
611 | ||
d92a3959 MT |
612 | if test -n "$init" |
613 | then | |
614 | cmd_init "--" "$@" || return | |
615 | fi | |
616 | ||
1b4735d9 | 617 | cloned_modules= |
15ffb7cd FG |
618 | module_list "$@" | { |
619 | err= | |
64394e3a | 620 | while read mode sha1 stage sm_path |
70c7ac22 | 621 | do |
be9d0a3a | 622 | die_if_unmatched "$mode" |
313ee0d6 NMC |
623 | if test "$stage" = U |
624 | then | |
64394e3a | 625 | echo >&2 "Skipping unmerged submodule $sm_path" |
313ee0d6 NMC |
626 | continue |
627 | fi | |
64394e3a | 628 | name=$(module_name "$sm_path") || exit |
5be60078 | 629 | url=$(git config submodule."$name".url) |
06b1abb5 | 630 | branch=$(get_submodule_config "$name" branch master) |
efc5fb6a JH |
631 | if ! test -z "$update" |
632 | then | |
633 | update_module=$update | |
634 | else | |
635 | update_module=$(git config submodule."$name".update) | |
636 | fi | |
637 | ||
638 | if test "$update_module" = "none" | |
639 | then | |
64394e3a | 640 | echo "Skipping submodule '$sm_path'" |
efc5fb6a JH |
641 | continue |
642 | fi | |
643 | ||
211b7f19 | 644 | if test -z "$url" |
70c7ac22 LH |
645 | then |
646 | # Only mention uninitialized submodules when its | |
647 | # path have been specified | |
648 | test "$#" != "0" && | |
64394e3a | 649 | say "$(eval_gettext "Submodule path '\$sm_path' not initialized |
1c2ef66f | 650 | Maybe you want to use 'update --init'?")" |
211b7f19 LH |
651 | continue |
652 | fi | |
653 | ||
64394e3a | 654 | if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git |
211b7f19 | 655 | then |
73b0898d | 656 | module_clone "$sm_path" "$name" "$url" "$reference" || exit |
1b4735d9 | 657 | cloned_modules="$cloned_modules;$name" |
bf2d8246 LH |
658 | subsha1= |
659 | else | |
64394e3a | 660 | subsha1=$(clear_local_git_env; cd "$sm_path" && |
5be60078 | 661 | git rev-parse --verify HEAD) || |
64394e3a | 662 | die "$(eval_gettext "Unable to find current revision in submodule path '\$sm_path'")" |
70c7ac22 | 663 | fi |
211b7f19 | 664 | |
06b1abb5 TK |
665 | if test -n "$remote" |
666 | then | |
667 | if test -z "$nofetch" | |
668 | then | |
669 | # Fetch remote before determining tracking $sha1 | |
670 | (clear_local_git_env; cd "$sm_path" && git-fetch) || | |
671 | die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")" | |
672 | fi | |
673 | remote_name=$(clear_local_git_env; cd "$sm_path" && get_default_remote) | |
674 | sha1=$(clear_local_git_env; cd "$sm_path" && | |
675 | git rev-parse --verify "${remote_name}/${branch}") || | |
676 | die "$(eval_gettext "Unable to find current ${remote_name}/${branch} revision in submodule path '\$sm_path'")" | |
677 | fi | |
678 | ||
01d47215 | 679 | if test "$subsha1" != "$sha1" -o -n "$force" |
70c7ac22 | 680 | then |
9db31bdf NMC |
681 | subforce=$force |
682 | # If we don't already have a -f flag and the submodule has never been checked out | |
683 | if test -z "$subsha1" -a -z "$force" | |
b9b378a0 | 684 | then |
9db31bdf | 685 | subforce="-f" |
b9b378a0 | 686 | fi |
31ca3ac3 FF |
687 | |
688 | if test -z "$nofetch" | |
689 | then | |
e5f522d6 JL |
690 | # Run fetch only if $sha1 isn't present or it |
691 | # is not reachable from a ref. | |
64394e3a | 692 | (clear_local_git_env; cd "$sm_path" && |
f5799e05 | 693 | ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) && |
e5f522d6 | 694 | test -z "$rev") || git-fetch)) || |
64394e3a | 695 | die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")" |
31ca3ac3 FF |
696 | fi |
697 | ||
1b4735d9 SO |
698 | # Is this something we just cloned? |
699 | case ";$cloned_modules;" in | |
700 | *";$name;"*) | |
701 | # then there is no local change to integrate | |
702 | update_module= ;; | |
703 | esac | |
704 | ||
877449c1 | 705 | must_die_on_failure= |
32948425 JH |
706 | case "$update_module" in |
707 | rebase) | |
708 | command="git rebase" | |
64394e3a RJ |
709 | die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$sm_path'")" |
710 | say_msg="$(eval_gettext "Submodule path '\$sm_path': rebased into '\$sha1'")" | |
877449c1 | 711 | must_die_on_failure=yes |
32948425 | 712 | ;; |
42b49178 JH |
713 | merge) |
714 | command="git merge" | |
64394e3a RJ |
715 | die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$sm_path'")" |
716 | say_msg="$(eval_gettext "Submodule path '\$sm_path': merged in '\$sha1'")" | |
877449c1 | 717 | must_die_on_failure=yes |
42b49178 | 718 | ;; |
32948425 | 719 | *) |
9db31bdf | 720 | command="git checkout $subforce -q" |
64394e3a RJ |
721 | die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$sm_path'")" |
722 | say_msg="$(eval_gettext "Submodule path '\$sm_path': checked out '\$sha1'")" | |
32948425 JH |
723 | ;; |
724 | esac | |
70c7ac22 | 725 | |
64394e3a | 726 | if (clear_local_git_env; cd "$sm_path" && $command "$sha1") |
15ffb7cd | 727 | then |
ff968f03 | 728 | say "$say_msg" |
877449c1 JH |
729 | elif test -n "$must_die_on_failure" |
730 | then | |
ff968f03 | 731 | die_with_status 2 "$die_msg" |
15ffb7cd | 732 | else |
ff968f03 | 733 | err="${err};$die_msg" |
877449c1 | 734 | continue |
15ffb7cd | 735 | fi |
70c7ac22 | 736 | fi |
b13fd5c1 JH |
737 | |
738 | if test -n "$recursive" | |
739 | then | |
64394e3a | 740 | (clear_local_git_env; cd "$sm_path" && eval cmd_update "$orig_flags") |
15ffb7cd FG |
741 | res=$? |
742 | if test $res -gt 0 | |
743 | then | |
64394e3a | 744 | die_msg="$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")" |
15ffb7cd FG |
745 | if test $res -eq 1 |
746 | then | |
ff968f03 | 747 | err="${err};$die_msg" |
15ffb7cd FG |
748 | continue |
749 | else | |
ff968f03 | 750 | die_with_status $res "$die_msg" |
15ffb7cd FG |
751 | fi |
752 | fi | |
b13fd5c1 | 753 | fi |
70c7ac22 | 754 | done |
15ffb7cd FG |
755 | |
756 | if test -n "$err" | |
757 | then | |
758 | OIFS=$IFS | |
759 | IFS=';' | |
760 | for e in $err | |
761 | do | |
762 | if test -n "$e" | |
763 | then | |
764 | echo >&2 "$e" | |
765 | fi | |
766 | done | |
767 | IFS=$OIFS | |
768 | exit 1 | |
769 | fi | |
770 | } | |
70c7ac22 LH |
771 | } |
772 | ||
bffe71f4 EM |
773 | set_name_rev () { |
774 | revname=$( ( | |
74ae1419 | 775 | clear_local_git_env |
bffe71f4 | 776 | cd "$1" && { |
5be60078 JH |
777 | git describe "$2" 2>/dev/null || |
778 | git describe --tags "$2" 2>/dev/null || | |
f669ac0b ML |
779 | git describe --contains "$2" 2>/dev/null || |
780 | git describe --all --always "$2" | |
bffe71f4 EM |
781 | } |
782 | ) ) | |
783 | test -z "$revname" || revname=" ($revname)" | |
784 | } | |
28f9af5d PY |
785 | # |
786 | # Show commit summary for submodules in index or working tree | |
787 | # | |
788 | # If '--cached' is given, show summary between index and given commit, | |
789 | # or between working tree and given commit | |
790 | # | |
791 | # $@ = [commit (default 'HEAD'),] requested paths (default all) | |
792 | # | |
793 | cmd_summary() { | |
f2dc06a3 | 794 | summary_limit=-1 |
d0f64dd4 | 795 | for_status= |
1c244f6e | 796 | diff_cmd=diff-index |
f2dc06a3 | 797 | |
28f9af5d PY |
798 | # parse $args after "submodule ... summary". |
799 | while test $# -ne 0 | |
800 | do | |
801 | case "$1" in | |
802 | --cached) | |
803 | cached="$1" | |
804 | ;; | |
1c244f6e JL |
805 | --files) |
806 | files="$1" | |
807 | ;; | |
d0f64dd4 PY |
808 | --for-status) |
809 | for_status="$1" | |
810 | ;; | |
f2dc06a3 PY |
811 | -n|--summary-limit) |
812 | if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2" | |
813 | then | |
814 | : | |
815 | else | |
816 | usage | |
817 | fi | |
818 | shift | |
819 | ;; | |
28f9af5d PY |
820 | --) |
821 | shift | |
822 | break | |
823 | ;; | |
824 | -*) | |
825 | usage | |
826 | ;; | |
827 | *) | |
828 | break | |
829 | ;; | |
830 | esac | |
831 | shift | |
832 | done | |
bffe71f4 | 833 | |
f2dc06a3 PY |
834 | test $summary_limit = 0 && return |
835 | ||
3deea89c | 836 | if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"}) |
28f9af5d PY |
837 | then |
838 | head=$rev | |
caa9c3ca | 839 | test $# = 0 || shift |
3deea89c JH |
840 | elif test -z "$1" -o "$1" = "HEAD" |
841 | then | |
14e940d7 JH |
842 | # before the first commit: compare with an empty tree |
843 | head=$(git hash-object -w -t tree --stdin </dev/null) | |
2ea6c2c9 | 844 | test -z "$1" || shift |
28f9af5d | 845 | else |
3deea89c | 846 | head="HEAD" |
28f9af5d PY |
847 | fi |
848 | ||
1c244f6e JL |
849 | if [ -n "$files" ] |
850 | then | |
851 | test -n "$cached" && | |
465d6a00 | 852 | die "$(gettext "The --cached option cannot be used with the --files option")" |
1c244f6e JL |
853 | diff_cmd=diff-files |
854 | head= | |
855 | fi | |
856 | ||
28f9af5d PY |
857 | cd_to_toplevel |
858 | # Get modified modules cared by user | |
18076502 | 859 | modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" | |
e1622bfc | 860 | sane_egrep '^:([0-7]* )?160000' | |
28f9af5d PY |
861 | while read mod_src mod_dst sha1_src sha1_dst status name |
862 | do | |
863 | # Always show modules deleted or type-changed (blob<->module) | |
864 | test $status = D -o $status = T && echo "$name" && continue | |
865 | # Also show added or modified modules which are checked out | |
866 | GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 && | |
867 | echo "$name" | |
868 | done | |
869 | ) | |
1cb639e6 | 870 | |
d0f64dd4 PY |
871 | test -z "$modules" && return |
872 | ||
18076502 | 873 | git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules | |
e1622bfc | 874 | sane_egrep '^:([0-7]* )?160000' | |
1cb639e6 PY |
875 | cut -c2- | |
876 | while read mod_src mod_dst sha1_src sha1_dst status name | |
877 | do | |
878 | if test -z "$cached" && | |
879 | test $sha1_dst = 0000000000000000000000000000000000000000 | |
880 | then | |
881 | case "$mod_dst" in | |
882 | 160000) | |
883 | sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD) | |
884 | ;; | |
885 | 100644 | 100755 | 120000) | |
886 | sha1_dst=$(git hash-object $name) | |
887 | ;; | |
888 | 000000) | |
889 | ;; # removed | |
890 | *) | |
891 | # unexpected type | |
6ff875c5 | 892 | eval_gettextln "unexpected mode \$mod_dst" >&2 |
1cb639e6 PY |
893 | continue ;; |
894 | esac | |
895 | fi | |
896 | missing_src= | |
897 | missing_dst= | |
898 | ||
899 | test $mod_src = 160000 && | |
30353a40 | 900 | ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null && |
1cb639e6 PY |
901 | missing_src=t |
902 | ||
903 | test $mod_dst = 160000 && | |
30353a40 | 904 | ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null && |
1cb639e6 | 905 | missing_dst=t |
bffe71f4 | 906 | |
1cb639e6 PY |
907 | total_commits= |
908 | case "$missing_src,$missing_dst" in | |
909 | t,) | |
f62f8212 | 910 | errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")" |
1cb639e6 PY |
911 | ;; |
912 | ,t) | |
f62f8212 | 913 | errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")" |
1cb639e6 PY |
914 | ;; |
915 | t,t) | |
f62f8212 | 916 | errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")" |
1cb639e6 PY |
917 | ;; |
918 | *) | |
919 | errmsg= | |
920 | total_commits=$( | |
921 | if test $mod_src = 160000 -a $mod_dst = 160000 | |
922 | then | |
923 | range="$sha1_src...$sha1_dst" | |
924 | elif test $mod_src = 160000 | |
925 | then | |
926 | range=$sha1_src | |
927 | else | |
928 | range=$sha1_dst | |
929 | fi | |
930 | GIT_DIR="$name/.git" \ | |
b0e621ad | 931 | git rev-list --first-parent $range -- | wc -l |
1cb639e6 | 932 | ) |
eed35595 | 933 | total_commits=" ($(($total_commits + 0)))" |
1cb639e6 PY |
934 | ;; |
935 | esac | |
936 | ||
937 | sha1_abbr_src=$(echo $sha1_src | cut -c1-7) | |
938 | sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7) | |
939 | if test $status = T | |
940 | then | |
b3e73449 ÆAB |
941 | blob="$(gettext "blob")" |
942 | submodule="$(gettext "submodule")" | |
1cb639e6 PY |
943 | if test $mod_dst = 160000 |
944 | then | |
b3e73449 | 945 | echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:" |
1cb639e6 | 946 | else |
b3e73449 | 947 | echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:" |
1cb639e6 PY |
948 | fi |
949 | else | |
950 | echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:" | |
951 | fi | |
952 | if test -n "$errmsg" | |
953 | then | |
954 | # Don't give error msg for modification whose dst is not submodule | |
955 | # i.e. deleted or changed to blob | |
956 | test $mod_dst = 160000 && echo "$errmsg" | |
957 | else | |
958 | if test $mod_src = 160000 -a $mod_dst = 160000 | |
959 | then | |
f2dc06a3 PY |
960 | limit= |
961 | test $summary_limit -gt 0 && limit="-$summary_limit" | |
1cb639e6 | 962 | GIT_DIR="$name/.git" \ |
f2dc06a3 | 963 | git log $limit --pretty='format: %m %s' \ |
1cb639e6 PY |
964 | --first-parent $sha1_src...$sha1_dst |
965 | elif test $mod_dst = 160000 | |
966 | then | |
967 | GIT_DIR="$name/.git" \ | |
968 | git log --pretty='format: > %s' -1 $sha1_dst | |
969 | else | |
970 | GIT_DIR="$name/.git" \ | |
971 | git log --pretty='format: < %s' -1 $sha1_src | |
972 | fi | |
973 | echo | |
974 | fi | |
975 | echo | |
d0f64dd4 PY |
976 | done | |
977 | if test -n "$for_status"; then | |
f17a5d34 | 978 | if [ -n "$files" ]; then |
eff80a9f | 979 | gettextln "Submodules changed but not updated:" | git stripspace -c |
f17a5d34 | 980 | else |
eff80a9f | 981 | gettextln "Submodule changes to be committed:" | git stripspace -c |
f17a5d34 | 982 | fi |
eff80a9f JH |
983 | printf "\n" | git stripspace -c |
984 | git stripspace -c | |
d0f64dd4 PY |
985 | else |
986 | cat | |
987 | fi | |
28f9af5d | 988 | } |
70c7ac22 | 989 | # |
941987a5 | 990 | # List all submodules, prefixed with: |
70c7ac22 LH |
991 | # - submodule not initialized |
992 | # + different revision checked out | |
993 | # | |
994 | # If --cached was specified the revision in the index will be printed | |
995 | # instead of the currently checked out revision. | |
996 | # | |
997 | # $@ = requested paths (default to all) | |
998 | # | |
23a485e3 | 999 | cmd_status() |
70c7ac22 | 1000 | { |
5c08dbbd JH |
1001 | # parse $args after "submodule ... status". |
1002 | while test $# -ne 0 | |
1003 | do | |
1004 | case "$1" in | |
1005 | -q|--quiet) | |
2e6a30ef | 1006 | GIT_QUIET=1 |
5c08dbbd JH |
1007 | ;; |
1008 | --cached) | |
1009 | cached=1 | |
1010 | ;; | |
64b19ffe JH |
1011 | --recursive) |
1012 | recursive=1 | |
1013 | ;; | |
5c08dbbd JH |
1014 | --) |
1015 | shift | |
1016 | break | |
1017 | ;; | |
1018 | -*) | |
1019 | usage | |
1020 | ;; | |
1021 | *) | |
1022 | break | |
1023 | ;; | |
1024 | esac | |
1025 | shift | |
1026 | done | |
1027 | ||
a7b3269c | 1028 | module_list "$@" | |
64394e3a | 1029 | while read mode sha1 stage sm_path |
70c7ac22 | 1030 | do |
be9d0a3a | 1031 | die_if_unmatched "$mode" |
64394e3a | 1032 | name=$(module_name "$sm_path") || exit |
5be60078 | 1033 | url=$(git config submodule."$name".url) |
64394e3a | 1034 | displaypath="$prefix$sm_path" |
313ee0d6 NMC |
1035 | if test "$stage" = U |
1036 | then | |
1037 | say "U$sha1 $displaypath" | |
1038 | continue | |
1039 | fi | |
64394e3a | 1040 | if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git |
70c7ac22 | 1041 | then |
64b19ffe | 1042 | say "-$sha1 $displaypath" |
70c7ac22 LH |
1043 | continue; |
1044 | fi | |
64394e3a RJ |
1045 | set_name_rev "$sm_path" "$sha1" |
1046 | if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path" | |
70c7ac22 | 1047 | then |
64b19ffe | 1048 | say " $sha1 $displaypath$revname" |
70c7ac22 LH |
1049 | else |
1050 | if test -z "$cached" | |
1051 | then | |
64394e3a RJ |
1052 | sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD) |
1053 | set_name_rev "$sm_path" "$sha1" | |
70c7ac22 | 1054 | fi |
64b19ffe JH |
1055 | say "+$sha1 $displaypath$revname" |
1056 | fi | |
1057 | ||
1058 | if test -n "$recursive" | |
1059 | then | |
1060 | ( | |
1061 | prefix="$displaypath/" | |
74ae1419 | 1062 | clear_local_git_env |
64394e3a | 1063 | cd "$sm_path" && |
e15bec0e | 1064 | eval cmd_status |
64b19ffe | 1065 | ) || |
64394e3a | 1066 | die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")" |
70c7ac22 LH |
1067 | fi |
1068 | done | |
1069 | } | |
2327f61e DA |
1070 | # |
1071 | # Sync remote urls for submodules | |
1072 | # This makes the value for remote.$remote.url match the value | |
1073 | # specified in .gitmodules. | |
1074 | # | |
1075 | cmd_sync() | |
1076 | { | |
1077 | while test $# -ne 0 | |
1078 | do | |
1079 | case "$1" in | |
1080 | -q|--quiet) | |
2e6a30ef | 1081 | GIT_QUIET=1 |
2327f61e DA |
1082 | shift |
1083 | ;; | |
82f49f29 PH |
1084 | --recursive) |
1085 | recursive=1 | |
1086 | shift | |
1087 | ;; | |
2327f61e DA |
1088 | --) |
1089 | shift | |
1090 | break | |
1091 | ;; | |
1092 | -*) | |
1093 | usage | |
1094 | ;; | |
1095 | *) | |
1096 | break | |
1097 | ;; | |
1098 | esac | |
1099 | done | |
1100 | cd_to_toplevel | |
1101 | module_list "$@" | | |
64394e3a | 1102 | while read mode sha1 stage sm_path |
2327f61e | 1103 | do |
be9d0a3a | 1104 | die_if_unmatched "$mode" |
64394e3a | 1105 | name=$(module_name "$sm_path") |
2327f61e | 1106 | url=$(git config -f .gitmodules --get submodule."$name".url) |
baede9f8 JH |
1107 | |
1108 | # Possibly a url relative to parent | |
1109 | case "$url" in | |
1110 | ./*|../*) | |
967b2c66 JS |
1111 | # rewrite foo/bar as ../.. to find path from |
1112 | # submodule work tree to superproject work tree | |
1113 | up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" && | |
1114 | # guarantee a trailing / | |
1115 | up_path=${up_path%/}/ && | |
1116 | # path from submodule work tree to submodule origin repo | |
1117 | sub_origin_url=$(resolve_relative_url "$url" "$up_path") && | |
1118 | # path from superproject work tree to submodule origin repo | |
1119 | super_config_url=$(resolve_relative_url "$url") || exit | |
1120 | ;; | |
1121 | *) | |
1122 | sub_origin_url="$url" | |
1123 | super_config_url="$url" | |
baede9f8 JH |
1124 | ;; |
1125 | esac | |
1126 | ||
ccee6086 | 1127 | if git config "submodule.$name.url" >/dev/null 2>/dev/null |
2327f61e | 1128 | then |
82f49f29 | 1129 | say "$(eval_gettext "Synchronizing submodule url for '\$prefix\$sm_path'")" |
967b2c66 | 1130 | git config submodule."$name".url "$super_config_url" |
ccee6086 | 1131 | |
64394e3a | 1132 | if test -e "$sm_path"/.git |
ccee6086 JH |
1133 | then |
1134 | ( | |
1135 | clear_local_git_env | |
64394e3a | 1136 | cd "$sm_path" |
ccee6086 | 1137 | remote=$(get_default_remote) |
967b2c66 | 1138 | git config remote."$remote".url "$sub_origin_url" |
82f49f29 PH |
1139 | |
1140 | if test -n "$recursive" | |
1141 | then | |
1142 | prefix="$prefix$sm_path/" | |
1143 | eval cmd_sync | |
1144 | fi | |
ccee6086 JH |
1145 | ) |
1146 | fi | |
2327f61e DA |
1147 | fi |
1148 | done | |
1149 | } | |
70c7ac22 | 1150 | |
5c08dbbd JH |
1151 | # This loop parses the command line arguments to find the |
1152 | # subcommand name to dispatch. Parsing of the subcommand specific | |
1153 | # options are primarily done by the subcommand implementations. | |
1154 | # Subcommand specific options such as --branch and --cached are | |
1155 | # parsed here as well, for backward compatibility. | |
1156 | ||
1157 | while test $# != 0 && test -z "$command" | |
70c7ac22 LH |
1158 | do |
1159 | case "$1" in | |
2327f61e | 1160 | add | foreach | init | update | status | summary | sync) |
5c08dbbd | 1161 | command=$1 |
70c7ac22 LH |
1162 | ;; |
1163 | -q|--quiet) | |
2e6a30ef | 1164 | GIT_QUIET=1 |
70c7ac22 | 1165 | ;; |
ecda0723 SV |
1166 | -b|--branch) |
1167 | case "$2" in | |
1168 | '') | |
1169 | usage | |
1170 | ;; | |
1171 | esac | |
1172 | branch="$2"; shift | |
1173 | ;; | |
70c7ac22 | 1174 | --cached) |
28f9af5d | 1175 | cached="$1" |
70c7ac22 LH |
1176 | ;; |
1177 | --) | |
1178 | break | |
1179 | ;; | |
1180 | -*) | |
1181 | usage | |
1182 | ;; | |
1183 | *) | |
1184 | break | |
1185 | ;; | |
1186 | esac | |
1187 | shift | |
1188 | done | |
1189 | ||
5c08dbbd | 1190 | # No command word defaults to "status" |
af9c9f97 RR |
1191 | if test -z "$command" |
1192 | then | |
1193 | if test $# = 0 | |
1194 | then | |
1195 | command=status | |
1196 | else | |
1197 | usage | |
1198 | fi | |
1199 | fi | |
5c08dbbd JH |
1200 | |
1201 | # "-b branch" is accepted only by "add" | |
1202 | if test -n "$branch" && test "$command" != add | |
1203 | then | |
ecda0723 | 1204 | usage |
5c08dbbd JH |
1205 | fi |
1206 | ||
28f9af5d PY |
1207 | # "--cached" is accepted only by "status" and "summary" |
1208 | if test -n "$cached" && test "$command" != status -a "$command" != summary | |
5c08dbbd | 1209 | then |
70c7ac22 | 1210 | usage |
5c08dbbd JH |
1211 | fi |
1212 | ||
1213 | "cmd_$command" "$@" |