]>
Commit | Line | Data |
---|---|---|
70c7ac22 LH |
1 | #!/bin/sh |
2 | # | |
ecda0723 | 3 | # git-submodules.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/-/ /') |
d27b876b | 8 | USAGE="[--quiet] add [-b branch] [-f|--force] [--reference <repository>] [--] <repository> [<path>] |
64b19ffe | 9 | or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...] |
1d5bec8b | 10 | or: $dashless [--quiet] init [--] [<path>...] |
9db31bdf | 11 | or: $dashless [--quiet] update [--init] [-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> |
1d5bec8b | 14 | or: $dashless [--quiet] sync [--] [<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= |
31ca3ac3 | 29 | nofetch= |
32948425 | 30 | update= |
15fc56a8 | 31 | prefix= |
70c7ac22 | 32 | |
f31a522a ML |
33 | # Resolve relative url by appending to parent's url |
34 | resolve_relative_url () | |
35 | { | |
98fcf840 | 36 | remote=$(get_default_remote) |
8e7e6f39 | 37 | remoteurl=$(git config "remote.$remote.url") || |
4d689320 | 38 | remoteurl=$(pwd) # the repository is its own authoritative upstream |
f31a522a | 39 | url="$1" |
99b120af | 40 | remoteurl=${remoteurl%/} |
ea640cc6 | 41 | sep=/ |
f31a522a ML |
42 | while test -n "$url" |
43 | do | |
44 | case "$url" in | |
45 | ../*) | |
46 | url="${url#../}" | |
ea640cc6 TR |
47 | case "$remoteurl" in |
48 | */*) | |
49 | remoteurl="${remoteurl%/*}" | |
50 | ;; | |
51 | *:*) | |
52 | remoteurl="${remoteurl%:*}" | |
53 | sep=: | |
54 | ;; | |
55 | *) | |
497ee872 | 56 | die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")" |
ea640cc6 TR |
57 | ;; |
58 | esac | |
f31a522a ML |
59 | ;; |
60 | ./*) | |
61 | url="${url#./}" | |
62 | ;; | |
63 | *) | |
64 | break;; | |
65 | esac | |
66 | done | |
ea640cc6 | 67 | echo "$remoteurl$sep${url%/}" |
f31a522a ML |
68 | } |
69 | ||
a7b3269c DA |
70 | # |
71 | # Get submodule info for registered submodules | |
72 | # $@ = path to limit submodule list | |
73 | # | |
74 | module_list() | |
75 | { | |
313ee0d6 NMC |
76 | git ls-files --error-unmatch --stage -- "$@" | |
77 | perl -e ' | |
78 | my %unmerged = (); | |
79 | my ($null_sha1) = ("0" x 40); | |
80 | while (<STDIN>) { | |
81 | chomp; | |
82 | my ($mode, $sha1, $stage, $path) = | |
83 | /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/; | |
84 | next unless $mode eq "160000"; | |
85 | if ($stage ne "0") { | |
86 | if (!$unmerged{$path}++) { | |
87 | print "$mode $null_sha1 U\t$path\n"; | |
88 | } | |
89 | next; | |
90 | } | |
91 | print "$_\n"; | |
92 | } | |
93 | ' | |
a7b3269c DA |
94 | } |
95 | ||
941987a5 LH |
96 | # |
97 | # Map submodule path to submodule name | |
98 | # | |
99 | # $1 = path | |
100 | # | |
101 | module_name() | |
102 | { | |
537601ac | 103 | # Do we have "submodule.<something>.path = $1" defined in .gitmodules file? |
fe22e542 | 104 | re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g') |
a5099bb4 | 105 | name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' | |
537601ac | 106 | sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' ) |
941987a5 | 107 | test -z "$name" && |
497ee872 | 108 | die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$path'")" |
941987a5 LH |
109 | echo "$name" |
110 | } | |
33aa6fff LH |
111 | |
112 | # | |
113 | # Clone a submodule | |
114 | # | |
23a485e3 | 115 | # Prior to calling, cmd_update checks that a possibly existing |
ecda0723 | 116 | # path is not a git repository. |
23a485e3 | 117 | # Likewise, cmd_add checks that path does not exist at all, |
ecda0723 SV |
118 | # since it is the location of a new submodule. |
119 | # | |
33aa6fff LH |
120 | module_clone() |
121 | { | |
122 | path=$1 | |
123 | url=$2 | |
d92a3959 | 124 | reference="$3" |
7e60407f JL |
125 | quiet= |
126 | if test -n "$GIT_QUIET" | |
127 | then | |
128 | quiet=-q | |
129 | fi | |
33aa6fff | 130 | |
d92a3959 MT |
131 | if test -n "$reference" |
132 | then | |
7e60407f | 133 | git-clone $quiet "$reference" -n "$url" "$path" |
d92a3959 | 134 | else |
7e60407f | 135 | git-clone $quiet -n "$url" "$path" |
d92a3959 | 136 | fi || |
497ee872 | 137 | die "$(eval_gettext "Clone of '\$url' into submodule path '\$path' failed")" |
33aa6fff LH |
138 | } |
139 | ||
ecda0723 SV |
140 | # |
141 | # Add a new submodule to the working tree, .gitmodules and the index | |
142 | # | |
ec05df35 | 143 | # $@ = repo path |
ecda0723 SV |
144 | # |
145 | # optional branch is stored in global branch variable | |
146 | # | |
23a485e3 | 147 | cmd_add() |
ecda0723 | 148 | { |
5c08dbbd JH |
149 | # parse $args after "submodule ... add". |
150 | while test $# -ne 0 | |
151 | do | |
152 | case "$1" in | |
153 | -b | --branch) | |
154 | case "$2" in '') usage ;; esac | |
155 | branch=$2 | |
156 | shift | |
157 | ;; | |
d27b876b JL |
158 | -f | --force) |
159 | force=$1 | |
160 | ;; | |
5c08dbbd | 161 | -q|--quiet) |
2e6a30ef | 162 | GIT_QUIET=1 |
5c08dbbd | 163 | ;; |
d92a3959 MT |
164 | --reference) |
165 | case "$2" in '') usage ;; esac | |
166 | reference="--reference=$2" | |
167 | shift | |
168 | ;; | |
169 | --reference=*) | |
170 | reference="$1" | |
171 | shift | |
172 | ;; | |
5c08dbbd JH |
173 | --) |
174 | shift | |
175 | break | |
176 | ;; | |
177 | -*) | |
178 | usage | |
179 | ;; | |
180 | *) | |
181 | break | |
182 | ;; | |
183 | esac | |
184 | shift | |
185 | done | |
186 | ||
ecda0723 SV |
187 | repo=$1 |
188 | path=$2 | |
189 | ||
1414e578 JL |
190 | if test -z "$path"; then |
191 | path=$(echo "$repo" | | |
192 | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g') | |
193 | fi | |
194 | ||
ec05df35 | 195 | if test -z "$repo" -o -z "$path"; then |
ecda0723 SV |
196 | usage |
197 | fi | |
198 | ||
ec05df35 ML |
199 | # assure repo is absolute or relative to parent |
200 | case "$repo" in | |
201 | ./*|../*) | |
202 | # dereference source url relative to parent's url | |
203 | realrepo=$(resolve_relative_url "$repo") || exit | |
204 | ;; | |
205 | *:*|/*) | |
206 | # absolute url | |
207 | realrepo=$repo | |
208 | ;; | |
209 | *) | |
497ee872 | 210 | die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")" |
ec05df35 ML |
211 | ;; |
212 | esac | |
213 | ||
db75ada5 MG |
214 | # normalize path: |
215 | # multiple //; leading ./; /./; /../; trailing / | |
216 | path=$(printf '%s/\n' "$path" | | |
217 | sed -e ' | |
218 | s|//*|/|g | |
219 | s|^\(\./\)*|| | |
220 | s|/\./|/|g | |
221 | :start | |
222 | s|\([^/]*\)/\.\./|| | |
223 | tstart | |
224 | s|/*$|| | |
225 | ') | |
5be60078 | 226 | git ls-files --error-unmatch "$path" > /dev/null 2>&1 && |
497ee872 | 227 | die "$(eval_gettext "'\$path' already exists in the index")" |
ecda0723 | 228 | |
d27b876b JL |
229 | if test -z "$force" && ! git add --dry-run --ignore-missing "$path" > /dev/null 2>&1 |
230 | then | |
6ff875c5 | 231 | eval_gettextln "The following path is ignored by one of your .gitignore files: |
3a4c3ed7 | 232 | \$path |
6ff875c5 | 233 | Use -f if you really want to add it." >&2 |
d27b876b JL |
234 | exit 1 |
235 | fi | |
236 | ||
d4264ca3 ML |
237 | # perhaps the path exists and is already a git repo, else clone it |
238 | if test -e "$path" | |
239 | then | |
e9656473 | 240 | if test -d "$path"/.git -o -f "$path"/.git |
d4264ca3 | 241 | then |
6ff875c5 | 242 | eval_gettextln "Adding existing repo at '\$path' to the index" |
d4264ca3 | 243 | else |
497ee872 | 244 | die "$(eval_gettext "'\$path' already exists and is not a valid git repo")" |
d4264ca3 | 245 | fi |
c2f93917 | 246 | |
d4264ca3 | 247 | else |
d4264ca3 | 248 | |
d92a3959 | 249 | module_clone "$path" "$realrepo" "$reference" || exit |
ea10b60c | 250 | ( |
74ae1419 | 251 | clear_local_git_env |
ea10b60c BJ |
252 | cd "$path" && |
253 | # ash fails to wordsplit ${branch:+-b "$branch"...} | |
254 | case "$branch" in | |
255 | '') git checkout -f -q ;; | |
502dc5b6 | 256 | ?*) git checkout -f -q -B "$branch" "origin/$branch" ;; |
ea10b60c | 257 | esac |
497ee872 | 258 | ) || die "$(eval_gettext "Unable to checkout submodule '\$path'")" |
d4264ca3 | 259 | fi |
c56dce3b | 260 | git config submodule."$path".url "$realrepo" |
d4264ca3 | 261 | |
d27b876b | 262 | git add $force "$path" || |
497ee872 | 263 | die "$(eval_gettext "Failed to add submodule '\$path'")" |
ecda0723 | 264 | |
a5099bb4 IY |
265 | git config -f .gitmodules submodule."$path".path "$path" && |
266 | git config -f .gitmodules submodule."$path".url "$repo" && | |
31991b02 | 267 | git add --force .gitmodules || |
497ee872 | 268 | die "$(eval_gettext "Failed to register submodule '\$path'")" |
ecda0723 SV |
269 | } |
270 | ||
19a31f9c ML |
271 | # |
272 | # Execute an arbitrary command sequence in each checked out | |
273 | # submodule | |
274 | # | |
275 | # $@ = command to execute | |
276 | # | |
277 | cmd_foreach() | |
278 | { | |
1d5bec8b JH |
279 | # parse $args after "submodule ... foreach". |
280 | while test $# -ne 0 | |
281 | do | |
282 | case "$1" in | |
283 | -q|--quiet) | |
284 | GIT_QUIET=1 | |
285 | ;; | |
15fc56a8 JH |
286 | --recursive) |
287 | recursive=1 | |
288 | ;; | |
1d5bec8b JH |
289 | -*) |
290 | usage | |
291 | ;; | |
292 | *) | |
293 | break | |
294 | ;; | |
295 | esac | |
296 | shift | |
297 | done | |
298 | ||
f030c96d ÆAB |
299 | toplevel=$(pwd) |
300 | ||
4dca1aa6 BC |
301 | # dup stdin so that it can be restored when running the external |
302 | # command in the subshell (and a recursive call to this function) | |
303 | exec 3<&0 | |
304 | ||
a7b3269c | 305 | module_list | |
19a31f9c ML |
306 | while read mode sha1 stage path |
307 | do | |
308 | if test -e "$path"/.git | |
309 | then | |
490b6d57 | 310 | say "$(eval_gettext "Entering '\$prefix\$path'")" |
1e7f2aad | 311 | name=$(module_name "$path") |
15fc56a8 JH |
312 | ( |
313 | prefix="$prefix$path/" | |
74ae1419 | 314 | clear_local_git_env |
15fc56a8 JH |
315 | cd "$path" && |
316 | eval "$@" && | |
317 | if test -n "$recursive" | |
318 | then | |
319 | cmd_foreach "--recursive" "$@" | |
320 | fi | |
4dca1aa6 | 321 | ) <&3 3<&- || |
497ee872 | 322 | die "$(eval_gettext "Stopping at '\$path'; script returned non-zero status.")" |
19a31f9c ML |
323 | fi |
324 | done | |
325 | } | |
326 | ||
70c7ac22 | 327 | # |
211b7f19 | 328 | # Register submodules in .git/config |
70c7ac22 LH |
329 | # |
330 | # $@ = requested paths (default to all) | |
331 | # | |
23a485e3 | 332 | cmd_init() |
70c7ac22 | 333 | { |
5c08dbbd JH |
334 | # parse $args after "submodule ... init". |
335 | while test $# -ne 0 | |
336 | do | |
337 | case "$1" in | |
338 | -q|--quiet) | |
2e6a30ef | 339 | GIT_QUIET=1 |
5c08dbbd JH |
340 | ;; |
341 | --) | |
342 | shift | |
343 | break | |
344 | ;; | |
345 | -*) | |
346 | usage | |
347 | ;; | |
348 | *) | |
349 | break | |
350 | ;; | |
351 | esac | |
352 | shift | |
353 | done | |
354 | ||
a7b3269c | 355 | module_list "$@" | |
70c7ac22 LH |
356 | while read mode sha1 stage path |
357 | do | |
211b7f19 | 358 | # Skip already registered paths |
941987a5 | 359 | name=$(module_name "$path") || exit |
2cd9de3e JL |
360 | if test -z "$(git config "submodule.$name.url")" |
361 | then | |
362 | url=$(git config -f .gitmodules submodule."$name".url) | |
363 | test -z "$url" && | |
0591c0a5 | 364 | die "$(eval_gettext "No url found for submodule path '\$path' in .gitmodules")" |
2cd9de3e JL |
365 | |
366 | # Possibly a url relative to parent | |
367 | case "$url" in | |
368 | ./*|../*) | |
369 | url=$(resolve_relative_url "$url") || exit | |
370 | ;; | |
371 | esac | |
372 | git config submodule."$name".url "$url" || | |
0591c0a5 | 373 | die "$(eval_gettext "Failed to register url for submodule path '\$path'")" |
2cd9de3e | 374 | fi |
70c7ac22 | 375 | |
2cd9de3e | 376 | # Copy "update" setting when it is not set yet |
32948425 JH |
377 | upd="$(git config -f .gitmodules submodule."$name".update)" |
378 | test -z "$upd" || | |
2cd9de3e | 379 | test -n "$(git config submodule."$name".update)" || |
32948425 | 380 | git config submodule."$name".update "$upd" || |
497ee872 | 381 | die "$(eval_gettext "Failed to register update mode for submodule path '\$path'")" |
ca2cedba | 382 | |
497ee872 | 383 | say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$path'")" |
70c7ac22 LH |
384 | done |
385 | } | |
386 | ||
387 | # | |
211b7f19 | 388 | # Update each submodule path to correct revision, using clone and checkout as needed |
70c7ac22 LH |
389 | # |
390 | # $@ = requested paths (default to all) | |
391 | # | |
23a485e3 | 392 | cmd_update() |
70c7ac22 | 393 | { |
5c08dbbd | 394 | # parse $args after "submodule ... update". |
98dbe63d | 395 | orig_flags= |
5c08dbbd JH |
396 | while test $# -ne 0 |
397 | do | |
398 | case "$1" in | |
399 | -q|--quiet) | |
2e6a30ef | 400 | GIT_QUIET=1 |
5c08dbbd | 401 | ;; |
be4d2c83 | 402 | -i|--init) |
d92a3959 | 403 | init=1 |
be4d2c83 | 404 | ;; |
31ca3ac3 | 405 | -N|--no-fetch) |
31ca3ac3 FF |
406 | nofetch=1 |
407 | ;; | |
9db31bdf NMC |
408 | -f|--force) |
409 | force=$1 | |
410 | ;; | |
ca2cedba | 411 | -r|--rebase) |
32948425 | 412 | update="rebase" |
ca2cedba | 413 | ;; |
d92a3959 MT |
414 | --reference) |
415 | case "$2" in '') usage ;; esac | |
416 | reference="--reference=$2" | |
98dbe63d KB |
417 | orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")" |
418 | shift | |
d92a3959 MT |
419 | ;; |
420 | --reference=*) | |
421 | reference="$1" | |
d92a3959 | 422 | ;; |
42b49178 | 423 | -m|--merge) |
42b49178 JH |
424 | update="merge" |
425 | ;; | |
b13fd5c1 | 426 | --recursive) |
b13fd5c1 JH |
427 | recursive=1 |
428 | ;; | |
5c08dbbd JH |
429 | --) |
430 | shift | |
431 | break | |
432 | ;; | |
433 | -*) | |
434 | usage | |
435 | ;; | |
436 | *) | |
437 | break | |
438 | ;; | |
439 | esac | |
98dbe63d KB |
440 | orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")" |
441 | shift | |
5c08dbbd JH |
442 | done |
443 | ||
d92a3959 MT |
444 | if test -n "$init" |
445 | then | |
446 | cmd_init "--" "$@" || return | |
447 | fi | |
448 | ||
1b4735d9 | 449 | cloned_modules= |
15ffb7cd FG |
450 | module_list "$@" | { |
451 | err= | |
70c7ac22 LH |
452 | while read mode sha1 stage path |
453 | do | |
313ee0d6 NMC |
454 | if test "$stage" = U |
455 | then | |
456 | echo >&2 "Skipping unmerged submodule $path" | |
457 | continue | |
458 | fi | |
941987a5 | 459 | name=$(module_name "$path") || exit |
5be60078 | 460 | url=$(git config submodule."$name".url) |
32948425 | 461 | update_module=$(git config submodule."$name".update) |
211b7f19 | 462 | if test -z "$url" |
70c7ac22 LH |
463 | then |
464 | # Only mention uninitialized submodules when its | |
465 | # path have been specified | |
466 | test "$#" != "0" && | |
1c2ef66f ÆAB |
467 | say "$(eval_gettext "Submodule path '\$path' not initialized |
468 | Maybe you want to use 'update --init'?")" | |
211b7f19 LH |
469 | continue |
470 | fi | |
471 | ||
ba88a1fe | 472 | if ! test -d "$path"/.git -o -f "$path"/.git |
211b7f19 | 473 | then |
d92a3959 | 474 | module_clone "$path" "$url" "$reference"|| exit |
1b4735d9 | 475 | cloned_modules="$cloned_modules;$name" |
bf2d8246 LH |
476 | subsha1= |
477 | else | |
74ae1419 | 478 | subsha1=$(clear_local_git_env; cd "$path" && |
5be60078 | 479 | git rev-parse --verify HEAD) || |
497ee872 | 480 | die "$(eval_gettext "Unable to find current revision in submodule path '\$path'")" |
70c7ac22 | 481 | fi |
211b7f19 | 482 | |
32948425 | 483 | if ! test -z "$update" |
ca2cedba | 484 | then |
32948425 | 485 | update_module=$update |
ca2cedba PH |
486 | fi |
487 | ||
70c7ac22 LH |
488 | if test "$subsha1" != "$sha1" |
489 | then | |
9db31bdf NMC |
490 | subforce=$force |
491 | # If we don't already have a -f flag and the submodule has never been checked out | |
492 | if test -z "$subsha1" -a -z "$force" | |
b9b378a0 | 493 | then |
9db31bdf | 494 | subforce="-f" |
b9b378a0 | 495 | fi |
31ca3ac3 FF |
496 | |
497 | if test -z "$nofetch" | |
498 | then | |
e5f522d6 JL |
499 | # Run fetch only if $sha1 isn't present or it |
500 | # is not reachable from a ref. | |
74ae1419 | 501 | (clear_local_git_env; cd "$path" && |
f5799e05 | 502 | ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) && |
e5f522d6 | 503 | test -z "$rev") || git-fetch)) || |
497ee872 | 504 | die "$(eval_gettext "Unable to fetch in submodule path '\$path'")" |
31ca3ac3 FF |
505 | fi |
506 | ||
1b4735d9 SO |
507 | # Is this something we just cloned? |
508 | case ";$cloned_modules;" in | |
509 | *";$name;"*) | |
510 | # then there is no local change to integrate | |
511 | update_module= ;; | |
512 | esac | |
513 | ||
877449c1 | 514 | must_die_on_failure= |
32948425 JH |
515 | case "$update_module" in |
516 | rebase) | |
517 | command="git rebase" | |
ee653c89 ÆAB |
518 | die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$path'")" |
519 | say_msg="$(eval_gettext "Submodule path '\$path': rebased into '\$sha1'")" | |
877449c1 | 520 | must_die_on_failure=yes |
32948425 | 521 | ;; |
42b49178 JH |
522 | merge) |
523 | command="git merge" | |
ee653c89 ÆAB |
524 | die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$path'")" |
525 | say_msg="$(eval_gettext "Submodule path '\$path': merged in '\$sha1'")" | |
877449c1 | 526 | must_die_on_failure=yes |
42b49178 | 527 | ;; |
32948425 | 528 | *) |
9db31bdf | 529 | command="git checkout $subforce -q" |
ee653c89 ÆAB |
530 | die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$path'")" |
531 | say_msg="$(eval_gettext "Submodule path '\$path': checked out '\$sha1'")" | |
32948425 JH |
532 | ;; |
533 | esac | |
70c7ac22 | 534 | |
15ffb7cd FG |
535 | if (clear_local_git_env; cd "$path" && $command "$sha1") |
536 | then | |
ff968f03 | 537 | say "$say_msg" |
877449c1 JH |
538 | elif test -n "$must_die_on_failure" |
539 | then | |
ff968f03 | 540 | die_with_status 2 "$die_msg" |
15ffb7cd | 541 | else |
ff968f03 | 542 | err="${err};$die_msg" |
877449c1 | 543 | continue |
15ffb7cd | 544 | fi |
70c7ac22 | 545 | fi |
b13fd5c1 JH |
546 | |
547 | if test -n "$recursive" | |
548 | then | |
15ffb7cd FG |
549 | (clear_local_git_env; cd "$path" && eval cmd_update "$orig_flags") |
550 | res=$? | |
551 | if test $res -gt 0 | |
552 | then | |
ff968f03 | 553 | die_msg="$(eval_gettext "Failed to recurse into submodule path '\$path'")" |
15ffb7cd FG |
554 | if test $res -eq 1 |
555 | then | |
ff968f03 | 556 | err="${err};$die_msg" |
15ffb7cd FG |
557 | continue |
558 | else | |
ff968f03 | 559 | die_with_status $res "$die_msg" |
15ffb7cd FG |
560 | fi |
561 | fi | |
b13fd5c1 | 562 | fi |
70c7ac22 | 563 | done |
15ffb7cd FG |
564 | |
565 | if test -n "$err" | |
566 | then | |
567 | OIFS=$IFS | |
568 | IFS=';' | |
569 | for e in $err | |
570 | do | |
571 | if test -n "$e" | |
572 | then | |
573 | echo >&2 "$e" | |
574 | fi | |
575 | done | |
576 | IFS=$OIFS | |
577 | exit 1 | |
578 | fi | |
579 | } | |
70c7ac22 LH |
580 | } |
581 | ||
bffe71f4 EM |
582 | set_name_rev () { |
583 | revname=$( ( | |
74ae1419 | 584 | clear_local_git_env |
bffe71f4 | 585 | cd "$1" && { |
5be60078 JH |
586 | git describe "$2" 2>/dev/null || |
587 | git describe --tags "$2" 2>/dev/null || | |
f669ac0b ML |
588 | git describe --contains "$2" 2>/dev/null || |
589 | git describe --all --always "$2" | |
bffe71f4 EM |
590 | } |
591 | ) ) | |
592 | test -z "$revname" || revname=" ($revname)" | |
593 | } | |
28f9af5d PY |
594 | # |
595 | # Show commit summary for submodules in index or working tree | |
596 | # | |
597 | # If '--cached' is given, show summary between index and given commit, | |
598 | # or between working tree and given commit | |
599 | # | |
600 | # $@ = [commit (default 'HEAD'),] requested paths (default all) | |
601 | # | |
602 | cmd_summary() { | |
f2dc06a3 | 603 | summary_limit=-1 |
d0f64dd4 | 604 | for_status= |
1c244f6e | 605 | diff_cmd=diff-index |
f2dc06a3 | 606 | |
28f9af5d PY |
607 | # parse $args after "submodule ... summary". |
608 | while test $# -ne 0 | |
609 | do | |
610 | case "$1" in | |
611 | --cached) | |
612 | cached="$1" | |
613 | ;; | |
1c244f6e JL |
614 | --files) |
615 | files="$1" | |
616 | ;; | |
d0f64dd4 PY |
617 | --for-status) |
618 | for_status="$1" | |
619 | ;; | |
f2dc06a3 PY |
620 | -n|--summary-limit) |
621 | if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2" | |
622 | then | |
623 | : | |
624 | else | |
625 | usage | |
626 | fi | |
627 | shift | |
628 | ;; | |
28f9af5d PY |
629 | --) |
630 | shift | |
631 | break | |
632 | ;; | |
633 | -*) | |
634 | usage | |
635 | ;; | |
636 | *) | |
637 | break | |
638 | ;; | |
639 | esac | |
640 | shift | |
641 | done | |
bffe71f4 | 642 | |
f2dc06a3 PY |
643 | test $summary_limit = 0 && return |
644 | ||
3deea89c | 645 | if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"}) |
28f9af5d PY |
646 | then |
647 | head=$rev | |
caa9c3ca | 648 | test $# = 0 || shift |
3deea89c JH |
649 | elif test -z "$1" -o "$1" = "HEAD" |
650 | then | |
14e940d7 JH |
651 | # before the first commit: compare with an empty tree |
652 | head=$(git hash-object -w -t tree --stdin </dev/null) | |
2ea6c2c9 | 653 | test -z "$1" || shift |
28f9af5d | 654 | else |
3deea89c | 655 | head="HEAD" |
28f9af5d PY |
656 | fi |
657 | ||
1c244f6e JL |
658 | if [ -n "$files" ] |
659 | then | |
660 | test -n "$cached" && | |
b9b9c22f | 661 | die "$(gettext -- "--cached cannot be used with --files")" |
1c244f6e JL |
662 | diff_cmd=diff-files |
663 | head= | |
664 | fi | |
665 | ||
28f9af5d PY |
666 | cd_to_toplevel |
667 | # Get modified modules cared by user | |
18076502 | 668 | modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" | |
e1622bfc | 669 | sane_egrep '^:([0-7]* )?160000' | |
28f9af5d PY |
670 | while read mod_src mod_dst sha1_src sha1_dst status name |
671 | do | |
672 | # Always show modules deleted or type-changed (blob<->module) | |
673 | test $status = D -o $status = T && echo "$name" && continue | |
674 | # Also show added or modified modules which are checked out | |
675 | GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 && | |
676 | echo "$name" | |
677 | done | |
678 | ) | |
1cb639e6 | 679 | |
d0f64dd4 PY |
680 | test -z "$modules" && return |
681 | ||
18076502 | 682 | git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules | |
e1622bfc | 683 | sane_egrep '^:([0-7]* )?160000' | |
1cb639e6 PY |
684 | cut -c2- | |
685 | while read mod_src mod_dst sha1_src sha1_dst status name | |
686 | do | |
687 | if test -z "$cached" && | |
688 | test $sha1_dst = 0000000000000000000000000000000000000000 | |
689 | then | |
690 | case "$mod_dst" in | |
691 | 160000) | |
692 | sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD) | |
693 | ;; | |
694 | 100644 | 100755 | 120000) | |
695 | sha1_dst=$(git hash-object $name) | |
696 | ;; | |
697 | 000000) | |
698 | ;; # removed | |
699 | *) | |
700 | # unexpected type | |
6ff875c5 | 701 | eval_gettextln "unexpected mode \$mod_dst" >&2 |
1cb639e6 PY |
702 | continue ;; |
703 | esac | |
704 | fi | |
705 | missing_src= | |
706 | missing_dst= | |
707 | ||
708 | test $mod_src = 160000 && | |
30353a40 | 709 | ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null && |
1cb639e6 PY |
710 | missing_src=t |
711 | ||
712 | test $mod_dst = 160000 && | |
30353a40 | 713 | ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null && |
1cb639e6 | 714 | missing_dst=t |
bffe71f4 | 715 | |
1cb639e6 PY |
716 | total_commits= |
717 | case "$missing_src,$missing_dst" in | |
718 | t,) | |
f62f8212 | 719 | errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")" |
1cb639e6 PY |
720 | ;; |
721 | ,t) | |
f62f8212 | 722 | errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")" |
1cb639e6 PY |
723 | ;; |
724 | t,t) | |
f62f8212 | 725 | errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")" |
1cb639e6 PY |
726 | ;; |
727 | *) | |
728 | errmsg= | |
729 | total_commits=$( | |
730 | if test $mod_src = 160000 -a $mod_dst = 160000 | |
731 | then | |
732 | range="$sha1_src...$sha1_dst" | |
733 | elif test $mod_src = 160000 | |
734 | then | |
735 | range=$sha1_src | |
736 | else | |
737 | range=$sha1_dst | |
738 | fi | |
739 | GIT_DIR="$name/.git" \ | |
b0e621ad | 740 | git rev-list --first-parent $range -- | wc -l |
1cb639e6 | 741 | ) |
eed35595 | 742 | total_commits=" ($(($total_commits + 0)))" |
1cb639e6 PY |
743 | ;; |
744 | esac | |
745 | ||
746 | sha1_abbr_src=$(echo $sha1_src | cut -c1-7) | |
747 | sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7) | |
748 | if test $status = T | |
749 | then | |
b3e73449 ÆAB |
750 | blob="$(gettext "blob")" |
751 | submodule="$(gettext "submodule")" | |
1cb639e6 PY |
752 | if test $mod_dst = 160000 |
753 | then | |
b3e73449 | 754 | echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:" |
1cb639e6 | 755 | else |
b3e73449 | 756 | echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:" |
1cb639e6 PY |
757 | fi |
758 | else | |
759 | echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:" | |
760 | fi | |
761 | if test -n "$errmsg" | |
762 | then | |
763 | # Don't give error msg for modification whose dst is not submodule | |
764 | # i.e. deleted or changed to blob | |
765 | test $mod_dst = 160000 && echo "$errmsg" | |
766 | else | |
767 | if test $mod_src = 160000 -a $mod_dst = 160000 | |
768 | then | |
f2dc06a3 PY |
769 | limit= |
770 | test $summary_limit -gt 0 && limit="-$summary_limit" | |
1cb639e6 | 771 | GIT_DIR="$name/.git" \ |
f2dc06a3 | 772 | git log $limit --pretty='format: %m %s' \ |
1cb639e6 PY |
773 | --first-parent $sha1_src...$sha1_dst |
774 | elif test $mod_dst = 160000 | |
775 | then | |
776 | GIT_DIR="$name/.git" \ | |
777 | git log --pretty='format: > %s' -1 $sha1_dst | |
778 | else | |
779 | GIT_DIR="$name/.git" \ | |
780 | git log --pretty='format: < %s' -1 $sha1_src | |
781 | fi | |
782 | echo | |
783 | fi | |
784 | echo | |
d0f64dd4 PY |
785 | done | |
786 | if test -n "$for_status"; then | |
f17a5d34 | 787 | if [ -n "$files" ]; then |
6ff875c5 | 788 | gettextln "# Submodules changed but not updated:" |
f17a5d34 | 789 | else |
6ff875c5 | 790 | gettextln "# Submodule changes to be committed:" |
f17a5d34 | 791 | fi |
d0f64dd4 PY |
792 | echo "#" |
793 | sed -e 's|^|# |' -e 's|^# $|#|' | |
794 | else | |
795 | cat | |
796 | fi | |
28f9af5d | 797 | } |
70c7ac22 | 798 | # |
941987a5 | 799 | # List all submodules, prefixed with: |
70c7ac22 LH |
800 | # - submodule not initialized |
801 | # + different revision checked out | |
802 | # | |
803 | # If --cached was specified the revision in the index will be printed | |
804 | # instead of the currently checked out revision. | |
805 | # | |
806 | # $@ = requested paths (default to all) | |
807 | # | |
23a485e3 | 808 | cmd_status() |
70c7ac22 | 809 | { |
5c08dbbd | 810 | # parse $args after "submodule ... status". |
98dbe63d | 811 | orig_flags= |
5c08dbbd JH |
812 | while test $# -ne 0 |
813 | do | |
814 | case "$1" in | |
815 | -q|--quiet) | |
2e6a30ef | 816 | GIT_QUIET=1 |
5c08dbbd JH |
817 | ;; |
818 | --cached) | |
819 | cached=1 | |
820 | ;; | |
64b19ffe JH |
821 | --recursive) |
822 | recursive=1 | |
823 | ;; | |
5c08dbbd JH |
824 | --) |
825 | shift | |
826 | break | |
827 | ;; | |
828 | -*) | |
829 | usage | |
830 | ;; | |
831 | *) | |
832 | break | |
833 | ;; | |
834 | esac | |
98dbe63d | 835 | orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")" |
5c08dbbd JH |
836 | shift |
837 | done | |
838 | ||
a7b3269c | 839 | module_list "$@" | |
70c7ac22 LH |
840 | while read mode sha1 stage path |
841 | do | |
941987a5 | 842 | name=$(module_name "$path") || exit |
5be60078 | 843 | url=$(git config submodule."$name".url) |
64b19ffe | 844 | displaypath="$prefix$path" |
313ee0d6 NMC |
845 | if test "$stage" = U |
846 | then | |
847 | say "U$sha1 $displaypath" | |
848 | continue | |
849 | fi | |
ba88a1fe | 850 | if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git |
70c7ac22 | 851 | then |
64b19ffe | 852 | say "-$sha1 $displaypath" |
70c7ac22 LH |
853 | continue; |
854 | fi | |
41c7c1bd | 855 | set_name_rev "$path" "$sha1" |
18076502 | 856 | if git diff-files --ignore-submodules=dirty --quiet -- "$path" |
70c7ac22 | 857 | then |
64b19ffe | 858 | say " $sha1 $displaypath$revname" |
70c7ac22 LH |
859 | else |
860 | if test -z "$cached" | |
861 | then | |
74ae1419 | 862 | sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD) |
41c7c1bd | 863 | set_name_rev "$path" "$sha1" |
70c7ac22 | 864 | fi |
64b19ffe JH |
865 | say "+$sha1 $displaypath$revname" |
866 | fi | |
867 | ||
868 | if test -n "$recursive" | |
869 | then | |
870 | ( | |
871 | prefix="$displaypath/" | |
74ae1419 | 872 | clear_local_git_env |
64b19ffe | 873 | cd "$path" && |
a7eff1a8 | 874 | eval cmd_status "$orig_args" |
64b19ffe | 875 | ) || |
497ee872 | 876 | die "$(eval_gettext "Failed to recurse into submodule path '\$path'")" |
70c7ac22 LH |
877 | fi |
878 | done | |
879 | } | |
2327f61e DA |
880 | # |
881 | # Sync remote urls for submodules | |
882 | # This makes the value for remote.$remote.url match the value | |
883 | # specified in .gitmodules. | |
884 | # | |
885 | cmd_sync() | |
886 | { | |
887 | while test $# -ne 0 | |
888 | do | |
889 | case "$1" in | |
890 | -q|--quiet) | |
2e6a30ef | 891 | GIT_QUIET=1 |
2327f61e DA |
892 | shift |
893 | ;; | |
894 | --) | |
895 | shift | |
896 | break | |
897 | ;; | |
898 | -*) | |
899 | usage | |
900 | ;; | |
901 | *) | |
902 | break | |
903 | ;; | |
904 | esac | |
905 | done | |
906 | cd_to_toplevel | |
907 | module_list "$@" | | |
908 | while read mode sha1 stage path | |
909 | do | |
910 | name=$(module_name "$path") | |
911 | url=$(git config -f .gitmodules --get submodule."$name".url) | |
baede9f8 JH |
912 | |
913 | # Possibly a url relative to parent | |
914 | case "$url" in | |
915 | ./*|../*) | |
916 | url=$(resolve_relative_url "$url") || exit | |
917 | ;; | |
918 | esac | |
919 | ||
ccee6086 | 920 | if git config "submodule.$name.url" >/dev/null 2>/dev/null |
2327f61e | 921 | then |
0591c0a5 | 922 | say "$(eval_gettext "Synchronizing submodule url for '\$name'")" |
ccee6086 JH |
923 | git config submodule."$name".url "$url" |
924 | ||
925 | if test -e "$path"/.git | |
926 | then | |
927 | ( | |
928 | clear_local_git_env | |
929 | cd "$path" | |
930 | remote=$(get_default_remote) | |
931 | git config remote."$remote".url "$url" | |
932 | ) | |
933 | fi | |
2327f61e DA |
934 | fi |
935 | done | |
936 | } | |
70c7ac22 | 937 | |
5c08dbbd JH |
938 | # This loop parses the command line arguments to find the |
939 | # subcommand name to dispatch. Parsing of the subcommand specific | |
940 | # options are primarily done by the subcommand implementations. | |
941 | # Subcommand specific options such as --branch and --cached are | |
942 | # parsed here as well, for backward compatibility. | |
943 | ||
944 | while test $# != 0 && test -z "$command" | |
70c7ac22 LH |
945 | do |
946 | case "$1" in | |
2327f61e | 947 | add | foreach | init | update | status | summary | sync) |
5c08dbbd | 948 | command=$1 |
70c7ac22 LH |
949 | ;; |
950 | -q|--quiet) | |
2e6a30ef | 951 | GIT_QUIET=1 |
70c7ac22 | 952 | ;; |
ecda0723 SV |
953 | -b|--branch) |
954 | case "$2" in | |
955 | '') | |
956 | usage | |
957 | ;; | |
958 | esac | |
959 | branch="$2"; shift | |
960 | ;; | |
70c7ac22 | 961 | --cached) |
28f9af5d | 962 | cached="$1" |
70c7ac22 LH |
963 | ;; |
964 | --) | |
965 | break | |
966 | ;; | |
967 | -*) | |
968 | usage | |
969 | ;; | |
970 | *) | |
971 | break | |
972 | ;; | |
973 | esac | |
974 | shift | |
975 | done | |
976 | ||
5c08dbbd JH |
977 | # No command word defaults to "status" |
978 | test -n "$command" || command=status | |
979 | ||
980 | # "-b branch" is accepted only by "add" | |
981 | if test -n "$branch" && test "$command" != add | |
982 | then | |
ecda0723 | 983 | usage |
5c08dbbd JH |
984 | fi |
985 | ||
28f9af5d PY |
986 | # "--cached" is accepted only by "status" and "summary" |
987 | if test -n "$cached" && test "$command" != status -a "$command" != summary | |
5c08dbbd | 988 | then |
70c7ac22 | 989 | usage |
5c08dbbd JH |
990 | fi |
991 | ||
992 | "cmd_$command" "$@" |