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