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