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