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