]> git.ipfire.org Git - thirdparty/git.git/blame - git-submodule.sh
submodule: fix handling of superproject origin URLs like foo, ./foo and ./foo/bar
[thirdparty/git.git] / git-submodule.sh
CommitLineData
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 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
211b7f19 435 # Skip already registered paths
64394e3a 436 name=$(module_name "$sm_path") || exit
2cd9de3e
JL
437 if test -z "$(git config "submodule.$name.url")"
438 then
439 url=$(git config -f .gitmodules submodule."$name".url)
440 test -z "$url" &&
64394e3a 441 die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")"
2cd9de3e
JL
442
443 # Possibly a url relative to parent
444 case "$url" in
445 ./*|../*)
446 url=$(resolve_relative_url "$url") || exit
447 ;;
448 esac
449 git config submodule."$name".url "$url" ||
64394e3a 450 die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")"
2cd9de3e 451 fi
70c7ac22 452
2cd9de3e 453 # Copy "update" setting when it is not set yet
32948425
JH
454 upd="$(git config -f .gitmodules submodule."$name".update)"
455 test -z "$upd" ||
2cd9de3e 456 test -n "$(git config submodule."$name".update)" ||
32948425 457 git config submodule."$name".update "$upd" ||
64394e3a 458 die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
ca2cedba 459
64394e3a 460 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")"
70c7ac22
LH
461 done
462}
463
464#
211b7f19 465# Update each submodule path to correct revision, using clone and checkout as needed
70c7ac22
LH
466#
467# $@ = requested paths (default to all)
468#
23a485e3 469cmd_update()
70c7ac22 470{
5c08dbbd 471 # parse $args after "submodule ... update".
98dbe63d 472 orig_flags=
5c08dbbd
JH
473 while test $# -ne 0
474 do
475 case "$1" in
476 -q|--quiet)
2e6a30ef 477 GIT_QUIET=1
5c08dbbd 478 ;;
be4d2c83 479 -i|--init)
d92a3959 480 init=1
be4d2c83 481 ;;
31ca3ac3 482 -N|--no-fetch)
31ca3ac3
FF
483 nofetch=1
484 ;;
9db31bdf
NMC
485 -f|--force)
486 force=$1
487 ;;
ca2cedba 488 -r|--rebase)
32948425 489 update="rebase"
ca2cedba 490 ;;
d92a3959
MT
491 --reference)
492 case "$2" in '') usage ;; esac
493 reference="--reference=$2"
98dbe63d
KB
494 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
495 shift
d92a3959
MT
496 ;;
497 --reference=*)
498 reference="$1"
d92a3959 499 ;;
42b49178 500 -m|--merge)
42b49178
JH
501 update="merge"
502 ;;
b13fd5c1 503 --recursive)
b13fd5c1
JH
504 recursive=1
505 ;;
efc5fb6a
JH
506 --checkout)
507 update="checkout"
508 ;;
5c08dbbd
JH
509 --)
510 shift
511 break
512 ;;
513 -*)
514 usage
515 ;;
516 *)
517 break
518 ;;
519 esac
98dbe63d
KB
520 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
521 shift
5c08dbbd
JH
522 done
523
d92a3959
MT
524 if test -n "$init"
525 then
526 cmd_init "--" "$@" || return
527 fi
528
1b4735d9 529 cloned_modules=
15ffb7cd
FG
530 module_list "$@" | {
531 err=
64394e3a 532 while read mode sha1 stage sm_path
70c7ac22 533 do
313ee0d6
NMC
534 if test "$stage" = U
535 then
64394e3a 536 echo >&2 "Skipping unmerged submodule $sm_path"
313ee0d6
NMC
537 continue
538 fi
64394e3a 539 name=$(module_name "$sm_path") || exit
5be60078 540 url=$(git config submodule."$name".url)
efc5fb6a
JH
541 if ! test -z "$update"
542 then
543 update_module=$update
544 else
545 update_module=$(git config submodule."$name".update)
546 fi
547
548 if test "$update_module" = "none"
549 then
64394e3a 550 echo "Skipping submodule '$sm_path'"
efc5fb6a
JH
551 continue
552 fi
553
211b7f19 554 if test -z "$url"
70c7ac22
LH
555 then
556 # Only mention uninitialized submodules when its
557 # path have been specified
558 test "$#" != "0" &&
64394e3a 559 say "$(eval_gettext "Submodule path '\$sm_path' not initialized
1c2ef66f 560Maybe you want to use 'update --init'?")"
211b7f19
LH
561 continue
562 fi
563
64394e3a 564 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
211b7f19 565 then
64394e3a 566 module_clone "$sm_path" "$url" "$reference"|| exit
1b4735d9 567 cloned_modules="$cloned_modules;$name"
bf2d8246
LH
568 subsha1=
569 else
64394e3a 570 subsha1=$(clear_local_git_env; cd "$sm_path" &&
5be60078 571 git rev-parse --verify HEAD) ||
64394e3a 572 die "$(eval_gettext "Unable to find current revision in submodule path '\$sm_path'")"
70c7ac22 573 fi
211b7f19 574
70c7ac22
LH
575 if test "$subsha1" != "$sha1"
576 then
9db31bdf
NMC
577 subforce=$force
578 # If we don't already have a -f flag and the submodule has never been checked out
579 if test -z "$subsha1" -a -z "$force"
b9b378a0 580 then
9db31bdf 581 subforce="-f"
b9b378a0 582 fi
31ca3ac3
FF
583
584 if test -z "$nofetch"
585 then
e5f522d6
JL
586 # Run fetch only if $sha1 isn't present or it
587 # is not reachable from a ref.
64394e3a 588 (clear_local_git_env; cd "$sm_path" &&
f5799e05 589 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
e5f522d6 590 test -z "$rev") || git-fetch)) ||
64394e3a 591 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
31ca3ac3
FF
592 fi
593
1b4735d9
SO
594 # Is this something we just cloned?
595 case ";$cloned_modules;" in
596 *";$name;"*)
597 # then there is no local change to integrate
598 update_module= ;;
599 esac
600
877449c1 601 must_die_on_failure=
32948425
JH
602 case "$update_module" in
603 rebase)
604 command="git rebase"
64394e3a
RJ
605 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$sm_path'")"
606 say_msg="$(eval_gettext "Submodule path '\$sm_path': rebased into '\$sha1'")"
877449c1 607 must_die_on_failure=yes
32948425 608 ;;
42b49178
JH
609 merge)
610 command="git merge"
64394e3a
RJ
611 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$sm_path'")"
612 say_msg="$(eval_gettext "Submodule path '\$sm_path': merged in '\$sha1'")"
877449c1 613 must_die_on_failure=yes
42b49178 614 ;;
32948425 615 *)
9db31bdf 616 command="git checkout $subforce -q"
64394e3a
RJ
617 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$sm_path'")"
618 say_msg="$(eval_gettext "Submodule path '\$sm_path': checked out '\$sha1'")"
32948425
JH
619 ;;
620 esac
70c7ac22 621
64394e3a 622 if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
15ffb7cd 623 then
ff968f03 624 say "$say_msg"
877449c1
JH
625 elif test -n "$must_die_on_failure"
626 then
ff968f03 627 die_with_status 2 "$die_msg"
15ffb7cd 628 else
ff968f03 629 err="${err};$die_msg"
877449c1 630 continue
15ffb7cd 631 fi
70c7ac22 632 fi
b13fd5c1
JH
633
634 if test -n "$recursive"
635 then
64394e3a 636 (clear_local_git_env; cd "$sm_path" && eval cmd_update "$orig_flags")
15ffb7cd
FG
637 res=$?
638 if test $res -gt 0
639 then
64394e3a 640 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
15ffb7cd
FG
641 if test $res -eq 1
642 then
ff968f03 643 err="${err};$die_msg"
15ffb7cd
FG
644 continue
645 else
ff968f03 646 die_with_status $res "$die_msg"
15ffb7cd
FG
647 fi
648 fi
b13fd5c1 649 fi
70c7ac22 650 done
15ffb7cd
FG
651
652 if test -n "$err"
653 then
654 OIFS=$IFS
655 IFS=';'
656 for e in $err
657 do
658 if test -n "$e"
659 then
660 echo >&2 "$e"
661 fi
662 done
663 IFS=$OIFS
664 exit 1
665 fi
666 }
70c7ac22
LH
667}
668
bffe71f4
EM
669set_name_rev () {
670 revname=$( (
74ae1419 671 clear_local_git_env
bffe71f4 672 cd "$1" && {
5be60078
JH
673 git describe "$2" 2>/dev/null ||
674 git describe --tags "$2" 2>/dev/null ||
f669ac0b
ML
675 git describe --contains "$2" 2>/dev/null ||
676 git describe --all --always "$2"
bffe71f4
EM
677 }
678 ) )
679 test -z "$revname" || revname=" ($revname)"
680}
28f9af5d
PY
681#
682# Show commit summary for submodules in index or working tree
683#
684# If '--cached' is given, show summary between index and given commit,
685# or between working tree and given commit
686#
687# $@ = [commit (default 'HEAD'),] requested paths (default all)
688#
689cmd_summary() {
f2dc06a3 690 summary_limit=-1
d0f64dd4 691 for_status=
1c244f6e 692 diff_cmd=diff-index
f2dc06a3 693
28f9af5d
PY
694 # parse $args after "submodule ... summary".
695 while test $# -ne 0
696 do
697 case "$1" in
698 --cached)
699 cached="$1"
700 ;;
1c244f6e
JL
701 --files)
702 files="$1"
703 ;;
d0f64dd4
PY
704 --for-status)
705 for_status="$1"
706 ;;
f2dc06a3
PY
707 -n|--summary-limit)
708 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
709 then
710 :
711 else
712 usage
713 fi
714 shift
715 ;;
28f9af5d
PY
716 --)
717 shift
718 break
719 ;;
720 -*)
721 usage
722 ;;
723 *)
724 break
725 ;;
726 esac
727 shift
728 done
bffe71f4 729
f2dc06a3
PY
730 test $summary_limit = 0 && return
731
3deea89c 732 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
28f9af5d
PY
733 then
734 head=$rev
caa9c3ca 735 test $# = 0 || shift
3deea89c
JH
736 elif test -z "$1" -o "$1" = "HEAD"
737 then
14e940d7
JH
738 # before the first commit: compare with an empty tree
739 head=$(git hash-object -w -t tree --stdin </dev/null)
2ea6c2c9 740 test -z "$1" || shift
28f9af5d 741 else
3deea89c 742 head="HEAD"
28f9af5d
PY
743 fi
744
1c244f6e
JL
745 if [ -n "$files" ]
746 then
747 test -n "$cached" &&
b9b9c22f 748 die "$(gettext -- "--cached cannot be used with --files")"
1c244f6e
JL
749 diff_cmd=diff-files
750 head=
751 fi
752
28f9af5d
PY
753 cd_to_toplevel
754 # Get modified modules cared by user
18076502 755 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
e1622bfc 756 sane_egrep '^:([0-7]* )?160000' |
28f9af5d
PY
757 while read mod_src mod_dst sha1_src sha1_dst status name
758 do
759 # Always show modules deleted or type-changed (blob<->module)
760 test $status = D -o $status = T && echo "$name" && continue
761 # Also show added or modified modules which are checked out
762 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
763 echo "$name"
764 done
765 )
1cb639e6 766
d0f64dd4
PY
767 test -z "$modules" && return
768
18076502 769 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
e1622bfc 770 sane_egrep '^:([0-7]* )?160000' |
1cb639e6
PY
771 cut -c2- |
772 while read mod_src mod_dst sha1_src sha1_dst status name
773 do
774 if test -z "$cached" &&
775 test $sha1_dst = 0000000000000000000000000000000000000000
776 then
777 case "$mod_dst" in
778 160000)
779 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
780 ;;
781 100644 | 100755 | 120000)
782 sha1_dst=$(git hash-object $name)
783 ;;
784 000000)
785 ;; # removed
786 *)
787 # unexpected type
6ff875c5 788 eval_gettextln "unexpected mode \$mod_dst" >&2
1cb639e6
PY
789 continue ;;
790 esac
791 fi
792 missing_src=
793 missing_dst=
794
795 test $mod_src = 160000 &&
30353a40 796 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
1cb639e6
PY
797 missing_src=t
798
799 test $mod_dst = 160000 &&
30353a40 800 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
1cb639e6 801 missing_dst=t
bffe71f4 802
1cb639e6
PY
803 total_commits=
804 case "$missing_src,$missing_dst" in
805 t,)
f62f8212 806 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")"
1cb639e6
PY
807 ;;
808 ,t)
f62f8212 809 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")"
1cb639e6
PY
810 ;;
811 t,t)
f62f8212 812 errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
1cb639e6
PY
813 ;;
814 *)
815 errmsg=
816 total_commits=$(
817 if test $mod_src = 160000 -a $mod_dst = 160000
818 then
819 range="$sha1_src...$sha1_dst"
820 elif test $mod_src = 160000
821 then
822 range=$sha1_src
823 else
824 range=$sha1_dst
825 fi
826 GIT_DIR="$name/.git" \
b0e621ad 827 git rev-list --first-parent $range -- | wc -l
1cb639e6 828 )
eed35595 829 total_commits=" ($(($total_commits + 0)))"
1cb639e6
PY
830 ;;
831 esac
832
833 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
834 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
835 if test $status = T
836 then
b3e73449
ÆAB
837 blob="$(gettext "blob")"
838 submodule="$(gettext "submodule")"
1cb639e6
PY
839 if test $mod_dst = 160000
840 then
b3e73449 841 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
1cb639e6 842 else
b3e73449 843 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
1cb639e6
PY
844 fi
845 else
846 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
847 fi
848 if test -n "$errmsg"
849 then
850 # Don't give error msg for modification whose dst is not submodule
851 # i.e. deleted or changed to blob
852 test $mod_dst = 160000 && echo "$errmsg"
853 else
854 if test $mod_src = 160000 -a $mod_dst = 160000
855 then
f2dc06a3
PY
856 limit=
857 test $summary_limit -gt 0 && limit="-$summary_limit"
1cb639e6 858 GIT_DIR="$name/.git" \
f2dc06a3 859 git log $limit --pretty='format: %m %s' \
1cb639e6
PY
860 --first-parent $sha1_src...$sha1_dst
861 elif test $mod_dst = 160000
862 then
863 GIT_DIR="$name/.git" \
864 git log --pretty='format: > %s' -1 $sha1_dst
865 else
866 GIT_DIR="$name/.git" \
867 git log --pretty='format: < %s' -1 $sha1_src
868 fi
869 echo
870 fi
871 echo
d0f64dd4
PY
872 done |
873 if test -n "$for_status"; then
f17a5d34 874 if [ -n "$files" ]; then
6ff875c5 875 gettextln "# Submodules changed but not updated:"
f17a5d34 876 else
6ff875c5 877 gettextln "# Submodule changes to be committed:"
f17a5d34 878 fi
d0f64dd4
PY
879 echo "#"
880 sed -e 's|^|# |' -e 's|^# $|#|'
881 else
882 cat
883 fi
28f9af5d 884}
70c7ac22 885#
941987a5 886# List all submodules, prefixed with:
70c7ac22
LH
887# - submodule not initialized
888# + different revision checked out
889#
890# If --cached was specified the revision in the index will be printed
891# instead of the currently checked out revision.
892#
893# $@ = requested paths (default to all)
894#
23a485e3 895cmd_status()
70c7ac22 896{
5c08dbbd 897 # parse $args after "submodule ... status".
98dbe63d 898 orig_flags=
5c08dbbd
JH
899 while test $# -ne 0
900 do
901 case "$1" in
902 -q|--quiet)
2e6a30ef 903 GIT_QUIET=1
5c08dbbd
JH
904 ;;
905 --cached)
906 cached=1
907 ;;
64b19ffe
JH
908 --recursive)
909 recursive=1
910 ;;
5c08dbbd
JH
911 --)
912 shift
913 break
914 ;;
915 -*)
916 usage
917 ;;
918 *)
919 break
920 ;;
921 esac
98dbe63d 922 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
5c08dbbd
JH
923 shift
924 done
925
a7b3269c 926 module_list "$@" |
64394e3a 927 while read mode sha1 stage sm_path
70c7ac22 928 do
64394e3a 929 name=$(module_name "$sm_path") || exit
5be60078 930 url=$(git config submodule."$name".url)
64394e3a 931 displaypath="$prefix$sm_path"
313ee0d6
NMC
932 if test "$stage" = U
933 then
934 say "U$sha1 $displaypath"
935 continue
936 fi
64394e3a 937 if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
70c7ac22 938 then
64b19ffe 939 say "-$sha1 $displaypath"
70c7ac22
LH
940 continue;
941 fi
64394e3a
RJ
942 set_name_rev "$sm_path" "$sha1"
943 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
70c7ac22 944 then
64b19ffe 945 say " $sha1 $displaypath$revname"
70c7ac22
LH
946 else
947 if test -z "$cached"
948 then
64394e3a
RJ
949 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
950 set_name_rev "$sm_path" "$sha1"
70c7ac22 951 fi
64b19ffe
JH
952 say "+$sha1 $displaypath$revname"
953 fi
954
955 if test -n "$recursive"
956 then
957 (
958 prefix="$displaypath/"
74ae1419 959 clear_local_git_env
64394e3a 960 cd "$sm_path" &&
a7eff1a8 961 eval cmd_status "$orig_args"
64b19ffe 962 ) ||
64394e3a 963 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
70c7ac22
LH
964 fi
965 done
966}
2327f61e
DA
967#
968# Sync remote urls for submodules
969# This makes the value for remote.$remote.url match the value
970# specified in .gitmodules.
971#
972cmd_sync()
973{
974 while test $# -ne 0
975 do
976 case "$1" in
977 -q|--quiet)
2e6a30ef 978 GIT_QUIET=1
2327f61e
DA
979 shift
980 ;;
981 --)
982 shift
983 break
984 ;;
985 -*)
986 usage
987 ;;
988 *)
989 break
990 ;;
991 esac
992 done
993 cd_to_toplevel
994 module_list "$@" |
64394e3a 995 while read mode sha1 stage sm_path
2327f61e 996 do
64394e3a 997 name=$(module_name "$sm_path")
2327f61e 998 url=$(git config -f .gitmodules --get submodule."$name".url)
baede9f8
JH
999
1000 # Possibly a url relative to parent
1001 case "$url" in
1002 ./*|../*)
967b2c66
JS
1003 # rewrite foo/bar as ../.. to find path from
1004 # submodule work tree to superproject work tree
1005 up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1006 # guarantee a trailing /
1007 up_path=${up_path%/}/ &&
1008 # path from submodule work tree to submodule origin repo
1009 sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1010 # path from superproject work tree to submodule origin repo
1011 super_config_url=$(resolve_relative_url "$url") || exit
1012 ;;
1013 *)
1014 sub_origin_url="$url"
1015 super_config_url="$url"
baede9f8
JH
1016 ;;
1017 esac
1018
ccee6086 1019 if git config "submodule.$name.url" >/dev/null 2>/dev/null
2327f61e 1020 then
0591c0a5 1021 say "$(eval_gettext "Synchronizing submodule url for '\$name'")"
967b2c66 1022 git config submodule."$name".url "$super_config_url"
ccee6086 1023
64394e3a 1024 if test -e "$sm_path"/.git
ccee6086
JH
1025 then
1026 (
1027 clear_local_git_env
64394e3a 1028 cd "$sm_path"
ccee6086 1029 remote=$(get_default_remote)
967b2c66 1030 git config remote."$remote".url "$sub_origin_url"
ccee6086
JH
1031 )
1032 fi
2327f61e
DA
1033 fi
1034 done
1035}
70c7ac22 1036
5c08dbbd
JH
1037# This loop parses the command line arguments to find the
1038# subcommand name to dispatch. Parsing of the subcommand specific
1039# options are primarily done by the subcommand implementations.
1040# Subcommand specific options such as --branch and --cached are
1041# parsed here as well, for backward compatibility.
1042
1043while test $# != 0 && test -z "$command"
70c7ac22
LH
1044do
1045 case "$1" in
2327f61e 1046 add | foreach | init | update | status | summary | sync)
5c08dbbd 1047 command=$1
70c7ac22
LH
1048 ;;
1049 -q|--quiet)
2e6a30ef 1050 GIT_QUIET=1
70c7ac22 1051 ;;
ecda0723
SV
1052 -b|--branch)
1053 case "$2" in
1054 '')
1055 usage
1056 ;;
1057 esac
1058 branch="$2"; shift
1059 ;;
70c7ac22 1060 --cached)
28f9af5d 1061 cached="$1"
70c7ac22
LH
1062 ;;
1063 --)
1064 break
1065 ;;
1066 -*)
1067 usage
1068 ;;
1069 *)
1070 break
1071 ;;
1072 esac
1073 shift
1074done
1075
5c08dbbd
JH
1076# No command word defaults to "status"
1077test -n "$command" || command=status
1078
1079# "-b branch" is accepted only by "add"
1080if test -n "$branch" && test "$command" != add
1081then
ecda0723 1082 usage
5c08dbbd
JH
1083fi
1084
28f9af5d
PY
1085# "--cached" is accepted only by "status" and "summary"
1086if test -n "$cached" && test "$command" != status -a "$command" != summary
5c08dbbd 1087then
70c7ac22 1088 usage
5c08dbbd
JH
1089fi
1090
1091"cmd_$command" "$@"