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