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